| Method from org.dom4j.io.DOMWriter Detail: |
protected void appendDOMTree(Document domDocument,
Node domCurrent,
List content) {
int size = content.size();
for (int i = 0; i < size; i++) {
Object object = content.get(i);
if (object instanceof Element) {
appendDOMTree(domDocument, domCurrent, (Element) object);
} else if (object instanceof String) {
appendDOMTree(domDocument, domCurrent, (String) object);
} else if (object instanceof Text) {
Text text = (Text) object;
appendDOMTree(domDocument, domCurrent, text.getText());
} else if (object instanceof CDATA) {
appendDOMTree(domDocument, domCurrent, (CDATA) object);
} else if (object instanceof Comment) {
appendDOMTree(domDocument, domCurrent, (Comment) object);
} else if (object instanceof Entity) {
appendDOMTree(domDocument, domCurrent, (Entity) object);
} else if (object instanceof ProcessingInstruction) {
appendDOMTree(domDocument, domCurrent,
(ProcessingInstruction) object);
}
}
}
|
protected void appendDOMTree(Document domDocument,
Node domCurrent,
Element element) {
String elUri = element.getNamespaceURI();
String elName = element.getQualifiedName();
org.w3c.dom.Element domElement = domDocument.createElementNS(elUri,
elName);
int stackSize = namespaceStack.size();
// add the namespace of the element first
Namespace elementNamespace = element.getNamespace();
if (isNamespaceDeclaration(elementNamespace)) {
namespaceStack.push(elementNamespace);
writeNamespace(domElement, elementNamespace);
}
// add the additional declared namespaces
List declaredNamespaces = element.declaredNamespaces();
for (int i = 0, size = declaredNamespaces.size(); i < size; i++) {
Namespace namespace = (Namespace) declaredNamespaces.get(i);
if (isNamespaceDeclaration(namespace)) {
namespaceStack.push(namespace);
writeNamespace(domElement, namespace);
}
}
// add the attributes
for (int i = 0, size = element.attributeCount(); i < size; i++) {
Attribute attribute = (Attribute) element.attribute(i);
String attUri = attribute.getNamespaceURI();
String attName = attribute.getQualifiedName();
String value = attribute.getValue();
domElement.setAttributeNS(attUri, attName, value);
}
// add content
appendDOMTree(domDocument, domElement, element.content());
domCurrent.appendChild(domElement);
while (namespaceStack.size() > stackSize) {
namespaceStack.pop();
}
}
|
protected void appendDOMTree(Document domDocument,
Node domCurrent,
CDATA cdata) {
org.w3c.dom.CDATASection domCDATA = domDocument
.createCDATASection(cdata.getText());
domCurrent.appendChild(domCDATA);
}
|
protected void appendDOMTree(Document domDocument,
Node domCurrent,
Comment comment) {
org.w3c.dom.Comment domComment = domDocument.createComment(comment
.getText());
domCurrent.appendChild(domComment);
}
|
protected void appendDOMTree(Document domDocument,
Node domCurrent,
String text) {
org.w3c.dom.Text domText = domDocument.createTextNode(text);
domCurrent.appendChild(domText);
}
|
protected void appendDOMTree(Document domDocument,
Node domCurrent,
Entity entity) {
org.w3c.dom.EntityReference domEntity = domDocument
.createEntityReference(entity.getName());
domCurrent.appendChild(domEntity);
}
|
protected void appendDOMTree(Document domDoc,
Node domCurrent,
ProcessingInstruction pi) {
org.w3c.dom.ProcessingInstruction domPI = domDoc
.createProcessingInstruction(pi.getTarget(), pi.getText());
domCurrent.appendChild(domPI);
}
|
protected String attributeNameForNamespace(Namespace namespace) {
String xmlns = "xmlns";
String prefix = namespace.getPrefix();
if (prefix.length() > 0) {
return xmlns + ":" + prefix;
}
return xmlns;
}
|
protected Document createDomDocument(Document document) throws DocumentException {
org.w3c.dom.Document result = null;
// use the given domDocumentClass (if not null)
if (domDocumentClass != null) {
try {
result = (org.w3c.dom.Document) domDocumentClass.newInstance();
} catch (Exception e) {
throw new DocumentException(
"Could not instantiate an instance "
+ "of DOM Document with class: "
+ domDocumentClass.getName(), e);
}
} else {
// lets try JAXP first before using the hardcoded default parsers
result = createDomDocumentViaJAXP();
if (result == null) {
Class theClass = getDomDocumentClass();
try {
result = (org.w3c.dom.Document) theClass.newInstance();
} catch (Exception e) {
throw new DocumentException("Could not instantiate an "
+ "instance of DOM Document " + "with class: "
+ theClass.getName(), e);
}
}
}
return result;
}
|
protected Document createDomDocument(Document document,
DOMImplementation domImpl) throws DocumentException {
String namespaceURI = null;
String qualifiedName = null;
org.w3c.dom.DocumentType docType = null;
return domImpl.createDocument(namespaceURI, qualifiedName, docType);
}
|
protected Document createDomDocumentViaJAXP() throws DocumentException {
try {
return JAXPHelper.createDocument(false, true);
} catch (Throwable e) {
if (!loggedWarning) {
loggedWarning = true;
if (SAXHelper.isVerboseErrorReporting()) {
// log all exceptions as warnings and carry
// on as we have a default SAX parser we can use
System.out.println("Warning: Caught exception attempting "
+ "to use JAXP to create a W3C DOM " + "document");
System.out.println("Warning: Exception was: " + e);
e.printStackTrace();
} else {
System.out.println("Warning: Error occurred using JAXP to "
+ "create a DOM document.");
}
}
}
return null;
}
|
public Class getDomDocumentClass() throws DocumentException {
Class result = domDocumentClass;
if (result == null) {
// lets try and find one in the classpath
int size = DEFAULT_DOM_DOCUMENT_CLASSES.length;
for (int i = 0; i < size; i++) {
try {
String name = DEFAULT_DOM_DOCUMENT_CLASSES[i];
result = Class.forName(name, true, DOMWriter.class
.getClassLoader());
if (result != null) {
break;
}
} catch (Exception e) {
// could not load class correctly
// lets carry on to the next one
}
}
}
return result;
}
|
protected boolean isNamespaceDeclaration(Namespace ns) {
if ((ns != null) && (ns != Namespace.NO_NAMESPACE)
&& (ns != Namespace.XML_NAMESPACE)) {
String uri = ns.getURI();
if ((uri != null) && (uri.length() > 0)) {
if (!namespaceStack.contains(ns)) {
return true;
}
}
}
return false;
}
|
protected void resetNamespaceStack() {
namespaceStack.clear();
namespaceStack.push(Namespace.XML_NAMESPACE);
}
|
public void setDomDocumentClass(Class domDocumentClass) {
this.domDocumentClass = domDocumentClass;
}
Sets the DOM org.w3c.dom.Document implementation class used by
the writer when creating DOM documents. |
public void setDomDocumentClassName(String name) throws DocumentException {
try {
this.domDocumentClass = Class.forName(name, true, DOMWriter.class
.getClassLoader());
} catch (Exception e) {
throw new DocumentException("Could not load the DOM Document "
+ "class: " + name, e);
}
}
Sets the DOM org.w3c.dom.Document implementation class name used
by the writer when creating DOM documents. |
public Document write(Document document) throws DocumentException {
if (document instanceof org.w3c.dom.Document) {
return (org.w3c.dom.Document) document;
}
resetNamespaceStack();
org.w3c.dom.Document domDocument = createDomDocument(document);
appendDOMTree(domDocument, domDocument, document.content());
namespaceStack.clear();
return domDocument;
}
|
public Document write(Document document,
DOMImplementation domImpl) throws DocumentException {
if (document instanceof org.w3c.dom.Document) {
return (org.w3c.dom.Document) document;
}
resetNamespaceStack();
org.w3c.dom.Document domDocument = createDomDocument(document, domImpl);
appendDOMTree(domDocument, domDocument, document.content());
namespaceStack.clear();
return domDocument;
}
|
protected void writeNamespace(Element domElement,
Namespace namespace) {
String attributeName = attributeNameForNamespace(namespace);
// domElement.setAttributeNS("", attributeName, namespace.getURI());
domElement.setAttribute(attributeName, namespace.getURI());
}
|