下面列出了javax.swing.text.EditorKit#createDefaultDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Document createDocument(EditorKit kit, URL page)
{
// we have pageProperties, because we can be in situation that
// old page is being removed & new page is not yet created...
// we need somewhere store important data.
Document doc = kit.createDefaultDocument();
if (pageProperties != null)
{
// transfer properties discovered in stream to the
// document property collection.
for (Enumeration<String> e = pageProperties.keys(); e
.hasMoreElements();)
{
Object key = e.nextElement();
doc.putProperty(key, pageProperties.get(key));
}
}
if (doc.getProperty(Document.StreamDescriptionProperty) == null)
{
doc.putProperty(Document.StreamDescriptionProperty, page);
}
return doc;
}
/**
* 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 "";
}
private StyledDocument createDocument(EditorKit kit) {
Document doc = kit.createDefaultDocument();
if (doc instanceof StyledDocument) {
return (StyledDocument)doc;
} else {
return new org.openide.text.FilterDocument(doc);
}
}
/** @return The writer or <code>null</code>. */
private Writer getWriterFromKit(File file, FileObject fo, FileLock lock, 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();
return new DocWriter(doc, fo, lock, file, kit, null, null);
}
return null;
}
private Document initializeModel(EditorKit kit, URL page) {
Document doc = kit.createDefaultDocument();
if (doc.getProperty(Document.StreamDescriptionProperty) == null) {
doc.putProperty(Document.StreamDescriptionProperty, page);
}
return doc;
}
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);
}
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);
}
private Source getSourceForMimeType(String mimeType) {
EditorKit kit = new DefaultEditorKit();
Document doc = kit.createDefaultDocument();
doc.putProperty("mimeType", mimeType);
return Source.create(doc);
}
/** @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;
}
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();
}
}
}