use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.
the class WebXmlParser method parseWebXml.
public boolean parseWebXml(InputSource source, WebXml dest, boolean fragment) {
boolean ok = true;
if (source == null) {
return ok;
}
XmlErrorHandler handler = new XmlErrorHandler();
Digester digester;
WebRuleSet ruleSet;
if (fragment) {
digester = webFragmentDigester;
ruleSet = webFragmentRuleSet;
} else {
digester = webDigester;
ruleSet = webRuleSet;
}
digester.push(dest);
digester.setErrorHandler(handler);
if (log.isDebugEnabled()) {
log.debug(sm.getString("webXmlParser.applicationStart", source.getSystemId()));
}
try {
digester.parse(source);
if (handler.getWarnings().size() > 0 || handler.getErrors().size() > 0) {
ok = false;
handler.logFindings(log, source.getSystemId());
}
} catch (SAXParseException e) {
log.error(sm.getString("webXmlParser.applicationParse", source.getSystemId()), e);
log.error(sm.getString("webXmlParser.applicationPosition", "" + e.getLineNumber(), "" + e.getColumnNumber()));
ok = false;
} catch (Exception e) {
log.error(sm.getString("webXmlParser.applicationParse", source.getSystemId()), e);
ok = false;
} finally {
InputSourceUtil.close(source);
digester.reset();
ruleSet.recycle();
}
return ok;
}
use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.
the class ContextConfig method processContextConfig.
/**
* Process a context.xml.
* @param digester The digester that will be used for XML parsing
* @param contextXml The URL to the context.xml configuration
*/
protected void processContextConfig(Digester digester, URL contextXml) {
if (log.isDebugEnabled()) {
log.debug("Processing context [" + context.getName() + "] configuration file [" + contextXml + "]");
}
InputSource source = null;
InputStream stream = null;
try {
source = new InputSource(contextXml.toString());
URLConnection xmlConn = contextXml.openConnection();
xmlConn.setUseCaches(false);
stream = xmlConn.getInputStream();
} catch (Exception e) {
log.error(sm.getString("contextConfig.contextMissing", contextXml), e);
}
if (source == null) {
return;
}
try {
source.setByteStream(stream);
digester.setClassLoader(this.getClass().getClassLoader());
digester.setUseContextClassLoader(false);
digester.push(context.getParent());
digester.push(context);
XmlErrorHandler errorHandler = new XmlErrorHandler();
digester.setErrorHandler(errorHandler);
digester.parse(source);
if (errorHandler.getWarnings().size() > 0 || errorHandler.getErrors().size() > 0) {
errorHandler.logFindings(log, contextXml.toString());
ok = false;
}
if (log.isDebugEnabled()) {
log.debug("Successfully processed context [" + context.getName() + "] configuration file [" + contextXml + "]");
}
} catch (SAXParseException e) {
log.error(sm.getString("contextConfig.contextParse", context.getName()), e);
log.error(sm.getString("contextConfig.defaultPosition", "" + e.getLineNumber(), "" + e.getColumnNumber()));
ok = false;
} catch (Exception e) {
log.error(sm.getString("contextConfig.contextParse", context.getName()), e);
ok = false;
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
log.error(sm.getString("contextConfig.contextClose"), e);
}
}
}
use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.
the class TagPluginParser method parse.
public void parse(URL url) throws IOException, SAXException {
try (InputStream is = url.openStream()) {
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
digester.push(this);
InputSource source = new InputSource(url.toExternalForm());
source.setByteStream(is);
digester.parse(source);
if (!handler.getWarnings().isEmpty() || !handler.getErrors().isEmpty()) {
handler.logFindings(log, source.getSystemId());
if (!handler.getErrors().isEmpty()) {
// throw the first to indicate there was a error during processing
throw handler.getErrors().iterator().next();
}
}
} finally {
digester.reset();
}
}
use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.
the class TestSchemaValidation method testWebapp_3_1.
@Test
public void testWebapp_3_1() throws Exception {
XmlErrorHandler handler = new XmlErrorHandler();
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
digester.setErrorHandler(handler);
digester.push(new WebXml());
WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.1/WEB-INF/web.xml"));
Assert.assertEquals("3.1", desc.getVersion());
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
}
use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.
the class TestSchemaValidation method testWebapp_4_0.
@Test
public void testWebapp_4_0() throws Exception {
XmlErrorHandler handler = new XmlErrorHandler();
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
digester.setErrorHandler(handler);
digester.push(new WebXml());
WebXml desc = (WebXml) digester.parse(new File("test/webapp-4.0/WEB-INF/web.xml"));
Assert.assertEquals("4.0", desc.getVersion());
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
}
Aggregations