KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > config > WsCompileConfigDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsdl.config;
21
22 import java.io.IOException JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc;
53 import org.xml.sax.SAXParseException JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55 import org.netbeans.modules.websvc.wsdl.xmlutils.SAXParseError;
56
57
58
59 /** DataObject for JAX-RPC WsCompile config files.
60  *
61  * @author Peter Williams
62  */

63 public class WsCompileConfigDataObject extends DD2beansDataObject implements WsCompileConfigCookie,
64         Node.Cookie, ConfigurationProxy.OutputProvider, FileChangeListener, ChangeListener JavaDoc {
65     
66     /** WsCompile confguration files are named [wsdl file name]-config.xml. This
67      * constant is the suffix '-config'.
68      */

69     public static final String JavaDoc WSCOMPILE_CONFIG_FILENAME_SUFFIX = "-config"; // NOI18N
70

71     /** WsCompile confguration files are named [wsdl file name]-config.xml. This
72      * constant is the file extension 'xml'.
73      */

74     public static final String JavaDoc WSCOMPILE_CONFIG_EXTENSION = "xml"; // NOI18N
75

76     private WeakReference JavaDoc packageHandlerRef = null;
77     
78     // If isClientWsdl is true, the WSDL file is in the WSDL folder of a web service
79
// client enabled module and thus will have operations and UI exposed that affect
80
// the service as it exists within the project. E.g. deleting such a file will
81
// actually remove the service from the project, not just delete the file on disk.
82
private boolean isClientConfig;
83     
84     // !PW Added these members when upgrading to full DD API model
85
private Configuration configuration;
86     private FileObjectObserver fileListener;
87     private boolean unparsable = true;
88     
89     /** Typical data object constructor.
90      */

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 JavaDoc in = DataObjectAdapters.inputSource(this);
100         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
101         getCookieSet().add(validateCookie);
102 // getCookieSet().add(WsCompileConfigCookie.class, this);
103

104         fileListener = new FileObjectObserver(pf);
105         
106         // !PW There must be a better way to do this. DataObject listens for changes
107
// across all source roots (including changes to the source roots themselves
108
// to keep in sync) so that, suppose a file of interest to this XML file
109
// is changed or deleted (Servlet for a WebApp, Handler for this config file),
110
// then this object will be notified.
111
//
112
// Anyway, enable this code and more below (in the FileChangeListener
113
// implementation) when handlers are added.
114
// Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
115
// if (project != null) {
116
// Sources sources = ProjectUtils.getSources(project);
117
// sources.addChangeListener (this);
118
// }
119
// refreshSourceFolders ();
120
}
121     
122 // private void refreshSourceFolders () {
123
// ArrayList srcRootList = new ArrayList ();
124
//
125
// Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
126
// if (project != null) {
127
// Sources sources = ProjectUtils.getSources(project);
128
// SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
129
// for (int i = 0; i < groups.length; i++) {
130
// if (WebModule.getWebModule (groups [i].getRootFolder ()) != null) {
131
// try {
132
// FileObject fo = groups [i].getRootFolder ();
133
// srcRootList.add (groups [i].getRootFolder ());
134
// FileSystem fs = fo.getFileSystem ();
135
// fs.removeFileChangeListener(this); //avoid being added multiple times
136
// fs.addFileChangeListener (this);
137
// } catch (FileStateInvalidException ex) {
138
// ErrorManager.getDefault ().notify (ex);
139
// }
140
// }
141
// }
142
// }
143
// srcRoots = (FileObject []) srcRootList.toArray (new FileObject [srcRootList.size ()]);
144
// }
145

