Java Code Examples for javax.swing.text.html.HTMLEditorKit#createDefaultDocument()

The following examples show how to use javax.swing.text.html.HTMLEditorKit#createDefaultDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: bug8005391.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 2
Source File: bug8028616.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ParserCB cb = new ParserCB();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();

    htmlDoc.getParser().parse(new StringReader(text), cb, true);

    synchronized (lock) {
        if (!isCallbackInvoked) {
            lock.wait(5000);
        }
    }

    if (!isCallbackInvoked) {
        throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
    }

    if (exception != null) {
        throw exception;
    }
}
 
Example 3
Source File: bug8028616.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ParserCB cb = new ParserCB();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();

    htmlDoc.getParser().parse(new StringReader(text), cb, true);

    synchronized (lock) {
        if (!isCallbackInvoked) {
            lock.wait(5000);
        }
    }

    if (!isCallbackInvoked) {
        throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
    }

    if (exception != null) {
        throw exception;
    }
}
 
Example 4
Source File: bug8005391.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 5
Source File: HTMLDocumentEditor.java    From egdownloader with GNU General Public License v2.0 6 votes vote down vote up
/** 构造方法 */
public HTMLDocumentEditor() {
	/** 设置主窗体标题 */
	super("HTMLDocumentEditor");
	HTMLEditorKit editorKit = new HTMLEditorKit();
	/** 创建默认文档指向网页引用document */
	document = (HTMLDocument) editorKit.createDefaultDocument();

	// 强制SWINGSET实现跨平台,不改变风格
	try {
		UIManager.setLookAndFeel(UIManager
				.getCrossPlatformLookAndFeelClassName());
		// 如果你想用系统的界面风格替代,请注释掉上一行代码,而取消下一行代码的注释:
		// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (Exception exc) {
		// 产生异常,则显示错误消息:加载L&F错误
		System.err.println("Error loading L&F: " + exc);
	}

	// 调用初始化方法
	init();
}
 
Example 6
Source File: bug8005391.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 7
Source File: bug8028616.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ParserCB cb = new ParserCB();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();

    htmlDoc.getParser().parse(new StringReader(text), cb, true);

    synchronized (lock) {
        if (!isCallbackInvoked) {
            lock.wait(5000);
        }
    }

    if (!isCallbackInvoked) {
        throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
    }

    if (exception != null) {
        throw exception;
    }
}
 
Example 8
Source File: bug8005391.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 9
Source File: bug8028616.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ParserCB cb = new ParserCB();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();

    htmlDoc.getParser().parse(new StringReader(text), cb, true);

    synchronized (lock) {
        if (!isCallbackInvoked) {
            lock.wait(5000);
        }
    }

    if (!isCallbackInvoked) {
        throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
    }

    if (exception != null) {
        throw exception;
    }
}
 
Example 10
Source File: bug8005391.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 11
Source File: bug8028616.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ParserCB cb = new ParserCB();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();

    htmlDoc.getParser().parse(new StringReader(text), cb, true);

    synchronized (lock) {
        if (!isCallbackInvoked) {
            lock.wait(5000);
        }
    }

    if (!isCallbackInvoked) {
        throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
    }

    if (exception != null) {
        throw exception;
    }
}
 
Example 12
Source File: bug8005391.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 13
Source File: bug8005391.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    int N = 10;

    for (int i = 0; i < N; i++) {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();

        String result = writer.toString();
        if (!result.contains("<tt><a")) {
            throw new RuntimeException("The <a> and <tt> tags are swapped");
        }
    }
}
 
Example 14
Source File: HTMLEditorKitWriterBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    String htmlDoc = "<pre><p> </pre>";
    try {
        HTMLEditorKit kit = new HTMLEditorKit();
        Class c = Class.forName(
                "javax.swing.text.html.parser.ParserDelegator");
        HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
        parser.parse(new CharArrayReader(htmlDoc.toCharArray()),
                htmlReader, true);
        htmlReader.flush();
        CharArrayWriter writer = new CharArrayWriter(1000);
        kit.write(writer, doc, 0, doc.getLength());
        writer.flush();
    } catch (Exception ex) {
        throw new RuntimeException("Test Failed " + ex);
    }
}
 
