1 19 20 package org.netbeans.modules.websvc.wsdl.config.api; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Map ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 31 import org.xml.sax.*; 32 33 import org.openide.util.NbBundle; 34 import org.openide.filesystems.*; 35 36 import org.netbeans.modules.websvc.wsdl.config.ConfigurationProxy; 37 38 45 public final class DDProvider { 46 private static DDProvider ddProvider; 47 private Map ddMap; 48 private Map baseBeanMap; 49 private Map errorMap; 50 private FCA fileChangeListener; 51 52 private DDProvider() { 53 ddMap=new java.util.HashMap (5); 54 baseBeanMap=new java.util.HashMap (5); 55 errorMap=new java.util.HashMap (5); 56 fileChangeListener = new FCA (); 57 } 58 59 63 public static synchronized DDProvider getDefault() { 64 if (ddProvider==null) { 65 ddProvider = new DDProvider(); 66 } 67 return ddProvider; 68 } 69 70 71 78 public Configuration getDDRoot(FileObject fo) throws java.io.IOException { 79 80 ConfigurationProxy configuration = getFromCache (fo); 81 if (configuration!=null) { 82 return configuration; 83 } 84 85 fo.addFileChangeListener(fileChangeListener); 86 87 SAXParseException error = null; 88 try { 89 Configuration original = getOriginalFromCache (fo); 90 if (original == null) { 91 error = parse(fo); 93 original = createConfiguration(fo.getInputStream()); 94 baseBeanMap.put(fo, new WeakReference (original)); 95 } else { 96 error = (SAXParseException) errorMap.get (fo); 97 } 98 configuration=new ConfigurationProxy(original); 99 if (error!=null) { 100 configuration.setStatus(Configuration.STATE_INVALID_PARSABLE); 101 configuration.setError(error); 102 } 103 } catch (SAXException ex) { 104 configuration = new ConfigurationProxy(null); 105 configuration.setStatus(Configuration.STATE_INVALID_UNPARSABLE); 106 if (ex instanceof SAXParseException) { 107 configuration.setError((SAXParseException)ex); 108 } else if ( ex.getException() instanceof SAXParseException) { 109 configuration.setError((SAXParseException)ex.getException()); 110 } 111 } 112 ddMap.put(fo, new WeakReference (configuration)); 113 return configuration; 114 } 115 116 124 public Configuration getDDRootCopy(FileObject fo) throws java.io.IOException { 125 return (Configuration)getDDRoot(fo).clone(); 126 } 127 128 private ConfigurationProxy getFromCache (FileObject fo) throws java.io.IOException { 129 WeakReference wr = (WeakReference ) ddMap.get(fo); 130 if (wr == null) { 131 return null; 132 } 133 ConfigurationProxy configuration = (ConfigurationProxy) wr.get (); 134 if (configuration == null) { 135 ddMap.remove (fo); 136 } 137 return configuration; 138 } 139 140 private Configuration getOriginalFromCache (FileObject fo) throws java.io.IOException { 141 WeakReference wr = (WeakReference ) baseBeanMap.get(fo); 142 if (wr == null) { 143 return null; 144 } 145 Configuration configuration = (Configuration) wr.get (); 146 if (configuration == null) { 147 baseBeanMap.remove (fo); 148 errorMap.remove (fo); 149 } 150 return configuration; 151 } 152 153 159 public Configuration getDDRoot(File f) throws IOException , SAXException { 160 return createConfiguration(new FileInputStream (f)); 161 } 162 163 178 private static Configuration createConfiguration(java.io.InputStream is) throws java.io.IOException , SAXException { 179 try { 180 return org.netbeans.modules.websvc.wsdl.config.impl.Configuration.createGraph(is); 181 } catch (RuntimeException ex) { 182 throw new SAXException (ex.getMessage()); 183 } 184 } 185 186 private static class DDResolver implements EntityResolver { 187 static DDResolver resolver; 188 static synchronized DDResolver getInstance() { 189 if(resolver==null) { 190 resolver=new DDResolver(); 191 } 192 return resolver; 193 } 194 public InputSource resolveEntity (String publicId, String systemId) { 195 String resource=null; 196 if("http://java.sun.com/xml/ns/jax-rpc/ri/config".equals(systemId)) { 201 resource="/org/netbeans/modules/websvc/wsdl/resources/jax-rpc-ri-config_1_1.xsd"; } 203 if(resource==null) { 204 return null; 205 } 206 java.net.URL url = this.getClass().getResource(resource); 207 return new InputSource(url.toString()); 208 } 209 } 210 211 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 212 private int errorType=-1; 213 SAXParseException error; 214 215 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 216 if (errorType<0) { 218 errorType=0; 219 error=sAXParseException; 220 } 221 } 223 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 224 if (errorType<1) { 226 errorType=1; 227 error=sAXParseException; 228 } 229 } 231 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 232 errorType=2; 234 throw sAXParseException; 235 } 236 237 public int getErrorType() { 238 return errorType; 239 } 240 public SAXParseException getError() { 241 return error; 242 } 243 } 244 245 public SAXParseException parse (FileObject fo) 246 throws org.xml.sax.SAXException , java.io.IOException { 247 DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler(); 248 try { 249 SAXParser parser = createSAXParserFactory().newSAXParser(); 250 XMLReader reader = parser.getXMLReader(); 251 reader.setErrorHandler(errorHandler); 252 reader.setEntityResolver(DDProvider.DDResolver.getInstance()); 253 254 reader.parse(new InputSource(fo.getInputStream())); 255 SAXParseException error = errorHandler.getError(); 256 if (error!=null) { 257 return error; 258 } 259 } catch (ParserConfigurationException ex) { 260 throw new org.xml.sax.SAXException (ex.getMessage()); 261 } catch (SAXException ex) { 262 throw ex; 263 } 264 return null; 265 } 266 267 269 private static SAXParserFactory createSAXParserFactory() throws ParserConfigurationException { 270 try { 271 SAXParserFactory fact = SAXParserFactory.newInstance(); 272 if (fact!=null) { 273 try { 274 fact.getClass().getMethod("getSchema", new Class []{}); return fact; 276 } catch (NoSuchMethodException ex) {} 277 } 278 return (SAXParserFactory ) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); } catch (Exception ex) { 280 throw new ParserConfigurationException (ex.getMessage()); 281 } 282 } 283 284 private class FCA extends FileChangeAdapter { 285 public void fileChanged(FileEvent evt) { 286 FileObject fo=evt.getFile(); 287 try { 288 ConfigurationProxy configuration = getFromCache (fo); 289 Configuration orig = getOriginalFromCache (fo); 290 if (configuration!=null) { 291 try { 292 SAXParseException error = parse(fo); 294 if (error!=null) { 295 configuration.setError(error); 297 configuration.setStatus(Configuration.STATE_INVALID_PARSABLE); 298 } else { 299 configuration.setError(null); 301 configuration.setStatus(Configuration.STATE_VALID); 302 } 303 Configuration original = createConfiguration(fo.getInputStream()); 304 baseBeanMap.put(fo, new WeakReference (original)); 305 errorMap.put(fo, configuration.getError ()); 306 307 if (configuration.getOriginal()==null) { 309 configuration.setOriginal(original); 311 } else { 312 configuration.getOriginal().merge(original,Configuration.MERGE_UPDATE); 314 } 315 } catch (SAXException ex) { 316 if (ex instanceof SAXParseException) { 318 configuration.setError((SAXParseException)ex); 319 } else if ( ex.getException() instanceof SAXParseException) { 320 configuration.setError((SAXParseException)ex.getException()); 321 } 322 configuration.setStatus(Configuration.STATE_INVALID_UNPARSABLE); 323 configuration.setOriginal(null); 324 } 325 } else if (orig != null) { 326 try { 327 Configuration original = createConfiguration(fo.getInputStream()); 328 if (original.getClass().equals (orig.getClass())) { 329 orig.merge(original,Configuration.MERGE_UPDATE); 330 } else { 331 baseBeanMap.put(fo, new WeakReference (original)); 332 } 333 } catch (SAXException ex) { 334 baseBeanMap.remove(fo); 335 } 336 } 337 } catch (java.io.IOException ex){} 338 } 339 } 340 } | Popular Tags |