public byte[] convertToXML(BpmnModel model, String encoding) { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding); XMLStreamWriter writer = xof.createXMLStreamWriter(out); XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer); xtw.writeEndElement(); xtw.writeEndElement(); xtw.writeEndDocument(); xtw.flush(); outputStream.close(); xtw.close(); return outputStream.toByteArray();
/** * @since 1.2 */ public void setRepairingNamespace(boolean repairing) { getOutputFactory().setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, repairing ? Boolean.TRUE : Boolean.FALSE); }
@Test public void marshalStaxResultXMLEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); Result result = StaxUtils.createStaxResult(eventWriter); marshaller.marshal(flight, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
private XmlWriter(Writer writer) { try { XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance(); stream = xmlFactory.createXMLStreamWriter(writer); } catch (Exception e) { throw new WriterException("Fail to initialize XML Stream writer", e); } }
@Override protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result); return new StaxEventHandler(eventWriter); }
@Override protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(result); return new StaxStreamHandler(streamWriter); }
@Override public String getFingerprint() throws AuthorizationAccessException { final List<AccessPolicy> policies = new ArrayList<>(getAccessPolicies()); Collections.sort(policies, Comparator.comparing(AccessPolicy::getIdentifier)); XMLStreamWriter writer = null; final StringWriter out = new StringWriter(); try { writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); writer.writeStartDocument(); writer.writeStartElement("accessPolicies"); for (AccessPolicy policy : policies) { writePolicy(writer, policy); } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); } catch (XMLStreamException e) { throw new AuthorizationAccessException("Unable to generate fingerprint", e); } finally { if (writer != null) { try { writer.close(); } catch (XMLStreamException e) { // nothing to do here } } } return out.toString(); }
xmlif = XMLInputFactory.newInstance(); xmlif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); baos = new ByteArrayOutputStream(); xmlWriter = outputFactory.createXMLStreamWriter(baos, "UTF-8"); baos = new ByteArrayOutputStream(); xmlWriter = outputFactory.createXMLStreamWriter(baos, "UTF-8"); xmlWriter.flush(); xmlWriter.close(); byteArray.setBytes(baos.toByteArray());
CompletionStage<Void> openStream(final OutputStream outputStream, final StreamHeader streamHeader) { return CompletableFuture.runAsync(() -> { this.outputStream = outputStream; try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { XMLStreamWriter writer = null; try { writer = xmppSession.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(byteArrayOutputStream, StandardCharsets.UTF_8.name()); streamOpened = false; streamHeader.writeTo(writer); write(byteArrayOutputStream.toByteArray()); outputStream.flush(); streamOpened = true; } finally { if (writer != null) { writer.close(); } } } catch (Exception e) { notifyException(e); } }, executor); }
@Test public void marshalJaxp14StaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); StAXResult result = new StAXResult(streamWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
static void writeOut( ByteArrayOutputStream bos ) throws XMLStreamException { XMLInputFactory infac = XMLInputFactory.newInstance(); XMLOutputFactory outfac = XMLOutputFactory.newInstance(); ByteArrayInputStream bin = new ByteArrayInputStream( bos.toByteArray() ); XMLStreamReader reader = infac.createXMLStreamReader( bin ); XMLStreamWriter writer = new IndentingXMLStreamWriter( outfac.createXMLStreamWriter( bos = new ByteArrayOutputStream() ) ); copy( reader, writer ); reader.close(); writer.close(); }
public Document parse(InputStream input) throws IOException { try { XMLStreamReader streamReader = inputFactory.createXMLStreamReader(input); XMLInputFactory readerFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(baos); eventWriter.add(eventReader); eventWriter.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); return getDocumentBuilder().parse(bais);
public XMLStreamReader getReader() throws XMLStreamException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(XMLREADER_DEFAULT_MESSAGE_SIZE); XMLStreamWriter xmlWriter = DBUtils.getXMLOutputFactory().createXMLStreamWriter(byteOut); this.serialize(xmlWriter); xmlWriter.close(); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); return DBUtils.getXMLInputFactory().createXMLStreamReader(byteIn); }
public void sendError(MessageExchange exchange, Exception error, HttpServletRequest request, HttpServletResponse response) throws Exception { addResponseHeaders(exchange, request, response); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); OutputStream encodingStream = getResponseEncodingStream(request.getHeader(HttpHeaders.ACCEPT_ENCODING), response.getOutputStream()); XMLStreamWriter writer = transformer.getOutputFactory().createXMLStreamWriter(encodingStream); writer.writeStartDocument(); writer.writeStartElement("error"); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); error.printStackTrace(pw); pw.close(); writer.writeCData(sw.toString()); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); encodingStream.close(); }
public static OMElement getPolicyComponentAsOMElement( PolicyComponent component) throws XMLStreamException, FactoryConfigurationError { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XMLOutputFactory.newInstance() .createXMLStreamWriter(baos); component.serialize(writer); writer.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); return (OMElement) XMLUtils.toOM(bais); }
XMLEventWriter eventWriter; XMLEventFactory eventFactory; XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream("testMerge1.xml")); eventFactory = XMLEventFactory.newInstance(); XMLEvent newLine = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); eventWriter.add(newLine); StartElement configStartElement = eventFactory.createStartElement("","","TestCaseBlock"); eventWriter.add(configStartElement); eventWriter.add(newLine); String[] filenames = new String[]{"test1.xml", "test2.xml","test3.xml"}; for(String filename:filenames){ XMLEventReader test = inputFactory.createXMLEventReader(filename, new FileInputStream(filename)); while(test.hasNext()){ XMLEvent event= test.nextEvent(); //avoiding start(<?xml version="1.0"?>) and end of the documents; if (event.getEventType()!= XMLEvent.START_DOCUMENT && event.getEventType() != XMLEvent.END_DOCUMENT) eventWriter.add(event); eventWriter.add(newLine); test.close(); } eventWriter.add(eventFactory.createEndElement("", "", "TestCaseBlock")); eventWriter.add(newLine); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close();
StringWriter sw = new StringWriter(); try { XMLStreamWriter xw = XMLOutputFactory.newInstance() .createXMLStreamWriter(sw); xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION); xw.writeStartElement(ROOT_ELEMENT_XML); for (Entry<String, String> entry : map.entrySet()) { if (StringUtil.isBlank(entry.getValue())) { continue; xw.writeStartElement(entry.getKey()); xw.writeCData(entry.getValue()); xw.writeEndElement(); } finally { try { sw.close(); } catch (IOException e) { return sw.getBuffer().toString();
@Test public void testRepairingNamespaces() throws Exception { XMLOutputFactory factory = XMLSystemFunctions.getOutputFactory(true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLEventWriter writer = factory.createXMLEventWriter(baos); XMLEventReader reader = XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a xmlns:x=\"http://foo\"><b x:y=\"1\"/></a>")); reader.nextTag(); reader = XMLType.getXmlInputFactory().createFilteredReader(reader, new EventFilter() { @Override public boolean accept(XMLEvent arg0) { if (arg0.isStartDocument() || arg0.isEndDocument()) { return false; } if (arg0.isEndElement() && ((EndElement)arg0).getName().getLocalPart().equals("a")) { return false; } return true; } }); writer.add(reader); writer.close(); assertEquals("<b xmlns=\"\" xmlns:x=\"http://foo\" x:y=\"1\"></b>", new String(baos.toByteArray(), "UTF-8")); }
java.io.StringWriter stringWriter = new java.io.StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(stringWriter); writer.writeEndElement(); writer.writeEndElement(); memory = null; writer.writeStartElement(reader.getLocalName()); writer.close(); return stringWriter.toString();
throws XMLStreamException, IOException { String lineSeparator = System.getProperty("line.separator"); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(outStream); EwsUtilities.writeTraceStartElement(writer, entryKind, false); writer.writeCharacters(lineSeparator); writer.writeCharacters(logEntry); writer.writeCharacters(lineSeparator); writer.writeEndElement(); writer.writeCharacters(lineSeparator); writer.flush(); writer.close(); outStream.flush(); String formattedLogMessage = outStream.toString(); formattedLogMessage = formattedLogMessage.replaceAll("'", "'"); formattedLogMessage = formattedLogMessage.replaceAll(""", "\"");