Example 15
Source File: HtmlSelection.java    From Ic2ExpReactorPlanner with GNU General Public License v2.0 6 votes vote down vote up
private static String convertToRTF(final String htmlStr) {

        OutputStream os = new ByteArrayOutputStream();
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();
        String rtfStr = null;

        String tempStr = htmlStr.replace("</font>", "#END_FONT#").replace("<br>", "#NEW_LINE#");
        InputStream is = new ByteArrayInputStream(tempStr.getBytes());
        try {
            Document doc = htmlEditorKit.createDefaultDocument();
            htmlEditorKit.read(is, doc, 0);
            rtfEditorKit.write(os, doc, 0, doc.getLength());
            rtfStr = os.toString();
            rtfStr = rtfStr.replace("#NEW_LINE#", "\\line ");
            rtfStr = rtfStr.replace("#END_FONT#", "\\cf0 ");
        } catch (IOException | BadLocationException e) {
            e.printStackTrace();
        }
        return rtfStr;
    }
 
Example 16
Source File: BrowserSwing.java    From Rel with Apache License 2.0 5 votes vote down vote up
private void setEnhancedOutputStyle(JTextPane pane) {
	pane.setContentType("text/html");
	pane.setEditable(false);
	pane.setEnabled(true);
	HTMLEditorKit editorKit = new HTMLEditorKit();
	HTMLDocument defaultDocument = (HTMLDocument) editorKit.createDefaultDocument();
	pane.setEditorKit(editorKit);
	pane.setDocument(defaultDocument);
	StyleSheet css = editorKit.getStyleSheet();
	for (String entry : style.getFormattedStyle())
		css.addRule(entry);
}
 
Example 17
Source File: HTMLDocumentEditor.java    From egdownloader with GNU General Public License v2.0 5 votes vote down vote up
public void openDocument() {
	try {
		File current = new File(".");
		JFileChooser chooser = new JFileChooser(current);
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.setFileFilter(new HTMLFileFilter());
		int approval = chooser.showSaveDialog(this);
		if (approval == JFileChooser.APPROVE_OPTION) {
			currentFile = chooser.getSelectedFile();
			setTitle(currentFile.getName());
			FileReader fr = new FileReader(currentFile);
			Document oldDoc = textPane.getDocument();
			if (oldDoc != null)
				oldDoc.removeUndoableEditListener(undoHandler);
			HTMLEditorKit editorKit = new HTMLEditorKit();
			document = (HTMLDocument) editorKit.createDefaultDocument();
			editorKit.read(fr, document, 0);
			document.addUndoableEditListener(undoHandler);
			textPane.setDocument(document);
			resetUndoManager();
		}
	} catch (BadLocationException ble) {
		System.err.println("BadLocationException: " + ble.getMessage());
	} catch (FileNotFoundException fnfe) {
		System.err.println("FileNotFoundException: " + fnfe.getMessage());
	} catch (IOException ioe) {
		System.err.println("IOException: " + ioe.getMessage());
	}
}
 
Example 18
Source File: CheckForUpdateAction.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
private JComponent createPanel(String html) {
	System.setProperty("awt.useSystemAAFontSettings", "on");
	final JEditorPane editorPane = new JEditorPane();    	
	HTMLEditorKit kit = new HTMLEditorKit();
	editorPane.setEditorKit(kit);
    editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editorPane.setFont(new Font("Arial", Font.PLAIN, 12));
    editorPane.setPreferredSize(new Dimension(350, 120));
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    editorPane.setBackground(new Color(234, 241, 248));        
   
    Document doc = kit.createDefaultDocument();
    editorPane.setDocument(doc);
    editorPane.setText(html);

    // Add Hyperlink listener to process hyperlinks
    editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(final HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                        editorPane.setToolTipText(e.getURL().toExternalForm());
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {                            
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());
                        editorPane.setToolTipText(null);
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {       
            	    FileUtil.openUrl(e.getURL().toString(), AboutAction.class);                       
            }
        }
    });        
    editorPane.addMouseListener(mouseListener);
    JScrollPane sp = new JScrollPane(editorPane);       
    return sp;
}
 
