1 23 24 package com.sun.enterprise.tools.verifier.web; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.Iterator ; 36 import java.util.logging.Level ; 37 import java.util.logging.LogRecord ; 38 import java.util.logging.Logger ; 39 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.NodeList ; 42 import org.xml.sax.EntityResolver ; 43 import org.xml.sax.ErrorHandler ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 import org.xml.sax.SAXParseException ; 47 import tools.com.sun.enterprise.util.XMLValidationHandler; 48 49 import com.sun.enterprise.deployment.TagLibConfigurationDescriptor; 50 import com.sun.enterprise.deployment.WebBundleDescriptor; 51 import com.sun.enterprise.deployment.util.ModuleDescriptor; 52 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 53 import com.sun.enterprise.logging.LogDomains; 54 import com.sun.enterprise.tools.verifier.Context; 55 import com.sun.enterprise.tools.verifier.FrameworkContext; 56 import com.sun.enterprise.tools.verifier.StringManagerHelper; 57 import com.sun.enterprise.tools.verifier.TagLibDescriptor; 58 import com.sun.enterprise.util.LocalStringManagerImpl; 59 import com.sun.enterprise.util.io.FileUtils; 60 61 66 67 public class TagLibFactory { 68 69 private DocumentBuilder builder; 70 private Context context; 71 private FrameworkContext frameworkContext; 72 private Logger logger = LogDomains.getLogger( 73 LogDomains.AVK_VERIFIER_LOGGER); 74 private boolean uninitialised = false; 75 private final LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager(); 76 77 78 84 public TagLibFactory(Context context, FrameworkContext frameworkContext) { 85 this.context = context; 86 this.frameworkContext = frameworkContext; 87 } 88 89 98 public TagLibDescriptor[] getTagLibDescriptors( 99 WebBundleDescriptor descriptor) { 100 ArrayList <TagLibDescriptor> tmp = new ArrayList <TagLibDescriptor>(); 101 Enumeration taglibConfig = null; 102 103 if (descriptor.getJspConfigDescriptor() != null) { 104 taglibConfig = descriptor.getJspConfigDescriptor().getTagLibs(); 105 } else { 106 return null; 107 } 108 init(); 109 while (taglibConfig.hasMoreElements()) { 110 TagLibConfigurationDescriptor taglibDescriptor = (TagLibConfigurationDescriptor) taglibConfig.nextElement(); 112 113 String taglibLocation = taglibDescriptor.getTagLibLocation(); 114 Document d = null; 115 try { 116 d = createDocument(taglibLocation, descriptor); 117 } catch (Exception e) { 118 logger.log(Level.WARNING, smh.getLocalString 119 (getClass().getName() + ".exception", "Continuing, though problem in creating taglib document. Cause: {0}", new Object []{e.getMessage()})); 122 if (e instanceof SAXParseException ) { 123 LogRecord logRecord = new LogRecord (Level.SEVERE, 124 smh.getLocalString 125 (getClass().getName() + ".exception2", "XML Error line : {0} in [ {1} ]. {2}", new Object [] {((SAXParseException ) e).getLineNumber(), 128 taglibLocation, 129 e.getLocalizedMessage()})); 130 logRecord.setThrown(e); 131 frameworkContext.getResultManager().log(logRecord); 132 } 133 continue; } 135 String version = getTLDSpecVersion(d); 136 TagLibDescriptor taglib = null; 137 taglib = new TagLibDescriptor(d, version, taglibLocation); 138 tmp.add(taglib); 139 } 140 141 int count = tmp.size(); 142 TagLibDescriptor arr[] = new TagLibDescriptor[count]; 143 int i = 0; 144 for (Iterator e = tmp.iterator(); e.hasNext(); i++) { 145 arr[i] = (TagLibDescriptor) e.next(); 146 } 147 return arr; 148 } 149 150 154 155 private void init() { 156 if (!uninitialised) { 157 synchronized (this) { 158 if (!uninitialised) { 159 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 160 String W3C_XML_SCHEMA = "http://www.w3c.org/2001/XMLSchema"; String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; factory.setNamespaceAware(true); 163 factory.setValidating(true); 164 factory.setAttribute( 165 "http://apache.org/xml/features/validation/schema", Boolean.TRUE); 167 factory.setAttribute( 168 "http://xml.org/sax/features/validation", Boolean.TRUE); 170 factory.setAttribute( 171 "http://apache.org/xml/features/allow-java-encodings", Boolean.TRUE); 173 factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 174 try { 175 builder = factory.newDocumentBuilder(); 176 } catch (ParserConfigurationException e) { 177 logger.log(Level.SEVERE, e.getMessage()); 178 } 179 EntityResolver dh = new XMLValidationHandler(true); 180 ErrorHandler eh = new XMLValidationHandler(true); 181 builder.setErrorHandler(eh); 182 builder.setEntityResolver(dh); 183 uninitialised = true; 184 } 185 } 186 } 187 } 188 189 199 private Document createDocument(String location, WebBundleDescriptor webd) 200 throws IOException , SAXException { 201 Document document = null; 202 InputSource source = null; 203 if (location.startsWith("/")) location = location.substring(1); 205 else 206 location = "WEB-INF/" + location; ModuleDescriptor moduleDesc = webd.getModuleDescriptor(); 208 String archBase = context.getAbstractArchive().getArchiveUri(); 209 String uri = null; 210 if(moduleDesc.isStandalone()){ 211 uri = archBase; 212 } else { 213 uri = archBase + File.separator + 214 FileUtils.makeFriendlyFileName(moduleDesc.getArchiveUri()); 215 } 216 FileArchive arch = new FileArchive(); 217 arch.open(uri); 218 InputStream is = arch.getEntry(location); 219 try { 220 if (is == null) 222 throw new IOException (smh.getLocalString 223 (getClass().getName() + ".exception1", "Wrong tld [ {0} ] specified in the web.xml of [ {1} ]", new Object []{location, moduleDesc.getArchiveUri()})); 226 source = new InputSource (is); 227 document = builder.parse(source); 228 } finally { 229 try{ 230 if(is != null) 231 is.close(); 232 } catch(Exception e) {} 233 } 234 235 return document; 236 } 237 238 244 private String getTLDSpecVersion(Document doc) { 245 String str = null; 246 if (doc.getDoctype() != null) { 247 NodeList nl = doc.getElementsByTagName("jsp-version"); if (nl != null && nl.getLength() != 0) { 249 str = nl.item(0).getFirstChild().getNodeValue(); } else { 251 nl = doc.getElementsByTagName("jspversion"); if (nl.getLength() != 0) { 253 str = nl.item(0).getFirstChild().getNodeValue(); } else 255 return "1.1"; } 257 } else 258 str = doc.getElementsByTagName("taglib").item(0).getAttributes() .getNamedItem("version") .getNodeValue(); 261 262 return str; 263 } 264 } 265 | Popular Tags |