|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream.writers; |
|
|
|
import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; |
|
import com.sun.org.apache.xerces.internal.dom.DocumentImpl; |
|
import java.lang.reflect.InvocationTargetException; |
|
import java.lang.reflect.Method; |
|
import javax.xml.XMLConstants; |
|
import javax.xml.namespace.NamespaceContext; |
|
import javax.xml.stream.XMLStreamException; |
|
import javax.xml.stream.XMLStreamWriter; |
|
import javax.xml.transform.dom.DOMResult; |
|
import org.w3c.dom.Attr; |
|
import org.w3c.dom.CDATASection; |
|
import org.w3c.dom.Comment; |
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Element; |
|
import org.w3c.dom.EntityReference; |
|
import org.w3c.dom.Node; |
|
import org.w3c.dom.ProcessingInstruction; |
|
import org.w3c.dom.Text; |
|
import org.xml.sax.helpers.NamespaceSupport; |
|
|
|
/** |
|
* This class provides support to build a DOM tree using XMLStreamWriter API's. |
|
* @author K Venugopal |
|
*/ |
|
|
|
/* |
|
* TODO : -Venu |
|
* Internal NamespaceManagement |
|
* setPrefix |
|
* support for isRepairNamespace property. |
|
* Some Unsupported Methods. |
|
* Change StringBuffer to StringBuilder, when JDK 1.5 will be minimum requirement for SJSXP. |
|
*/ |
|
|
|
public class XMLDOMWriterImpl implements XMLStreamWriterBase { |
|
|
|
|
|
private Document ownerDoc = null; |
|
private Node currentNode = null; |
|
private Node node = null; |
|
private NamespaceSupport namespaceContext = null; |
|
private boolean [] needContextPop = null; |
|
private StringBuffer stringBuffer = null; |
|
private int resizeValue = 20; |
|
private int depth = 0; |
|
|
|
|
|
|
|
*/ |
|
public XMLDOMWriterImpl(DOMResult result) { |
|
|
|
node = result.getNode(); |
|
if( node.getNodeType() == Node.DOCUMENT_NODE){ |
|
ownerDoc = (Document)node; |
|
currentNode = ownerDoc; |
|
}else{ |
|
ownerDoc = node.getOwnerDocument(); |
|
currentNode = node; |
|
} |
|
stringBuffer = new StringBuffer(); |
|
needContextPop = new boolean[resizeValue]; |
|
namespaceContext = new NamespaceSupport(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void close() throws XMLStreamException { |
|
//no-op |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void flush() throws XMLStreamException { |
|
//no-op |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public javax.xml.namespace.NamespaceContext getNamespaceContext() { |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getPrefix(String namespaceURI) throws XMLStreamException { |
|
String prefix = null; |
|
if(this.namespaceContext != null){ |
|
prefix = namespaceContext.getPrefix(namespaceURI); |
|
} |
|
return prefix; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object getProperty(String str) throws IllegalArgumentException { |
|
throw new UnsupportedOperationException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setDefaultNamespace(String uri) throws XMLStreamException { |
|
namespaceContext.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX, uri); |
|
if(!needContextPop[depth]){ |
|
needContextPop[depth] = true; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setNamespaceContext(javax.xml.namespace.NamespaceContext namespaceContext) throws XMLStreamException { |
|
throw new UnsupportedOperationException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setPrefix(String prefix, String uri) throws XMLStreamException { |
|
if(prefix == null){ |
|
throw new XMLStreamException("Prefix cannot be null"); |
|
} |
|
namespaceContext.declarePrefix(prefix, uri); |
|
if(!needContextPop[depth]){ |
|
needContextPop[depth] = true; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeAttribute(String localName, String value) throws XMLStreamException { |
|
|
|
if(currentNode.getNodeType() == Node.ELEMENT_NODE){ |
|
Attr attr = ownerDoc.createAttribute(localName); |
|
attr.setValue(value); |
|
((Element)currentNode).setAttributeNode(attr); |
|
}else{ |
|
|
|
throw new IllegalStateException("Current DOM Node type is "+ currentNode.getNodeType() + |
|
"and does not allow attributes to be set "); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeAttribute(String namespaceURI,String localName,String value)throws XMLStreamException { |
|
if(currentNode.getNodeType() == Node.ELEMENT_NODE){ |
|
String prefix = null; |
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
if(namespaceContext != null){ |
|
prefix = namespaceContext.getPrefix(namespaceURI); |
|
} |
|
|
|
if(prefix == null){ |
|
throw new XMLStreamException("Namespace URI "+namespaceURI + |
|
"is not bound to any prefix" ); |
|
} |
|
|
|
String qualifiedName = null; |
|
if(prefix.equals("")){ |
|
qualifiedName = localName; |
|
}else{ |
|
qualifiedName = getQName(prefix,localName); |
|
} |
|
Attr attr = ownerDoc.createAttributeNS(namespaceURI, qualifiedName); |
|
attr.setValue(value); |
|
((Element)currentNode).setAttributeNode(attr); |
|
}else{ |
|
|
|
throw new IllegalStateException("Current DOM Node type is "+ currentNode.getNodeType() + |
|
"and does not allow attributes to be set "); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeAttribute(String prefix,String namespaceURI,String localName,String value)throws XMLStreamException { |
|
if(currentNode.getNodeType() == Node.ELEMENT_NODE){ |
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
if(prefix == null){ |
|
throw new XMLStreamException("prefix cannot be null"); |
|
} |
|
String qualifiedName = null; |
|
if(prefix.equals("")){ |
|
qualifiedName = localName; |
|
}else{ |
|
|
|
qualifiedName = getQName(prefix,localName); |
|
} |
|
Attr attr = ownerDoc.createAttributeNS(namespaceURI, qualifiedName); |
|
attr.setValue(value); |
|
((Element)currentNode).setAttributeNodeNS(attr); |
|
}else{ |
|
|
|
throw new IllegalStateException("Current DOM Node type is "+ currentNode.getNodeType() + |
|
"and does not allow attributes to be set "); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeCData(String data) throws XMLStreamException { |
|
if(data == null){ |
|
throw new XMLStreamException("CDATA cannot be null"); |
|
} |
|
|
|
CDATASection cdata = ownerDoc.createCDATASection(data); |
|
getNode().appendChild(cdata); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeCharacters(String charData) throws XMLStreamException { |
|
Text text = ownerDoc.createTextNode(charData); |
|
currentNode.appendChild(text); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeCharacters(char[] values, int param, int param2) throws XMLStreamException { |
|
|
|
Text text = ownerDoc.createTextNode(new String(values,param,param2)); |
|
currentNode.appendChild(text); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeComment(String str) throws XMLStreamException { |
|
Comment comment = ownerDoc.createComment(str); |
|
getNode().appendChild(comment); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeDTD(String str) throws XMLStreamException { |
|
throw new UnsupportedOperationException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { |
|
if(currentNode.getNodeType() == Node.ELEMENT_NODE){ |
|
String qname = XMLConstants.XMLNS_ATTRIBUTE; |
|
((Element)currentNode).setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,qname, namespaceURI); |
|
}else{ |
|
|
|
throw new IllegalStateException("Current DOM Node type is "+ currentNode.getNodeType() + |
|
"and does not allow attributes to be set "); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEmptyElement(String localName) throws XMLStreamException { |
|
if(ownerDoc != null){ |
|
Element element = ownerDoc.createElement(localName); |
|
if(currentNode!=null){ |
|
currentNode.appendChild(element); |
|
}else{ |
|
ownerDoc.appendChild(element); |
|
} |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { |
|
if(ownerDoc != null){ |
|
String qualifiedName = null; |
|
String prefix = null; |
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
|
|
if(namespaceContext != null){ |
|
prefix = namespaceContext.getPrefix(namespaceURI); |
|
} |
|
if(prefix == null){ |
|
throw new XMLStreamException("Namespace URI "+namespaceURI + |
|
"is not bound to any prefix" ); |
|
} |
|
if("".equals(prefix)){ |
|
qualifiedName = localName; |
|
}else{ |
|
|
|
qualifiedName = getQName(prefix,localName); |
|
|
|
} |
|
Element element = ownerDoc.createElementNS(namespaceURI, qualifiedName); |
|
if(currentNode!=null){ |
|
currentNode.appendChild(element); |
|
}else{ |
|
ownerDoc.appendChild(element); |
|
} |
|
//currentNode = element; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
if(ownerDoc != null){ |
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
if(prefix == null){ |
|
throw new XMLStreamException("Prefix cannot be null"); |
|
} |
|
String qualifiedName = null; |
|
if("".equals(prefix)){ |
|
qualifiedName = localName; |
|
}else{ |
|
qualifiedName = getQName(prefix,localName); |
|
} |
|
Element el = ownerDoc.createElementNS(namespaceURI,qualifiedName); |
|
if(currentNode!=null){ |
|
currentNode.appendChild(el); |
|
}else{ |
|
ownerDoc.appendChild(el); |
|
} |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEndDocument() throws XMLStreamException { |
|
|
|
currentNode = null; |
|
for(int i=0; i< depth;i++){ |
|
if(needContextPop[depth]){ |
|
needContextPop[depth] = false; |
|
namespaceContext.popContext(); |
|
} |
|
depth--; |
|
} |
|
depth =0; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEndElement() throws XMLStreamException { |
|
Node node= currentNode.getParentNode(); |
|
if(currentNode.getNodeType() == Node.DOCUMENT_NODE){ |
|
currentNode = null; |
|
}else{ |
|
currentNode = node; |
|
} |
|
if(needContextPop[depth]){ |
|
needContextPop[depth] = false; |
|
namespaceContext.popContext(); |
|
} |
|
depth--; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEntityRef(String name) throws XMLStreamException { |
|
EntityReference er = ownerDoc.createEntityReference(name); |
|
currentNode.appendChild(er); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { |
|
|
|
if (prefix == null) { |
|
throw new XMLStreamException("prefix cannot be null"); |
|
} |
|
|
|
if (namespaceURI == null) { |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
|
|
String qname = null; |
|
|
|
if (prefix.equals("")) { |
|
qname = XMLConstants.XMLNS_ATTRIBUTE; |
|
} else { |
|
qname = getQName(XMLConstants.XMLNS_ATTRIBUTE,prefix); |
|
} |
|
|
|
((Element)currentNode).setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,qname, namespaceURI); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeProcessingInstruction(String target) throws XMLStreamException { |
|
if(target == null){ |
|
throw new XMLStreamException("Target cannot be null"); |
|
} |
|
ProcessingInstruction pi = ownerDoc.createProcessingInstruction(target, ""); |
|
currentNode.appendChild(pi); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeProcessingInstruction(String target, String data) throws XMLStreamException { |
|
if(target == null){ |
|
throw new XMLStreamException("Target cannot be null"); |
|
} |
|
ProcessingInstruction pi = ownerDoc.createProcessingInstruction(target, data); |
|
currentNode.appendChild(pi); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument() throws XMLStreamException { |
|
ownerDoc.setXmlVersion("1.0"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument(String version) throws XMLStreamException { |
|
writeStartDocument(null, version, false, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument(String encoding, String version) throws XMLStreamException { |
|
writeStartDocument(encoding, version, false, false); |
|
} |
|
|
|
@Override |
|
public void writeStartDocument(String encoding, String version, boolean standalone, boolean standaloneSet) throws XMLStreamException { |
|
if (encoding != null && ownerDoc.getClass().isAssignableFrom(DocumentImpl.class)) { |
|
((DocumentImpl)ownerDoc).setXmlEncoding(encoding); |
|
} |
|
|
|
ownerDoc.setXmlVersion(version); |
|
|
|
if (standaloneSet) { |
|
ownerDoc.setXmlStandalone(standalone); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartElement(String localName) throws XMLStreamException { |
|
if(ownerDoc != null){ |
|
Element element = ownerDoc.createElement(localName); |
|
if(currentNode!=null){ |
|
currentNode.appendChild(element); |
|
}else{ |
|
ownerDoc.appendChild(element); |
|
} |
|
currentNode = element; |
|
} |
|
if(needContextPop[depth]){ |
|
namespaceContext.pushContext(); |
|
} |
|
incDepth(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { |
|
if(ownerDoc != null){ |
|
String qualifiedName = null; |
|
String prefix = null; |
|
|
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
|
|
if(namespaceContext != null){ |
|
prefix = namespaceContext.getPrefix(namespaceURI); |
|
} |
|
if(prefix == null){ |
|
throw new XMLStreamException("Namespace URI "+namespaceURI + |
|
"is not bound to any prefix" ); |
|
} |
|
if("".equals(prefix)){ |
|
qualifiedName = localName; |
|
}else{ |
|
qualifiedName = getQName(prefix,localName); |
|
} |
|
|
|
Element element = ownerDoc.createElementNS(namespaceURI, qualifiedName); |
|
|
|
if(currentNode!=null){ |
|
currentNode.appendChild(element); |
|
}else{ |
|
ownerDoc.appendChild(element); |
|
} |
|
currentNode = element; |
|
} |
|
if(needContextPop[depth]){ |
|
namespaceContext.pushContext(); |
|
} |
|
incDepth(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
|
|
if(ownerDoc != null){ |
|
String qname = null; |
|
if(namespaceURI == null ){ |
|
throw new XMLStreamException("NamespaceURI cannot be null"); |
|
} |
|
if(localName == null){ |
|
throw new XMLStreamException("Local name cannot be null"); |
|
} |
|
if(prefix == null){ |
|
throw new XMLStreamException("Prefix cannot be null"); |
|
} |
|
|
|
if(prefix.equals("")){ |
|
qname = localName; |
|
}else{ |
|
qname = getQName(prefix,localName); |
|
} |
|
|
|
Element el = ownerDoc.createElementNS(namespaceURI,qname); |
|
|
|
if(currentNode!=null){ |
|
currentNode.appendChild(el); |
|
}else{ |
|
ownerDoc.appendChild(el); |
|
} |
|
currentNode = el; |
|
if(needContextPop[depth]){ |
|
namespaceContext.pushContext(); |
|
} |
|
incDepth(); |
|
|
|
} |
|
} |
|
|
|
private String getQName(String prefix , String localName){ |
|
stringBuffer.setLength(0); |
|
stringBuffer.append(prefix); |
|
stringBuffer.append(":"); |
|
stringBuffer.append(localName); |
|
return stringBuffer.toString(); |
|
} |
|
|
|
private Node getNode(){ |
|
if(currentNode == null){ |
|
return ownerDoc; |
|
} else{ |
|
return currentNode; |
|
} |
|
} |
|
private void incDepth() { |
|
depth++; |
|
if (depth == needContextPop.length) { |
|
boolean[] array = new boolean[depth + resizeValue]; |
|
System.arraycopy(needContextPop, 0, array, 0, depth); |
|
needContextPop = array; |
|
} |
|
} |
|
} |