attributeset.java
来自「java jdk 1.4的源码」· Java 代码 · 共 553 行 · 第 1/2 页
JAVA
553 行
Attr attr = (Attr) getNamedItem (name); if (attr == null) return ""; else return attr.getValue (); } public Node getNamedItem (String name) { int length = list.size (); Node value; for (int i = 0; i < length; i++) { value = item (i); if (value.getNodeName ().equals (name)) return value; } return null; } /** * <b>DOM2:</b> */ public Node getNamedItemNS(String namespaceURI, String localName) { // DOM L2 spec specifies that Attr.localName is null for L1 created // Attr and Element nodes, therefore this method cannot be used to // lookup such a node. if (localName == null) { return null; } for (int i = 0; i < list.size(); i++) { Node value = item(i); String iLocalName = value.getLocalName(); if (localName.equals(iLocalName)) { String iNamespaceURI = value.getNamespaceURI(); if (namespaceURI == iNamespaceURI || (namespaceURI != null && namespaceURI.equals(iNamespaceURI))) { return value; } } } return null; } public int getLength () { return list.size (); } public Node item (int index) { if (index < 0 || index >= list.size ()) return null; return (Node) list.elementAt (index); } public Node removeNamedItem(String name) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } for (int i = 0; i < list.size(); i++) { Node value = (Node)list.elementAt(i); if (value.getNodeName().equals(name)) { // Found a match list.removeElementAt(i); AttributeNode attr = (AttributeNode)value; // Replace with Attr node of default value if it has one String defaultValue = attr.getDefaultValue(); if (defaultValue != null) { AttributeNode newAttr = attr.cloneAttributeNode(true); newAttr.setOwnerElement(attr.getOwnerElement()); newAttr.setValue(defaultValue); newAttr.setSpecified(false); list.addElement(newAttr); } // Set the ownerElement of attr to null since we're // removing it attr.setOwnerElement(null); return attr; } } throw new DomEx(DomEx.NOT_FOUND_ERR); } /** * <b>DOM2:</b> */ public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } // See comments for getNamedItemNS() for why localName cannot be null if (localName == null) { throw new DomEx(DomEx.NOT_FOUND_ERR); } for (int i = 0; i < list.size(); i++) { Node value = (Node)list.elementAt(i); String iLocalName = value.getLocalName(); if (localName.equals(iLocalName)) { String iNamespaceURI = value.getNamespaceURI(); if (namespaceURI == iNamespaceURI || (namespaceURI != null && namespaceURI.equals(iNamespaceURI))) { // Found a match list.removeElementAt(i); AttributeNode attr = (AttributeNode)value; // Replace with Attr node of default value if it has one String defaultValue = attr.getDefaultValue(); if (defaultValue != null) { AttributeNode newAttr = attr.cloneAttributeNode(true); newAttr.setOwnerElement(attr.getOwnerElement()); newAttr.setValue(defaultValue); newAttr.setSpecified(false); list.addElement(newAttr); } // Set the ownerElement of attr to null since we're // removing it attr.setOwnerElement(null); return attr; } } } throw new DomEx(DomEx.NOT_FOUND_ERR); } /** * Note: this method both checks and sets the "ownerElement" of the * "value" parameter. So if "ownerElement" is already set, an * incorrect error results. Callers should avoid setting * "ownerElement". */ public Node setNamedItem(Node value) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } if (!(value instanceof AttributeNode) || value.getOwnerDocument() != ownerElement.getOwnerDocument()) { throw new DomEx(DomEx.WRONG_DOCUMENT_ERR); } AttributeNode att = (AttributeNode)value; if (att.getOwnerElement() != null) { throw new DomEx(DomEx.INUSE_ATTRIBUTE_ERR); } int length = list.size(); AttributeNode oldAtt; for (int i = 0; i < length; i++) { oldAtt = (AttributeNode)item(i); if (oldAtt.getNodeName().equals(value.getNodeName())) { if (oldAtt.isReadonly()) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } att.setOwnerElement(ownerElement); list.setElementAt(att, i); oldAtt.setOwnerElement(null); return oldAtt; } } att.setOwnerElement(ownerElement); list.addElement(value); return null; } /** * <b>DOM2:</b> * Spec technically allows other types of nodes also, but this code * assumes Attr nodes only */ public Node setNamedItemNS(Node arg) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } if (!(arg instanceof AttributeNode) || arg.getOwnerDocument() != ownerElement.getOwnerDocument()) { throw new DomEx(DomEx.WRONG_DOCUMENT_ERR); } AttributeNode attr = (AttributeNode) arg; if (attr.getOwnerElement() != null) { throw new DomEx(DomEx.INUSE_ATTRIBUTE_ERR); } // Both localName and namespaceURI can be null for Attr nodes // created by DOM Level 1 methods String localName = attr.getLocalName(); String namespaceURI = attr.getNamespaceURI(); int length = list.size(); for (int i = 0; i < length; i++) { AttributeNode oldNode = (AttributeNode) item(i); String iLocalName = oldNode.getLocalName(); String iNamespaceURI = oldNode.getNamespaceURI(); if ((localName == iLocalName || (localName != null && localName.equals(iLocalName))) && (namespaceURI == iNamespaceURI || (namespaceURI != null && namespaceURI.equals(iNamespaceURI)))) { // Found a matching node so replace it if (oldNode.isReadonly()) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } attr.setOwnerElement(ownerElement); list.setElementAt(attr, i); oldNode.setOwnerElement(null); return oldNode; } } // Append instead of replace attr.setOwnerElement(ownerElement); list.addElement(attr); return null; } /** * Writes out the attribute list. Attributes known to have been * derived from the DTD are not (at this time) written out. Part * of writing standalone XML is first ensuring that all attributes * are flagged as being specified in the "printed" form (or else * are defaulted only in the internal DTD subset). */ public void writeXml (XmlWriteContext context) throws IOException { Writer out = context.getWriter (); int length = list.size (); AttributeNode tmp; for (int i = 0; i < length; i++) { tmp = (AttributeNode) list.elementAt (i); if (tmp.getSpecified ()) { out.write (' '); tmp.writeXml (context); } } } /** * Does nothing; this type of node has no children. */ public void writeChildrenXml (XmlWriteContext context) throws IOException { } public String toString () { try { CharArrayWriter w = new CharArrayWriter (); XmlWriteContext x = new XmlWriteContext (w); writeXml (x); return w.toString (); } catch (IOException e) { return super.toString (); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?