use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.
the class ValidatorTask method execute.
// --------------------------------------------------------- Public Methods
/**
* Execute the specified command. This logic only performs the common
* attribute validation required by all subclasses; it does not perform
* any functional logic directly.
*
* @exception BuildException if a validation error occurs
*/
@Override
public void execute() throws BuildException {
if (path == null) {
throw new BuildException("Must specify 'path'");
}
File file = new File(path, Constants.ApplicationWebXml);
if (!file.canRead()) {
throw new BuildException("Cannot find web.xml");
}
// Commons-logging likes having the context classloader set
ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(ValidatorTask.class.getClassLoader());
// Called through trusted manager interface. If running under a
// SecurityManager assume that untrusted applications may be deployed.
Digester digester = DigesterFactory.newDigester(true, true, null, Globals.IS_SECURITY_ENABLED);
try (InputStream stream = new BufferedInputStream(new FileInputStream(file.getCanonicalFile()))) {
InputSource is = new InputSource(file.toURI().toURL().toExternalForm());
is.setByteStream(stream);
digester.parse(is);
handleOutput("web.xml validated");
} catch (Exception e) {
if (isFailOnError()) {
throw new BuildException("Validation failure", e);
} else {
handleErrorOutput("Validation failure: " + e);
}
} finally {
Thread.currentThread().setContextClassLoader(oldCL);
closeRedirector();
}
}
use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.
the class MemoryRealm method getDigester.
// ------------------------------------------------------ Protected Methods
/**
* @return a configured <code>Digester</code> to use for processing
* the XML input file, creating a new one if necessary.
*/
protected synchronized Digester getDigester() {
if (digester == null) {
digester = new Digester();
digester.setValidating(false);
try {
digester.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
} catch (Exception e) {
log.warn(sm.getString("memoryRealm.xmlFeatureEncoding"), e);
}
digester.addRuleSet(new MemoryRuleSet());
}
return (digester);
}
use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.
the class MemoryRealm method startInternal.
// ------------------------------------------------------ Lifecycle Methods
/**
* Prepare for the beginning of active use of the public methods of this
* component and implement the requirements of
* {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
@Override
protected void startInternal() throws LifecycleException {
String pathName = getPathname();
try (InputStream is = ConfigFileLoader.getInputStream(pathName)) {
// Load the contents of the database file
if (log.isDebugEnabled()) {
log.debug(sm.getString("memoryRealm.loadPath", pathName));
}
Digester digester = getDigester();
try {
synchronized (digester) {
digester.push(this);
digester.parse(is);
}
} catch (Exception e) {
throw new LifecycleException(sm.getString("memoryRealm.readXml"), e);
} finally {
digester.reset();
}
} catch (IOException ioe) {
throw new LifecycleException(sm.getString("memoryRealm.loadExist", pathName), ioe);
}
super.startInternal();
}
use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.
the class TestWebRuleSet method parse.
private synchronized void parse(WebXml webXml, String target, boolean fragment, boolean expected) {
Digester d;
if (fragment) {
d = fragmentDigester;
fragmentRuleSet.recycle();
} else {
d = webDigester;
webRuleSet.recycle();
}
d.push(webXml);
File f = new File("test/org/apache/catalina/startup/" + target);
boolean result = true;
try (InputStream is = new FileInputStream(f)) {
d.parse(is);
} catch (Exception e) {
if (expected) {
// Didn't expect an exception
e.printStackTrace();
}
result = false;
}
if (expected) {
assertTrue(result);
} else {
assertFalse(result);
}
}
use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.
the class TestWebXml method doTestValidateVersion.
private void doTestValidateVersion(String version) throws IOException, SAXException {
WebXml webxml = new WebXml();
// Special cases
if ("2.2".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
} else if ("2.3".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
} else {
webxml.setVersion(version);
}
// Merged web.xml that is published as MERGED_WEB_XML context attribute
// in the simplest case consists of webapp's web.xml file
// plus the default conf/web.xml one.
Set<WebXml> defaults = new HashSet<>();
defaults.add(getDefaultWebXmlFragment());
webxml.merge(defaults);
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
InputSource is = new InputSource(new StringReader(webxml.toXml()));
WebXml webxmlResult = new WebXml();
digester.push(webxmlResult);
digester.parse(is);
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
Assert.assertEquals(version, webxml.getVersion());
Assert.assertEquals(version, webxmlResult.getVersion());
}
Aggregations