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><xml-multiple></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);
}
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;
}
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;
}
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()));
}
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);
}
}
Aggregations