use of org.xml.sax.XMLReader in project tomcat by apache.
the class JspDocumentParser method getSAXParser.
/*
* Gets SAXParser.
*
* @param validating Indicates whether the requested SAXParser should
* be validating
* @param jspDocParser The JSP document parser
*
* @return The SAXParser
*/
private static SAXParser getSAXParser(boolean validating, JspDocumentParser jspDocParser) throws Exception {
ClassLoader original;
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedGetTccl pa = new PrivilegedGetTccl();
original = AccessController.doPrivileged(pa);
} else {
original = Thread.currentThread().getContextClassLoader();
}
try {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedSetTccl pa = new PrivilegedSetTccl(JspDocumentParser.class.getClassLoader());
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(JspDocumentParser.class.getClassLoader());
}
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
// Preserve xmlns attributes
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
factory.setValidating(validating);
if (validating) {
// Enable DTD validation
factory.setFeature("http://xml.org/sax/features/validation", true);
// Enable schema validation
factory.setFeature("http://apache.org/xml/features/validation/schema", true);
}
// Configure the parser
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setProperty(LEXICAL_HANDLER_PROPERTY, jspDocParser);
xmlReader.setErrorHandler(jspDocParser);
return saxParser;
} finally {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedSetTccl pa = new PrivilegedSetTccl(original);
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(original);
}
}
}
use of org.xml.sax.XMLReader in project asciidoctor-fopub by asciidoctor.
the class InputHandler method createMainSource.
/**
* Creates a Source for the main input file. Processes XInclude if
* available in the XML parser.
*
* @return the Source for the main input file
*/
protected Source createMainSource() {
Source source;
InputStream in;
String uri;
if (this.sourcefile != null) {
try {
in = new java.io.FileInputStream(this.sourcefile);
uri = this.sourcefile.toURI().toASCIIString();
} catch (FileNotFoundException e) {
//handled elsewhere
return new StreamSource(this.sourcefile);
}
} else {
in = System.in;
uri = null;
}
try {
InputSource is = new InputSource(in);
is.setSystemId(uri);
XMLReader xr = getXMLReader();
if (entityResolver != null) {
xr.setEntityResolver(entityResolver);
}
source = new SAXSource(xr, is);
} catch (SAXException e) {
if (this.sourcefile != null) {
source = new StreamSource(this.sourcefile);
} else {
source = new StreamSource(in, uri);
}
} catch (ParserConfigurationException e) {
if (this.sourcefile != null) {
source = new StreamSource(this.sourcefile);
} else {
source = new StreamSource(in, uri);
}
}
return source;
}
use of org.xml.sax.XMLReader in project asciidoctor-fopub by asciidoctor.
the class InputHandler method createXSLTSource.
/**
* Creates a Source for the selected stylesheet.
*
* @return the Source for the selected stylesheet or null if there's no stylesheet
*/
protected Source createXSLTSource() {
Source xslt = null;
if (this.stylesheet != null) {
if (entityResolver != null) {
try {
InputSource is = new InputSource(this.stylesheet.getPath());
XMLReader xr = getXMLReader();
xr.setEntityResolver(entityResolver);
xslt = new SAXSource(xr, is);
} catch (SAXException e) {
// return StreamSource
} catch (ParserConfigurationException e) {
// return StreamSource
}
}
if (xslt == null) {
xslt = new StreamSource(this.stylesheet);
}
}
return xslt;
}
use of org.xml.sax.XMLReader in project blade by biezhi.
the class XmlParser method parse.
/* ------------------------------------------------------------ */
public synchronized Node parse(InputSource source) throws IOException, SAXException {
_dtd = null;
Handler handler = new Handler();
XMLReader reader = _parser.getXMLReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.setEntityResolver(handler);
if (LOG.isDebugEnabled())
LOG.debug("parsing: sid=" + source.getSystemId() + ",pid=" + source.getPublicId());
_parser.parse(source, handler);
if (handler._error != null)
throw handler._error;
Node doc = (Node) handler._top.get(0);
handler.clear();
return doc;
}
use of org.xml.sax.XMLReader in project blade by biezhi.
the class XmlParser method parse.
/* ------------------------------------------------------------ */
/**
* Parse InputStream.
* @param in the input stream of the xml to parse
* @return the root node of the xml
* @throws IOException if unable to load the xml
* @throws SAXException if unable to parse the xml
*/
public synchronized Node parse(InputStream in) throws IOException, SAXException {
_dtd = null;
Handler handler = new Handler();
XMLReader reader = _parser.getXMLReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.setEntityResolver(handler);
_parser.parse(new InputSource(in), handler);
if (handler._error != null)
throw handler._error;
Node doc = (Node) handler._top.get(0);
handler.clear();
return doc;
}
Aggregations