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 018 package org.apache.catalina.ant; 019 020 021 import java.io.BufferedInputStream; 022 import java.io.File; 023 import java.io.FileInputStream; 024 import java.io.InputStream; 025 026 import org.apache.catalina.startup.Constants; 027 import org.apache.catalina.startup.DigesterFactory; 028 import org.apache.tomcat.util.digester.Digester; 029 import org.apache.tools.ant.BuildException; 030 import org.apache.tools.ant.Task; 031 import org.xml.sax.InputSource; 032 033 034 /** 035 * Task for validating a web application deployment descriptor, using XML 036 * schema validation. 037 * 038 * @author Remy Maucherat 039 * @version $Revision: 1.8 $ $Date: 2004/06/26 17:41:33 $ 040 * @since 5.0 041 */ 042 043 public class ValidatorTask extends Task { 044 045 046 // ----------------------------------------------------- Instance Variables 047 048 049 // ------------------------------------------------------------- Properties 050 051 052 /** 053 * The path to the webapp directory. 054 */ 055 protected String path = null; 056 057 public String getPath() { 058 return (this.path); 059 } 060 061 public void setPath(String path) { 062 this.path = path; 063 } 064 065 066 // --------------------------------------------------------- Public Methods 067 068 069 /** 070 * Execute the specified command. This logic only performs the common 071 * attribute validation required by all subclasses; it does not perform 072 * any functional logic directly. 073 * 074 * @exception BuildException if a validation error occurs 075 */ 076 public void execute() throws BuildException { 077 078 if (path == null) { 079 throw new BuildException("Must specify 'path'"); 080 } 081 082 File file = new File(path, Constants.ApplicationWebXml); 083 if ((!file.exists()) || (!file.canRead())) { 084 throw new BuildException("Cannot find web.xml"); 085 } 086 087 // Commons-logging likes having the context classloader set Rate088 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 089 Thread.currentThread().setContextClassLoader 090 (ValidatorTask.class.getClassLoader()); 091 092 Digester digester = DigesterFactory.newDigester(true, true, null); 093 try { 094 file = file.getCanonicalFile(); 095 InputStream stream = 096 new BufferedInputStream(new FileInputStream(file)); 097 InputSource is = new InputSource(file.toURL().toExternalForm()); 098 is.setByteStream(stream); 099 digester.parse(is); 100 System.out.println("web.xml validated"); 101 } catch (Throwable t) { 102 throw new BuildException("Validation failure", t); 103 } finally { 104 Thread.currentThread().setContextClassLoader(oldCL); 105 } 106 107 } 108 109 110 }