1 19 20 package org.netbeans.modules.j2ee.dd.api.application; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.ResourceBundle ; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 31 import org.netbeans.modules.j2ee.dd.impl.application.ApplicationProxy; 32 import org.netbeans.modules.schema2beans.BaseBean; 33 import org.netbeans.modules.schema2beans.Common; 34 import org.openide.filesystems.FileChangeAdapter; 35 import org.openide.filesystems.FileEvent; 36 import org.openide.filesystems.FileObject; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.DocumentType ; 39 import org.w3c.dom.NamedNodeMap ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.xml.sax.EntityResolver ; 43 import org.xml.sax.InputSource ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.SAXParseException ; 46 47 52 public final class DDProvider { 53 54 private static final String APP_13_DOCTYPE = "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"; private static final DDProvider ddProvider = new DDProvider(); 57 58 private Map ddMap; 59 60 static ResourceBundle bundle = ResourceBundle.getBundle("org/netbeans/modules/j2ee/dd/Bundle"); 61 62 63 private DDProvider() { 64 ddMap = new HashMap (5); 66 } 67 68 72 public static DDProvider getDefault() { 73 return ddProvider; 74 } 75 76 83 public synchronized Application getDDRoot(FileObject fo) throws IOException { 84 if (fo == null) { 85 return null; 86 } 87 ApplicationProxy ejbJarProxy = null; 88 synchronized (ddMap) { 89 ejbJarProxy = getFromCache (fo); 90 if (ejbJarProxy!=null) { 91 return ejbJarProxy; 92 } 93 } 94 95 fo.addFileChangeListener(new FileChangeAdapter() { 96 public void fileChanged(FileEvent evt) { 97 FileObject fo=evt.getFile(); 98 try { 99 synchronized (ddMap) { 100 ApplicationProxy ejbJarProxy = getFromCache (fo); 101 String version = null; 102 if (ejbJarProxy!=null) { 103 try { 104 DDParse parseResult = parseDD(fo); 105 version = parseResult.getVersion(); 106 setProxyErrorStatus(ejbJarProxy, parseResult); 107 Application newValue = createApplication(parseResult); 108 if (!version.equals(ejbJarProxy.getVersion().toString())) { 110 ejbJarProxy.setOriginal(newValue); 111 } else { if (ejbJarProxy.getOriginal()==null) { 114 ejbJarProxy.setOriginal(newValue); 115 } else { 116 ejbJarProxy.getOriginal().merge(newValue,Application.MERGE_UPDATE); 117 } 118 } 119 } catch (SAXException ex) { 120 if (ex instanceof SAXParseException ) { 121 ejbJarProxy.setError((SAXParseException )ex); 122 } else if ( ex.getException() instanceof SAXParseException ) { 123 ejbJarProxy.setError((SAXParseException )ex.getException()); 124 } 125 ejbJarProxy.setStatus(Application.STATE_INVALID_UNPARSABLE); 126 ejbJarProxy.setProxyVersion(version); 134 } 135 } 136 } 137 } catch (IOException ex){} 138 } 139 }); 140 141 try { 142 DDParse parseResult = parseDD(fo); 143 SAXParseException error = parseResult.getWarning(); 144 Application original = createApplication(parseResult); 145 ejbJarProxy = new ApplicationProxy(original,parseResult.getVersion()); 146 setProxyErrorStatus(ejbJarProxy, parseResult); 147 } catch (SAXException ex) { 148 ejbJarProxy = new ApplicationProxy(org.netbeans.modules.j2ee.dd.impl.application.model_1_4.Application.createGraph(),"2.0"); 150 ejbJarProxy.setStatus(Application.STATE_INVALID_UNPARSABLE); 151 if (ex instanceof SAXParseException ) { 152 ejbJarProxy.setError((SAXParseException )ex); 153 } else if ( ex.getException() instanceof SAXParseException ) { 154 ejbJarProxy.setError((SAXParseException )ex.getException()); 155 } 156 } 157 ddMap.put(fo, (ejbJarProxy)); 158 return ejbJarProxy; 159 } 160 161 169 public Application getDDRootCopy(FileObject fo) throws IOException { 170 return (Application)getDDRoot(fo).clone(); 171 } 172 173 private ApplicationProxy getFromCache (FileObject fo) { 174 183 return (ApplicationProxy) ddMap.get(fo); 184 } 185 186 192 public Application getDDRoot(InputSource is) throws IOException , SAXException { 193 DDParse parse = parseDD(is); 194 Application ejbJar = createApplication(parse); 195 ApplicationProxy proxy = new ApplicationProxy(ejbJar, ejbJar.getVersion().toString()); 196 setProxyErrorStatus(proxy, parse); 197 return proxy; 198 } 199 200 204 public BaseBean getBaseBean(CommonDDBean bean) { 205 if (bean instanceof BaseBean) { 206 return (BaseBean)bean; 207 } else if (bean instanceof ApplicationProxy) { 208 return (BaseBean) ((ApplicationProxy)bean).getOriginal(); 209 } 210 return null; 211 } 212 213 private static void setProxyErrorStatus(ApplicationProxy ejbJarProxy, DDParse parse) { 214 SAXParseException error = parse.getWarning(); 215 ejbJarProxy.setError(error); 216 if (error!=null) { 217 ejbJarProxy.setStatus(Application.STATE_INVALID_PARSABLE); 218 } else { 219 ejbJarProxy.setStatus(Application.STATE_VALID); 220 } 221 } 222 223 private static Application createApplication(DDParse parse) { 224 Application jar = null; 225 String version = parse.getVersion(); 226 if (Application.VERSION_1_4.equals(version)) { 227 return new org.netbeans.modules.j2ee.dd.impl.application.model_1_4.Application(parse.getDocument(), Common.USE_DEFAULT_VALUES); 228 } else if (Application.VERSION_1_3.equals(version)) { 229 return new org.netbeans.modules.j2ee.dd.impl.application.model_1_3.Application(parse.getDocument(), Common.USE_DEFAULT_VALUES); 230 } else if (Application.VERSION_5.equals(version)) { 231 return new org.netbeans.modules.j2ee.dd.impl.application.model_5.Application(parse.getDocument(), Common.USE_DEFAULT_VALUES); 232 } 233 234 return jar; 235 } 236 237 private static class DDResolver implements EntityResolver { 238 static DDResolver resolver; 239 static synchronized DDResolver getInstance() { 240 if (resolver==null) { 241 resolver=new DDResolver(); 242 } 243 return resolver; 244 } 245 public InputSource resolveEntity (String publicId, String systemId) { 246 if (APP_13_DOCTYPE.equals(publicId)) { 251 return new InputSource ("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/application_1_3.dtd"); } else if ("http://java.sun.com/xml/ns/j2ee/application_1_4.xsd".equals(systemId)) { 254 return new InputSource ("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/application_1_4.xsd"); } else if ("http://java.sun.com/xml/ns/javaee/application_5.xsd".equals(systemId)) { 256 return new InputSource ("nbres:/org/netbeans/modules/javaee/dd/impl/resources/application_5.xsd"); } else { 258 return null; 260 } 261 } 262 } 263 264 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 265 private int errorType=-1; 266 SAXParseException error; 267 268 public void warning(SAXParseException sAXParseException) throws SAXException { 269 if (errorType<0) { 270 errorType=0; 271 error=sAXParseException; 272 } 273 } 275 public void error(SAXParseException sAXParseException) throws SAXException { 276 if (errorType<1) { 277 errorType=1; 278 error=sAXParseException; 279 } 280 } 282 public void fatalError(SAXParseException sAXParseException) throws SAXException { 283 errorType=2; 284 throw sAXParseException; 285 } 286 287 public int getErrorType() { 288 return errorType; 289 } 290 public SAXParseException getError() { 291 return error; 292 } 293 } 294 295 public SAXParseException parse(FileObject fo) 296 throws SAXException , IOException { 297 DDParse parseResult = parseDD(fo); 298 return parseResult.getWarning(); 299 } 300 301 private DDParse parseDD (FileObject fo) 302 throws SAXException , IOException { 303 return parseDD(fo.getInputStream()); 304 } 305 306 private DDParse parseDD (InputStream is) 307 throws SAXException , IOException { 308 return parseDD(new InputSource (is)); 309 } 310 311 private DDParse parseDD (InputSource is) 312 throws SAXException , IOException { 313 DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler(); 314 315 DocumentBuilder parser=null; 316 try { 317 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 318 parser = fact.newDocumentBuilder(); 319 } catch (ParserConfigurationException ex) { 320 throw new SAXException (ex.getMessage()); 321 } 322 parser.setErrorHandler(errorHandler); 323 parser.setEntityResolver(DDResolver.getInstance()); 324 Document d = parser.parse(is); 325 SAXParseException error = errorHandler.getError(); 326 return new DDParse(d, error); 327 } 328 329 332 private static class DDParse { 333 private final Document document; 334 private final SAXParseException saxException; 335 private String version; 336 public DDParse(Document d, SAXParseException saxEx) { 337 document = d; 338 saxException = saxEx; 339 extractVersion(); 340 } 341 342 345 public Document getDocument() { 346 return document; 347 } 348 349 352 private void extractVersion () { 353 version = Application.VERSION_5; 355 356 DocumentType dt = document.getDoctype(); 358 359 if(dt == null) { 360 NodeList nl = document.getElementsByTagName("application"); if(nl != null && nl.getLength() > 0) { 363 Node appNode = nl.item(0); 364 NamedNodeMap attrs = appNode.getAttributes(); 365 Node vNode = attrs.getNamedItem("version"); if(vNode != null) { 367 String versionValue = vNode.getNodeValue(); 368 if(Application.VERSION_1_4.equals(versionValue)) { 369 version = Application.VERSION_1_4; 370 } else if(Application.VERSION_1_3.equals(versionValue)) { 371 version = Application.VERSION_1_3; 372 } else { 373 version = Application.VERSION_5; } 375 } 376 } 377 } else { 378 if (APP_13_DOCTYPE.equals(dt.getPublicId())) { 379 version = Application.VERSION_1_3; 380 } 381 } 382 } 383 384 public String getVersion() { 385 return version; 386 } 387 388 391 public SAXParseException getWarning() { 392 return saxException; 393 } 394 } 395 396 } 397 | Popular Tags |