/** * Extracts text from an HTML document and stores it in the document. * * @param filesInputStream An input stream pointing to the HTML document to be read. * @throws BadLocationException * @throws IOException */ private static char[] loadHTML(InputStream filesInputStream) throws IOException, BadLocationException { EditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); doc.putProperty("IgnoreCharsetDirective", true); kit.read(filesInputStream, doc, 0); char[] origText = doc.getText(0, doc.getLength()).toCharArray(); return origText; }
private void updateEditorKit(final EditorKit kit) { if (editorKit != null) { editorKit.deinstall(this); } EditorKit oldEditorKit = editorKit; if (kit != null) { kit.install(this); } editorKit = kit; firePropertyChange("editorKit", oldEditorKit, kit); }
@Override protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream out) throws IOException, BadLocationException { // Detect the encoding, using UTF8 if the encoding is not set. String enc = EncodingUtil.detectEncoding(doc); if (enc == null) { enc = "UTF8"; // NOI18N } try { // Test the encoding on a dummy stream. new OutputStreamWriter(new ByteArrayOutputStream(1), enc); // If that worked, we can go ahead with the encoding. Writer writer = new OutputStreamWriter(out, enc); kit.write(writer, doc, 0, doc.getLength()); } catch (UnsupportedEncodingException uee) { // Safest option is to write nothing, preserving the original file. IOException ioex = new IOException("Unsupported encoding " + enc); // NOI18N ErrorManager.getDefault() .annotate( ioex, NbBundle.getMessage( WadlEditorSupport.class, "MSG_WadlEditorSupport_Unsupported_Encoding", enc)); throw ioex; } }
public void read(final InputStream stream, final Object type) throws IOException { if (type instanceof String) { setContentType((String) type); } try { Document doc = getDocument(); doc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, Boolean.TRUE); editorKit.read(new InputStreamReader(stream), doc, 0); } catch (BadLocationException e) { } }
private void documentLoading(final InputStream str, final Document doc, final URL url) throws IOException { try { editorKit.read(str, doc, 0); } catch (ChangedCharSetException e) { try { doc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, Boolean.TRUE); doc.remove(0, doc.getLength()); final String htmlAttribute = e.getCharSetSpec(); final int charSetIndex = htmlAttribute.lastIndexOf("charset="); if (charSetIndex >= 0) { String charSet = htmlAttribute.substring(charSetIndex + 8); InputStreamReader reader = new InputStreamReader(url.openStream(), Charset.forName(charSet)); editorKit.read(reader, doc, 0); } } catch (BadLocationException e1) { } } catch (BadLocationException e) { } }
public void setPage(final URL page) throws IOException { if (page == null) { throw new IOException(Messages.getString("swing.03", "Page")); // $NON-NLS-1$ //$NON-NLS-2$ } String url = page.toString(); String baseUrl = getBaseURL(url); Document oldDoc = getDocument(); if (baseUrl != null && oldDoc != null && baseUrl.equals(oldDoc.getProperty(Document.StreamDescriptionProperty))) { scrollToReference(page.getRef()); return; } InputStream stream = getStream(page); if (stream == null) { return; } Document newDoc = editorKit.createDefaultDocument(); // Perhaps, it is reasonable only for HTMLDocument... if (newDoc instanceof HTMLDocument) { newDoc.putProperty(Document.StreamDescriptionProperty, baseUrl); newDoc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, new Boolean(false)); try { ((HTMLDocument) newDoc).setBase(new URL(baseUrl)); } catch (IOException e) { } } // TODO Asynch loading doesn't work with completely. // Also page property change event is written incorrectly now // (at the asynchrounous loading), because loading may not be // completed. // int asynchronousLoadPriority = getAsynchronousLoadPriority(newDoc); int asynchronousLoadPriority = -1; if (asynchronousLoadPriority >= 0) { setDocument(newDoc); AsynchLoad newThread = new AsynchLoad(asynchronousLoadPriority, stream, page); newThread.start(); if (newThread.successfulLoading) { changePage(page); } } else { try { documentLoading(stream, newDoc, page); stream.close(); setDocument(newDoc); changePage(page); } catch (IOException e) { } } }
// DefaultEditorKit does not provide a view factory. The private // class JEditorPane.PlainEditorKit does the exact same thing. public ViewFactory getViewFactory() { ViewFactory ret = delegate.getViewFactory(); if (ret != null) { if (Settings.debug) System.err.println("Delegating getViewFactory: " + ret); return ret; } else { if (Settings.debug) System.err.println("Providing own view factory"); return new ViewFactory() { public View create(Element elem) { return new WrappedPlainView(elem); } }; } }
private Action getAction(String key) { if (key == null) { return null; } // Try to find the action from kit. EditorKit kit = editorPane.getEditorKit(); if (kit == null) { // kit is cleared in closeDocument() return null; } Action[] actions = kit.getActions(); for (int i = 0; i < actions.length; i++) { if (key.equals(actions[i].getValue(Action.NAME))) { return actions[i]; } } return null; }
@Override protected void loadFromStreamToKit(StyledDocument doc, InputStream in, EditorKit kit) throws IOException, BadLocationException { // Detect the encoding to get optimized reader if UTF-8. String enc = EncodingUtil.detectEncoding(in); if (enc == null) { enc = "UTF8"; // NOI18N } try { Reader reader = new InputStreamReader(in, enc); kit.read(reader, doc, 0); } catch (CharConversionException cce) { } catch (UnsupportedEncodingException uee) { } }
public final String getContentType() { return ((editorKit != null) ? editorKit.getContentType() : null); }
// Here's where the font is set on the editor pane, and we also keep // track of the panes in use for future font updates. public void install(JEditorPane pane) { if (Settings.debug) System.err.println("Installing kit into pane"); delegate.install(pane); panes.add(pane); pane.setFont(Settings.font); }
public void write(Writer wr, Document doc, int pos, int length) throws IOException, BadLocationException { if (Settings.debug) System.err.println("Delegating write to Writer"); delegate.write(wr, doc, pos, length); }
public void write(OutputStream os, Document doc, int pos, int length) throws IOException, BadLocationException { if (Settings.debug) System.err.println("Delegating write to OutputStream"); delegate.write(os, doc, pos, length); }
public void read(Reader rd, Document doc, int pos) throws IOException, BadLocationException { if (Settings.debug) System.err.println("Delegating read from Reader"); delegate.read(rd, doc, pos); }
public void read(InputStream is, Document doc, int pos) throws IOException, BadLocationException { if (Settings.debug) System.err.println("Delegating read from InputStream"); delegate.read(is, doc, pos); }
public Document createDefaultDocument() { Document ret = delegate.createDefaultDocument(); if (Settings.debug) System.err.println("Delegating createDefaultDocument: " + ret); return ret; }
public Caret createCaret() { if (Settings.debug) System.err.println("Delegating createCaret"); return delegate.createCaret(); }
public Action[] getActions() { if (Settings.debug) System.err.println("Delegating getActions"); return delegate.getActions(); }
// Delegated methods: public String getContentType() { String ret = delegate.getContentType(); if (Settings.debug) System.err.println("Delegating getContentType: " + ret); return ret; }
private void updateDocument(final EditorKit kit) { if (kit != null) { setDocument(kit.createDefaultDocument()); } }
public void deinstall(JEditorPane pane) { if (Settings.debug) System.err.println("Deinstalling kit from pane"); panes.remove(pane); delegate.deinstall(pane); }
public void addMessage(ChatMessage message) { boolean isSendingContent = message.getMessage().equals(ChatConstants.SENT_CONTENT_TAG); boolean isReceivingContent = message.getMessage().equals(ChatConstants.RECEIVE_CONTENT_TAG); if (!isSendingContent && !isReceivingContent) { EmoticonHandler.processMessage(message); } text = new StringBuilder(); String formattedTime = formatter.format(message.getTime()); String color = colors.get(message.getSender().getEmail()); color = color == null ? REMOTE_USER_COLOR : color; if (lastMessage == null || !lastMessage.getSender().equals(message.getSender()) || isSendingContent || isReceivingContent) { text.append(BR); text.append(TABLE_OPEN); text.append(TR_DEFAULT); text.append(color); text.append(MAJOR_TAG); if (isReceivingContent) { text.append(TD_WIDTH_RECEIVING_COINTENT); } else { text.append(TD_USER_WRITER); } text.append(FONT_OPEN_SIZE_11); text.append(filter(message)); text.append(FONT_CLOSE); text.append(TD_CLOSE); text.append(TD_TIME_USER_WRITER); text.append(FONT_OPEN_SIZE_10); text.append(formattedTime); text.append(FONT_CLOSE); text.append(TD_CLOSE); text.append(TR_CLOSE); text.append(TABLE_CLOSE); } else { if (!isReceivingContent && !isSendingContent && !wasReceivedAlert && !wasSenderdAlert) { text.append(TABLE_OPEN); text.append(TR_OPEN); text.append(TD_SPACE); text.append(TD_CLOSE); text.append(TD_TIME); text.append(color); text.append(MAJOR_TAG); text.append(DIV_TAG); text.append(FONT_OPEN_SIZE_10); text.append(formattedTime); text.append(DIV_CLOSE); text.append(FONT_CLOSE); text.append(TD_CLOSE); text.append(TR_CLOSE); text.append(TABLE_CLOSE); } } text.append(TABLE_OPEN); text.append(TR_OPEN); text.append(TD_MESSAGE); text.append(FONT_OPEN_SIZE_13); text.append(message.getMessage()); text.append(FONT_CLOSE); text.append(TD_CLOSE); text.append(TR_CLOSE); text.append(TABLE_CLOSE); this.lastMessage = message; try { int length = area.getDocument().getLength(); Reader r = new StringReader(text.toString()); EditorKit kit = area.getEditorKit(); kit.read(r, area.getDocument(), length); } catch (IOException e) { LOG.error(e, e); } catch (BadLocationException e) { LOG.error(e, e); } scrolled = true; wasReceivedAlert = isReceivingContent; wasSenderdAlert = isSendingContent; }