use of javax.xml.transform.sax.SAXSource in project galley by Commonjava.
the class XMLInfrastructure method fallbackParseDocument.
private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
try {
Source source;
if (safeInputFactory != null) {
xml = repairXmlDeclaration(xml);
final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
source = new StAXSource(eventReader);
} else {
// Deal with ø and other undeclared entities...
xml = escapeNonXMLEntityRefs(xml);
final XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", false);
source = new SAXSource(reader, new InputSource(new StringReader(xml)));
}
final DOMResult result = new DOMResult();
final Transformer transformer = newTransformer();
transformer.transform(source, result);
return (Document) result.getNode();
} catch (final TransformerException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final SAXException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final XMLStreamException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
}
}
use of javax.xml.transform.sax.SAXSource in project tomee by apache.
the class JaxbOpenejb method unmarshal.
public static <T> T unmarshal(final Class<T> type, final InputStream in, final boolean filter) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
final JAXBContext ctx = getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
System.out.println(validationEvent);
return false;
}
});
final SAXSource source;
if (filter) {
final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
source = new SAXSource(xmlFilter, inputSource);
} else {
source = new SAXSource(inputSource);
}
currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source, type).getValue();
} finally {
currentPublicId.set(null);
}
}
use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class TestBase64StreamingWithGetSAXSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMElement elem = factory.createOMElement("test", null);
// Create a data source that would eat up all memory when loaded. If the test
// doesn't fail with an OutOfMemoryError, we know that the OMText implementation
// supports streaming.
DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
OMText text = factory.createOMText(new DataHandler(ds), false);
elem.addChild(text);
SAXSource saxSource = elem.getSAXSource(true);
XMLReader xmlReader = saxSource.getXMLReader();
xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
xmlReader.parse(saxSource.getInputSource());
}
use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class SAXBuilderFactory method getBuilder.
@Override
public OMXMLParserWrapper getBuilder(OMMetaFactory metaFactory, InputSource inputSource) throws Exception {
SAXParserFactory factory = implementation.newSAXParserFactory();
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
SAXParser parser = factory.newSAXParser();
SAXSource source = new SAXSource(new CoalescingXMLFilter(parser.getXMLReader()), inputSource);
return OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source, false);
}
use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class SerializeFromSAXSource method serialize.
@Override
public XML serialize(OMContainer container) throws Exception {
SAXSource source = container.getSAXSource(cache);
XMLReader xmlReader = source.getXMLReader();
ByteArrayOutputStream out = new ByteArrayOutputStream();
SAXSerializer serializer = new SAXSerializer();
// A SAXSource has no way to tell its consumer about the encoding of the document.
// Just set it to UTF-8 to have a well defined encoding.
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputStream(out);
xmlReader.setContentHandler(serializer);
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", serializer);
xmlReader.parse(source.getInputSource());
return new XMLAsByteArray(out.toByteArray());
}
Aggregations