1 23 package com.sun.enterprise.deployment.node; 24 25 26 import com.sun.enterprise.deployment.xml.ApplicationTagNames; 27 import org.xml.sax.SAXParseException ; 28 import org.xml.sax.SAXNotRecognizedException ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.helpers.DefaultHandler ; 31 import org.xml.sax.helpers.NamespaceSupport ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.Locator ; 36 37 38 import java.io.*; 39 import java.util.*; 40 import java.util.logging.Level ; 41 42 import javax.xml.parsers.SAXParserFactory ; 43 import javax.xml.parsers.SAXParser ; 44 45 import com.sun.enterprise.deployment.node.ejb.EjbBundleNode; 46 import com.sun.enterprise.deployment.node.web.WebBundleNode; 47 import com.sun.enterprise.deployment.node.connector.ConnectorNode; 48 import com.sun.enterprise.deployment.node.appclient.AppClientNode; 49 import com.sun.enterprise.deployment.node.runtime.application.ApplicationRuntimeNode; 50 import com.sun.enterprise.deployment.node.runtime.EjbBundleRuntimeNode; 51 import com.sun.enterprise.deployment.node.runtime.web.WebBundleRuntimeNode; 52 import com.sun.enterprise.deployment.node.runtime.AppClientRuntimeNode; 53 import com.sun.enterprise.deployment.xml.DTDRegistry; 54 import com.sun.enterprise.deployment.xml.TagNames; 55 import com.sun.enterprise.deployment.xml.WebTagNames; 56 import com.sun.enterprise.deployment.util.DOLUtils; 57 import com.sun.enterprise.deployment.EnvironmentProperty; 58 import com.sun.enterprise.util.LocalStringManagerImpl; 59 60 61 67 public class SaxParserHandler extends DefaultHandler { 68 69 70 public static final String JAXP_SCHEMA_LANGUAGE = 71 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 72 public static final String JAXP_SCHEMA_SOURCE = 73 "http://java.sun.com/xml/jaxp/properties/schemaSource"; 74 public static final String W3C_XML_SCHEMA = 75 "http://www.w3.org/2001/XMLSchema"; 76 77 protected static Hashtable mapping = null; 78 private static Hashtable rootNodes = null; 79 private List nodes = new ArrayList(); 80 public XMLNode topNode = null; 81 protected String publicID=null; 82 private StringBuffer elementData=null; 83 private Map prefixMapping=null; 84 85 private boolean stopOnXMLErrors = false; 86 87 private boolean pushedNamespaceContext=false; 88 private NamespaceSupport namespaces; 89 90 private static LocalStringManagerImpl localStrings= 92 new LocalStringManagerImpl(SaxParserHandler.class); 93 94 public SaxParserHandler() { 95 Init(); 96 97 namespaces = new NamespaceSupport (); 99 } 100 101 private static void Init() { 102 103 if (mapping==null) { 104 mapping = new Hashtable(); 105 rootNodes= new Hashtable(); 106 107 String rootNode = ApplicationNode.registerBundle(mapping); 108 rootNodes.put(rootNode, ApplicationNode.class); 109 110 rootNode = EjbBundleNode.registerBundle(mapping); 111 rootNodes.put(rootNode, EjbBundleNode.class); 112 113 rootNode = ConnectorNode.registerBundle(mapping); 114 rootNodes.put(rootNode, ConnectorNode.class); 115 116 rootNode = WebBundleNode.registerBundle(mapping); 117 rootNodes.put(rootNode, WebBundleNode.class); 118 119 rootNode = AppClientNode.registerBundle(mapping); 120 rootNodes.put(rootNode, AppClientNode.class); 121 122 rootNode = WebServicesDescriptorNode.ROOT_ELEMENT.getQName(); 123 rootNodes.put(rootNode, WebServicesDescriptorNode.class); 124 125 rootNode = JaxrpcMappingDescriptorNode.ROOT_ELEMENT.getQName(); 126 rootNodes.put(rootNode, JaxrpcMappingDescriptorNode.class); 127 128 rootNode = PersistenceNode.ROOT_ELEMENT.getQName(); 129 rootNodes.put(rootNode, PersistenceNode.class); 130 131 rootNodes.put(com.sun.enterprise.deployment.xml.RuntimeTagNames.S1AS_APPLICATION_RUNTIME_TAG, ApplicationRuntimeNode.class); 132 ApplicationRuntimeNode.registerBundle(mapping); 133 rootNodes.put(com.sun.enterprise.deployment.xml.RuntimeTagNames.S1AS_WEB_RUNTIME_TAG, WebBundleRuntimeNode.class); 134 WebBundleRuntimeNode.registerBundle(mapping); 135 rootNodes.put(com.sun.enterprise.deployment.xml.RuntimeTagNames.S1AS_EJB_RUNTIME_TAG, EjbBundleRuntimeNode.class); 136 EjbBundleRuntimeNode.registerBundle(mapping); 137 rootNodes.put(com.sun.enterprise.deployment.xml.RuntimeTagNames.S1AS_APPCLIENT_RUNTIME_TAG, AppClientRuntimeNode.class); 138 AppClientRuntimeNode.registerBundle(mapping); 139 rootNodes.put(com.sun.enterprise.deployment.xml.RuntimeTagNames.S1AS_CONNECTOR_RUNTIME_TAG, com.sun.enterprise.deployment.node.runtime.connector.ConnectorNode.class); 140 com.sun.enterprise.deployment.node.runtime.connector.ConnectorNode.registerBundle(mapping); 141 142 for (java.util.Enumeration publicIDs=mapping.keys();publicIDs.hasMoreElements();) { 144 String publicID = (String ) publicIDs.nextElement(); 145 String dtd = (String ) mapping.get(publicID); 146 mapping.put(publicID, dtd.substring(dtd.lastIndexOf('/')+1)); 147 } 148 } 149 } 150 151 public static void registerMapping(String publicID, String systemID) { 152 mapping.put(publicID, systemID); 153 } 154 155 156 public InputSource resolveEntity(String publicID, String systemID) throws SAXException { 157 try { 158 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 159 DOLUtils.getDefaultLogger().fine("Asked to resolve " + publicID + " system id = " + systemID); 160 } 161 if (publicID==null) { 162 if (systemID==null || systemID.lastIndexOf('/')==systemID.length()) { 164 return null; 165 } 166 167 String fileName = getSchemaURLFor(systemID.substring(systemID.lastIndexOf('/')+1)); 168 if (fileName==null) { 171 fileName = systemID; 172 } 173 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 174 DOLUtils.getDefaultLogger().fine("Resolved to " + fileName); 175 } 176 return new InputSource (fileName); 177 } 178 if (mapping.containsKey(publicID)) { 179 this.publicID = publicID; 180 return new InputSource (new BufferedInputStream(getDTDUrlFor((String ) mapping.get(publicID)))); 181 } 182 } catch(Exception ioe) { 183 ioe.printStackTrace(); 184 throw new SAXException (ioe); 185 } 186 return null; 187 } 188 189 193 public void setStopOnError(boolean stop) { 194 stopOnXMLErrors = stop; 195 } 196 197 198 public void error(SAXParseException spe) throws SAXParseException { 199 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorFailure", 200 new Object [] {errorReportingString , String.valueOf(spe.getLineNumber()), 201 String.valueOf(spe.getColumnNumber()), spe.getLocalizedMessage()}); 202 if (stopOnXMLErrors) { 203 throw spe; 204 } 205 } 206 207 public void warning(SAXParseException spe) throws SAXParseException { 208 String x = spe.getMessage(); 209 } 210 211 public void fatalError(SAXParseException spe) throws SAXParseException { 212 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorFailure", 213 new Object [] {errorReportingString , String.valueOf(spe.getLineNumber()), 214 String.valueOf(spe.getColumnNumber()), spe.getLocalizedMessage()}); 215 if (stopOnXMLErrors) { 216 throw spe; 217 } 218 } 219 220 223 protected InputStream getDTDUrlFor(String dtdFileName) { 224 225 String dtdLoc = DTDRegistry.DTD_LOCATION.replace('/', File.separatorChar); 226 File f = new File(dtdLoc +File.separatorChar+ dtdFileName); 227 228 try { 229 return new BufferedInputStream(new FileInputStream(f)); 230 } catch(FileNotFoundException fnfe) { 231 DOLUtils.getDefaultLogger().fine("Cannot find DTD " + dtdFileName); 232 return null; 233 } 234 } 235 236 241 public static String getSchemaURLFor(String schemaSystemID) throws IOException { 242 File f = getSchemaFileFor(schemaSystemID); 243 if (f!=null) { 244 return f.toURI().toURL().toString(); 245 } else { 246 return null; 247 } 248 } 249 250 255 public static File getSchemaFileFor(String schemaSystemID) throws IOException { 256 257 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 258 DOLUtils.getDefaultLogger().fine("Getting Schema " + schemaSystemID); 259 } 260 String schemaLoc = DTDRegistry.SCHEMA_LOCATION.replace('/', File.separatorChar); 261 File f = new File(schemaLoc +File.separatorChar+ schemaSystemID); 262 if (!f.exists()) { 263 DOLUtils.getDefaultLogger().fine("Cannot find schema " + schemaSystemID); 264 return null; 265 } 266 return f; 267 } 268 269 270 public void notationDecl(java.lang.String name, 271 java.lang.String publicId, 272 java.lang.String systemId) 273 throws SAXException { 274 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 275 DOLUtils.getDefaultLogger().fine("Received notation " + name + " :=: " + publicId + " :=: " + systemId); 276 } 277 } 278 279 280 public void startPrefixMapping(String prefix, 281 String uri) 282 throws SAXException { 283 284 if (prefixMapping==null) { 285 prefixMapping = new HashMap(); 286 } 287 288 if( !pushedNamespaceContext ) { 292 namespaces.pushContext(); 293 pushedNamespaceContext = true; 294 } 295 namespaces.declarePrefix(prefix,uri); 296 prefixMapping.put(prefix, uri); 297 } 298 299 300 public void startElement(String uri, String localName, String qName, Attributes attributes) { 301 if( !pushedNamespaceContext ) { 302 namespaces.pushContext(); 305 } 306 pushedNamespaceContext = false; 309 310 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 311 DOLUtils.getDefaultLogger().finer("start of element " + uri + " with local name "+ localName + " and " + qName); 312 } 313 XMLNode node=null; 314 elementData=new StringBuffer (); 315 316 if (nodes.isEmpty()) { 317 Class rootNodeClass = (Class ) rootNodes.get(localName); 319 if (rootNodeClass==null) { 320 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 321 new Object [] {localName , " not supprted !"}); 322 } else { 323 try { 324 node = (XMLNode) rootNodeClass.newInstance(); 325 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 326 DOLUtils.getDefaultLogger().fine("Instanciating " + node); 327 } 328 if (node instanceof RootXMLNode) { 329 if (publicID!=null) { 330 ((RootXMLNode) node).setDocType(publicID); 331 } 332 addPrefixMapping(node); 333 } 334 nodes.add(node); 335 topNode = node; 336 node.getDescriptor(); 337 } catch(Exception e) { 338 e.printStackTrace(); 339 return; 340 } 341 } 342 } else { 343 node = (XMLNode) nodes.get(nodes.size()-1); 344 } 345 346 if (node!=null) { 347 XMLElement element = new XMLElement(qName, namespaces); 348 if (node.handlesElement(element)) { 349 node.startElement(element, attributes); 350 } else { 351 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 352 DOLUtils.getDefaultLogger().fine("Asking for new handler for " + element + " to " + node); 353 } 354 XMLNode newNode = node.getHandlerFor(element); 355 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 356 DOLUtils.getDefaultLogger().fine("Got " + newNode); 357 } 358 nodes.add(newNode); 359 addPrefixMapping(newNode); 360 newNode.startElement(element, attributes); 361 } 362 } 363 } 364 365 public void endElement(String uri, String localName, String qName) { 366 367 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 368 DOLUtils.getDefaultLogger().finer("End of element " + uri + " local name "+ localName + " and " + qName + " value " + elementData); 369 } 370 if (nodes.size()==0) { 371 elementData=null; 373 return; 374 } 375 XMLElement element = new XMLElement(qName, namespaces); 376 XMLNode topNode = (XMLNode) nodes.get(nodes.size()-1); 377 if (elementData!=null && (elementData.length()!=0 || allowsEmptyValue(element.getQName()))) { 378 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 379 DOLUtils.getDefaultLogger().finer("For element " + element.getQName() + " And value " + elementData); 380 } 381 if (element.getQName().equals(WebTagNames.URL_PATTERN)) { 382 topNode.setElementValue(element, elementData.toString()); 384 } else if (element.getQName().equals( 385 TagNames.ENVIRONMENT_PROPERTY_VALUE)) { 386 Object envEntryDesc = topNode.getDescriptor(); 387 if (envEntryDesc != null && 388 envEntryDesc instanceof EnvironmentProperty) { 389 EnvironmentProperty envProp = 390 (EnvironmentProperty)envEntryDesc; 391 if (envProp.getType() != null && 395 (envProp.getType().equals("java.lang.String") || 396 envProp.getType().equals("java.lang.Character"))) { 397 topNode.setElementValue(element, 398 elementData.toString()); 399 } else { 400 topNode.setElementValue(element, 401 elementData.toString().trim()); 402 } 403 } else { 404 topNode.setElementValue(element, 405 elementData.toString().trim()); 406 } 407 } else { 408 topNode.setElementValue(element, 409 elementData.toString().trim()); 410 } 411 elementData=null; 412 } 413 if (topNode.endElement(element)) { 414 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 415 DOLUtils.getDefaultLogger().fine("Removing top node " + topNode); 416 } 417 nodes.remove(nodes.size()-1); 418 } 419 420 namespaces.popContext(); 421 pushedNamespaceContext=false; 422 } 423 424 public void characters(char[] ch, int start, int stop) { 425 if (elementData!=null) { 426 elementData = elementData.append(ch,start, stop); 427 } 428 } 429 430 public XMLNode getTopNode() { 431 return topNode; 432 } 433 434 public void setTopNode(XMLNode node) { 435 topNode = node; 436 nodes.add(node); 437 } 438 439 private void addPrefixMapping(XMLNode node) { 440 if (prefixMapping!=null) { 441 for (Iterator itr = prefixMapping.keySet().iterator();itr.hasNext();) { 442 String prefix = (String ) itr.next(); 443 node.addPrefixMapping(prefix, (String ) prefixMapping.get(prefix)); 444 } 445 prefixMapping=null; 446 } 447 } 448 449 450 public static void main(String args[]) { 452 453 if (args.length==0) { 454 return; 455 } else { 456 String fileName = args[0]; 457 File inFile = new File(fileName); 458 if (!inFile.exists()) { 459 return; 460 } 461 try { 462 com.sun.enterprise.deployment.io.DeploymentDescriptorFile ddFile = 463 com.sun.enterprise.deployment.io.DeploymentDescriptorFileFactory.getDDFileFor(inFile); 464 465 long timeStart = System.currentTimeMillis(); 466 InputStream is; 467 com.sun.enterprise.deployment.RootDeploymentDescriptor desc=null; 468 ddFile.setXMLValidation(true); 469 for (int i=0;i<10;i++) { 470 is = new BufferedInputStream(new FileInputStream(inFile)); 471 desc = (com.sun.enterprise.deployment.RootDeploymentDescriptor) ddFile.read(is); 472 is.close(); 473 } 474 if (desc!=null && args.length>1) { 475 if (args[1]!="-o") { 476 is = new BufferedInputStream(new FileInputStream(new File(args[1]))); 477 ddFile = com.sun.enterprise.deployment.io.runtime.RuntimeDDFileFactory.getDDFileFor(desc); 478 ddFile.read(desc, is); 479 } 480 } 481 482 long timeEnd = System.currentTimeMillis(); 483 484 if (args.length>2) 485 if (args[2].equals("-o")) { 486 ddFile.write((com.sun.enterprise.deployment.Descriptor) desc, new File(args[3])); 487 } 488 489 } catch (Throwable t) { 490 t.printStackTrace(); 491 } 492 } 493 } 494 495 private String errorReportingString=""; 496 499 public void setErrorReportingString(String s) { 500 errorReportingString = s; 501 } 502 503 525 private boolean allowsEmptyValue(String elementName) { 526 return (elementName.equals(ApplicationTagNames.LIBRARY_DIRECTORY)); 527 } 528 } 529 | Popular Tags |