📄 abstractdocument.java
字号:
*/ public boolean containsAttributes(AttributeSet attrs) { return attributes.containsAttributes(attrs); } /** * Returns a copy of the attributes of this element. * * @return a copy of the attributes of this element */ public AttributeSet copyAttributes() { return attributes.copyAttributes(); } /** * Returns the attribute value with the specified key. If this attribute * is not defined in this element and this element has a resolving * parent, the search goes upward to the resolve parent chain. * * @param key the key of the requested attribute * * @return the attribute value for <code>key</code> of <code>null</code> * if <code>key</code> is not found locally and cannot be resolved * in this element's resolve parents */ public Object getAttribute(Object key) { return attributes.getAttribute(key); } /** * Returns the number of defined attributes in this element. * * @return the number of defined attributes in this element */ public int getAttributeCount() { return attributes.getAttributeCount(); } /** * Returns the names of the attributes of this element. * * @return the names of the attributes of this element */ public Enumeration getAttributeNames() { return attributes.getAttributeNames(); } /** * Returns the resolve parent of this element. * This is taken from the AttributeSet, but if this is null, * this method instead returns the Element's parent's * AttributeSet * * @return the resolve parent of this element * * @see #setResolveParent(AttributeSet) */ public AttributeSet getResolveParent() { if (attributes.getResolveParent() != null) return attributes.getResolveParent(); return element_parent.getAttributes(); } /** * Returns <code>true</code> if an attribute with the specified name * is defined in this element, <code>false</code> otherwise. * * @param attrName the name of the requested attributes * * @return <code>true</code> if an attribute with the specified name * is defined in this element, <code>false</code> otherwise */ public boolean isDefined(Object attrName) { return attributes.isDefined(attrName); } /** * Returns <code>true</code> if the specified <code>AttributeSet</code> * is equal to this element's <code>AttributeSet</code>, <code>false</code> * otherwise. * * @param attrs the attributes to compare this element to * * @return <code>true</code> if the specified <code>AttributeSet</code> * is equal to this element's <code>AttributeSet</code>, * <code>false</code> otherwise */ public boolean isEqual(AttributeSet attrs) { return attributes.isEqual(attrs); } /** * Returns the attributes of this element. * * @return the attributes of this element */ public AttributeSet getAttributes() { return this; } /** * Returns the {@link Document} to which this element belongs. * * @return the {@link Document} to which this element belongs */ public Document getDocument() { return AbstractDocument.this; } /** * Returns the child element at the specified <code>index</code>. * * @param index the index of the requested child element * * @return the requested element */ public abstract Element getElement(int index); /** * Returns the name of this element. * * @return the name of this element */ public String getName() { return (String) getAttribute(NameAttribute); } /** * Returns the parent element of this element. * * @return the parent element of this element */ public Element getParentElement() { return element_parent; } /** * Returns the offset inside the document model that is after the last * character of this element. * * @return the offset inside the document model that is after the last * character of this element */ public abstract int getEndOffset(); /** * Returns the number of child elements of this element. * * @return the number of child elements of this element */ public abstract int getElementCount(); /** * Returns the index of the child element that spans the specified * offset in the document model. * * @param offset the offset for which the responsible element is searched * * @return the index of the child element that spans the specified * offset in the document model */ public abstract int getElementIndex(int offset); /** * Returns the start offset if this element inside the document model. * * @return the start offset if this element inside the document model */ public abstract int getStartOffset(); /** * Prints diagnostic output to the specified stream. * * @param stream the stream to write to * @param indent the indentation level */ public void dump(PrintStream stream, int indent) { StringBuffer b = new StringBuffer(); for (int i = 0; i < indent; ++i) b.append(' '); b.append('<'); b.append(getName()); // Dump attributes if there are any. if (getAttributeCount() > 0) { b.append('\n'); Enumeration attNames = getAttributeNames(); while (attNames.hasMoreElements()) { for (int i = 0; i < indent + 2; ++i) b.append(' '); Object attName = attNames.nextElement(); b.append(attName); b.append('='); Object attribute = getAttribute(attName); b.append(attribute); b.append('\n'); } } b.append(">\n"); // Dump element content for leaf elements. if (isLeaf()) { for (int i = 0; i < indent + 2; ++i) b.append(' '); int start = getStartOffset(); int end = getEndOffset(); b.append('['); b.append(start); b.append(','); b.append(end); b.append("]["); try { b.append(getDocument().getText(start, end - start)); } catch (BadLocationException ex) { AssertionError err = new AssertionError("BadLocationException " + "must not be thrown " + "here."); err.initCause(ex); throw err; } b.append("]\n"); } stream.print(b.toString()); // Dump child elements if any. int count = getElementCount(); for (int i = 0; i < count; ++i) { Element el = getElement(i); if (el instanceof AbstractElement) ((AbstractElement) el).dump(stream, indent + 2); } } } /** * An implementation of {@link Element} to represent composite * <code>Element</code>s that contain other <code>Element</code>s. */ public class BranchElement extends AbstractElement { /** The serialization UID (compatible with JDK1.5). */ private static final long serialVersionUID = -6037216547466333183L; /** The child elements of this BranchElement. */ private Element[] children = new Element[0]; /** * Creates a new <code>BranchElement</code> with the specified * parent and attributes. * * @param parent the parent element of this <code>BranchElement</code> * @param attributes the attributes to set on this * <code>BranchElement</code> */ public BranchElement(Element parent, AttributeSet attributes) { super(parent, attributes); } /** * Returns the children of this <code>BranchElement</code>. * * @return the children of this <code>BranchElement</code> */ public Enumeration children() { if (children.length == 0) return null; Vector tmp = new Vector(); for (int index = 0; index < children.length; ++index) tmp.add(children[index]); return tmp.elements(); } /** * Returns <code>true</code> since <code>BranchElements</code> allow * child elements. * * @return <code>true</code> since <code>BranchElements</code> allow * child elements */ public boolean getAllowsChildren() { return true; } /** * Returns the child element at the specified <code>index</code>. * * @param index the index of the requested child element * * @return the requested element */ public Element getElement(int index) { if (index < 0 || index >= children.length) return null; return children[index]; } /** * Returns the number of child elements of this element. * * @return the number of child elements of this element */ public int getElementCount() { return children.length; } /** * Returns the index of the child element that spans the specified * offset in the document model. * * @param offset the offset for which the responsible element is searched * * @return the index of the child element that spans the specified * offset in the document model */ public int getElementIndex(int offset) { // If offset is less than the start offset of our first child, // return 0 if (offset < getStartOffset()) return 0; // XXX: There is surely a better algorithm // as beginning from first element each time. for (int index = 0; index < children.length - 1; ++index) { Element elem = children[index]; if ((elem.getStartOffset() <= offset) && (offset < elem.getEndOffset())) return index; // If the next element's start offset is greater than offset // then we have to return the closest Element, since no Elements // will contain the offset if (children[index + 1].getStartOffset() > offset) { if ((offset - elem.getEndOffset()) > (children[index + 1].getStartOffset() - offset)) return index + 1; else return index; } } // If offset is greater than the index of the last element, return // the index of the last element. return getElementCount() - 1; } /** * Returns the offset inside the document model that is after the last * character of this element. * This is the end offset of the last child element. If this element * has no children, this method throws a <code>NullPointerException</code>. * * @return the offset inside the document model that is after the last * character of this element * * @throws NullPointerException if this branch element has no children */ public int getEndOffset() { if (getElementCount() == 0) throw new NullPointerException("This BranchElement has no children."); return children[children.length - 1].getEndOffset(); } /** * Returns the name of this element. This is {@link #ParagraphElementName} * in this case. * * @return the name of this element */ public String getName() { return ParagraphElementName; } /** * Returns the start offset of this element inside the document model. * This is the start offset of the first child element. If this element * has no children, this method throws a <code>NullPointerException</code>. * * @return the start offset of this element inside the document model * * @throws NullPointerException if this branch element has no children */ public int getStartOffset() { if (getElementCount() == 0) throw new NullPointerException("This BranchElement has no children."); return children[0].getStartOffset(); } /** * Returns <code>false</code> since <code>BranchElement</code> are no * leafes. * * @return <code>false</code> since <code>BranchElement</code> are no * leafes */ public boolean isLeaf() { return false; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -