Search in sources :

Example 46 with StAXSource

use of javax.xml.transform.stax.StAXSource in project hale by halestudio.

the class JsonXML method toXML.

public static void toXML(Reader jsonReader, Writer xmlWriter) throws XMLStreamException, FactoryConfigurationError, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
    /*
		 * If the <code>multiplePI</code> property is set to <code>true</code>,
		 * the StAXON reader will generate <code>&lt;xml-multiple&gt;</code>
		 * processing instructions which would be copied to the XML output.
		 * These can be used by StAXON when converting back to JSON to trigger
		 * array starts. Set to <code>false</code> if you don't need to go back
		 * to JSON.
		 */
    JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).build();
    /*
		 * Create source (JSON).
		 */
    XMLStreamReader reader = new JsonXMLInputFactory(config, new JsonStreamFactoryImpl()).createXMLStreamReader(jsonReader);
    Source source = new StAXSource(reader);
    /*
		 * Create result (XML).
		 */
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(xmlWriter);
    // format
    Result result = new StAXResult(new PrettyXMLStreamWriter(writer));
    // output
    /*
		 * Copy source to result via "identity transform".
		 */
    TransformerFactory.newInstance().newTransformer().transform(source, result);
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) JsonXMLConfigBuilder(de.odysseus.staxon.json.JsonXMLConfigBuilder) JsonXMLConfig(de.odysseus.staxon.json.JsonXMLConfig) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyXMLStreamWriter(de.odysseus.staxon.xml.util.PrettyXMLStreamWriter) JsonXMLInputFactory(de.odysseus.staxon.json.JsonXMLInputFactory) JsonStreamFactoryImpl(de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl) StAXSource(javax.xml.transform.stax.StAXSource) PrettyXMLStreamWriter(de.odysseus.staxon.xml.util.PrettyXMLStreamWriter) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult)

Example 47 with StAXSource

use of javax.xml.transform.stax.StAXSource in project sis by apache.

the class XML method unmarshal.

/**
 * Unmarshal an object from the given stream, DOM or other sources.
 * Together with the {@linkplain #unmarshal(Source, Class, Map) Unmarshal by Declared Type} variant,
 * this is the most flexible unmarshalling method provided in this {@code XML} class.
 * The source is specified by the {@code input} argument implementation, for example
 * {@link javax.xml.transform.stream.StreamSource} for reading from a file or input stream.
 * The optional {@code properties} map can contain any key documented in this {@code XML} class,
 * together with the keys documented in the <cite>supported properties</cite> section of the the
 * {@link Unmarshaller} class.
 *
 * @param  input       the file from which to read a XML representation.
 * @param  properties  an optional map of properties to give to the unmarshaller, or {@code null} if none.
 * @return the object unmarshalled from the given input.
 * @throws JAXBException if a property has an illegal value, or if an error occurred during the unmarshalling.
 *
 * @since 0.4
 */
public static Object unmarshal(final Source input, final Map<String, ?> properties) throws JAXBException {
    ensureNonNull("input", input);
    final MarshallerPool pool = getPool();
    final Unmarshaller unmarshaller = pool.acquireUnmarshaller(properties);
    final Object object;
    /*
         * STAX sources are not handled by javax.xml.bind.helpers.AbstractUnmarshallerImpl implementation as of JDK 8.
         * We have to handle those cases ourselves. This workaround should be removed if a future JDK version handles
         * those cases.
         */
    if (input instanceof StAXSource) {
        @Workaround(library = "JDK", version = "1.8") final XMLStreamReader reader = ((StAXSource) input).getXMLStreamReader();
        if (reader != null) {
            object = unmarshaller.unmarshal(reader);
        } else {
            object = unmarshaller.unmarshal(((StAXSource) input).getXMLEventReader());
        }
    } else {
        object = unmarshaller.unmarshal(input);
    }
    pool.recycle(unmarshaller);
    return object;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Unmarshaller(javax.xml.bind.Unmarshaller) Workaround(org.apache.sis.util.Workaround)

Example 48 with StAXSource

use of javax.xml.transform.stax.StAXSource in project mssql-jdbc by Microsoft.

the class SQLServerEntityResolver method getStAXSource.

private StAXSource getStAXSource() throws SQLException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader r = factory.createXMLStreamReader(contents);
        StAXSource result = new StAXSource(r);
        return result;
    } catch (XMLStreamException e) {
        MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_noParserSupport"));
        Object[] msgArgs = { e.toString() };
        SQLServerException.makeFromDriverError(con, null, form.format(msgArgs), null, true);
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) MessageFormat(java.text.MessageFormat) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 49 with StAXSource

use of javax.xml.transform.stax.StAXSource in project santuario-java by apache.

the class XMLSecurityStreamReaderTest method testIdentityTransformSource.

