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

The following examples show how to use javax.swing.text.html.HTMLEditorKit#read() . 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: FSwingHtml.java    From pra with MIT License 6 votes vote down vote up
public static HTMLDocument loadHtml(BufferedReader br){
//String html){//
	try {
		HTMLEditorKit kit = new HTMLEditorKit();
		HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
		doc.putProperty("IgnoreCharsetDirective"
				,Boolean.TRUE);//, new Boolean(true));

		kit.read(br,   doc,   0);  
	  br.close();
	  return doc;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;		
}
 
Example 2
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 3
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());
	}
}