use of javax.xml.transform.sax.SAXSource in project tomee by apache.
the class JaxbWls method unmarshal.
public static <T> Object unmarshal(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
final JAXBContext ctx = JaxbWls.getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
System.out.println(validationEvent);
return false;
}
});
final JaxbWls.NamespaceFilter xmlFilter = new JaxbWls.NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
JaxbWls.currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source, type);
} finally {
JaxbWls.currentPublicId.set(null);
}
}
use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class TestGetSAXSourceWithPushOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement sourcedElement = factory.createOMElement(new AbstractPushOMDataSource() {
@Override
public void serialize(XMLStreamWriter writer) throws XMLStreamException {
scenario.serialize(writer);
}
@Override
public boolean isDestructiveWrite() {
return false;
}
});
Iterator<Map.Entry<String, String>> it = scenario.getNamespaceContext().entrySet().iterator();
OMElement parent;
if (it.hasNext()) {
Map.Entry<String, String> binding = it.next();
parent = factory.createOMElement("parent", factory.createOMNamespace(binding.getValue(), binding.getKey()));
while (it.hasNext()) {
binding = it.next();
parent.declareNamespace(factory.createOMNamespace(binding.getValue(), binding.getKey()));
}
} else {
parent = factory.createOMElement("parent", null);
}
parent.addChild(sourcedElement);
SAXSource saxSource = (serializeParent ? parent : sourcedElement).getSAXSource(true);
OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, saxSource, false).getDocumentElement();
if (serializeParent) {
element = element.getFirstElement();
}
scenario.validate(element, false);
}
use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class BuilderSpec method from.
static BuilderSpec from(StAXParserConfiguration configuration, Source source) {
if (source instanceof SAXSource) {
return from((SAXSource) source, true);
} else if (source instanceof DOMSource) {
return from(((DOMSource) source).getNode(), true);
} else if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
InputSource is = new InputSource();
is.setByteStream(streamSource.getInputStream());
is.setCharacterStream(streamSource.getReader());
is.setPublicId(streamSource.getPublicId());
is.setSystemId(streamSource.getSystemId());
return from(configuration, is);
} else if (source instanceof StAXSource) {
return from(((StAXSource) source).getXMLStreamReader());
} else {
try {
return new BuilderSpec(new FilteredXmlInput(new StAXPullInput(StAXUtils.getXMLInputFactory().createXMLStreamReader(source), true, null), NamespaceRepairingFilter.DEFAULT), null);
} catch (XMLStreamException ex) {
throw new OMException(ex);
}
}
}
use of javax.xml.transform.sax.SAXSource in project ddf by codice.
the class XacmlClient method addNamespaceAndPrefixes.
/**
* Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
* XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
* response.
*
* @param xacmlResponse The XACML response as a string.
* @return DOM representation of the XACML response with namespaces and namespace prefixes.
* @throws PdpException
*/
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
XMLReader xmlReader = null;
try {
XMLReader xmlParser = XMLReaderFactory.createXMLReader();
xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
xmlReader = new XMLFilterImpl(xmlParser) {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
}
};
} catch (SAXException e) {
String message = "Unable to read XACML response:\n" + xacmlResponse;
LOGGER.info(message);
throw new PdpException(message, e);
}
DOMResult domResult;
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
domResult = new DOMResult();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))), domResult);
} catch (TransformerException e) {
String message = "Unable to transform XACML response:\n" + xacmlResponse;
LOGGER.info(message);
throw new PdpException(message, e);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
return domResult;
}
use of javax.xml.transform.sax.SAXSource in project camel by apache.
the class TidyMarkupDataFormat method asNodeTidyMarkup.
/**
* Return the HTML Markup as an {@link org.w3c.dom.Node}
*
* @param inputStream
* The input Stream to convert
* @return org.w3c.dom.Node The HTML Markup as a DOM Node
* @throws CamelException
*/
public Node asNodeTidyMarkup(InputStream inputStream) throws CamelException {
XMLReader parser = createTagSoupParser();
StringWriter w = new StringWriter();
parser.setContentHandler(createContentHandler(w));
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
transformer.transform(new SAXSource(parser, new InputSource(inputStream)), result);
return result.getNode();
} catch (Exception e) {
throw new CamelException("Failed to convert the HTML to tidy Markup", e);
}
}
Aggregations