1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19 package org.apache.catalina.ant;
20
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26
27 import org.apache.catalina.startup.Constants;
28 import org.apache.catalina.startup.DigesterFactory;
29 import org.apache.tomcat.util.digester.Digester;
30 import org.apache.tools.ant.BuildException;
31 import org.xml.sax.InputSource;
32
33
34 /**
35 * Task for validating a web application deployment descriptor, using XML
36 * schema validation.
37 *
38 * @author Remy Maucherat
39 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
40 * @since 5.0
41 */
42
43 public class ValidatorTask extends BaseRedirectorHelperTask {
44
45
46 // ----------------------------------------------------- Instance Variables
47
48
49 // ------------------------------------------------------------- Properties
50
51
52 /**
53 * The path to the webapp directory.
54 */
55 protected String path = null;
56
57 public String getPath() {
58 return (this.path);
59 }
60
61 public void setPath(String path) {
62 this.path = path;
63 }
64
65
66 // --------------------------------------------------------- Public Methods
67
68
69 /**
70 * Execute the specified command. This logic only performs the common
71 * attribute validation required by all subclasses; it does not perform
72 * any functional logic directly.
73 *
74 * @exception BuildException if a validation error occurs
75 */
76 public void execute() throws BuildException {
77
78 if (path == null) {
79 throw new BuildException("Must specify 'path'");
80 }
81
82 File file = new File(path, Constants.ApplicationWebXml);
83 if ((!file.exists()) || (!file.canRead())) {
84 throw new BuildException("Cannot find web.xml");
85 }
86
87 // Commons-logging likes having the context classloader set
88 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
89 Thread.currentThread().setContextClassLoader
90 (ValidatorTask.class.getClassLoader());
91
92 Digester digester = DigesterFactory.newDigester(true, true, null);
93 try {
94 file = file.getCanonicalFile();
95 InputStream stream =
96 new BufferedInputStream(new FileInputStream(file));
97 InputSource is = new InputSource(file.toURL().toExternalForm());
98 is.setByteStream(stream);
99 digester.parse(is);
100 handleOutput("web.xml validated");
101 } catch (Throwable t) {
102 if (isFailOnError()) {
103 throw new BuildException("Validation failure", t);
104 } else {
105 handleErrorOutput("Validation failure: " + t);
106 }
107 } finally {
108 Thread.currentThread().setContextClassLoader(oldCL);
109 closeRedirector();
110 }
111
112 }
113
114
115 }