1 19 20 package org.netbeans.api.web.dd; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import org.netbeans.modules.web.dd.impl.WebAppProxy; 27 import org.openide.filesystems.*; 28 import org.xml.sax.*; 29 import java.util.Map ; 30 import org.openide.util.NbBundle; 31 import java.net.URL ; 32 33 39 40 public final class DDProvider { 41 private static DDProvider ddProvider; 42 private Map ddMap; 43 private Map baseBeanMap; 44 private Map errorMap; 45 private FCA fileChangeListener; 46 47 private static final String EXCEPTION_PREFIX="version:"; 49 50 private DDProvider() { 51 ddMap=new java.util.HashMap (5); 52 baseBeanMap=new java.util.HashMap (5); 53 errorMap=new java.util.HashMap (5); 54 fileChangeListener = new FCA (); 55 } 56 57 61 public static synchronized DDProvider getDefault() { 62 if (ddProvider==null) ddProvider = new DDProvider(); 63 return ddProvider; 64 } 65 66 67 74 public WebApp getDDRoot(FileObject fo) throws java.io.IOException { 75 76 WebAppProxy webApp = getFromCache (fo); 77 if (webApp!=null) return webApp; 78 79 80 fo.addFileChangeListener(fileChangeListener); 81 82 String version=null; 83 SAXParseException error = null; 84 try { 85 WebApp original = getOriginalFromCache (fo); 86 if (original == null) { 87 version = getVersion(fo.getInputStream()); 88 error = parse(fo); 90 original = createWebApp(fo.getInputStream(), version); 91 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 92 } else { 93 version = original.getVersion (); 94 error = (SAXParseException) errorMap.get (fo.getURL ()); 95 } 96 webApp=new WebAppProxy(original,version); 97 if (error!=null) { 98 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE); 99 webApp.setError(error); 100 } 101 } catch (SAXException ex) { 102 webApp = new WebAppProxy(null,version); 103 webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE); 104 if (ex instanceof SAXParseException) { 105 webApp.setError((SAXParseException)ex); 106 } else if ( ex.getException() instanceof SAXParseException) { 107 webApp.setError((SAXParseException)ex.getException()); 108 } 109 } 110 ddMap.put(fo.getURL(), new WeakReference (webApp)); 111 return webApp; 112 } 113 114 122 public WebApp getDDRootCopy(FileObject fo) throws java.io.IOException { 123 return (WebApp)getDDRoot(fo).clone(); 124 } 125 126 private WebAppProxy getFromCache (FileObject fo) throws java.io.IOException { 127 WeakReference wr = (WeakReference ) ddMap.get(fo.getURL ()); 128 if (wr == null) { 129 return null; 130 } 131 WebAppProxy webApp = (WebAppProxy) wr.get (); 132 if (webApp == null) { 133 ddMap.remove (fo.getURL ()); 134 } 135 return webApp; 136 } 137 138 private WebApp getOriginalFromCache (FileObject fo) throws java.io.IOException { 139 WeakReference wr = (WeakReference ) baseBeanMap.get(fo.getURL ()); 140 if (wr == null) { 141 return null; 142 } 143 WebApp webApp = (WebApp) wr.get (); 144 if (webApp == null) { 145 baseBeanMap.remove (fo.getURL ()); 146 errorMap.remove (fo.getURL ()); 147 if (ddMap.get (fo.getURL ()) == null) { 148 } 149 } 150 return webApp; 151 } 152 153 159 public WebApp getDDRoot(File f) throws IOException , SAXException { 160 return createWebApp(new FileInputStream (f), getVersion(new FileInputStream (f))); 161 } 162 163 169 public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.api.web.dd.common.CommonDDBean bean) { 170 if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean; 171 else if (bean instanceof WebAppProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebAppProxy)bean).getOriginal(); 172 return null; 173 } 174 175 private static WebApp createWebApp(java.io.InputStream is, String version) throws java.io.IOException , SAXException { 176 try { 177 if (WebApp.VERSION_2_3.equals(version)) { 178 return org.netbeans.modules.web.dd.impl.model_2_3.WebApp.createGraph(is); 179 } else { 180 return org.netbeans.modules.web.dd.impl.model_2_4.WebApp.createGraph(is); 181 } 182 } catch (RuntimeException ex) { 183 throw new SAXException (ex.getMessage()); 184 } 185 } 186 187 189 private static String getVersion(java.io.InputStream is) throws java.io.IOException , SAXException { 190 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 191 fact.setValidating(false); 192 try { 193 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 194 XMLReader reader = parser.getXMLReader(); 195 reader.setContentHandler(new VersionHandler()); 196 reader.setEntityResolver(DDResolver.getInstance()); 197 try { 198 reader.parse(new InputSource(is)); 199 } catch (SAXException ex) { 200 is.close(); 201 String message = ex.getMessage(); 202 if (message!=null && message.startsWith(EXCEPTION_PREFIX)) 203 return message.substring(EXCEPTION_PREFIX.length()); 204 else throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_cannotParse"),ex); 205 } 206 is.close(); 207 throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_cannotFindRoot")); 208 } catch(javax.xml.parsers.ParserConfigurationException ex) { 209 throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_parserProblem"),ex); 210 } 211 } 212 213 private static class VersionHandler extends org.xml.sax.helpers.DefaultHandler { 214 public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException { 215 if ("web-app".equals(rawName)) { String version = atts.getValue("version"); throw new SAXException(EXCEPTION_PREFIX+(version==null?WebApp.VERSION_2_3:version)); 218 } 219 } 220 } 221 222 private static class DDResolver implements EntityResolver { 223 static DDResolver resolver; 224 static synchronized DDResolver getInstance() { 225 if (resolver==null) { 226 resolver=new DDResolver(); 227 } 228 return resolver; 229 } 230 public InputSource resolveEntity (String publicId, String systemId) { 231 String resource=null; 232 if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId)) { resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_3.dtd"; } else if ("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_2.dtd"; } else if ("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd".equals(systemId)) { 238 resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_4.xsd"; } 240 if (resource==null) return null; 241 java.net.URL url = this.getClass().getResource(resource); 242 return new InputSource(url.toString()); 243 } 244 } 245 246 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 247 private int errorType=-1; 248 SAXParseException error; 249 250 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 251 if (errorType<0) { 252 errorType=0; 253 error=sAXParseException; 254 } 255 } 257 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 258 if (errorType<1) { 259 errorType=1; 260 error=sAXParseException; 261 } 262 } 264 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 265 errorType=2; 266 throw sAXParseException; 267 } 268 269 public int getErrorType() { 270 return errorType; 271 } 272 public SAXParseException getError() { 273 return error; 274 } 275 } 276 277 public SAXParseException parse (FileObject fo) 278 throws org.xml.sax.SAXException , java.io.IOException { 279 DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler(); 280 try { 281 XMLReader reader = new org.apache.xerces.parsers.SAXParser(); 282 reader.setErrorHandler(errorHandler); 283 reader.setEntityResolver(DDProvider.DDResolver.getInstance()); 284 reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.parse(new InputSource(fo.getInputStream())); 288 SAXParseException error = errorHandler.getError(); 289 if (error!=null) return error; 290 } catch (SAXException ex) { 291 throw ex; 292 } 293 return null; 294 } 295 296 private class FCA extends FileChangeAdapter { 297 public void fileChanged(FileEvent evt) { 298 FileObject fo=evt.getFile(); 299 try { 300 WebAppProxy webApp = getFromCache (fo); 301 WebApp orig = getOriginalFromCache (fo); 302 if (webApp!=null) { 303 String version = null; 304 try { 305 version = getVersion(fo.getInputStream()); 306 SAXParseException error = parse(fo); 308 if (error!=null) { 309 webApp.setError(error); 310 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE); 311 } else { 312 webApp.setError(null); 313 webApp.setStatus(WebApp.STATE_VALID); 314 } 315 WebApp original = createWebApp(fo.getInputStream(), version); 316 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 317 errorMap.put(fo.getURL(), webApp.getError ()); 318 if (!version.equals(webApp.getVersion())) { 320 webApp.setOriginal(original); 321 } else { if (webApp.getOriginal()==null) { 324 webApp.setOriginal(original); 325 } else { 326 webApp.getOriginal().merge(original,WebApp.MERGE_UPDATE); 327 } 328 } 329 } catch (SAXException ex) { 330 if (ex instanceof SAXParseException) { 331 webApp.setError((SAXParseException)ex); 332 } else if ( ex.getException() instanceof SAXParseException) { 333 webApp.setError((SAXParseException)ex.getException()); 334 } 335 webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE); 336 webApp.setOriginal(null); 337 webApp.setProxyVersion(version); 338 } 339 } else if (orig != null) { 340 String version = null; 341 try { 342 version = getVersion(fo.getInputStream()); 343 WebApp original = createWebApp(fo.getInputStream(), version); 344 if (original.getClass().equals (orig.getClass())) { 345 orig.merge(original,WebApp.MERGE_UPDATE); 346 } else { 347 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 348 } 349 } catch (SAXException ex) { 350 baseBeanMap.remove(fo.getURL()); 351 } 352 } 353 } catch (java.io.IOException ex){} 354 } 355 } 356 } | Popular Tags |