Example 19
Source File: AboutAction.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
private JComponent createPanel() {
	System.setProperty("awt.useSystemAAFontSettings", "on");
	final JEditorPane editorPane = new JEditorPane();    	
	HTMLEditorKit kit = new HTMLEditorKit();
	editorPane.setEditorKit(kit);
    editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editorPane.setFont(new Font("Arial", Font.PLAIN, 12));
    editorPane.setPreferredSize(new Dimension(350, 180));
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    editorPane.setBackground(new Color(234, 241, 248));        
    // add some styles to the html
    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule(".firstCol {margin-left: 25px; }");
    styleSheet.addRule(".secondCol {color: blue; }");
    Document doc = kit.createDefaultDocument();
    editorPane.setDocument(doc);
    editorPane.setText(
            "<html>" +
            "<body>" +
            "<table border='0px' BGCOLOR=\"#EAF1F8\">" +
            "<tr><td colspan=2>" +
            "<img src='" + ImageUtil.getImageURL("logo").toExternalForm() + "'>" +
            "</td></tr>" +
            "<tr><td class=\"firstCol\"><b>" + VERSION + "</b></td><td class=\"secondCol\">" + 
            	VERSION_NO + 
            "</td></tr>" +
            "<tr><td class=\"firstCol\"><b>" + BUILD + "</b></td><td class=\"secondCol\">" + 
            	ReleaseInfo.getBuildNumber() + " (" + BUILD_DATE + ")" + 
            "</td></tr>" +
            "<tr><td class=\"firstCol\"><b>" + SITE + "</b></td><td class=\"secondCol\">"+ 
            	"<a href=\"" + SITE_VALUE + "\">" + SITE_SMALL_VALUE + 
            "</a></td></tr>" +
            "<tr><td class=\"firstCol\"><b>" + COPYRIGHT + "</b></td><td class=\"secondCol\">" + 
            	DEVELOPER + 
            "</td></tr>" +
            "</table>" +
            "</body>" +
            "</html>"
    );

    // Add Hyperlink listener to process hyperlinks
    editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(final HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                        editorPane.setToolTipText(e.getURL().toExternalForm());
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {                            
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());
                        editorPane.setToolTipText(null);
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {       
            	    FileUtil.openUrl(e.getURL().toString(), AboutAction.class);                       
            }
        }
    });        
    editorPane.addMouseListener(mouseListener);
    JScrollPane sp = new JScrollPane(editorPane);       
    return sp;
}
 
Example 20
Source File: TutorialBrowser.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
private Component createContentPanel() {
	contentGbc = new GridBagConstraints();

	contentPanel = new JPanel(new GridBagLayout()) {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
		}
	};
	contentPanel.setBackground(Colors.WHITE);

	jEditorPane = new JEditorPane() {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
		}
	};
	jEditorPane.setEditable(false);
	contentGbc.gridx = 0;
	contentGbc.gridy = 0;
	contentGbc.weightx = 1.0f;
	contentGbc.fill = GridBagConstraints.HORIZONTAL;
	contentPanel.add(jEditorPane, contentGbc);

	// add filler at bottom
	contentGbc.gridy += 1;
	contentGbc.weighty = 1.0f;
	contentGbc.fill = GridBagConstraints.BOTH;
	contentPanel.add(new JLabel(), contentGbc);

	// prepare contentGbc for feedback form
	contentGbc.gridy += 1;
	contentGbc.weighty = 0.0f;
	contentGbc.fill = GridBagConstraints.HORIZONTAL;

	scrollPane = new ExtendedJScrollPane(contentPanel);
	scrollPane.setBorder(null);

	HTMLEditorKit kit = new HTMLEditorKit();
	jEditorPane.setEditorKit(kit);
	jEditorPane.setMargin(new Insets(0, 0, 0, 0));

	Document doc = kit.createDefaultDocument();
	jEditorPane.setDocument(doc);
	jEditorPane.setText(String.format(INFO_TEMPLATE, NO_TUTORIAL_SELECTED));
	jEditorPane.addHyperlinkListener(e -> {

		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			ActionStatisticsCollector.INSTANCE.log(ActionStatisticsCollector.TYPE_GETTING_STARTED,
					TUTORIAL_BROWSER_DOCK_KEY, "open_remote_url");
			RMUrlHandler.handleUrl(e.getURL().toString());
		}
	});
	return scrollPane;
}