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

The following examples show how to use javax.swing.text.EditorKit#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: GuardedBlockTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
    if (guardedEditor == null) {
        guardedEditor = new FormGEditor();
        GuardedSectionsFactory gFactory = GuardedSectionsFactory.find("text/x-java");
        if (gFactory != null) {
            guardedProvider = gFactory.create(guardedEditor);
        }
    }

    if (guardedProvider != null) {
        guardedEditor.doc = doc;
        Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
        Reader reader = guardedProvider.createGuardedReader(stream, c);
        try {
            kit.read(reader, doc, 0);
        } finally {
            reader.close();
        }
    } else {
        super.loadFromStreamToKit(doc, stream, kit);
    }
}
 
Example 2
Source File: BIEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit)
        throws IOException, BadLocationException {
    
    if (guardedEditor == null) {
        guardedEditor = new BIGES();
        GuardedSectionsFactory gFactory = GuardedSectionsFactory.find(((DataEditorSupport.Env) env).getMimeType());
        if (gFactory != null) {
            guardedProvider = gFactory.create(guardedEditor);
        }
    }
    
    if (guardedProvider != null) {
        guardedEditor.doc = doc;
        Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
        Reader reader = guardedProvider.createGuardedReader(stream, c);
        try {
            kit.read(reader, doc, 0);
        } finally {
            reader.close();
        }
    } else {
        super.loadFromStreamToKit(doc, stream, kit);
    }
}
 
Example 3
Source File: FormEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
    if (guardedEditor == null) {
        guardedEditor = new FormGEditor();
        GuardedSectionsFactory gFactory = GuardedSectionsFactory.find(((DataEditorSupport.Env) env).getMimeType());
        if (gFactory != null) {
            guardedProvider = gFactory.create(guardedEditor);
        }
    }
    
    if (guardedProvider != null) {
        guardedEditor.doc = doc;
        Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
        Reader reader = guardedProvider.createGuardedReader(stream, c);
        try {
            kit.read(reader, doc, 0);
        } finally {
            reader.close();
        }
    } else {
        super.loadFromStreamToKit(doc, stream, kit);
    }
}
 
Example 4
Source File: BrowserPane.java    From SwingBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
void read(InputStream in, Document doc) throws IOException
{
    EditorKit kit = getEditorKit();

    try
    {
        kit.read(in, doc, 0);

    } catch (ChangedCharSetException ccse)
    {
        // ignored, may be in the future will be processed
        throw ccse;
    } catch (BadLocationException ble)
    {
        throw new IOException(ble);
    }

}
 
Example 5
Source File: StringUtils.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Given some html, extracts its text.
 */
public static String extractTextFromHTML(String html) {
    try {
        EditorKit kit = new HTMLEditorKit();
        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.
        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 (Exception e) {
    }
    return "";
}
 
Example 6
Source File: XMLJ2eeEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden method from CloneableEditorSupport.
 */
protected void loadFromStreamToKit (StyledDocument doc, InputStream stream,
                                        EditorKit kit)
    			throws IOException, BadLocationException {
    // kit and kit() are not accessible so we pretend
    // to create the kit; actually this should just return kit.
    EditorKit k = this.createEditorKit();
    InputStreamReader isr = new InputStreamReader(stream, "UTF8"); // NOI18N
    Reader reader = new BufferedReader(isr);
    k.read(reader, doc, 0);
    reader.close();
}
 
Example 7
Source File: ServletEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Overriding the default loading from stream - need to look at encoding */
@Override
protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
    FileObject fo = getServlet().getPrimaryFile();
    String encoding =  (String) fo.getAttribute(ATTR_FILE_ENCODING); //NOI18N
    
    if (encoding == null) {
        encoding = "ISO-8859-1"; // NOI18N
    }
    InputStreamReader reader = new InputStreamReader(stream, encoding);
    kit.read(reader, doc, 0);
}
 
Example 8
Source File: PropertiesEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the file from the stream, filter the guarded section
 * comments, and mark the sections in the editor. Overrides superclass method. 
 * @param document the document to read into
 * @param inputStream the open stream to read from
 * @param editorKit the associated editor kit
 * @throws <code>IOException</code> if there was a problem reading the file
 * @throws <code>BadLocationException</code> should not normally be thrown
 * @see #saveFromKitToStream
 */
@Override
protected void loadFromStreamToKit(StyledDocument document, InputStream inputStream, EditorKit editorKit) throws IOException, BadLocationException {
    final Charset c = getCharset();
    final Reader reader = new BufferedReader(new InputStreamReader(inputStream, c));

    try {
        editorKit.read(reader, document, 0);
    } finally {
        reader.close();
    }
}
 
