KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > StrutsConfigDataObject


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.web.struts;
21
22 //import org.netbeans.modules.xml.catalog.settings.CatalogSettings;
23
import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import org.netbeans.modules.web.struts.config.model.StrutsConfig;
26 import org.openide.filesystems.FileObject;
27 import org.openide.loaders.DataObjectExistsException;
28 import org.openide.loaders.MultiDataObject;
29 import org.openide.nodes.CookieSet;
30 import org.openide.nodes.Node;
31 import org.xml.sax.InputSource JavaDoc;
32
33 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
34 import org.netbeans.api.xml.cookies.CheckXMLCookie;
35 import org.netbeans.spi.xml.cookies.*;
36 import org.openide.ErrorManager;
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.ErrorHandler JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.SAXParseException JavaDoc;
41
42 /**
43  *
44  * @author Petr Pisl
45  */

46 public class StrutsConfigDataObject extends MultiDataObject
47                                     implements org.openide.nodes.CookieSet.Factory {
48     
49     private static StrutsCatalog strutsCatalog = new StrutsCatalog();
50     private boolean documentDirty = true;
51     private boolean documentValid=true;
52     protected boolean nodeDirty = false;
53     private InputStream JavaDoc inputStream;
54     private SAXParseError error;
55     private StrutsConfig lastGoodConfig = null;
56     
57     /** Editor support for text data object. */
58     private transient StrutsConfigEditorSupport editorSupport;
59     
60     /** Property name for property documentValid */
61     public static final String JavaDoc PROP_DOC_VALID = "documentValid"; // NOI18N
62

63     /** Creates a new instance of StrutsConfigDataObject */
64     public StrutsConfigDataObject(FileObject pf, StrutsConfigLoader loader) throws DataObjectExistsException {
65         super(pf, loader);
66         init();
67
68     }
69  
70     private void init() {
71         CookieSet cookies = getCookieSet();
72         
73         getCookieSet().add(StrutsConfigEditorSupport.class, this);
74         
75         // Creates Check XML and Validate XML context actions
76
InputSource JavaDoc in = DataObjectAdapters.inputSource(this);
77         CheckXMLCookie checkCookie = new CheckXMLSupport(in);
78         getCookieSet().add(checkCookie);
79         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
80         getCookieSet().add(validateCookie);
81     }
82     
83     /**
84      * Provides node that should represent this data object. When a node for
85      * representation in a parent is requested by a call to getNode (parent)
86      * it is the exact copy of this node
87      * with only parent changed. This implementation creates instance
88      * <CODE>DataNode</CODE>.
89      * <P>
90      * This method is called only once.
91      *
92      * @return the node representation for this data object
93      * @see DataNode
94      */

95     protected synchronized Node createNodeDelegate () {
96     return new StrutsConfigNode(this);
97     }
98     
99     /** Implements <code>CookieSet.Factory</code> interface. */
100     public Node.Cookie createCookie(Class JavaDoc clazz) {
101         if(clazz.isAssignableFrom(StrutsConfigEditorSupport.class))
102             return getEditorSupport();
103         else
104             return null;
105     }
106     
107     /** Gets editor support for this data object. */
108     public StrutsConfigEditorSupport getEditorSupport() {
109         if(editorSupport == null) {
110             synchronized(this) {
111                 if(editorSupport == null)
112                     editorSupport = new StrutsConfigEditorSupport(this);
113             }
114         }
115         
116         return editorSupport;
117     }
118     
119     public StrutsConfig getStrutsConfig() throws java.io.IOException JavaDoc {
120         if (lastGoodConfig == null)
121             parsingDocument();
122         return lastGoodConfig;
123     }
124     
125     public StrutsConfig getStrutsConfig (boolean parsenow) throws java.io.IOException JavaDoc{
126         if (parsenow){
127             StrutsConfig previous = lastGoodConfig;
128             parsingDocument();
129             if (lastGoodConfig == null)
130                 lastGoodConfig = previous;
131         }
132         return getStrutsConfig();
133     }
134     
135     /** This method is used for obtaining the current source of xml document.
136     * First try if document is in the memory. If not, provide the input from
137     * underlayed file object.
138     * @return The input source from memory or from file
139     * @exception IOException if some problem occurs
140     */

141     protected InputStream JavaDoc prepareInputSource() throws java.io.IOException JavaDoc {
142         if ((getEditorSupport() != null) && (getEditorSupport().isDocumentLoaded())) {
143             // loading from the memory (Document)
144
return getEditorSupport().getInputStream();
145         }
146         else {
147             return getPrimaryFile().getInputStream();
148         }
149     }
150     
151     /** This method has to be called everytime after prepareInputSource calling.
152      * It is used for closing the stream, because it is not possible to access the
153      * underlayed stream hidden in InputSource.
154      * It is save to call this method without opening.
155      */

156     protected void closeInputSource() {
157         InputStream JavaDoc is = inputStream;
158         if (is != null) {
159             try {
160                 is.close();
161             }
162             catch (IOException JavaDoc e) {
163                 // nothing to do, if exception occurs during saving.
164
}
165             if (is == inputStream) {
166                 inputStream = null;
167             }
168         }
169     }
170     
171     public void write(StrutsConfig config) throws java.io.IOException JavaDoc {
172         java.io.File JavaDoc file = org.openide.filesystems.FileUtil.toFile(getPrimaryFile());
173         org.openide.filesystems.FileObject configFO = getPrimaryFile();
174         try {
175             org.openide.filesystems.FileLock lock = configFO.lock();
176             try {
177                 java.io.OutputStream JavaDoc os =configFO.getOutputStream(lock);
178                 try {
179                     config.write(os);
180                 } finally {
181                     os.close();
182                 }
183             }
184             finally {
185                 lock.releaseLock();
186             }
187         } catch (org.openide.filesystems.FileAlreadyLockedException ex) {
188             // PENDING should write a message
189
}
190     }
191     
192     /** This method parses XML document and calls updateNode method which
193     * updates corresponding Node.
194     */

195     public void parsingDocument(){
196         error = null;
197         try {
198             error = updateNode(prepareInputSource());
199         }
200         catch (Exception JavaDoc e) {
201             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
202             setDocumentValid(false);
203             return;
204         }
205         finally {
206             closeInputSource();
207             documentDirty=false;
208         }
209         if (error == null){
210             setDocumentValid(true);
211         }else {
212             setDocumentValid(false);
213         }
214         setNodeDirty(false);
215     }
216     
217     public void setDocumentValid (boolean valid){
218         if (documentValid!=valid) {
219             if (valid)
220                 repairNode();
221             documentValid=valid;
222             firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE);
223         }
224     }
225     
226     /** This method repairs Node Delegate (usually after changing document by property editor)
227     */

