📄 element.java
字号:
return t.isPartOfParentText() ? t.getText(): ""; } //-- Namespaceable --// /** * Sets the namespace. * If ns is null, the default namespace is assumed (not necessary * {@link Namespace#NO_NAMESPACE}. * <p>According W3C/DOM, unlike element, an attribute doesn't allow * a namespace that has an URI but without a prefix. */ public final void setNamespace(Namespace ns) { checkWritable(); if (ns == null) { if (_ns != null && _ns.getPrefix().length() == 0) return; //nothing to do ns = getNamespace(""); if (ns == null) ns = Namespace.NO_NAMESPACE; } final Namespace old = getNamespace(ns.getPrefix()); if (old != null && old.equals(ns)) ns = old; //re-use if already defined _ns = ns; } /** Sets the namespace. */ public final void setNamespace(String prefix, String nsURI) { if (nsURI == null) nsURI = ""; final Namespace ns = getNamespace(prefix); if (ns != null) { if (ns.getURI().equals(nsURI)) { setNamespace(ns); return; } } setNamespace(new Namespace(prefix, nsURI)); } public final Namespace getNamespace() { return _ns; } public final String getTagName() { return _ns.tagNameOf(_lname); } public final void setTagName(String tname) { checkWritable(); int kp = tname.indexOf(':'); String prefix = kp >= 0 ? tname.substring(0, kp): ""; String lname = kp >= 0 ? tname.substring(kp + 1): tname; setPrefix(prefix); setLocalName(lname); } public final String getLocalName() { return _lname; } public final void setLocalName(String lname) { checkWritable(); Verifier.checkElementName(lname, getLocator()); _lname = lname; } //-- Item --// /** Clear the modification flag. * <p>Note: if both includingDescendant and * {@link #isAttributeModificationAware} are true, attributes' * modified flags are cleaned. */ public void clearModified(boolean includingDescendant) { if (includingDescendant && _attrModAware) { for (final Iterator it = _attrs.iterator(); it.hasNext();) ((Item)it.next()).clearModified(true); } super.clearModified(includingDescendant); } public Item clone(boolean preserveModified) { Element elem = (Element)super.clone(preserveModified); if (_addNamespaces != null) { elem._addNamespaces = new LinkedHashMap(); elem._addNamespaces.putAll(_addNamespaces); } if (_attrs != null) { elem._attrs = elem.newAttrArray(); //NOTE: AttrArray is an inner class, so we must use the right //object to create the array. Here is 'elem'. for (final Iterator it = _attrs.iterator(); it.hasNext();) { Item v = ((Item)it.next()).clone(preserveModified); boolean bClearModified = !preserveModified || !v.isModified(); elem._attrs.add(v); //v becomes modified (v.setParent is called) if (bClearModified) v.clearModified(false); } } elem._modified = preserveModified && _modified; return elem; } /** * Gets the tag name of the element -- the name with prefix. * To get the local name, use getLocalName. */ public final String getName() { return getTagName(); } /** * Sets the tag name of the element. * It will affect the local name and the namespace's prefix. */ public final void setName(String tname) { setTagName(tname); } /** Returns the catenation of {@link Textual} children; never null. * Note: both <tag/> and <tag></tag> returns an * empty string. To tell the difference, check the number of children. * @see #getText(boolean) */ public final String getText() { if (_children.size() == 1) //optimize this case return getTextOfChild(_children.get(0)); final StringBuffer sb = new StringBuffer(256); for (final Iterator it = _children.iterator(); it.hasNext();) sb.append(getTextOfChild(it.next())); return sb.toString(); } /** Returns the catenation of {@link Textual} children; never null. * * @param trim whether to trim before returning * @see #getText() */ public final String getText(boolean trim) { String t = getText(); return trim && t != null ? t.trim(): t; } //-- Attributable --// public final boolean isAttributeModificationAware() { return _attrModAware; } public final void setAttributeModificationAware(boolean aware) { _attrModAware = aware; } public final List getAttributeItems() { if (_attrs == null) _attrs = newAttrArray(); return _attrs; } /** Creates an empty list of attributes. */ protected List newAttrArray() { return new AttrArray(); } public final int getAttributeIndex (int indexFrom, String namespace, String name, int mode) { if (_attrs == null || indexFrom < 0 || indexFrom >= _attrs.size()) return -1; final Pattern ptn = (mode & FIND_BY_REGEX) != 0 ? Pattern.compile(name): null; final Iterator it = _attrs.listIterator(indexFrom); for (int j = indexFrom; it.hasNext(); ++j) if (match((Attribute)it.next(), namespace, name, ptn, mode)) return j; return -1; } public final int getAttributeIndex(int indexFrom, String tname) { return getAttributeIndex(indexFrom, null, tname, FIND_BY_TAGNAME); } public final Attribute getAttributeItem(String namespace, String name, int mode) { int j= getAttributeIndex(0, namespace, name, mode); return j >= 0 ? (Attribute)_attrs.get(j): null; } public final Attribute getAttributeItem(String tname) { int j= getAttributeIndex(0, tname); return j >= 0 ? (Attribute)_attrs.get(j): null; } public final List getAttributes(String namespace, String name, int mode) { if (_attrs == null) return Collections.EMPTY_LIST; Pattern ptn = (mode & FIND_BY_REGEX) != 0 ? Pattern.compile(name): null; final List list = new LinkedList(); for (final Iterator it = _attrs.iterator(); it.hasNext();) { Attribute attr = (Attribute)it.next(); if (match(attr, namespace, name, ptn, mode)) list.add(attr); } return list; } public final Attribute setAttribute(Attribute attr) { checkWritable(); int j = getAttributeIndex(0, attr.getTagName()); if (j >= 0) { return (Attribute)getAttributeItems().set(j, attr); } else { getAttributeItems().add(attr); return null; } } public final String getAttributeValue (String namespace, String name, int mode) { final Attribute attr = getAttributeItem(namespace, name, mode); return attr != null ? attr.getValue(): null; } public final String getAttributeValue(String tname) { Attribute attr = getAttributeItem(tname); return attr != null ? attr.getValue(): null; } public final Attribute setAttributeValue(String tname, String value) { checkWritable(); Attribute attr = getAttributeItem(tname); if (attr != null) attr.setValue(value); else getAttributeItems().add(new Attribute(tname, value)); return attr; } //-- Node --// public final short getNodeType() { return ELEMENT_NODE; } /** * Always null. Unlike other nodes, it is not the same as getText. */ public final String getNodeValue() { return null; } public final NamedNodeMap getAttributes() { return new AttrMap(); } public final boolean hasAttributes() { return _attrs != null && !_attrs.isEmpty(); } public final String getNamespaceURI() { return _ns.getURI(); } public final String getPrefix() { return _ns.getPrefix(); } public final void setPrefix(String prefix) { setNamespace(prefix, _ns.getURI()); } //-- Element --// public final NodeList getElementsByTagName(String tname) { return new FacadeNodeList( getElements(null, tname, FIND_BY_TAGNAME|FIND_RECURSIVE)); } public final NodeList getElementsByTagNameNS(String nsURI, String lname) { return new FacadeNodeList(getElements(nsURI, lname, FIND_RECURSIVE)); } public final Attr getAttributeNode(String tname) { return getAttributeItem(tname); } public final Attr getAttributeNodeNS(String nsURI, String lname) { return getAttributeItem(nsURI, lname, 0); } public final String getAttribute(String tname) { String val = getAttributeValue(tname); return val != null ? val: ""; //w3c spec } public final String getAttributeNS(String nsURI, String lname) { Attribute attr = getAttributeItem(nsURI, lname, 0); return attr != null ? attr.getValue(): ""; } public final void setAttribute(String tname, String value) { setAttributeValue(tname, value); } public final void setAttributeNS (String nsURI, String tname, String value) { checkWritable(); int kp = tname.indexOf(':'); String prefix = kp >= 0 ? tname.substring(0, kp): ""; String lname = kp >= 0 ? tname.substring(kp + 1): tname; Attribute attr = getAttributeItem(nsURI, lname, 0); if (attr != null) { attr.setPrefix(prefix); //also change prefix attr.setValue(value); } else { getAttributeItems().add(new Attribute(nsURI, tname, value)); } } public final Attr setAttributeNode(Attr newAttr) { return setAttribute((Attribute)newAttr); } public final Attr setAttributeNodeNS(Attr newAttr) { Attribute attr = (Attribute)newAttr; int j = getAttributeIndex( 0, attr.getNamespace().getURI(), attr.getLocalName(), 0); if (j >= 0) { return (Attr)getAttributeItems().set(j, newAttr); } else { getAttributeItems().add(newAttr); return null; } } public final void removeAttribute(String tname) { int j = getAttributeIndex(0, tname); if (j >= 0) _attrs.remove(j); } public final void removeAttributeNS(String nsURI, String lname) { int j = getAttributeIndex(0, nsURI, lname, 0); if (j >= 0) _attrs.remove(j); } public final Attr removeAttributeNode(Attr oldAttr) { Attribute attr = (Attribute)oldAttr; int j = getAttributeIndex(0, attr.getTagName()); if (j >= 0) { return (Attr)_attrs.remove(j); } else { throw new DOMException(DOMException.NOT_FOUND_ERR, getLocator()); } } public final boolean hasAttribute(String tname) { return getAttributeIndex(0, tname) >= 0; } public final boolean hasAttributeNS(String nsURI, String lname) { return getAttributeIndex(0, nsURI, lname, 0) >= 0; } public final String toString() { StringBuffer sb = new StringBuffer(64) .append("[Element: <").append(getTagName()); String uri = getNamespace().getURI(); if (uri.length() != 0) sb.append(" [").append(uri).append(']'); if (_attrs != null) { for (final Iterator it = _attrs.iterator(); it.hasNext();) { Attribute attr = (Attribute)it.next(); sb.append(' ').append(attr.getTagName()) .append("=\"").append(attr.getValue()).append('"'); } } return sb.append("/>]").toString(); } public TypeInfo getSchemaTypeInfo() { throw new UnsupportedOperationException("DOM Level 3"); } public void setIdAttribute(String name, boolean isId) throws DOMException { //Level 3 not yet } public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException { //Level 3 not yet } public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException { //Level 3 not yet } //-- AttrArray --// protected class AttrArray extends CheckableTreeArray { protected AttrArray() { } //-- utilities --// private Attribute checkAdd(Object newItem) { checkWritable(); if (!(newItem instanceof Attribute)) throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, "Invalid type", getLocator()); Attribute attr = (Attribute)newItem; if (attr.getOwner() != null) throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Attribute, "+attr.toString()+", owned by other; detach or clone it", getLocator()); return attr; } //-- CheckableTreeArray --// protected void onAdd(Object newElement, Object followingElement) { checkAdd(newElement, followingElement, false); } protected void onSet(Object newElement, Object replaced) { assert(replaced != null); checkAdd(newElement, replaced, true); } private void checkAdd(Object newItem, Object other, boolean replace) { //first, remove any existent with the same uri and name Object attrRemoved = null; Attribute attr = checkAdd(newItem); int j = getAttributeIndex(0, attr.getTagName()); if (j >= 0 && (!replace || get(j) != other)) throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Attribute name, " + attr.getTagName() +", is conflicts with existent one (" + j + ')', getLocator()); try { if (replace) onRemove(other); attr.setOwner(Element.this); }catch(RuntimeException ex) { if (replace) { Attribute attrRep = (Attribute)other; if (attrRep.getOwner() == null) attrRep.setOwner(Element.this); //restore it } if (attrRemoved != null) super.add(j, attrRemoved); //restore it throw ex; } } protected void onRemove(Object item) { checkWritable(); ((Attribute)item).setOwner(null); } } protected class AttrMap implements NamedNodeMap { protected AttrMap() { } //-- NamedNodeMap --// public final int getLength() { return _attrs != null ? _attrs.size(): 0; } public final Node item(int index) { return index < 0 || index >= getLength() ? null: (Node)_attrs.get(index); } public final Node getNamedItem(String tname) { return getAttributeItem(tname); } public final Node getNamedItemNS(String nsURI, String lname) { return getAttributeItem(nsURI, lname, 0); } public final Node removeNamedItem(String tname) { int j = getAttributeIndex(0, tname); return j >= 0 ? (Node)_attrs.remove(j): null; } public final Node removeNamedItemNS(String nsURI, String lname) { int j = getAttributeIndex(0, nsURI, lname, 0); return j >= 0 ? (Node)_attrs.remove(j): null; } public final Node setNamedItem(Node node) { return setAttributeNode((Attr)node); } public final Node setNamedItemNS(Node node) { return setAttributeNodeNS((Attr)node); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -