1 23 24 package com.sun.enterprise.deployment.io; 25 26 import java.io.*; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.logging.Level ; 30 31 import javax.xml.parsers.SAXParserFactory ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 import org.xml.sax.SAXNotRecognizedException ; 39 import org.xml.sax.InputSource ; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 43 import com.sun.enterprise.deployment.node.XMLNode; 44 45 import com.sun.enterprise.deployment.Descriptor; 46 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 47 48 import com.sun.enterprise.deployment.node.SaxParserHandler; 49 import com.sun.enterprise.deployment.node.SaxParserHandlerFactory; 50 import com.sun.enterprise.deployment.node.J2EEDocumentBuilder; 51 import com.sun.enterprise.deployment.node.RootXMLNode; 52 53 import com.sun.enterprise.util.LocalStringManagerImpl; 54 import com.sun.enterprise.util.io.FileUtils; 55 import com.sun.enterprise.deployment.util.DOLUtils; 56 57 63 public abstract class DeploymentDescriptorFile { 64 65 public final static String FULL_VALIDATION = "full"; 66 public final static String PARSING_VALIDATION = "parsing"; 67 68 private boolean xmlValidation = true; 70 71 private String validationLevel=PARSING_VALIDATION; 73 74 private String errorReportingString=null; 76 77 private static LocalStringManagerImpl localStrings= 79 new LocalStringManagerImpl(DeploymentDescriptorFile.class); 80 81 82 public DeploymentDescriptorFile() { 83 } 84 85 89 public SAXParser getSAXParser() { 90 return getSAXParser(false); 91 } 92 93 99 public SAXParser getSAXParser (boolean validating) { 100 try { 101 SAXParserFactory spf = SAXParserFactory.newInstance(); 102 103 spf.setNamespaceAware(true); 105 106 spf.setValidating(validating); 108 109 113 if (spf.getClass().getName().indexOf("xerces")!=-1) { 116 spf.setFeature( 117 "http://apache.org/xml/features/allow-java-encodings", true); 118 } else { 119 DOLUtils.getDefaultLogger().log(Level.WARNING, "modify your java command line to include the -Djava.endorsed.dirs option"); 120 } 121 122 try { 123 124 spf.setFeature("http://apache.org/xml/features/validation/schema",validating); 126 127 SAXParser sp = spf.newSAXParser(); 128 129 String path = getDefaultSchemaSource(); 131 if (path!=null) { 132 sp.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",path); 133 } 134 135 sp.getXMLReader().setFeature( 139 "http://apache.org/xml/features/validation/dynamic", validating); 140 141 return sp; 142 143 } catch (SAXNotRecognizedException x) { 144 DOLUtils.getDefaultLogger().log(Level.SEVERE, 146 "INFO: JAXP SAXParser property not recognized: " 147 + SaxParserHandler.JAXP_SCHEMA_LANGUAGE); 148 DOLUtils.getDefaultLogger().log(Level.SEVERE, 149 "Check to see if parser conforms to JAXP 1.2 spec."); 150 151 } 152 } catch (Exception e) { 153 e.printStackTrace(); 154 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 155 new Object []{e.getMessage()}); 156 } 157 return null; 158 } 159 163 public DocumentBuilder getDocumentBuilder(boolean validating) { 164 try { 165 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 166 167 dbf.setNamespaceAware(true); 169 170 dbf.setValidating(validating); 172 173 try { 175 String path = getDefaultSchemaSource(); 177 if (path!=null) { 178 dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",path); 179 } 180 return dbf.newDocumentBuilder(); 181 } catch (ParserConfigurationException x) { 182 DOLUtils.getDefaultLogger().log(Level.SEVERE, 184 "Error: JAXP DOMParser property not recognized: " 185 + SaxParserHandler.JAXP_SCHEMA_LANGUAGE); 186 DOLUtils.getDefaultLogger().log(Level.SEVERE, 187 "Check to see if parser conforms to JAXP 1.2 spec."); 188 189 } 190 } catch (Exception e) { 191 e.printStackTrace(); 192 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 193 new Object []{e.getMessage()}); 194 } 195 return null; 196 } 197 198 205 public Descriptor read(InputStream is) 206 throws IOException, SAXParseException { 207 return read(null, is); 208 } 209 210 218 public Descriptor read(Descriptor descriptor, File in) 219 throws IOException, SAXParseException { 220 221 FileInputStream fis = new FileInputStream(in); 222 try { 223 return read(descriptor, fis); 224 } finally { 225 fis.close(); 226 } 227 } 228 229 237 public Descriptor read(Descriptor descriptor, AbstractArchive in) 238 throws IOException, SAXParseException { 239 240 InputStream is = in.getEntry(getDeploymentDescriptorPath()); 241 try { 242 return read(descriptor, is); 243 } finally { 244 is.close(); 245 } 246 } 247 248 256 public Descriptor read(Descriptor descriptor, InputStream is) 257 throws IOException, SAXParseException { 258 259 errorReportingString = FileUtils.revertFriendlyFilenameExtension(errorReportingString); 260 String error = (errorReportingString == null)? errorReportingString:new File(errorReportingString).getName(); 261 String errorReporting = localStrings.getLocalString( 262 "enterprise.deployment.io.errorcontext", 263 "archive {0} and deployment descriptor file {1}", 264 new Object []{ error, getDeploymentDescriptorPath()}); 265 266 SAXParser sp = getSAXParser(getXMLValidation()); 267 SaxParserHandler dh = SaxParserHandlerFactory.newInstance(); 268 if (validationLevel.equals(FULL_VALIDATION)) { 269 dh.setStopOnError(true); 270 } 271 if (descriptor!=null) { 272 dh.setTopNode(getRootXMLNode(descriptor)); 273 } 274 275 dh.setErrorReportingString(errorReporting); 276 277 InputSource input =new InputSource (is); 278 try { 279 sp.parse(input,dh); 280 } catch(SAXParseException e) { 281 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 282 new Object []{e.getMessage()}); 283 284 errorReporting += " " + e.getLocalizedMessage(); 285 SAXParseException spe = new SAXParseException (errorReporting, 286 e.getSystemId(), 287 e.getPublicId(), 288 e.getLineNumber(), 289 e.getColumnNumber(), 290 e); 291 292 throw spe; 293 } catch(SAXException e) { 294 if (e.getException()!=null) { 295 e.getException().printStackTrace(); 296 } 297 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 298 new Object []{e.getMessage()}); 299 return null; 300 } catch (IOException e) { 301 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", 302 new Object []{e.getMessage()}); 303 304 StackTraceElement [] stElements = e.getStackTrace(); 312 for (int i = 0; i < stElements.length; i++) { 313 StackTraceElement stElement = stElements[i]; 314 if (stElement.getClassName().equals("java.net.Socket") && 315 stElement.getMethodName().equals("connect")) { 316 String msg = localStrings.getLocalString( 317 "enterprise.deployment.can_not_locate_dtd", 318 "Unable to locate the DTD to validate your deployment descriptor file [{1}] in archive [{0}]. Please make sure the DOCTYPE is correct (no typo in public ID or system ID) and you have proper access to the Internet.", 319 new Object []{ error, getDeploymentDescriptorPath()}); 320 IOException ioe = new IOException(msg); 321 ioe.initCause(e); 322 throw ioe; 323 } 324 } 325 326 throw e; 327 } 328 if (dh.getTopNode()!=null) { 329 Object topDesc = dh.getTopNode().getDescriptor(); 330 if (topDesc instanceof Descriptor) { 331 return (Descriptor) topDesc; 332 } 333 } 334 return null; 335 } 336 337 341 public Document getDocument(Descriptor descriptor) { 342 return J2EEDocumentBuilder.getDocument(descriptor, getRootXMLNode(descriptor)); 343 } 344 345 351 public void write(Descriptor descriptor, OutputStream os) throws IOException { 352 try { 353 J2EEDocumentBuilder.write(descriptor, getRootXMLNode(descriptor), os); 354 } catch(IOException ioe) { 355 throw ioe; 356 } catch(Exception e) { 357 IOException ioe = new IOException(e.getMessage()); 358 ioe.initCause(e); 359 throw ioe; 360 } 361 } 362 363 369 public void write(Descriptor descriptor, String path) throws IOException { 370 371 String dir; 372 String fileName = getDeploymentDescriptorPath(); 373 if (fileName.lastIndexOf('/')!=-1) { 374 dir = path + File.separator + fileName.substring(0, fileName.lastIndexOf('/')); 375 fileName = fileName.substring(fileName.lastIndexOf('/')+1); 376 } else { 377 dir = path; 378 } 379 File dirs = new File(dir.replace('/', File.separatorChar)); 380 if (!dirs.exists()) { 381 dirs.mkdirs(); 382 } 383 File out = new File(dirs, fileName); 384 write(descriptor, out); 385 } 386 387 393 public void write(Descriptor descriptor, File out) throws IOException { 394 FileOutputStream fos = new FileOutputStream(out); 395 try { 396 write(descriptor, fos ); 397 } catch(Exception e) { 398 IOException ioe = new IOException(e.getMessage()); 399 ioe.initCause(e); 400 throw ioe; 401 } 402 fos.close(); 403 } 404 405 409 public abstract String getDeploymentDescriptorPath(); 410 411 417 public abstract RootXMLNode getRootXMLNode(Descriptor descriptor); 418 419 422 protected boolean getXMLValidation() { 423 return xmlValidation; 424 } 425 426 430 public void setXMLValidation(boolean validate) { 431 xmlValidation = validate; 432 } 433 434 442 public void setXMLValidationLevel(String level) { 443 validationLevel = level; 444 } 445 446 449 public String getXMLValidationLevel() { 450 return validationLevel; 451 } 452 453 456 protected String getDefaultSchemaSource() { 457 RootXMLNode node = getRootXMLNode(null); 458 if (node!=null) { 459 List <String > systemIDs = node.getSystemIDs(); 460 if (systemIDs != null) { 461 String path = null; 462 for (int i = 0; i < systemIDs.size(); i++) { 463 if (path == null) { 464 path = systemIDs.get(i) + " "; 465 } else { 466 path = path + systemIDs.get(i) + " "; 467 } 468 } 469 return path.trim(); 470 } 471 } 472 return null; 473 } 474 475 478 public void setErrorReportingString(String s) { 479 this.errorReportingString = s; 480 } 481 482 } 483 | Popular Tags |