Java 8 HTMLEditorKit - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 HTMLEditorKit class.

This code example shows how to use the following methods:createDefaultDocument, read
    /** 
     * Given some html, extracts its text. 
     */ 
    public static String extractTextFromHTML(final String html) {
        try { 
            final EditorKit kit = new HTMLEditorKit();
            final Document doc = kit.createDefaultDocument();
 
            // The Document class does not yet handle charset's properly. 
            doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
 
            // Create a reader on the HTML content. 
            final Reader rd = new StringReader(html);
 
            // Parse the HTML. 
            kit.read(rd, doc, 0);
 
            // The HTML text is now stored in the document 
            return doc.getText(0, doc.getLength());
        } catch (final Exception e) {
        } 
6
CodeRank
This code example shows how to use the following methods:read, createDefaultDocument
    public InputStream getDestinationStream(InputStream source)
            throws Exception
    { 
        // try and read the document - set to ignore character set directive, 
        // assuming that the input stream is already set properly (I hope) 
        HTMLEditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();
 
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
 
        kit.read(source, doc, 0);
 
        String extractedText = doc.getText(0, doc.getLength());
 
        // generate an input stream with the extracted text 
        byte[] textBytes = extractedText.getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(textBytes);
 
        return bais; // will this work? or will the byte array be out of scope?
    } 
6
CodeRank
This code example shows how to use the following methods:read, createDefaultDocument
    public InputStream getDestinationStream(InputStream source)
            throws Exception
    { 
        // try and read the document - set to ignore character set directive, 
        // assuming that the input stream is already set properly (I hope) 
        HTMLEditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();
 
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
 
        kit.read(source, doc, 0);
 
        String extractedText = doc.getText(0, doc.getLength());
 
        // generate an input stream with the extracted text 
        byte[] textBytes = extractedText.getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(textBytes);
 
        return bais; // will this work? or will the byte array be out of scope?
    } 
5
CodeRank
This code example shows how to use the following methods:getStyleSheet
        // Try reinit default DTD 
        new ParserDelegator();
 
        assertTrue(dtdValue == appContext.get(dtdKey), "ParserDelegator.defaultDTD created a duplicate");
 
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
 
        if (styleSheet == null) {
            // First AppContext 
            styleSheet = htmlEditorKit.getStyleSheet();
 
            assertTrue(styleSheet != null, "htmlEditorKit.getStyleSheet() returns null");
            assertTrue(htmlEditorKit.getStyleSheet() == styleSheet, "Something wrong with htmlEditorKit.getStyleSheet()");
        } else { 
            assertTrue(htmlEditorKit.getStyleSheet() != styleSheet, "HtmlEditorKit.getStyleSheet() mixed AppContexts");
        } 
    } 
 
    private static void assertTrue(boolean b, String msg) {
        if (!b) {
5
CodeRank
This code example shows how to use the following methods:createDefaultDocument, read
	 *            HTML source 
	 * @param writer 
	 *            XHTML target 
	 */ 
	public static void html2xhtml(final Reader reader, final Writer writer) throws IOException, BadLocationException {
		final HTMLEditorKit kit = new HTMLEditorKit();
		final Document doc = kit.createDefaultDocument();
		kit.read(reader, doc, doc.getLength());
		final XHTMLWriter xhw = new XHTMLWriter(writer, (HTMLDocument) doc);
		xhw.write();
	} 
 
	/** 
	 * External call to convert a source HTML file to a target XHTML file. 
	 * <p> 
	 * Usage: <tt>java XHTMLWriter &lt;source file&gt; &lt;target file&gt;</tt> 
	 *  
	 * @param args 
	 *            Shell arguments 
	 */ 
5
CodeRank
This code example shows how to use the following methods:setText
      this.text = text;
      String htmltxt = "<html><font size=5>"+text+"</font></html>";
       
      super.setText(htmltxt);
       
      JTextPane tp = new JTextPane();
      tp.setText(htmltxt);
 
      if (getPreferredSize().getWidth() > maximum_width){
          
         htmltxt = "<html><div width='"+maximum_width+"'><font size=5>"+text+"</font></div></html>";
         super.setText(htmltxt);
      } 
       
      setSize(getPreferredSize());
   } 
       
    
   @Override 
   public String getText(){
6
CodeRank
This code example shows how to use the following methods:setContentType, setDocument
   * Sets the content type. 
   *  
   * @param type	the content type 
   */ 
  public void setContentType(String type) {
    m_PrintPane.setContentType(type);
  } 
 
  /** 
   * Returns the document to print. 
   *  
   * @return		the document or null 
   */ 
  public Document getDocument() {
    if (m_PrintPane != null) 
      return m_PrintPane.getDocument();
    else  
      return null; 
  } 
 

6
CodeRank
This code example shows how to use the following methods:setBackground, setBorder, setForeground, setPreferredSize, setContentType
This code example shows how to use the following methods:setSize, paint, setPreferredSize, setEditorKit, setPage
		Validate.isTrue(width > 0 && height > 0, "Invalid dimensions. Dimensions must be greater than 0.");
		 
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		 
		JEditorPane editorPane = createEditorPane(width, height);
		editorPane.setPage(htmlSource);
		editorPane.paint(image.getGraphics());
		 
		return image;
	} 
 
	/** 
	 * Create and return an image of the provided HTML content. The image will have 
	 * the provided dimensions. 
	 * <p/> 
	 * Note: This method will not change any styles to ensure that the html document will fit inside the 
	 * specified dimensions. 
	 */ 
	public static BufferedImage createImageFromHtml(String sourceHtml, int width, int height)
	{ 

6
CodeRank
This code example shows how to use the following methods:putClientProperty, setBackground, setOpaque, setContentType, setEditorKit
     * Adapts a {@link JEditorPane} to be used as a powerful replacement of {@link javax.swing.JLabel}. 
     * @param pane The editor pane to adapt 
     * @param allBold If {@code true}, makes all text to be displayed in bold 
     */ 
    public static void makeJLabelLike(JEditorPane pane, boolean allBold) {
        pane.setContentType("text/html");
        pane.setOpaque(false);
        pane.setEditable(false);
        adaptForNimbus(pane);
 
        JosmHTMLEditorKit kit = new JosmHTMLEditorKit();
        final Font f = UIManager.getFont("Label.font");
        final StyleSheet ss = new StyleSheet();
        ss.addRule((allBold ? "html" : "strong, b") + " {" + getFontRule(f) + "}");
        ss.addRule("a {text-decoration: underline; color: blue}");
        ss.addRule("h1 {" + getFontRule(GuiHelper.getTitleFont()) + "}");
        ss.addRule("ol {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: decimal}");
        ss.addRule("ul {margin-left: 1cm; margin-top: 0.1cm; margin-bottom: 0.2cm; list-style-type: disc}");
        kit.setStyleSheet(ss);
        pane.setEditorKit(kit);

6
CodeRank
See Code Examples for Java 8 HTMLEditorKit Methods: