List of usage examples for javax.xml.stream XMLStreamWriter writeProcessingInstruction
public void writeProcessingInstruction(String target, String data) throws XMLStreamException;
From source file:microsoft.exchange.webservices.data.core.EwsServiceXmlWriter.java
/** * @param xmlNode XML node//ww w. jav a 2s. c o m * @param xmlStreamWriter XML stream writer * @throws XMLStreamException the XML stream exception */ public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { if (xmlNode instanceof Element) { addElement((Element) xmlNode, xmlStreamWriter); } else if (xmlNode instanceof Text) { xmlStreamWriter.writeCharacters(xmlNode.getNodeValue()); } else if (xmlNode instanceof CDATASection) { xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData()); } else if (xmlNode instanceof Comment) { xmlStreamWriter.writeComment(((Comment) xmlNode).getData()); } else if (xmlNode instanceof EntityReference) { xmlStreamWriter.writeEntityRef(xmlNode.getNodeValue()); } else if (xmlNode instanceof ProcessingInstruction) { ProcessingInstruction procInst = (ProcessingInstruction) xmlNode; xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(), procInst.getData()); } else if (xmlNode instanceof Document) { writeToDocument((Document) xmlNode, xmlStreamWriter); } }
From source file:XmlReaderToWriter.java
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { switch (xmlr.getEventType()) { case XMLEvent.START_ELEMENT: final String localName = xmlr.getLocalName(); final String namespaceURI = xmlr.getNamespaceURI(); if (namespaceURI != null && namespaceURI.length() > 0) { final String prefix = xmlr.getPrefix(); if (prefix != null) writer.writeStartElement(prefix, localName, namespaceURI); else//ww w.ja va2 s . co m writer.writeStartElement(namespaceURI, localName); } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) { writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i)); } for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) { String attUri = xmlr.getAttributeNamespace(i); if (attUri != null) writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); else writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } break; case XMLEvent.END_ELEMENT: writer.writeEndElement(); break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData()); break; case XMLEvent.CDATA: writer.writeCData(xmlr.getText()); break; case XMLEvent.COMMENT: writer.writeComment(xmlr.getText()); break; case XMLEvent.ENTITY_REFERENCE: writer.writeEntityRef(xmlr.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlr.getCharacterEncodingScheme(); String version = xmlr.getVersion(); if (encoding != null && version != null) writer.writeStartDocument(encoding, version); else if (version != null) writer.writeStartDocument(xmlr.getVersion()); break; case XMLEvent.END_DOCUMENT: writer.writeEndDocument(); break; case XMLEvent.DTD: writer.writeDTD(xmlr.getText()); break; } }
From source file:Main.java
/** * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter *//*w ww .ja v a 2 s . co m*/ public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { switch (xmlr.getEventType()) { case XMLEvent.START_ELEMENT: final String localName = xmlr.getLocalName(); final String namespaceURI = xmlr.getNamespaceURI(); if (namespaceURI != null && namespaceURI.length() > 0) { final String prefix = xmlr.getPrefix(); if (prefix != null) { writer.writeStartElement(prefix, localName, namespaceURI); } else { writer.writeStartElement(namespaceURI, localName); } } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) { String prefix = xmlr.getNamespacePrefix(i); if (prefix == null) { writer.writeDefaultNamespace(xmlr.getNamespaceURI(i)); } else { writer.writeNamespace(prefix, xmlr.getNamespaceURI(i)); } } for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) { final String attUri = xmlr.getAttributeNamespace(i); if (attUri != null && attUri.length() > 0) { final String prefix = xmlr.getAttributePrefix(i); if (prefix != null) { writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } else { writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } } else { writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: writer.writeEndElement(); break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: char[] text = new char[xmlr.getTextLength()]; xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength()); writer.writeCharacters(text, 0, text.length); break; case XMLEvent.PROCESSING_INSTRUCTION: writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData()); break; case XMLEvent.CDATA: writer.writeCData(xmlr.getText()); break; case XMLEvent.COMMENT: writer.writeComment(xmlr.getText()); break; case XMLEvent.ENTITY_REFERENCE: writer.writeEntityRef(xmlr.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlr.getCharacterEncodingScheme(); String version = xmlr.getVersion(); if (encoding != null && version != null) { writer.writeStartDocument(encoding, version); } else if (version != null) { writer.writeStartDocument(xmlr.getVersion()); } break; case XMLEvent.END_DOCUMENT: writer.writeEndDocument(); break; case XMLEvent.DTD: writer.writeDTD(xmlr.getText()); break; } }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * Method serializeProcessingInstruction. * * @param reader//from w ww. j a v a 2 s .c o m * @param writer * @throws XMLStreamException */ protected void serializeProcessingInstruction(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { writer.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); }
From source file:org.apache.synapse.commons.json.JsonDataSource.java
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException { XMLStreamReader reader = getReader(); xmlWriter.writeStartDocument();/*from ww w.ja va 2s .c om*/ while (reader.hasNext()) { int x = reader.next(); switch (x) { case XMLStreamConstants.START_ELEMENT: xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); int namespaceCount = reader.getNamespaceCount(); for (int i = namespaceCount - 1; i >= 0; i--) { xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); } int attributeCount = reader.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } break; case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.CHARACTERS: xmlWriter.writeCharacters(reader.getText()); break; case XMLStreamConstants.CDATA: xmlWriter.writeCData(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: xmlWriter.writeEndElement(); break; case XMLStreamConstants.END_DOCUMENT: xmlWriter.writeEndDocument(); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: xmlWriter.writeComment(reader.getText()); break; case XMLStreamConstants.DTD: xmlWriter.writeDTD(reader.getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); break; case XMLStreamConstants.ENTITY_REFERENCE: xmlWriter.writeEntityRef(reader.getLocalName()); break; default: throw new OMException(); } } xmlWriter.writeEndDocument(); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.chorusbdd.chorus.tools.webagent.jettyhandler.XmlStreamingHandler.java
protected void addStylesheetInstruction(XMLStreamWriter writer, String stylesheetName) throws XMLStreamException { writer.writeProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/" + stylesheetName + "'"); writer.writeCharacters("\n"); }
From source file:org.dita.dost.reader.ChunkMapReader.java
/** * Create the new topic stump./* ww w . j av a 2 s . co m*/ */ private void createTopicStump(final File newFile) { OutputStream newFileWriter = null; try { newFileWriter = new FileOutputStream(newFile); final XMLStreamWriter o = XMLOutputFactory.newInstance().createXMLStreamWriter(newFileWriter, UTF8); o.writeStartDocument(); o.writeProcessingInstruction(PI_WORKDIR_TARGET, UNIX_SEPARATOR + newFile.getParentFile().getAbsolutePath()); o.writeProcessingInstruction(PI_WORKDIR_TARGET_URI, newFile.getParentFile().toURI().toString()); o.writeStartElement(ELEMENT_NAME_DITA); o.writeEndElement(); o.writeEndDocument(); o.close(); newFileWriter.flush(); } catch (final RuntimeException e) { throw e; } catch (final Exception e) { logger.error(e.getMessage(), e); } finally { try { if (newFileWriter != null) { newFileWriter.close(); } } catch (final Exception e) { logger.error(e.getMessage(), e); } } }
From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java
static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {/*from w w w. j a v a 2s. co m*/ // TransformerFactory tf = TransformerFactory.newInstance(); // Transformer t = tf.newTransformer(); // StAXSource source = new StAXSource(xmlStreamReader); // StAXResult result = new StAXResult(xmlStreamWriter); // t.transform(source, result); System.out.println("Defnition Id : " + xmlStreamReader.getAttributeValue(null, "id")); boolean finish = false; while (xmlStreamReader.hasNext() && !finish) { switch (xmlStreamReader.getEventType()) { case XMLEvent.START_ELEMENT: String prefix = xmlStreamReader.getPrefix(); String namespaceURI = xmlStreamReader.getNamespaceURI(); if (namespaceURI != null) { if (prefix != null) { xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(), xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI()); } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(), xmlStreamReader.getLocalName()); } } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName()); } for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) { xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)); } int count = xmlStreamReader.getAttributeCount(); for (int i = 0; i < count; i++) { // xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i), // xmlStreamReader.getAttributeNamespace(i), // xmlStreamReader.getAttributeLocalName(i), // xmlStreamReader.getAttributeValue(i)); String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i), attrPrefix = xmlStreamReader.getAttributePrefix(i); if (attrNamespaceURI != null) { if (attrPrefix != null) { xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } else { xmlStreamWriter.writeAttribute(attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } else { xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: xmlStreamWriter.writeEndElement(); if (xmlStreamReader.getLocalName().equals("definitions")) { finish = true; } break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(), xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(), xmlStreamReader.getPIData()); break; case XMLEvent.CDATA: xmlStreamWriter.writeCData(xmlStreamReader.getText()); break; case XMLEvent.COMMENT: xmlStreamWriter.writeComment(xmlStreamReader.getText()); break; case XMLEvent.ENTITY_REFERENCE: xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlStreamReader.getCharacterEncodingScheme(); String version = xmlStreamReader.getVersion(); if (encoding != null && version != null) { xmlStreamWriter.writeStartDocument(encoding, version); } else if (version != null) { xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion()); } break; case XMLEvent.END_DOCUMENT: xmlStreamWriter.writeEndDocument(); break; case XMLEvent.DTD: xmlStreamWriter.writeDTD(xmlStreamReader.getText()); break; } if (!finish) { xmlStreamReader.next(); } } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java
void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) { try {//from ww w .j av a2s. c om // TransformerFactory tf = TransformerFactory.newInstance(); // Transformer t = tf.newTransformer(); // StAXSource source = new StAXSource(xmlStreamReader); // StAXResult result = new StAXResult(xmlStreamWriter); // t.transform(source, result); boolean finish = false; while (xmlStreamReader.hasNext() && !finish) { switch (xmlStreamReader.getEventType()) { case XMLEvent.START_ELEMENT: String prefix = xmlStreamReader.getPrefix(); String namespaceURI = xmlStreamReader.getNamespaceURI(); if (namespaceURI != null) { if (prefix != null) { xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(), xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI()); } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(), xmlStreamReader.getLocalName()); } } else { xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName()); } for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) { xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)); } int count = xmlStreamReader.getAttributeCount(); for (int i = 0; i < count; i++) { // xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i), // xmlStreamReader.getAttributeNamespace(i), // xmlStreamReader.getAttributeLocalName(i), // xmlStreamReader.getAttributeValue(i)); String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i), attrPrefix = xmlStreamReader.getAttributePrefix(i); if (attrNamespaceURI != null) { if (attrPrefix != null) { xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } else { xmlStreamWriter.writeAttribute(attrNamespaceURI, xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } else { xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i), xmlStreamReader.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: xmlStreamWriter.writeEndElement(); if (xmlStreamReader.getLocalName().equals("definitions")) { finish = true; } break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(), xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(), xmlStreamReader.getPIData()); break; case XMLEvent.CDATA: xmlStreamWriter.writeCData(xmlStreamReader.getText()); break; case XMLEvent.COMMENT: xmlStreamWriter.writeComment(xmlStreamReader.getText()); break; case XMLEvent.ENTITY_REFERENCE: xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlStreamReader.getCharacterEncodingScheme(); String version = xmlStreamReader.getVersion(); if (encoding != null && version != null) { xmlStreamWriter.writeStartDocument(encoding, version); } else if (version != null) { xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion()); } break; case XMLEvent.END_DOCUMENT: xmlStreamWriter.writeEndDocument(); break; case XMLEvent.DTD: xmlStreamWriter.writeDTD(xmlStreamReader.getText()); break; } if (!finish) { xmlStreamReader.next(); } } } catch (XMLStreamException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;//ww w. j a va 2 s. co m XMLStreamReader rdr = null; OutputStream out = null; XMLStreamWriter outWrt = null; try { Set<String> processedPropertyNames = new HashSet<String>(); in = new BufferedInputStream(new FileInputStream(configFile)); rdr = XML_INPUT_FACTORY.createXMLStreamReader(in); File tmpFile = File.createTempFile("hadoop-plugin", null); out = new FileOutputStream(tmpFile); outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); ByteArrayOutputStream stash = new ByteArrayOutputStream(); XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash); boolean outputActive = true; outWrt.writeStartDocument(); while (rdr.hasNext()) { int event = rdr.next(); XMLStreamWriter wrt = outputActive ? outWrt : stashWrt; switch (event) { case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CDATA: wrt.writeCData(rdr.getText()); break; case XMLStreamConstants.CHARACTERS: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.COMMENT: wrt.writeComment(rdr.getText()); break; case XMLStreamConstants.DTD: wrt.writeDTD(rdr.getText()); break; case XMLStreamConstants.END_DOCUMENT: wrt.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { String encoding = rdr.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } String propertyTagSoFar = Charset.forName(encoding) .decode(ByteBuffer.wrap(stash.toByteArray())).toString(); DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps); //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write. outWrt.flush(); out.write(propAndTag.updatedTag.getBytes("UTF-8")); processedPropertyNames.add(propAndTag.propertyName); //reset stuff stash.reset(); wrt = outWrt; outputActive = true; } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) { //now add the new props for (String prop : processedPropertyNames) { allProps.remove(prop); } for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) { outWrt.writeStartElement(PROPERTY_TAG_NAME); outWrt.writeStartElement(NAME_TAG_NAME); outWrt.writeCharacters(e.getKey()); outWrt.writeEndElement(); outWrt.writeStartElement(VALUE_TAG_NAME); outWrt.writeCharacters(e.getValue().getStringValue()); outWrt.writeEndElement(); outWrt.writeEndElement(); } } wrt.writeEndElement(); break; case XMLStreamConstants.ENTITY_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.ENTITY_REFERENCE: wrt.writeEntityRef(rdr.getText()); break; case XMLStreamConstants.NAMESPACE: for (int i = 0; i < rdr.getNamespaceCount(); ++i) { wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i)); } break; case XMLStreamConstants.NOTATION_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.PROCESSING_INSTRUCTION: wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData()); break; case XMLStreamConstants.SPACE: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.START_DOCUMENT: //this seems to be never called for some strange reason //wrt.writeStartDocument(); break; case XMLStreamConstants.START_ELEMENT: wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(), rdr.getName().getNamespaceURI()); for (int i = 0; i < rdr.getAttributeCount(); ++i) { wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i), rdr.getAttributeLocalName(i), rdr.getAttributeValue(i)); } if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { wrt.writeCharacters(""); outputActive = false; } break; } } outWrt.flush(); out.flush(); out.close(); in.close(); //now copy the temp file in the place of the original one FileUtil.copyFile(tmpFile, configFile); } finally { rdr.close(); outWrt.flush(); outWrt.close(); try { in.close(); } finally { out.flush(); out.close(); } } }