| Method from org.dom4j.tree.AbstractElement Detail: |
public void accept(Visitor visitor) {
visitor.visit(this);
// visit attributes
for (int i = 0, size = attributeCount(); i < size; i++) {
Attribute attribute = attribute(i);
visitor.visit(attribute);
}
// visit content
for (int i = 0, size = nodeCount(); i < size; i++) {
Node node = node(i);
node.accept(visitor);
}
}
|
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 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 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);
}
|
public void add(Namespace namespace) {
addNode(namespace);
}
|
public void add(ProcessingInstruction pi) {
addNode(pi);
}
|
public void add(Text text) {
addNode(text);
}
|
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(getDocumentFactory().createAttribute(this, name, value));
} else if (attribute.isReadOnly()) {
remove(attribute);
add(getDocumentFactory().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(getDocumentFactory().createAttribute(this, qName, value));
} else if (attribute.isReadOnly()) {
remove(attribute);
add(getDocumentFactory().createAttribute(this, qName, value));
} else {
attribute.setValue(value);
}
} else if (attribute != null) {
remove(attribute);
}
return this;
}
|
public Element addCDATA(String cdata) {
CDATA node = getDocumentFactory().createCDATA(cdata);
addNewNode(node);
return this;
}
|
public Element addComment(String comment) {
Comment node = getDocumentFactory().createComment(comment);
addNewNode(node);
return this;
}
|
public Element addElement(String name) {
DocumentFactory factory = getDocumentFactory();
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 = getDocumentFactory().createEntity(name, text);
addNewNode(node);
return this;
}
|
public Element addNamespace(String prefix,
String uri) {
Namespace node = getDocumentFactory().createNamespace(prefix, uri);
addNewNode(node);
return this;
}
|
protected void addNewNode(Node node) {
contentList().add(node);
childAdded(node);
}
Like addNode() but does not require a parent check |
protected void addNewNode(int index,
Node node) {
contentList().add(index, node);
childAdded(node);
}
|
protected void addNode(Node node) {
if (node.getParent() != null) {
// XXX: could clone here
String message = "The Node already has an existing parent of \""
+ node.getParent().getQualifiedName() + "\"";
throw new IllegalAddException(this, node, message);
}
addNewNode(node);
}
|
protected void addNode(int index,
Node node) {
if (node.getParent() != null) {
// XXX: could clone here
String message = "The Node already has an existing parent of \""
+ node.getParent().getQualifiedName() + "\"";
throw new IllegalAddException(this, node, message);
}
addNewNode(index, node);
}
|
public Element addProcessingInstruction(String target,
String data) {
ProcessingInstruction node = getDocumentFactory()
.createProcessingInstruction(target, data);
addNewNode(node);
return this;
}
|
public Element addProcessingInstruction(String target,
Map data) {
ProcessingInstruction node = getDocumentFactory()
.createProcessingInstruction(target, data);
addNewNode(node);
return this;
}
|
public Element addText(String text) {
Text node = getDocumentFactory().createText(text);
addNewNode(node);
return this;
}
|
public List additionalNamespaces() {
List list = contentList();
int size = list.size();
BackedList answer = createResultList();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Namespace) {
Namespace namespace = (Namespace) object;
if (!namespace.equals(getNamespace())) {
answer.addLocal(namespace);
}
}
}
return answer;
}
|
public List additionalNamespaces(String defaultNamespaceURI) {
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 Namespace) {
Namespace namespace = (Namespace) object;
if (!defaultNamespaceURI.equals(namespace.getURI())) {
answer.addLocal(namespace);
}
}
}
return answer;
}
|
public void appendAttributes(Element element) {
for (int i = 0, size = element.attributeCount(); i < size; i++) {
Attribute attribute = element.attribute(i);
if (attribute.supportsParent()) {
addAttribute(attribute.getQName(), attribute.getValue());
} else {
add(attribute);
}
}
}
|
public String asXML() {
try {
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, new OutputFormat());
writer.write(this);
writer.flush();
return out.toString();
} catch (IOException e) {
throw new RuntimeException("IOException while generating "
+ "textual representation: " + e.getMessage());
}
}
|
public Attribute attribute(int index) {
return (Attribute) attributeList().get(index);
}
|
public Attribute attribute(String name) {
List list = attributeList();
int size = list.size();
for (int i = 0; i < size; i++) {
Attribute attribute = (Attribute) list.get(i);
if (name.equals(attribute.getName())) {
return attribute;
}
}
return null;
}
|
public Attribute attribute(QName qName) {
List list = attributeList();
int size = list.size();
for (int i = 0; i < size; i++) {
Attribute attribute = (Attribute) list.get(i);
if (qName.equals(attribute.getQName())) {
return attribute;
}
}
return null;
}
|
public Attribute attribute(String name,
Namespace namespace) {
return attribute(getDocumentFactory().createQName(name, namespace));
}
|
public int attributeCount() {
return attributeList().size();
}
|
public Iterator attributeIterator() {
return attributeList().iterator();
}
|
abstract protected List attributeList()
|
abstract protected List attributeList(int attributeCount)
|
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;
}
|
public List attributes() {
return new ContentListFacade(this, attributeList());
}
|
protected void childAdded(Node node) {
if (node != null) {
node.setParent(this);
}
}
Called when a new child node is added to create any parent relationships |
protected void childRemoved(Node node) {
if (node != null) {
node.setParent(null);
node.setDocument(null);
}
}
|
protected List createAttributeList() {
return createAttributeList(DEFAULT_CONTENT_LIST_SIZE);
}
A Factory Method pattern which creates a List implementation used to
store attributes |
protected List createAttributeList(int size) {
return new ArrayList(size);
}
A Factory Method pattern which creates a List implementation used to
store attributes |
public Element createCopy() {
Element clone = createElement(getQName());
clone.appendAttributes(this);
clone.appendContent(this);
return clone;
}
This returns a deep clone of this element. The new element is detached
from its parent, and getParent() on the clone will return null.
|
public Element createCopy(String name) {
Element clone = createElement(name);
clone.appendAttributes(this);
clone.appendContent(this);
return clone;
}
|
public Element createCopy(QName qName) {
Element clone = createElement(qName);
clone.appendAttributes(this);
clone.appendContent(this);
return clone;
}
|
protected Element createElement(String name) {
return getDocumentFactory().createElement(name);
}
|
protected Element createElement(QName qName) {
return getDocumentFactory().createElement(qName);
}
|
protected Iterator createSingleIterator(Object result) {
return new SingleIterator(result);
}
|
public List declaredNamespaces() {
BackedList answer = createResultList();
// if (getNamespaceURI().length() > 0) {
//
// answer.addLocal(getNamespace());
//
// }
//
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Namespace) {
answer.addLocal(object);
}
}
return answer;
}
|
public Element element(String name) {
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Element) {
Element element = (Element) object;
if (name.equals(element.getName())) {
return element;
}
}
}
return null;
}
|
public Element element(QName qName) {
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Element) {
Element element = (Element) object;
if (qName.equals(element.getQName())) {
return element;
}
}
}
return null;
}
|
public Element element(String name,
Namespace namespace) {
return element(getDocumentFactory().createQName(name, namespace));
}
|
public Iterator elementIterator() {
List list = elements();
return list.iterator();
}
|
public Iterator elementIterator(String name) {
List list = elements(name);
return list.iterator();
}
|
public Iterator elementIterator(QName qName) {
List list = elements(qName);
return list.iterator();
}
|
public Iterator elementIterator(String name,
Namespace ns) {
return elementIterator(getDocumentFactory().createQName(name, ns));
}
|
public String elementText(String name) {
Element element = element(name);
return (element != null) ? element.getText() : null;
}
|
public String elementText(QName qName) {
Element element = element(qName);
return (element != null) ? element.getText() : null;
}
|
public String elementTextTrim(String name) {
Element element = element(name);
return (element != null) ? element.getTextTrim() : null;
}
|
public String elementTextTrim(QName qName) {
Element element = element(qName);
return (element != null) ? element.getTextTrim() : null;
}
|
public List elements() {
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 Element) {
answer.addLocal(object);
}
}
return answer;
}
|
public List elements(String name) {
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 Element) {
Element element = (Element) object;
if (name.equals(element.getName())) {
answer.addLocal(element);
}
}
}
return answer;
}
|
public List elements(QName qName) {
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 Element) {
Element element = (Element) object;
if (qName.equals(element.getQName())) {
answer.addLocal(element);
}
}
}
return answer;
}
|
public List elements(String name,
Namespace namespace) {
return elements(getDocumentFactory().createQName(name, namespace));
}
|
public void ensureAttributesCapacity(int minCapacity) {
if (minCapacity > 1) {
List list = attributeList();
if (list instanceof ArrayList) {
ArrayList arrayList = (ArrayList) list;
arrayList.ensureCapacity(minCapacity);
}
}
}
Ensures that the list of attributes has the given size |
public Object getData() {
return getText();
}
|
protected DocumentFactory getDocumentFactory() {
QName qName = getQName();
// QName might be null as we might not have been constructed yet
if (qName != null) {
DocumentFactory factory = qName.getDocumentFactory();
if (factory != null) {
return factory;
}
}
return DOCUMENT_FACTORY;
}
|
public String getName() {
return getQName().getName();
}
|
public Namespace getNamespace() {
return getQName().getNamespace();
}
|
public Namespace getNamespaceForPrefix(String prefix) {
if (prefix == null) {
prefix = "";
}
if (prefix.equals(getNamespacePrefix())) {
return getNamespace();
} else if (prefix.equals("xml")) {
return Namespace.XML_NAMESPACE;
} else {
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Namespace) {
Namespace namespace = (Namespace) object;
if (prefix.equals(namespace.getPrefix())) {
return namespace;
}
}
}
}
Element parent = getParent();
if (parent != null) {
Namespace answer = parent.getNamespaceForPrefix(prefix);
if (answer != null) {
return answer;
}
}
if ((prefix == null) || (prefix.length() < = 0)) {
return Namespace.NO_NAMESPACE;
}
return null;
}
|
public Namespace getNamespaceForURI(String uri) {
if ((uri == null) || (uri.length() < = 0)) {
return Namespace.NO_NAMESPACE;
} else if (uri.equals(getNamespaceURI())) {
return getNamespace();
} else {
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if (object instanceof Namespace) {
Namespace namespace = (Namespace) object;
if (uri.equals(namespace.getURI())) {
return namespace;
}
}
}
return null;
}
}
|
public String getNamespacePrefix() {
return getQName().getNamespacePrefix();
}
|
public String getNamespaceURI() {
return getQName().getNamespaceURI();
}
|
public List getNamespacesForURI(String uri) {
BackedList answer = createResultList();
// if (getNamespaceURI().equals(uri)) {
//
// answer.addLocal(getNamespace());
//
// }
List list = contentList();
int size = list.size();
for (int i = 0; i < size; i++) {
Object object = list.get(i);
if ((object instanceof Namespace)
&& ((Namespace) object).getURI().equals(uri)) {
answer.addLocal(object);
}
}
return answer;
}
|
public short getNodeType() {
return ELEMENT_NODE;
}
|
public String getPath(Element context) {
if (this == context) {
return ".";
}
Element parent = getParent();
if (parent == null) {
return "/" + getXPathNameStep();
} else if (parent == context) {
return getXPathNameStep();
}
return parent.getPath(context) + "/" + getXPathNameStep();
}
|
public QName getQName(String qualifiedName) {
String prefix = "";
String localName = qualifiedName;
int index = qualifiedName.indexOf(":");
if (index > 0) {
prefix = qualifiedName.substring(0, index);
localName = qualifiedName.substring(index + 1);
}
Namespace namespace = getNamespaceForPrefix(prefix);
if (namespace != null) {
return getDocumentFactory().createQName(localName, namespace);
} else {
return getDocumentFactory().createQName(localName);
}
}
|
public String getQualifiedName() {
return getQName().getQualifiedName();
}
|
public String getStringValue() {
List list = contentList();
int size = list.size();
if (size > 0) {
if (size == 1) {
// optimised to avoid StringBuffer creation
return getContentAsStringValue(list.get(0));
} else {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < size; i++) {
Object node = list.get(i);
String string = getContentAsStringValue(node);
if (string.length() > 0) {
if (USE_STRINGVALUE_SEPARATOR) {
if (buffer.length() > 0) {
buffer.append(' ");
}
}
buffer.append(string);
}
}
return buffer.toString();
}
}
return "";
}
|
public String getUniquePath(Element context) {
Element parent = getParent();
if (parent == null) {
return "/" + getXPathNameStep();
}
StringBuffer buffer = new StringBuffer();
if (parent != context) {
buffer.append(parent.getUniquePath(context));
buffer.append("/");
}
buffer.append(getXPathNameStep());
List mySiblings = parent.elements(getQName());
if (mySiblings.size() > 1) {
int idx = mySiblings.indexOf(this);
if (idx >= 0) {
buffer.append("[");
buffer.append(Integer.toString(++idx));
buffer.append("]");
}
}
return buffer.toString();
}
|
public String getXPathNameStep() {
String uri = getNamespaceURI();
if ((uri == null) || (uri.length() == 0)) {
return getName();
}
String prefix = getNamespacePrefix();
if ((prefix == null) || (prefix.length() == 0)) {
return "*[name()='" + getName() + "']";
}
return getQualifiedName();
}
Returns the XPath expression to match this Elements name which is
getQualifiedName() if there is a namespace prefix defined or if no
namespace is present then it is getName() or if a namespace is defined
with no prefix then the expression is [name()='X'] where X = getName(). |
public Node getXPathResult(int index) {
Node answer = node(index);
if ((answer != null) && !answer.supportsParent()) {
return answer.asXPathResult(this);
}
return answer;
}
|
public boolean hasMixedContent() {
List content = contentList();
if ((content == null) || content.isEmpty() || (content.size() < 2)) {
return false;
}
Class prevClass = null;
for (Iterator iter = content.iterator(); iter.hasNext();) {
Object object = iter.next();
Class newClass = object.getClass();
if (newClass != prevClass) {
if (prevClass != null) {
return true;
}
prevClass = newClass;
}
}
return false;
}
|
public int indexOf(Node node) {
return contentList().indexOf(node);
}
|
public boolean isRootElement() {
Document document = getDocument();
if (document != null) {
Element root = document.getRootElement();
if (root == this) {
return true;
}
}
return false;
}
|
public boolean isTextOnly() {
List content = contentList();
if ((content == null) || content.isEmpty()) {
return true;
}
for (Iterator iter = content.iterator(); iter.hasNext();) {
Object object = iter.next();
if (!(object instanceof CharacterData)
&& !(object instanceof String)) {
return false;
}
}
return true;
}
|
public Node node(int index) {
if (index >= 0) {
List list = contentList();
if (index >= list.size()) {
return null;
}
Object node = list.get(index);
if (node != null) {
if (node instanceof Node) {
return (Node) node;
} else {
return getDocumentFactory().createText(node.toString());
}
}
}
return null;
}
|
public int nodeCount() {
return contentList().size();
}
|
public Iterator nodeIterator() {
return contentList().iterator();
}
|
public void normalize() {
List content = contentList();
Text previousText = null;
int i = 0;
while (i < content.size()) {
Node node = (Node) content.get(i);
if (node instanceof Text) {
Text text = (Text) node;
if (previousText != null) {
previousText.appendText(text.getText());
remove(text);
} else {
String value = text.getText();
// only remove empty Text nodes, not whitespace nodes
// if ( value == null || value.trim().length() < = 0 ) {
if ((value == null) || (value.length() < = 0)) {
remove(text);
} else {
previousText = text;
i++;
}
}
} else {
if (node instanceof Element) {
Element element = (Element) node;
element.normalize();
}
previousText = null;
i++;
}
}
}
Puts all Text nodes in the full depth of the sub-tree
underneath this Node, including attribute nodes, into a
"normal" form where only structure (e.g., elements, comments, processing
instructions, CDATA sections, and entity references) separates
Text nodes, i.e., there are neither adjacent
Text nodes nor empty Text nodes. This can
be used to ensure that the DOM view of a document is the same as if it
were saved and re-loaded, and is useful when operations (such as XPointer
lookups) that depend on a particular document tree structure are to be
used.In cases where the document contains CDATASections,
the normalize operation alone may not be sufficient, since XPointers do
not differentiate between Text nodes and
CDATASection nodes. |
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 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 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;
}
|
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;
}
}
|
public boolean remove(CDATA cdata) {
return removeNode(cdata);
}
|
public boolean remove(Comment comment) {
return removeNode(comment);
}
|
public boolean remove(Element element) {
return removeNode(element);
}
|
public boolean remove(Entity entity) {
return removeNode(entity);
}
|
public boolean remove(Namespace namespace) {
return removeNode(namespace);
}
|
public boolean remove(ProcessingInstruction pi) {
return removeNode(pi);
}
|
public boolean remove(Text text) {
return removeNode(text);
}
|
protected boolean removeNode(Node node) {
boolean answer = contentList().remove(node);
if (answer) {
childRemoved(node);
}
return answer;
}
|
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;
}
|
public void setAttributeValue(String name,
String value) {
addAttribute(name, value);
} Deprecated! As - of version 0.5. Please use #addAttribute(String,String) instead. WILL BE REMOVED IN
dom4j-1.6 !!
|
public void setAttributeValue(QName qName,
String value) {
addAttribute(qName, value);
} Deprecated! As - of version 0.5. Please use #addAttribute(String,String) instead. WILL BE REMOVED IN
dom4j-1.6 !!
|
public void setAttributes(Attributes attributes,
NamespaceStack namespaceStack,
boolean noNamespaceAttributes) {
// now lets add all attribute values
int size = attributes.getLength();
if (size > 0) {
DocumentFactory factory = getDocumentFactory();
if (size == 1) {
// allow lazy construction of the List of Attributes
String name = attributes.getQName(0);
if (noNamespaceAttributes || !name.startsWith("xmlns")) {
String attributeURI = attributes.getURI(0);
String attributeLocalName = attributes.getLocalName(0);
String attributeValue = attributes.getValue(0);
QName attributeQName = namespaceStack.getAttributeQName(
attributeURI, attributeLocalName, name);
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 attributeName = attributes.getQName(i);
if (noNamespaceAttributes
|| !attributeName.startsWith("xmlns")) {
String attributeURI = attributes.getURI(i);
String attributeLocalName = attributes.getLocalName(i);
String attributeValue = attributes.getValue(i);
QName attributeQName = namespaceStack
.getAttributeQName(attributeURI,
attributeLocalName, attributeName);
Attribute attribute = factory.createAttribute(this,
attributeQName, attributeValue);
list.add(attribute);
childAdded(attribute);
}
}
}
}
}
This method provides a more optimal way of setting all the attributes on
an Element particularly for use in org.dom4j.io.SAXReader . |
public void setData(Object data) {
// ignore this method
}
|
public void setName(String name) {
setQName(getDocumentFactory().createQName(name));
}
|
public void setNamespace(Namespace namespace) {
setQName(getDocumentFactory().createQName(getName(), namespace));
}
|
public void setText(String text) {
/* remove all text nodes */
List allContent = contentList();
if (allContent != null) {
Iterator it = allContent.iterator();
while (it.hasNext()) {
Node node = (Node) it.next();
switch (node.getNodeType()) {
case CDATA_SECTION_NODE:
// case ENTITY_NODE:
case ENTITY_REFERENCE_NODE:
case TEXT_NODE:
it.remove();
default:
break;
}
}
}
addText(text);
}
|
public String toString() {
String uri = getNamespaceURI();
if ((uri != null) && (uri.length() > 0)) {
if (VERBOSE_TOSTRING) {
return super.toString() + " [Element: < " + getQualifiedName()
+ " uri: " + uri + " attributes: " + attributeList()
+ " content: " + contentList() + " / >]";
} else {
return super.toString() + " [Element: < " + getQualifiedName()
+ " uri: " + uri + " attributes: " + attributeList()
+ "/ >]";
}
} else {
if (VERBOSE_TOSTRING) {
return super.toString() + " [Element: < " + getQualifiedName()
+ " attributes: " + attributeList() + " content: "
+ contentList() + " / >]";
} else {
return super.toString() + " [Element: < " + getQualifiedName()
+ " attributes: " + attributeList() + "/ >]";
}
}
}
|
public void write(Writer out) throws IOException {
XMLWriter writer = new XMLWriter(out, new OutputFormat());
writer.write(this);
}
|