146     private void initClientConfig() {
147         isClientConfig = false;
148         FileObject configFO = getPrimaryFile();
149         
150         // Check to make sure it has a non-null parent (can't be in WSDL folder if it does).
151
FileObject parentFO = configFO.getParent();
152         if(parentFO != null) {
153             // Does this module support web service clients?
154
WebServicesClientSupport clientSupport = WebServicesClientSupport.getWebServicesClientSupport(configFO);
155             if(clientSupport != null) {
156                 // Is this file object in the WSDL folder of the client?
157
FileObject wsdlFolder = clientSupport.getWsdlFolder();
158                 if(wsdlFolder != null && wsdlFolder.equals(parentFO)) {
159                     // If we get here, the following conditions should be true:
160
// The Config file is in a code module that supports webservice clients.
161
// The Config file is in the proper WSDL folder of that module.
162
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     // !PW FIXME Do we need this method? What do we need it for?
181
// private FileObject getAssociatedWsdl() {
182
// FileObject wsdlFO = null;
183
// FileObject configFO = getPrimaryFile();
184
// String baseName = configFO.getName();
185
//
186
// if(baseName.endsWith(WSCOMPILE_CONFIG_FILENAME_SUFFIX) && baseName.length() > 7) {
187
// FileObject parentFO = configFO.getParent();
188
// if(parentFO != null && parentFO.isFolder()) {
189
// String wsdlName = baseName.substring(0, baseName.length()-7);
190
// wsdlFO = parentFO.getFileObject(wsdlName, WsdlDataObject.WSDL_EXTENSION);
191
// }
192
// }
193
//
194
// return wsdlFO;
195
// }
196

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     /** Create a node to represent the WSDL file. Overrides superclass method.
210      * @return node delegate */

211     protected Node createNodeDelegate() {
212         return new WsCompileConfigDataNode(this);
213     }
214     
215     /** Gets the Icon Base for node delegate when parser accepts the xml document as valid
216      *
217      * PENDING: move into node
218      * @return Icon Base for node delegate
219      */

220     protected String JavaDoc getIconBaseForInvalidDocument() {
221         return "org/netbeans/modules/websvc/wsdl/config/resources/config_broken"; // NOI18N
222
}
223     
224     /** Gets the Icon Base for node delegate when parser finds error(s) in xml document
225      * @return Icon Base for node delegate
226      *
227      * PENDING: move into node
228      */

229     protected String JavaDoc getIconBaseForValidDocument() {
230         return "org/netbeans/modules/websvc/wsdl/config/resources/config"; // NOI18N
231
}
232     
233     /** Create document from the Node. This method is called after Node (Node properties)is changed.
234      * The document is generated from data modul (isDocumentGenerable=true)
235      */

236     protected String JavaDoc generateDocument() {
237         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
238         try {
239             configuration.write(out);
240             out.close();
241             return out.toString("UTF8"); //NOI18N
242
} catch(IOException JavaDoc ex) {
243             ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
244         } catch(IllegalStateException JavaDoc ex){
245             ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
246         }
247         
248         return out.toString();
249     }
250     
251     /** Update document in text editor. This method is called after Node (Node properties)is changed.
252      * The document is updated programaticaly - not from data modul (isDocumentGeneratable=false)
253      * @param doc document which should be updated
254      */

255     protected String JavaDoc updateDocument(javax.swing.text.Document JavaDoc doc) {
256         return null;
257     }
258     
259     /** Method from ConfigurationProxy.OutputProvider
260      */

261     public void write(Configuration configurationProxy) throws IOException JavaDoc {
262         Configuration config = getConfiguration();
263         if(config!=null) {
264             config.merge(configurationProxy, Configuration.MERGE_UNION);
265         }
266         setNodeDirty(true);
267     }
268     
269     /** Method from ConfigurationProxy.OutputProvider
270      */

271     public FileObject getTarget() {
272         return getPrimaryFile();
273     }
274     
275     protected SAXParseError updateNode(InputSource JavaDoc is) throws IOException JavaDoc {
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 JavaDoc ex) {
285                 if(!unparsable) {
286                     unparsable=true;
287                 }
288                 return new SAXParseError(new SAXParseException JavaDoc(ex.getMessage(),new org.xml.sax.helpers.LocatorImpl JavaDoc()));
289             }
290         }
291         Configuration config=null;
292         try {
293             org.w3c.dom.Document JavaDoc doc = getDocument(is);
294             config = org.netbeans.modules.websvc.wsdl.config.impl.Configuration.createGraph(doc);
295             unparsable = false;
296 // !PW Not implemented yet. Add to Configuration interface if required.
297
// if(config.getError()!= null) {
298
// // semantic error we can badge or tooltip here
299
// System.out.println(config.getError().getMessage());
300
// }
301
} catch(SAXParseException JavaDoc ex) {
302             if(!unparsable) {
303                 unparsable=true;
304             }
305             return new SAXParseError(ex);
306         } catch(SAXException JavaDoc ex) {
307             if(!unparsable) {
308                 unparsable=true;
309             }
310             throw new IOException JavaDoc();
311         }
312         if(config!=null) {
313             // set new graph or merge it with old one
314
configuration.merge(config, org.netbeans.modules.schema2beans.BaseBean.MERGE_UPDATE);
315         }
316         return null;
317     }
318     
319     private org.w3c.dom.Document JavaDoc getDocument(InputSource JavaDoc inputSource) throws SAXParseException JavaDoc {
320         try {
321             // creating w3c document
322
org.w3c.dom.Document JavaDoc 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 JavaDoc e) {
327             // XXX Change that
328
throw new SAXParseException JavaDoc(e.getMessage(), new org.xml.sax.helpers.LocatorImpl JavaDoc());
329         }
330     }
331     
332     public String JavaDoc getServicePackageName() {
333         String JavaDoc 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 JavaDoc 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 JavaDoc ex) {
355                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
356             }
357         }
358         
359         
360     }
361     
362     protected void handleDelete() throws IOException JavaDoc {
363         if(isClientConfig()) {
364             // !PW FIXME Deleting a client config should probably invoke the same
365
// delete code that deleting the corresponding client WSDL would.
366
super.handleDelete();
367         } else {
368             super.handleDelete();
369         }
370     }
371     
372     protected DataObject handleCopy(DataFolder f) throws IOException JavaDoc {
373         DataObject dObj = super.handleCopy(f);
374         try {
375             dObj.setValid(false);
376         } catch(java.beans.PropertyVetoException JavaDoc e) {
377         }
378         return dObj;
379     }
380     
381 // !PW Not needed yet.
382
// protected void dispose () {
383
// // no more changes in DD
384
// synchronized (this) {
385
// updates = null;
386
// if (updateTask != null) {
387
// updateTask.cancel();
388
// }
389
// }
390
// super.dispose ();
391
// }
392

