📄 abstractelement.java
字号:
if (size == 1) { // allow lazy construction of the List of Attributes String attributeQualifiedName = attributes.getQName(0); if (noNamespaceAttributes || !attributeQualifiedName.startsWith("xmlns")) { String attributeURI = attributes.getURI(0); String attributeLocalName = attributes.getLocalName(0); String attributeValue = attributes.getValue(0); QName attributeQName = namespaceStack.getAttributeQName( attributeURI, attributeLocalName, attributeQualifiedName); add(factory.createAttribute(this, attributeQName, attributeValue)); } } else { List list = attributeList(size); list.clear(); for (int i = 0; i < size; i++) { // optimised to avoid the call to attribute(QName) to // lookup an attribute for a given QName String attributeQualifiedName = attributes.getQName(i); if (noNamespaceAttributes || !attributeQualifiedName.startsWith("xmlns")) { String attributeURI = attributes.getURI(i); String attributeLocalName = attributes.getLocalName(i); String attributeValue = attributes.getValue(i); QName attributeQName = namespaceStack.getAttributeQName( attributeURI, attributeLocalName, attributeQualifiedName); Attribute attribute = factory.createAttribute(this, attributeQName, attributeValue); list.add(attribute); childAdded(attribute); } } } } } public String attributeValue(String name) { Attribute attrib = attribute(name); if (attrib == null) { return null; } else { return attrib.getValue(); } } public String attributeValue(QName qName) { Attribute attrib = attribute(qName); if (attrib == null) { return null; } else { return attrib.getValue(); } } public String attributeValue(String name, String defaultValue) { String answer = attributeValue(name); return (answer != null) ? answer : defaultValue; } public String attributeValue(QName qName, String defaultValue) { String answer = attributeValue(qName); return (answer != null) ? answer : defaultValue; } /** * @deprecated As of version 0.5. Please use * {@link #addAttribute(String,String)} instead. **/ public void setAttributeValue(String name, String value) { addAttribute(name, value); } /** * @deprecated As of version 0.5. Please use * {@link #addAttribute(String,String)} instead. **/ public void setAttributeValue(QName qName, String value) { addAttribute(qName, value); } public void add(Attribute attribute) { if (attribute.getParent() != null) { String message = "The Attribute already has an existing parent \"" + attribute.getParent().getQualifiedName() + "\""; throw new IllegalAddException(this, attribute, message); } if (attribute.getValue() == null) { // try remove a previous attribute with the same // name since adding an attribute with a null value // is equivalent to removing it. Attribute oldAttribute = attribute(attribute.getQName()); if (oldAttribute != null) { remove(oldAttribute); } } else { attributeList().add(attribute); childAdded(attribute); } } public boolean remove(Attribute attribute) { List list = attributeList(); boolean answer = list.remove(attribute); if (answer) { childRemoved(attribute); } else { // we may have a copy of the attribute Attribute copy = attribute(attribute.getQName()); if (copy != null) { list.remove(copy); answer = true; } } return answer; } // Processing instruction API //------------------------------------------------------------------------- public List processingInstructions() { List list = contentList(); BackedList answer = createResultList(); int size = list.size(); for (int i = 0; i < size; i++) { Object object = list.get(i); if (object instanceof ProcessingInstruction) { answer.addLocal(object); } } return answer; } public List processingInstructions(String target) { List list = contentList(); BackedList answer = createResultList(); int size = list.size(); for (int i = 0; i < size; i++) { Object object = list.get(i); if (object instanceof ProcessingInstruction) { ProcessingInstruction pi = (ProcessingInstruction) object; if (target.equals(pi.getName())) { answer.addLocal(pi); } } } return answer; } public ProcessingInstruction processingInstruction(String target) { List list = contentList(); int size = list.size(); for (int i = 0; i < size; i++) { Object object = list.get(i); if (object instanceof ProcessingInstruction) { ProcessingInstruction pi = (ProcessingInstruction) object; if (target.equals(pi.getName())) { return pi; } } } return null; } public boolean removeProcessingInstruction(String target) { List list = contentList(); for (Iterator iter = list.iterator(); iter.hasNext();) { Object object = iter.next(); if (object instanceof ProcessingInstruction) { ProcessingInstruction pi = (ProcessingInstruction) object; if (target.equals(pi.getName())) { iter.remove(); return true; } } } return false; } // Content Model methods //------------------------------------------------------------------------- public Node getXPathResult(int index) { Node answer = node(index); if (answer != null && !answer.supportsParent()) { return answer.asXPathResult(this); } return answer; } public Element addAttribute(String name, String value) { // adding a null value is equivalent to removing the attribute Attribute attribute = attribute(name); if (value != null) { if (attribute == null) { add(getNodeFactory().createAttribute(this, name, value)); } else if (attribute.isReadOnly()) { remove(attribute); add(getNodeFactory().createAttribute(this, name, value)); } else { attribute.setValue(value); } } else if (attribute != null) { remove(attribute); } return this; } public Element addAttribute(QName qName, String value) { // adding a null value is equivalent to removing the attribute Attribute attribute = attribute(qName); if (value != null) { if (attribute == null) { add(getNodeFactory().createAttribute(this, qName, value)); } else if (attribute.isReadOnly()) { remove(attribute); add(getNodeFactory().createAttribute(this, qName, value)); } else { attribute.setValue(value); } } else if (attribute != null) { remove(attribute); } return this; } public Element addCDATA(String cdata) { CDATA node = getNodeFactory().createCDATA(cdata); addNewNode(node); return this; } public Element addComment(String comment) { Comment node = getNodeFactory().createComment(comment); addNewNode(node); return this; } public Element addElement(String name) { NodeFactory factory = getNodeFactory(); int index = name.indexOf(":"); String prefix = ""; String localName = name; Namespace namespace = null; if (index > 0) { prefix = name.substring(0, index); localName = name.substring(index + 1); namespace = getNamespaceForPrefix(prefix); if (namespace == null) { throw new IllegalAddException( "No such namespace prefix: " + prefix + " is in scope on: " + this + " so cannot add element: " + name); } } else { namespace = getNamespaceForPrefix(""); } Element node; if (namespace != null) { QName qname = factory.createQName(localName, namespace); node = factory.createElement(qname); } else { node = factory.createElement(name); } addNewNode(node); return node; } public Element addEntity(String name, String text) { Entity node = getNodeFactory().createEntity(name, text); addNewNode(node); return this; } public Element addNamespace(String prefix, String uri) { Namespace node = getNodeFactory().createNamespace(prefix, uri); addNewNode(node); return this; } public Element addProcessingInstruction(String target, String data) { ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data); addNewNode(node); return this; } public Element addProcessingInstruction(String target, Map data) { ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data); addNewNode(node); return this; } public Element addText(String text) { Text node = getNodeFactory().createText(text); addNewNode(node); return this; } // polymorphic node methods public void add(Node node) { switch (node.getNodeType()) { case ELEMENT_NODE: add((Element) node); break; case ATTRIBUTE_NODE: add((Attribute) node); break; case TEXT_NODE: add((Text) node); break; case CDATA_SECTION_NODE: add((CDATA) node); break; case ENTITY_REFERENCE_NODE: add((Entity) node); break; case PROCESSING_INSTRUCTION_NODE: add((ProcessingInstruction) node); break; case COMMENT_NODE: add((Comment) node); break; /* XXXX: to do! case DOCUMENT_TYPE_NODE: add((DocumentType) node); break; */ case NAMESPACE_NODE: add((Namespace) node); break; default : invalidNodeTypeAddException(node); } } public boolean remove(Node node) { switch (node.getNodeType()) { case ELEMENT_NODE: return remove((Element) node); case ATTRIBUTE_NODE: return remove((Attribute) node); case TEXT_NODE: return remove((Text) node); case CDATA_SECTION_NODE: return remove((CDATA) node); case ENTITY_REFERENCE_NODE: return remove((Entity) node); case PROCESSING_INSTRUCTION_NODE: return remove((ProcessingInstruction) node); case COMMENT_NODE: return remove((Comment) node); /* case DOCUMENT_TYPE_NODE: return remove((DocumentType) node); */ case NAMESPACE_NODE: return remove((Namespace) node); default : return false; } } // typesafe versions using node classes public void add(CDATA cdata) { addNode(cdata); } public void add(Comment comment) { addNode(comment); } public void add(Element element) { addNode(element); } public void add(Entity entity) { addNode(entity);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -