| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.stax; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.PipelineConfiguration; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.om.NamespaceBinding; |
| 15 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 16 |
import net.sf.saxon.om.NodeName; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.SchemaType; |
| 19 |
import net.sf.saxon.type.SimpleType; |
| 20 |
|
| 21 |
import javax.xml.stream.XMLStreamException; |
| 22 |
import javax.xml.stream.XMLStreamWriter; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ReceiverToXMLStreamWriter implements Receiver { |
| 28 |
|
| 29 |
protected PipelineConfiguration pipe; |
| 30 |
protected Configuration config; |
| 31 |
protected String systemId; |
| 32 |
protected String baseURI; |
| 33 |
private XMLStreamWriter writer; |
| 34 |
|
| 35 |
public ReceiverToXMLStreamWriter(XMLStreamWriter writer) { |
| 36 |
this.writer = writer; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public XMLStreamWriter getXMLStreamWriter() { |
| 46 |
return writer; |
| 47 |
} |
| 48 |
|
| 49 |
public void setPipelineConfiguration(PipelineConfiguration pipe) { |
| 50 |
this.pipe = pipe; |
| 51 |
config = pipe.getConfiguration(); |
| 52 |
} |
| 53 |
|
| 54 |
public PipelineConfiguration getPipelineConfiguration() { |
| 55 |
return pipe; |
| 56 |
} |
| 57 |
|
| 58 |
public void setSystemId(String systemId) { |
| 59 |
this.systemId = systemId; |
| 60 |
} |
| 61 |
|
| 62 |
public String getSystemId() { |
| 63 |
return systemId; |
| 64 |
} |
| 65 |
|
| 66 |
public void open() throws XPathException { |
| 67 |
} |
| 68 |
|
| 69 |
public void startDocument(int properties) throws XPathException { |
| 70 |
try { |
| 71 |
writer.writeStartDocument(); |
| 72 |
} catch (XMLStreamException e) { |
| 73 |
throw new XPathException(e); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public void endDocument() throws XPathException { |
| 78 |
try { |
| 79 |
writer.writeEndDocument(); |
| 80 |
} catch (XMLStreamException e) { |
| 81 |
throw new XPathException(e); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException { |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 90 |
String local = elemName.getLocalPart(); |
| 91 |
String uri = elemName.getURI(); |
| 92 |
String prefix = elemName.getPrefix(); |
| 93 |
try { |
| 94 |
if (prefix.equals("") && uri.equals("")) { |
| 95 |
writer.writeStartElement(local); |
| 96 |
} else if (prefix.equals("") && !uri.equals("")) { |
| 97 |
writer.writeStartElement(prefix, local, uri); |
| 98 |
} else { |
| 99 |
writer.writeStartElement(prefix, local, uri); |
| 100 |
} |
| 101 |
} catch (XMLStreamException e) { |
| 102 |
throw new XPathException(e); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 108 |
for (NamespaceBinding ns : namespaceBindings) { |
| 109 |
try { |
| 110 |
writer.writeNamespace(ns.getPrefix(), ns.getURI()); |
| 111 |
} catch (XMLStreamException e) { |
| 112 |
throw new XPathException(e); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 118 |
String local = attName.getLocalPart(); |
| 119 |
String uri = attName.getURI(); |
| 120 |
String prefix = attName.getPrefix(); |
| 121 |
try { |
| 122 |
if (prefix.equals("") && uri.equals("")) { |
| 123 |
writer.writeAttribute(local, value.toString()); |
| 124 |
} else if (prefix.equals("") & !uri.equals("")) { |
| 125 |
writer.writeAttribute(uri, local, value.toString()); |
| 126 |
} else { |
| 127 |
writer.writeAttribute(prefix, uri, local, value.toString()); |
| 128 |
} |
| 129 |
} catch (XMLStreamException e) { |
| 130 |
throw new XPathException(e); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public void startContent() throws XPathException { |
| 135 |
} |
| 136 |
|
| 137 |
public void endElement() throws XPathException { |
| 138 |
try { |
| 139 |
writer.writeEndElement(); |
| 140 |
} catch (XMLStreamException e) { |
| 141 |
throw new XPathException(e); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 146 |
try { |
| 147 |
writer.writeCharacters(chars.toString()); |
| 148 |
} catch (XMLStreamException e) { |
| 149 |
throw new XPathException(e); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public void processingInstruction(String name, CharSequence data, Location locationId, int properties) throws XPathException { |
| 154 |
try { |
| 155 |
writer.writeProcessingInstruction(name, data.toString()); |
| 156 |
} catch (XMLStreamException e) { |
| 157 |
throw new XPathException(e); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public void comment(CharSequence content, Location locationId, int properties) throws XPathException { |
| 162 |
try { |
| 163 |
writer.writeComment(content.toString()); |
| 164 |
} catch (XMLStreamException e) { |
| 165 |
throw new XPathException(e); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public void close() throws XPathException { |
| 170 |
try { |
| 171 |
writer.close(); |
| 172 |
} catch (XMLStreamException e) { |
| 173 |
throw new XPathException(e); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public boolean usesTypeAnnotations() { |
| 178 |
return false; |
| 179 |
} |
| 180 |
} |
| 181 |
|