001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.ant;
018:
019: import java.io.BufferedInputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.InputStream;
023:
024: import org.apache.catalina.startup.Constants;
025: import org.apache.catalina.startup.DigesterFactory;
026: import org.apache.commons.digester.Digester;
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.Task;
029: import org.xml.sax.InputSource;
030:
031: /**
032: * Task for validating a web application deployment descriptor, using XML
033: * schema validation.
034: *
035: * @author Remy Maucherat
036: * @version $Revision: 1.7 $ $Date: 2004/02/27 14:58:41 $
037: * @since 5.0
038: */
039:
040: public class ValidatorTask extends Task {
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: // ------------------------------------------------------------- Properties
045:
046: /**
047: * The path to the webapp directory.
048: */
049: protected String path = null;
050:
051: public String getPath() {
052: return (this .path);
053: }
054:
055: public void setPath(String path) {
056: this .path = path;
057: }
058:
059: // --------------------------------------------------------- Public Methods
060:
061: /**
062: * Execute the specified command. This logic only performs the common
063: * attribute validation required by all subclasses; it does not perform
064: * any functional logic directly.
065: *
066: * @exception BuildException if a validation error occurs
067: */
068: public void execute() throws BuildException {
069:
070: if (path == null) {
071: throw new BuildException("Must specify 'path'");
072: }
073:
074: File file = new File(path, Constants.ApplicationWebXml);
075: if ((!file.exists()) || (!file.canRead())) {
076: throw new BuildException("Cannot find web.xml");
077: }
078:
079: // Commons-logging likes having the context classloader set
080: ClassLoader oldCL = Thread.currentThread()
081: .getContextClassLoader();
082: Thread.currentThread().setContextClassLoader(
083: ValidatorTask.class.getClassLoader());
084:
085: Digester digester = DigesterFactory.newDigester(true, true,
086: null);
087: try {
088: file = file.getCanonicalFile();
089: InputStream stream = new BufferedInputStream(
090: new FileInputStream(file));
091: InputSource is = new InputSource(file.toURL()
092: .toExternalForm());
093: is.setByteStream(stream);
094: digester.parse(is);
095: System.out.println("web.xml validated");
096: } catch (Throwable t) {
097: throw new BuildException("Validation failure", t);
098: } finally {
099: Thread.currentThread().setContextClassLoader(oldCL);
100: }
101:
102: }
103:
104: }
|