📄 htmldocument.java
字号:
return null; } /** * Sets the content type language used for style sheets that do not * explicitly specify the type. The default is text/css. * @param contentType the content type language for the style sheets */ /* public */ void setDefaultStyleSheetType(String contentType) { putProperty(StyleType, contentType); } /** * Returns the content type language used for style sheets. The default * is text/css. * @return the content type language used for the style sheets */ /* public */ String getDefaultStyleSheetType() { String retValue = (String)getProperty(StyleType); if (retValue == null) { return "text/css"; } return retValue; } /** * Sets the parser that is used by the methods that insert html * into the existing document, such as <code>setInnerHTML</code>, * and <code>setOuterHTML</code>. * <p> * <code>HTMLEditorKit.createDefaultDocument</code> will set the parser * for you. If you create an <code>HTMLDocument</code> by hand, * be sure and set the parser accordingly. * @param parser the parser to be used for text insertion * * @since 1.3 */ public void setParser(HTMLEditorKit.Parser parser) { this.parser = parser; putProperty("__PARSER__", null); } /** * Returns the parser that is used when inserting HTML into the existing * document. * @return the parser used for text insertion * * @since 1.3 */ public HTMLEditorKit.Parser getParser() { Object p = getProperty("__PARSER__"); if (p instanceof HTMLEditorKit.Parser) { return (HTMLEditorKit.Parser)p; } return parser; } /** * Replaces the children of the given element with the contents * specified as an HTML string. * <p>This will be seen as at least two events, n inserts followed by * a remove. * <p>For this to work correcty, the document must have an * <code>HTMLEditorKit.Parser</code> set. This will be the case * if the document was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the branch element whose children will be replaced * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalArgumentException if <code>elem</code> is a leaf * @throws IllegalStateException if an <code>HTMLEditorKit.Parser</code> * has not been defined * @since 1.3 */ public void setInnerHTML(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null && elem.isLeaf()) { throw new IllegalArgumentException ("Can not set inner HTML of a leaf"); } if (elem != null && htmlText != null) { int oldCount = elem.getElementCount(); int insertPosition = elem.getStartOffset(); insertHTML(elem, elem.getStartOffset(), htmlText, true); if (elem.getElementCount() > oldCount) { // Elements were inserted, do the cleanup. removeElements(elem, elem.getElementCount() - oldCount, oldCount); } } } /** * Replaces the given element in the parent with the contents * specified as an HTML string. * <p>This will be seen as at least two events, n inserts followed by * a remove. * <p>When replacing a leaf this will attempt to make sure there is * a newline present if one is needed. This may result in an additional * element being inserted. Consider, if you were to replace a character * element that contained a newline with <img> this would create * two elements, one for the image, ane one for the newline. * <p>If you try to replace the element at length you will most likely * end up with two elements, eg setOuterHTML(getCharacterElement * (getLength()), "blah") will result in two leaf elements at the end, * one representing 'blah', and the other representing the end element. * <p>For this to work correcty, the document must have an * HTMLEditorKit.Parser set. This will be the case if the document * was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the branch element whose children will be replaced * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalStateException if an HTMLEditorKit.Parser has not * been set * @since 1.3 */ public void setOuterHTML(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null && elem.getParentElement() != null && htmlText != null) { int start = elem.getStartOffset(); int end = elem.getEndOffset(); int startLength = getLength(); // We don't want a newline if elem is a leaf, and doesn't contain // a newline. boolean wantsNewline = !elem.isLeaf(); if (!wantsNewline && (end > startLength || getText(end - 1, 1).charAt(0) == NEWLINE[0])){ wantsNewline = true; } Element parent = elem.getParentElement(); int oldCount = parent.getElementCount(); insertHTML(parent, start, htmlText, wantsNewline); // Remove old. int newLength = getLength(); if (oldCount != parent.getElementCount()) { int removeIndex = parent.getElementIndex(start + newLength - startLength); removeElements(parent, removeIndex, 1); } } } /** * Inserts the HTML specified as a string at the start * of the element. * <p>For this to work correcty, the document must have an * <code>HTMLEditorKit.Parser</code> set. This will be the case * if the document was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the branch element to be the root for the new text * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalStateException if an HTMLEditorKit.Parser has not * been set on the document * @since 1.3 */ public void insertAfterStart(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null && elem.isLeaf()) { throw new IllegalArgumentException ("Can not insert HTML after start of a leaf"); } insertHTML(elem, elem.getStartOffset(), htmlText, false); } /** * Inserts the HTML specified as a string at the end of * the element. * <p> If <code>elem</code>'s children are leaves, and the * character at a <code>elem.getEndOffset() - 1</code> is a newline, * this will insert before the newline so that there isn't text after * the newline. * <p>For this to work correcty, the document must have an * <code>HTMLEditorKit.Parser</code> set. This will be the case * if the document was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the element to be the root for the new text * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalStateException if an HTMLEditorKit.Parser has not * been set on the document * @since 1.3 */ public void insertBeforeEnd(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null && elem.isLeaf()) { throw new IllegalArgumentException ("Can not set inner HTML before end of leaf"); } int offset = elem.getEndOffset(); if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() && getText(offset - 1, 1).charAt(0) == NEWLINE[0]) { offset--; } insertHTML(elem, offset, htmlText, false); } /** * Inserts the HTML specified as a string before the start of * the given element. * <p>For this to work correcty, the document must have an * <code>HTMLEditorKit.Parser</code> set. This will be the case * if the document was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the element to be the root for the new text * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalStateException if an HTMLEditorKit.Parser has not * been set on the document * @since 1.3 */ public void insertBeforeStart(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null) { Element parent = elem.getParentElement(); if (parent != null) { insertHTML(parent, elem.getStartOffset(), htmlText, false); } } } /** * Inserts the HTML specified as a string after the * the end of the given element. * <p>For this to work correcty, the document must have an * <code>HTMLEditorKit.Parser</code> set. This will be the case * if the document was created from an HTMLEditorKit via the * <code>createDefaultDocument</code> method. * * @param elem the element to be the root for the new text * @param htmlText the string to be parsed and assigned to <code>elem</code> * @throws IllegalStateException if an HTMLEditorKit.Parser has not * been set on the document * @since 1.3 */ public void insertAfterEnd(Element elem, String htmlText) throws BadLocationException, IOException { verifyParser(); if (elem != null) { Element parent = elem.getParentElement(); if (parent != null) { int offset = elem.getEndOffset(); if (offset > getLength()) { offset--; } else if (elem.isLeaf() && getText(offset - 1, 1). charAt(0) == NEWLINE[0]) { offset--; } insertHTML(parent, offset, htmlText, false); } } } /** * Returns the element that has the given id <code>Attribute</code>. * If the element can't be found, <code>null</code> is returned. * Note that this method works on an <code>Attribute</code>, * <i>not</i> a character tag. In the following HTML snippet: * <code><a id="HelloThere"></code> the attribute is * 'id' and the character tag is 'a'. * This is a convenience method for * <code>getElement(RootElement, HTML.Attribute.id, id)</code>. * This is not thread-safe. * * @param id the string representing the desired <code>Attribute</code> * @return the element with the specified <code>Attribute</code> * or <code>null</code> if it can't be found, * or <code>null</code> if <code>id</code> is <code>null</code> * @see javax.swing.text.html.HTML.Attribute * @since 1.3 */ public Element getElement(String id) { if (id == null) { return null; } return getElement(getDefaultRootElement(), HTML.Attribute.ID, id, true); } /** * Returns the child element of <code>e</code> that contains the * attribute, <code>attribute</code> with value <code>value</code>, or * <code>null</code> if one isn't found. This is not thread-safe. * * @param e the root element where the search begins * @param attribute the desired <code>Attribute</code> * @param value the values for the specified <code>Attribute</code> * @return the element with the specified <code>Attribute</code> * and the specified <code>value</code>, or <code>null</code> * if it can't be found * @see javax.swing.text.html.HTML.Attribute * @since 1.3 */ public Element getElement(Element e, Object attribute, Object value) { return getElement(e, attribute, value, true); } /** * Returns the child element of <code>e</code> that contains the * attribute, <code>attribute</code> with value <code>value</code>, or * <code>null</code> if one isn't found. This is not thread-safe. * <p> * If <code>searchLeafAttributes</code> is true, and <code>e</code> is * a leaf, any attributes that are instances of <code>HTML.Tag</code> * with a value that is an <code>AttributeSet</code> will also be checked. * * @param e the root element where the search begins * @param attribute the desired <code>Attribute</code> * @param value the values for the specified <code>Attribute</code> * @return the element with the specified <code>Attribute</code> * and the specified <code>value</code>, or <code>null</code> * if it can't be found * @see javax.swing.text.html.HTML.Attribute */ private Element getElement(Element e, Object attribute, Object value, boolean searchLeafAttributes) { AttributeSet attr = e.getAttributes(); if (attr != null && attr.isDefined(attribute)) { if (value.equals(attr.getAttribute(attribute))) { return e; } } if (!e.isLeaf()) { for (int counter = 0, maxCounter = e.getElementCount(); counter < maxCounter; counter++) { Element retValue = getElement(e.getElement(counter), attribute, value, searchLeafAttributes); if (retValue != null) { return retValue; } } } else if (searchLeafAttributes && attr != null) { // For some leaf elements we store the actual attributes inside // the AttributeSet of the Element (such as anchors). Enumeration names = attr.getAttributeNames(); if (names != null) { while (names.hasMoreElements()) { Object name = names.nextElement(); if ((name instanceof HTML.Tag) && (attr.getAttribute(name) instanceof AttributeSet)) { AttributeSet check = (AttributeSet)attr. getAttribute(name); if (check.isDefined(attribute) && value.equals(check.getAttribute(attribute))) { return e; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -