use of org.xml.sax.InputSource in project commons-gdx by gemserk.
the class DocumentParser method parse.
/**
* Returns an XML Document from an InputStream using a DocumentBuilder with the specified validation and namespaceaware flags.
*
* @param schema
* The Schema to be used to parse the Document, null if no Schema wanted.
* @param is
* The InputStream to be processed.
* @param validating
* If the document should be validated or not
* @param namespaceaware
* If the document should be processed with name space awareness or not.
*/
public Document parse(Schema schema, InputStream is, boolean validating, boolean namespaceaware) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if (schema != null)
factory.setSchema(schema);
factory.setValidating(validating);
factory.setNamespaceAware(namespaceaware);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new DefaultErrorHandler());
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
return builder.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.xml.sax.InputSource in project buck by facebook.
the class PositionXmlParser method parse.
/**
* Parses the XML content from the given byte array
*
* @param data the raw XML data (with unknown encoding)
* @param checkDtd whether or not download the DTD and validate it
* @return the corresponding document
* @throws ParserConfigurationException if a SAX parser is not available
* @throws SAXException if the document contains a parsing error
* @throws IOException if something is seriously wrong. This should not
* happen since the input source is known to be constructed from
* a string.
*/
@NonNull
public static Document parse(@NonNull byte[] data, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException {
String xml = getXmlString(data);
xml = XmlUtils.stripBom(xml);
return parse(xml, new InputSource(new StringReader(xml)), true, checkDtd);
}
use of org.xml.sax.InputSource in project buck by facebook.
the class PositionXmlParser method parse.
@NonNull
private static Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
if (checkDtd) {
factory.setFeature(NAMESPACE_FEATURE, true);
factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
factory.setFeature(PROVIDE_XMLNS_URIS, true);
} else {
factory.setFeature(LOAD_EXTERNAL_DTD, false);
}
SAXParser parser = factory.newSAXParser();
DomBuilder handler = new DomBuilder(xml);
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
parser.parse(input, handler);
return handler.getDocument();
} catch (SAXException e) {
if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
// Byte order mark in the string? Skip it. There are many markers
// (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
// just skip those up to the XML prolog beginning character, <
//$NON-NLS-1$ //$NON-NLS-2$
xml = xml.replaceFirst("^([\\W]+)<", "<");
return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
}
throw e;
}
}
use of org.xml.sax.InputSource in project buck by facebook.
the class XmlUtils method parseUtfXmlFile.
/**
* Parses the given UTF file as a DOM document, using the JDK parser. The parser does not
* validate, and is optionally namespace aware.
*
* @param file the UTF encoded file to parse
* @param namespaceAware whether the parser is namespace aware
* @return the DOM document
*/
@NonNull
public static Document parseUtfXmlFile(@NonNull File file, boolean namespaceAware) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Reader reader = getUtfReader(file);
try {
InputSource is = new InputSource(reader);
factory.setNamespaceAware(namespaceAware);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
} finally {
reader.close();
}
}
use of org.xml.sax.InputSource in project XobotOS by xamarin.
the class Xml method parse.
/**
* Parses the given xml string and fires events on the given SAX handler.
*/
public static void parse(String xml, ContentHandler contentHandler) throws SAXException {
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(new StringReader(xml)));
} catch (IOException e) {
throw new AssertionError(e);
}
}
Aggregations