393 // --------------------------------------------------------------------
394
// org.openide.filesystems.FileChangeListener implementation
395
// --------------------------------------------------------------------
396
public void fileRenamed(FileRenameEvent fe) {
397         // !PW can listen here for handler files being renamed so we can fix
398
// the handlers.
399
}
400     
401     public void fileFolderCreated(FileEvent fe) {
402     }
403     
404     public void fileDeleted(FileEvent fe) {
405         // !PW can listen here for handler files being deleted so we can remove
406
// the handlers.
407
}
408     
409     public void fileDataCreated(FileEvent fe) {
410 // !PW enable this when handlers are added.
411
// FileObject fo = fileEvent.getFile();
412
// String resourceName = getPackageName (fo);
413
// if (resourceName != null && "java".equals(fo.getExt())) { //NOI18N
414
// elementCreated(resourceName);
415
// }
416
}
417     
418     public void fileChanged(FileEvent fe) {
419     }
420     
421     public void fileAttributeChanged(FileAttributeEvent fe) {
422     }
423     
424 // --------------------------------------------------------------------
425
// javax.swing.event.ChangeListener implementation
426
// --------------------------------------------------------------------
427
public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
428 // refreshSourceFolders();
429
}
430     
431     public HelpCtx getHelpCtx() {
432         return HelpCtx.DEFAULT_HELP;
433     }
434     
435     /** WeakListener for accepting external changes to the config file.
436      */

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()) { // change from outside
451
config.setWriting(false);
452                 needRewriting = false;
453             }
454             if(isSavingDocument()) { // document is being saved
455
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