1 19 20 package org.netbeans.modules.websvc.wsdl.config; 21 22 import java.io.IOException ; 23 import java.io.ByteArrayOutputStream ; 24 import java.lang.ref.WeakReference ; 25 import javax.swing.event.ChangeListener ; 26 27 import org.openide.ErrorManager; 28 import org.openide.nodes.Node; 29 import org.openide.loaders.DataFolder; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.MultiFileLoader; 32 import org.openide.loaders.DataObjectExistsException; 33 import org.openide.filesystems.FileAttributeEvent; 34 import org.openide.filesystems.FileChangeListener; 35 import org.openide.filesystems.FileEvent; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileRenameEvent; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.WeakListeners; 40 41 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 42 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 43 import org.netbeans.spi.xml.cookies.ValidateXMLSupport; 44 45 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport; 46 import org.netbeans.modules.websvc.wsdl.config.ConfigurationProxy; 47 import org.netbeans.modules.websvc.wsdl.config.api.DDProvider; 48 import org.netbeans.modules.websvc.wsdl.config.api.Configuration; 49 import org.netbeans.modules.websvc.wsdl.config.api.Wsdl; 50 import org.netbeans.modules.websvc.jaxrpc.nodes.WsCompileConfigCookie; 51 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXParseException ; 54 import org.xml.sax.SAXException ; 55 import org.netbeans.modules.websvc.wsdl.xmlutils.SAXParseError; 56 57 58 59 63 public class WsCompileConfigDataObject extends DD2beansDataObject implements WsCompileConfigCookie, 64 Node.Cookie, ConfigurationProxy.OutputProvider, FileChangeListener, ChangeListener { 65 66 69 public static final String WSCOMPILE_CONFIG_FILENAME_SUFFIX = "-config"; 71 74 public static final String WSCOMPILE_CONFIG_EXTENSION = "xml"; 76 private WeakReference packageHandlerRef = null; 77 78 private boolean isClientConfig; 83 84 private Configuration configuration; 86 private FileObjectObserver fileListener; 87 private boolean unparsable = true; 88 89 91 public WsCompileConfigDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException { 92 super(pf, loader); 93 init(pf, loader); 94 } 95 96 private void init(FileObject pf, MultiFileLoader loader) { 97 initClientConfig(); 98 99 InputSource in = DataObjectAdapters.inputSource(this); 100 ValidateXMLCookie validateCookie = new ValidateXMLSupport(in); 101 getCookieSet().add(validateCookie); 102 104 fileListener = new FileObjectObserver(pf); 105 106 } 121 122 146 private void initClientConfig() { 147 isClientConfig = false; 148 FileObject configFO = getPrimaryFile(); 149 150 FileObject parentFO = configFO.getParent(); 152 if(parentFO != null) { 153 WebServicesClientSupport clientSupport = WebServicesClientSupport.getWebServicesClientSupport(configFO); 155 if(clientSupport != null) { 156 FileObject wsdlFolder = clientSupport.getWsdlFolder(); 158 if(wsdlFolder != null && wsdlFolder.equals(parentFO)) { 159 isClientConfig = true; 163 } 164 } 165 } 166 } 167 168 public boolean isUnparsable(){ 169 return unparsable; 170 } 171 172 public Configuration getConfiguration() { 173 if(configuration == null) { 174 parsingDocument(); 175 } 176 177 return configuration; 178 } 179 180 197 protected boolean isClientConfig() { 198 return isClientConfig; 199 } 200 201 public boolean isRenameAllowed() { 202 return !isClientConfig(); 203 } 204 205 public boolean isDeleteAllowed() { 206 return true; 207 } 208 209 211 protected Node createNodeDelegate() { 212 return new WsCompileConfigDataNode(this); 213 } 214 215 220 protected String getIconBaseForInvalidDocument() { 221 return "org/netbeans/modules/websvc/wsdl/config/resources/config_broken"; } 223 224 229 protected String getIconBaseForValidDocument() { 230 return "org/netbeans/modules/websvc/wsdl/config/resources/config"; } 232 233 236 protected String generateDocument() { 237 ByteArrayOutputStream out = new ByteArrayOutputStream (); 238 try { 239 configuration.write(out); 240 out.close(); 241 return out.toString("UTF8"); } catch(IOException ex) { 243 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 244 } catch(IllegalStateException ex){ 245 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 246 } 247 248 return out.toString(); 249 } 250 251 255 protected String updateDocument(javax.swing.text.Document doc) { 256 return null; 257 } 258 259 261 public void write(Configuration configurationProxy) throws IOException { 262 Configuration config = getConfiguration(); 263 if(config!=null) { 264 config.merge(configurationProxy, Configuration.MERGE_UNION); 265 } 266 setNodeDirty(true); 267 } 268 269 271 public FileObject getTarget() { 272 return getPrimaryFile(); 273 } 274 275 protected SAXParseError updateNode(InputSource is) throws IOException { 276 if(configuration==null) { 277 try { 278 configuration = DDProvider.getDefault().getDDRoot(getPrimaryFile()); 279 firePropertyChange(Node.PROP_PROPERTY_SETS,null,null); 280 if(unparsable) { 281 unparsable=false; 282 } 283 return null; 284 } catch (RuntimeException ex) { 285 if(!unparsable) { 286 unparsable=true; 287 } 288 return new SAXParseError(new SAXParseException (ex.getMessage(),new org.xml.sax.helpers.LocatorImpl ())); 289 } 290 } 291 Configuration config=null; 292 try { 293 org.w3c.dom.Document doc = getDocument(is); 294 config = org.netbeans.modules.websvc.wsdl.config.impl.Configuration.createGraph(doc); 295 unparsable = false; 296 } catch(SAXParseException ex) { 302 if(!unparsable) { 303 unparsable=true; 304 } 305 return new SAXParseError(ex); 306 } catch(SAXException ex) { 307 if(!unparsable) { 308 unparsable=true; 309 } 310 throw new IOException (); 311 } 312 if(config!=null) { 313 configuration.merge(config, org.netbeans.modules.schema2beans.BaseBean.MERGE_UPDATE); 315 } 316 return null; 317 } 318 319 private org.w3c.dom.Document getDocument(InputSource inputSource) throws SAXParseException { 320 try { 321 org.w3c.dom.Document doc = org.netbeans.modules.schema2beans.GraphManager. 323 createXmlDocument(inputSource, false, org.openide.xml.EntityCatalog.getDefault(), 324 new J2eeErrorHandler(this)); 325 return doc; 326 } catch(Exception e) { 327 throw new SAXParseException (e.getMessage(), new org.xml.sax.helpers.LocatorImpl ()); 329 } 330 } 331 332 public String getServicePackageName() { 333 String packageName = null; 334 Configuration cfg = getConfiguration(); 335 if(cfg != null) { 336 Wsdl wsdl = cfg.getWsdl(); 337 if(wsdl != null) { 338 packageName = wsdl.getPackageName(); 339 } 340 } 341 return packageName; 342 } 343 344 public void setServicePackageName(String newPackage) { 345 Configuration cfg = getConfiguration(); 346 if(cfg != null) { 347 Wsdl wsdl = cfg.getWsdl(); 348 if(wsdl != null) { 349 wsdl.setPackageName(newPackage); 350 } 351 352 try { 353 cfg.write(getTarget()); 354 } catch(IOException ex) { 355 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 356 } 357 } 358 359 360 } 361 362 protected void handleDelete() throws IOException { 363 if(isClientConfig()) { 364 super.handleDelete(); 367 } else { 368 super.handleDelete(); 369 } 370 } 371 372 protected DataObject handleCopy(DataFolder f) throws IOException { 373 DataObject dObj = super.handleCopy(f); 374 try { 375 dObj.setValid(false); 376 } catch(java.beans.PropertyVetoException e) { 377 } 378 return dObj; 379 } 380 381 393 public void fileRenamed(FileRenameEvent fe) { 397 } 400 401 public void fileFolderCreated(FileEvent fe) { 402 } 403 404 public void fileDeleted(FileEvent fe) { 405 } 408 409 public void fileDataCreated(FileEvent fe) { 410 } 417 418 public void fileChanged(FileEvent fe) { 419 } 420 421 public void fileAttributeChanged(FileAttributeEvent fe) { 422 } 423 424 public void stateChanged(javax.swing.event.ChangeEvent e) { 428 } 430 431 public HelpCtx getHelpCtx() { 432 return HelpCtx.DEFAULT_HELP; 433 } 434 435 437 private class FileObjectObserver implements FileChangeListener { 438 439 FileObjectObserver(FileObject fo) { 440 fo.addFileChangeListener( 441 (FileChangeListener) WeakListeners.create(FileChangeListener.class, this, fo)); 442 } 443 444 public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) { 445 } 446 447 public void fileChanged(FileEvent fileEvent) { 448 ConfigurationProxy config = (ConfigurationProxy) WsCompileConfigDataObject.this.getConfiguration(); 449 boolean needRewriting = true; 450 if(config != null && config.isWriting()) { config.setWriting(false); 452 needRewriting = false; 453 } 454 if(isSavingDocument()) { setSavingDocument(false); 456 needRewriting=false; 457 } 458 if(needRewriting) { 459 getEditorSupport().restartTimer(); 460 } 461 } 462 463 public void fileDataCreated(FileEvent fileEvent) { 464 } 465 466 public void fileDeleted(FileEvent fileEvent) { 467 } 468 469 public void fileFolderCreated(FileEvent fileEvent) { 470 } 471 472 public void fileRenamed(FileRenameEvent fileRenameEvent) { 473 } 474 } 475 } 476 | Popular Tags |