@Test
public void testIdentityTransformSource() throws Exception {
    XMLSecurityProperties securityProperties = new XMLSecurityProperties();
    InboundSecurityContextImpl securityContext = new InboundSecurityContextImpl();
    InputProcessorChainImpl inputProcessorChain = new InputProcessorChainImpl(securityContext);
    inputProcessorChain.addProcessor(new EventReaderProcessor());
    XMLSecurityStreamReader xmlSecurityStreamReader = new XMLSecurityStreamReader(inputProcessorChain, securityProperties);
    // use the sun internal TransformerFactory since the current xalan version don't know how to handle StaxSources:
    TransformerFactory transformerFactory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", this.getClass().getClassLoader());
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new StAXSource(xmlSecurityStreamReader), new StreamResult(baos));
    XMLAssert.assertXMLEqual(readTestFile(), baos.toString(StandardCharsets.UTF_8.name()));
}
Also used : InputProcessorChainImpl(org.apache.xml.security.stax.impl.InputProcessorChainImpl) TransformerFactory(javax.xml.transform.TransformerFactory) XMLSecurityStreamReader(org.apache.xml.security.stax.impl.XMLSecurityStreamReader) StreamResult(javax.xml.transform.stream.StreamResult) InboundSecurityContextImpl(org.apache.xml.security.stax.impl.InboundSecurityContextImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 50 with StAXSource

use of javax.xml.transform.stax.StAXSource in project teiid by teiid.

the class WSProcedureExecution method execute.

@SuppressWarnings("unchecked")
public void execute() throws TranslatorException {
    List<Argument> arguments = this.procedure.getArguments();
    String style = (String) arguments.get(0).getArgumentValue().getValue();
    String action = (String) arguments.get(1).getArgumentValue().getValue();
    XMLType docObject = (XMLType) arguments.get(2).getArgumentValue().getValue();
    Source source = null;
    try {
        Class type = StAXSource.class;
        if (executionFactory.getDefaultServiceMode() == Mode.MESSAGE) {
            type = DOMSource.class;
        }
        source = convertToSource(type, docObject);
        String endpoint = (String) arguments.get(3).getArgumentValue().getValue();
        if (style == null) {
            style = executionFactory.getDefaultBinding().getBindingId();
        } else {
            try {
                style = Binding.valueOf(style.toUpperCase()).getBindingId();
            } catch (IllegalArgumentException e) {
                // $NON-NLS-1$
                throw new TranslatorException(WSExecutionFactory.UTIL.getString("invalid_invocation", Arrays.toString(Binding.values())));
            }
        }
        Dispatch dispatch = conn.createDispatch(style, endpoint, type, executionFactory.getDefaultServiceMode());
        if (Binding.HTTP.getBindingId().equals(style)) {
            if (action == null) {
                // $NON-NLS-1$
                action = "POST";
            }
            dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, action);
            if (source != null && !"POST".equalsIgnoreCase(action)) {
                // $NON-NLS-1$
                if (this.executionFactory.getXMLParamName() == null) {
                    // $NON-NLS-1$
                    throw new WebServiceException(WSExecutionFactory.UTIL.getString("http_usage_error"));
                }
                try {
                    Transformer t = TransformerFactory.newInstance().newTransformer();
                    StringWriter writer = new StringWriter();
                    // TODO: prevent this from being too large
                    t.transform(source, new StreamResult(writer));
                    // $NON-NLS-1$
                    String param = Util.httpURLEncode(this.executionFactory.getXMLParamName()) + "=" + Util.httpURLEncode(writer.toString());
                    endpoint = WSConnection.Util.appendQueryString(endpoint, param);
                } catch (TransformerException e) {
                    throw new WebServiceException(e);
                }
            }
        } else {
            if (action != null) {
                dispatch.getRequestContext().put(Dispatch.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
                dispatch.getRequestContext().put(Dispatch.SOAPACTION_URI_PROPERTY, action);
            }
        }
        if (source == null) {
            // JBoss Native DispatchImpl throws exception when the source is null
            // $NON-NLS-1$
            source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<none/>")));
        }
        this.returnValue = (Source) dispatch.invoke(source);
    } catch (SQLException e) {
        throw new TranslatorException(e);
    } catch (WebServiceException e) {
        throw new TranslatorException(e);
    } catch (XMLStreamException e) {
        throw new TranslatorException(e);
    } finally {
        Util.closeSource(source);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Argument(org.teiid.language.Argument) WebServiceException(javax.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) SQLException(java.sql.SQLException) Dispatch(javax.xml.ws.Dispatch) StAXSource(javax.xml.transform.stax.StAXSource) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) XMLType(org.teiid.core.types.XMLType) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) TranslatorException(org.teiid.translator.TranslatorException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6