1 19 20 package org.netbeans.modules.j2ee.dd.api.webservices; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.math.BigDecimal ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import org.netbeans.modules.j2ee.dd.impl.webservices.WebServicesProxy; 29 import org.netbeans.modules.schema2beans.Common; 30 import org.openide.filesystems.*; 31 import org.xml.sax.*; 32 import java.util.Map ; 33 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 34 import org.w3c.dom.Document ; 35 36 public final class DDProvider { 37 38 private static final DDProvider ddProvider = new DDProvider(); 39 private Map ddMap; 40 private Map <MetadataUnit, Webservices> annotationDDMap; 41 42 43 private DDProvider() { 44 ddMap = new java.util.HashMap (5); 46 annotationDDMap = new java.util.WeakHashMap <MetadataUnit, Webservices>(5); 47 } 48 49 53 public static DDProvider getDefault() { 54 return ddProvider; 55 } 56 57 public Webservices getMergedDDRoot(MetadataUnit mu) throws IOException { 58 if (mu == null) { 59 return null; 60 } 61 Webservices xmlRoot = getDDRoot(mu.getDeploymentDescriptor()); 62 if (xmlRoot != null && !xmlRoot.getVersion().equals(new BigDecimal (Webservices.VERSION_1_2))) { 64 return xmlRoot; 65 } 66 return null; 67 } 68 69 76 public synchronized Webservices getDDRoot(FileObject fo) throws java.io.IOException { 77 if(fo == null) return null; 78 WebServicesProxy webSvcProxy = getFromCache (fo); 79 if (webSvcProxy!=null) { 80 return webSvcProxy; 81 } 82 83 fo.addFileChangeListener(new FileChangeAdapter() { 84 public void fileChanged(FileEvent evt) { 85 FileObject fo=evt.getFile(); 86 try { 87 WebServicesProxy webSvcProxy = getFromCache (fo); 88 String version = null; 89 if (webSvcProxy!=null) { 90 try { 91 DDParse parseResult = parseDD(fo); 92 version = parseResult.getVersion(); 93 setProxyErrorStatus(webSvcProxy, parseResult); 94 Webservices newValue = createWebSvcJar(parseResult); 95 if (!version.equals(webSvcProxy.getVersion().toString())) { 97 webSvcProxy.setOriginal(newValue); 98 } else { if (webSvcProxy.getOriginal()==null) { 101 webSvcProxy.setOriginal(newValue); 102 } else { 103 webSvcProxy.getOriginal().merge(newValue,Webservices.MERGE_UPDATE); 104 } 105 } 106 } catch (SAXException ex) { 107 if (ex instanceof SAXParseException) { 108 webSvcProxy.setError((SAXParseException)ex); 109 } else if ( ex.getException() instanceof SAXParseException) { 110 webSvcProxy.setError((SAXParseException)ex.getException()); 111 } 112 webSvcProxy.setStatus(Webservices.STATE_INVALID_UNPARSABLE); 113 webSvcProxy.setProxyVersion(Webservices.VERSION_1_1); 121 } 122 } 123 } catch (java.io.IOException ex){} 124 } 125 }); 126 127 try { 128 DDParse parseResult = parseDD(fo); 129 Webservices original = createWebSvcJar(parseResult); 130 webSvcProxy = new WebServicesProxy(original,parseResult.getVersion()); 131 setProxyErrorStatus(webSvcProxy, parseResult); 132 } catch (SAXException ex) { 133 webSvcProxy = new WebServicesProxy(org.netbeans.modules.j2ee.dd.impl.webservices.model_1_2.Webservices.createGraph(),"1.2"); 135 webSvcProxy.setStatus(Webservices.STATE_INVALID_UNPARSABLE); 136 if (ex instanceof SAXParseException) { 137 webSvcProxy.setError((SAXParseException)ex); 138 } else if ( ex.getException() instanceof SAXParseException) { 139 webSvcProxy.setError((SAXParseException)ex.getException()); 140 } 141 } 142 ddMap.put(fo, (webSvcProxy)); 143 return webSvcProxy; 144 } 145 146 154 public Webservices getDDRootCopy(FileObject fo) throws java.io.IOException { 155 return (Webservices)getDDRoot(fo).clone(); 156 } 157 158 private WebServicesProxy getFromCache (FileObject fo) { 159 return (WebServicesProxy) ddMap.get(fo); 160 } 161 162 168 public Webservices getDDRoot(InputSource is) throws IOException , SAXException { 169 DDParse parse = parseDD(is); 170 Webservices webSvcJar = createWebSvcJar(parse); 171 WebServicesProxy proxy = new WebServicesProxy(webSvcJar, webSvcJar.getVersion().toString()); 172 setProxyErrorStatus(proxy, parse); 173 return proxy; 174 } 175 176 180 public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.modules.j2ee.dd.api.common.CommonDDBean bean) { 181 if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean; 182 else if (bean instanceof WebServicesProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebServicesProxy)bean).getOriginal(); 183 return null; 184 } 185 186 private static void setProxyErrorStatus(WebServicesProxy webSvcProxy, DDParse parse) { 187 SAXParseException error = parse.getWarning(); 188 webSvcProxy.setError(error); 189 if (error!=null) { 190 webSvcProxy.setStatus(Webservices.STATE_INVALID_PARSABLE); 191 } else { 192 webSvcProxy.setStatus(Webservices.STATE_VALID); 193 } 194 } 195 196 private static Webservices createWebSvcJar(DDParse parse) throws SAXException{ 197 Webservices jar = null; 198 String version = parse.getVersion(); 199 if (Webservices.VERSION_1_1.equals(version)) { 200 try { 201 return new org.netbeans.modules.j2ee.dd.impl.webservices.model_1_1.Webservices(parse.getDocument(), Common.USE_DEFAULT_VALUES); 202 } catch (RuntimeException ex) { 203 throw new SAXException(ex.getMessage()); 204 } 205 } else if (Webservices.VERSION_1_2.equals(version)) { 206 try { 207 return new org.netbeans.modules.j2ee.dd.impl.webservices.model_1_2.Webservices(parse.getDocument(), Common.USE_DEFAULT_VALUES); 208 } catch (RuntimeException ex) { 209 throw new SAXException(ex.getMessage()); 210 } 211 } 212 213 return jar; 214 } 215 216 private static class DDResolver implements EntityResolver { 217 static DDResolver resolver; 218 static synchronized DDResolver getInstance() { 219 if (resolver==null) { 220 resolver=new DDResolver(); 221 } 222 return resolver; 223 } 224 public InputSource resolveEntity (String publicId, String systemId) { 225 if ("http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd".equals(systemId)) { 226 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/j2ee_web_services_1_1.xsd"); } else if ("http://java.sun.com/xml/ns/j2ee/j2ee_web_services_1_1.xsd".equals(systemId)) { 228 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/j2ee_web_services_1_1.xsd"); } else if ("http://www.ibm.com/webservices/xsd/javaee_web_services_1_2.xsd".equals(systemId)) { 230 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/javaee_web_services_1_2.xsd"); } else if ("http://java.sun.com/xml/ns/j2ee/javaee_web_services_1_2.xsd".equals(systemId)) { 232 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/javaee_web_services_1_2.xsd"); } else { 234 return null; 236 } 237 } 238 } 239 240 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 241 private int errorType=-1; 242 SAXParseException error; 243 244 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 245 if (errorType<0) { 246 errorType=0; 247 error=sAXParseException; 248 } 249 } 251 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 252 if (errorType<1) { 253 errorType=1; 254 error=sAXParseException; 255 } 256 } 258 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 259 errorType=2; 260 throw sAXParseException; 261 } 262 263 public int getErrorType() { 264 return errorType; 265 } 266 public SAXParseException getError() { 267 return error; 268 } 269 } 270 271 public SAXParseException parse(FileObject fo) 272 throws org.xml.sax.SAXException , java.io.IOException { 273 DDParse parseResult = parseDD(fo); 274 return parseResult.getWarning(); 275 } 276 277 private DDParse parseDD (FileObject fo) 278 throws SAXException, java.io.IOException { 279 return parseDD(fo.getInputStream()); 280 } 281 282 private DDParse parseDD (InputStream is) 283 throws SAXException, java.io.IOException { 284 return parseDD(new InputSource(is)); 285 } 286 287 private DDParse parseDD (InputSource is) 288 throws SAXException, java.io.IOException { 289 DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler(); 290 DocumentBuilder parser=null; 291 try { 292 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 293 parser = fact.newDocumentBuilder(); 294 } catch (ParserConfigurationException ex) { 295 throw new SAXException(ex.getMessage()); 296 } 297 parser.setErrorHandler(errorHandler); 298 parser.setEntityResolver(DDProvider.DDResolver.getInstance()); 299 Document d = parser.parse(is); 300 SAXParseException error = errorHandler.getError(); 301 return new DDParse(d, error); 302 } 303 304 307 private static class DDParse { 308 private Document document; 309 private SAXParseException saxException; 310 private String version; 311 public DDParse(Document d, SAXParseException saxEx) { 312 document = d; 313 saxException = saxEx; 314 extractVersion(); 315 } 316 317 320 public Document getDocument() { 321 return document; 322 } 323 324 327 private void extractVersion () { 328 String ver = document.getDocumentElement().getAttribute("version"); version = (Webservices.VERSION_1_1.equals(ver))?Webservices.VERSION_1_1:Webservices.VERSION_1_2; 331 } 332 333 public String getVersion() { 334 return version; 335 } 336 337 340 public SAXParseException getWarning() { 341 return saxException; 342 } 343 } 344 345 } 346 | Popular Tags |