Example 9
Source File: HTML.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void convertHtmlToText(Reader reader, Writer writer) throws IOException, BadLocationException {

        EditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = new HTMLDocument();
        kit.read(reader, doc, 0);

        HTMLtoPlainTextWriter2 x = new HTMLtoPlainTextWriter2(writer, doc);
        x.write();
        writer.close();

    }
 
Example 10
Source File: InfoPane.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setText(String text)
{
	//This is done so the vertical scroll bar goes back up to the top when the text is changed
	EditorKit kit = textPane.getEditorKit();
	Document newDoc = kit.createDefaultDocument();
	try
	{
		kit.read(new StringReader(text), newDoc, 0);
	}
	catch (IOException | BadLocationException ex)
	{
		throw new UnreachableError(ex);
	}
	textPane.setDocument(newDoc);
}
 
Example 11
Source File: InfoPane.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setText(String text)
{
	//This is done so the vertical scroll bar goes back up to the top when the text is changed
	EditorKit kit = textPane.getEditorKit();
	Document newDoc = kit.createDefaultDocument();
	try
	{
		kit.read(new StringReader(text), newDoc, 0);
	}
	catch (IOException | BadLocationException ex)
	{
		throw new UnreachableError(ex);
	}
	textPane.setDocument(newDoc);
}
 
Example 12
Source File: Editor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit)
throws IOException, BadLocationException {
    Editor.this.doc = doc;
    kit.read(stream, doc, 0);
}
 
Example 13
Source File: Utils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
    kit.read(new InputStreamReader(stream, charset), doc, 0);
}
 
Example 14
Source File: XmlMultiViewEditorSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit)
throws IOException, BadLocationException {
    kit.read(new InputStreamReader(stream, dObj.getEncodingHelper().getEncoding()), doc, 0);
}
 
Example 15
Source File: EncodedReaderFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** @return The reader or <code>null</code>. */
private Reader getReaderFromKit(File file, FileObject fo, String mimeType) throws FileNotFoundException {
    EditorKit kit = CloneableEditorSupport.getEditorKit(mimeType);
    if (kit.getContentType().equalsIgnoreCase("text/plain") && "text/x-dtd".equalsIgnoreCase(mimeType)) {
         // Use XML kit for DTDs if not defined otherwise
        kit = CloneableEditorSupport.getEditorKit("text/xml");
    }
    //System.out.println("  KIT for "+mimeType+" = "+kit);
    if (kit != null) {
        Document doc = kit.createDefaultDocument();
        InputStream stream = null;
        try {
            if (file != null) {
                stream = new FileInputStream(file);
            } else {
                stream = fo.getInputStream();
            }
            kit.read(stream, doc, 0);
            String text = doc.getText(0, doc.getLength());
            //System.out.println("  TEXT = "+text);
            doc = null; // Release it, we have the text
            return new StringReader(text);
        } catch (IOException ioex) {
            FileNotFoundException fnfex;
            if (file != null) {
                fnfex = new FileNotFoundException("Can not read file "+file.getAbsolutePath());
            } else {
                fnfex = new FileNotFoundException("Can not read file "+fo);
            }
            fnfex.initCause(ioex);
            throw fnfex;
        } catch (BadLocationException blex) { // Something wrong???
            ErrorManager.getDefault().notify(blex);
        } finally {
            if (stream != null) {
                try { stream.close(); } catch (IOException e) {}
            }
        }
    }
    return null;
}
 
Example 16
Source File: UndoFixedEditorPane.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private void setText(String t, String contentType) {
    synchronized (setTextLock) {
        if (t == null) {
            t = "";
        }

        if (!t.equals(getText())) {
            boolean plain = t.length() > Configuration.syntaxHighlightLimit.get();
            if (plain) {
                contentType = "text/plain";
                originalContentType = getContentType();
                changeContentType(contentType);
            } else if (originalContentType != null) {
                changeContentType(originalContentType);
                originalContentType = null;
            }

            Stopwatch sw = Stopwatch.startNew();
            try {
                Reader r = new StringReader(t);
                EditorKit kit = createEditorKitForContentType(contentType);
                Document doc = kit.createDefaultDocument();
                if (doc instanceof SyntaxDocument) {
                    ((SyntaxDocument) doc).setIgnoreUpdate(true);
                }

                kit.read(r, doc, 0);

                if (doc instanceof SyntaxDocument) {
                    ((SyntaxDocument) doc).setIgnoreUpdate(false);
                }

                setDocument(doc);
            } catch (BadLocationException | IOException ex) {
                Logger.getLogger(UndoFixedEditorPane.class.getName()).log(Level.SEVERE, null, ex);
            }

            sw.stop();
            if (!plain && sw.getElapsedMilliseconds() > 5000) {
                Logger.getLogger(UndoFixedEditorPane.class.getName()).log(Level.WARNING, "Syntax highlightig took long time. You can try to decrease the syntax highlight limit in advanced settings.");
            }

            clearUndos();
        }
    }
}