228     protected void repairNode(){
229         // PENDING: set the icon in Node
230
// ((DataNode)getNodeDelegate()).setIconBase (getIconBaseForValidDocument());
231
org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); // NOI18N
232
/* if (inOut!=null) {
233             inOut.closeInputOutput();
234             errorAnnotation.detach();
235         }*/

236     }
237     
238     private org.w3c.dom.Document JavaDoc getDomDocument(InputStream JavaDoc inputSource) throws SAXParseException JavaDoc {
239         try {
240             // creating w3c document
241
org.w3c.dom.Document JavaDoc doc = org.netbeans.modules.schema2beans.GraphManager.
242                 createXmlDocument(new org.xml.sax.InputSource JavaDoc(inputSource), false, strutsCatalog,
243                 new J2eeErrorHandler(this));
244             return doc;
245         } catch(Exception JavaDoc e) {
246             // XXX Change that
247
throw new SAXParseException JavaDoc(e.getMessage(), new org.xml.sax.helpers.LocatorImpl JavaDoc());
248         }
249     }
250     
251     /** Update the node from document. This method is called after document is changed.
252     * @param is Input source for the document
253     * @return number of the line with error (document is invalid), 0 (xml document is valid)
254     */

255     // TODO is prepared for handling arrors, but not time to finish it.
256
protected SAXParseError updateNode(InputStream JavaDoc is) throws java.io.IOException JavaDoc{
257         try {
258             Document JavaDoc doc = getDomDocument(is);
259             lastGoodConfig = StrutsConfig.createGraph(doc);
260         }
261         catch(SAXParseException JavaDoc ex) {
262             return new SAXParseError(ex);
263         } catch(SAXException JavaDoc ex) {
264             throw new IOException JavaDoc();
265         }
266         return null;
267     }
268    
269     public boolean isDocumentValid(){
270         return documentValid;
271     }
272     /** setter for property documentDirty. Method updateDocument() usually setsDocumentDirty to false
273     */

274     public void setDocumentDirty(boolean dirty){
275         documentDirty=dirty;
276     }
277
278     /** Getter for property documentDirty.
279     * @return Value of property documentDirty.
280     */

281     public boolean isDocumentDirty(){
282         return documentDirty;
283     }
284     
285     /** Getter for property nodeDirty.
286     * @return Value of property nodeDirty.
287     */

288     public boolean isNodeDirty(){
289         return nodeDirty;
290     }
291
292     /** Setter for property nodeDirty.
293      * @param dirty New value of property nodeDirty.
294      */

295     public void setNodeDirty(boolean dirty){
296         nodeDirty=dirty;
297     }
298     org.openide.nodes.CookieSet getCookieSet0() {
299         return getCookieSet();
300     }
301     
302     public static class J2eeErrorHandler implements ErrorHandler JavaDoc {
303
304         private StrutsConfigDataObject dataObject;
305
306         public J2eeErrorHandler(StrutsConfigDataObject obj) {
307              dataObject=obj;
308         }
309
310         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
311             dataObject.createSAXParseError(exception);
312             throw exception;
313         }
314
315         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
316             dataObject.createSAXParseError(exception);
317             throw exception;
318         }
319
320         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
321             dataObject.createSAXParseError(exception);
322             throw exception;
323         }
324     }
325     
326     private void createSAXParseError(SAXParseException JavaDoc error){
327         this.error = new SAXParseError(error);
328     }
329 }
330
Popular Tags