use of org.xml.sax.XMLReader 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 org.xml.sax.XMLReader in project tika by apache.
the class ParseContext method getXMLReader.
/**
* Returns the XMLReader specified in this parsing context. If a reader
* is not explicitly specified, then one is created using the specified
* or the default SAX parser.
*
* @see #getSAXParser()
* @since Apache Tika 1.13
* @return XMLReader
* @throws TikaException
*/
public XMLReader getXMLReader() throws TikaException {
XMLReader reader = get(XMLReader.class);
if (reader != null) {
return reader;
}
try {
reader = getSAXParser().getXMLReader();
} catch (SAXException e) {
throw new TikaException("Unable to create an XMLReader", e);
}
reader.setEntityResolver(IGNORING_SAX_ENTITY_RESOLVER);
return reader;
}
use of org.xml.sax.XMLReader 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());
}
use of org.xml.sax.XMLReader 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 org.xml.sax.XMLReader in project webservices-axiom by apache.
the class SAXReader method proceed.
@Override
public boolean proceed() throws StreamException {
XMLReader reader = source.getXMLReader();
XmlHandlerContentHandler contentHandler = new XmlHandlerContentHandler(handler, expandEntityReferences);
reader.setContentHandler(contentHandler);
reader.setDTDHandler(contentHandler);
try {
reader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
} catch (SAXException ex) {
// Ignore
}
try {
reader.setProperty("http://xml.org/sax/properties/declaration-handler", contentHandler);
} catch (SAXException ex) {
// Ignore
}
try {
reader.parse(source.getInputSource());
} catch (IOException ex) {
throw new StreamException(ex);
} catch (SAXException ex) {
throw new StreamException(ex);
}
return true;
}
Aggregations