KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > JSFConfigDataObject


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.jsf;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import org.netbeans.editor.BaseDocument;
25 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataObjectExistsException;
29 import org.openide.loaders.MultiDataObject;
30 import org.openide.nodes.CookieSet;
31 import org.openide.nodes.Node;
32 import org.w3c.dom.Document JavaDoc;
33 import org.xml.sax.*;
34 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
35 import org.netbeans.api.xml.cookies.CheckXMLCookie;
36 import org.netbeans.spi.xml.cookies.*;
37
38 /**
39  *
40  * @author Petr Pisl
41  */

42 public class JSFConfigDataObject extends MultiDataObject
43                                     implements org.openide.nodes.CookieSet.Factory {
44     
45     private static JSFCatalog jsfCatalog = new JSFCatalog();
46     private boolean documentDirty = true;
47     private boolean documentValid=true;
48     protected boolean nodeDirty = false;
49     private InputStream JavaDoc inputStream;
50     /** Editor support for text data object. */
51     private transient JSFConfigEditorSupport editorSupport;
52     private SAXParseError error;
53     private FacesConfig lastGoodFacesConfig = null;
54     
55     /** Property name for property documentValid */
56     public static final String JavaDoc PROP_DOC_VALID = "documentValid"; // NOI18N
57

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

91     protected synchronized Node createNodeDelegate () {
92     return new JSFConfigNode(this);
93     }
94     
95     /** Implements <code>CookieSet.Factory</code> interface. */
96     public Node.Cookie createCookie(Class JavaDoc clazz) {
97         if(clazz.isAssignableFrom(JSFConfigEditorSupport.class))
98             return getEditorSupport();
99         else
100             return null;
101     }
102     
103     /** Gets editor support for this data object. */
104     public JSFConfigEditorSupport getEditorSupport() {
105         if(editorSupport == null) {
106             synchronized(this) {
107                 if(editorSupport == null)
108                     editorSupport = new JSFConfigEditorSupport(this);
109             }
110         }
111         
112         return editorSupport;
113     }
114     
115     public FacesConfig getFacesConfig() throws java.io.IOException JavaDoc {
116         if (lastGoodFacesConfig == null)
117             parsingDocument();
118         return lastGoodFacesConfig;
119     }
120     
121     /** This method is used for obtaining the current source of xml document.
122     * First try if document is in the memory. If not, provide the input from
123     * underlayed file object.
124     * @return The input source from memory or from file
125     * @exception IOException if some problem occurs
126     */

127     protected InputStream JavaDoc prepareInputSource() throws java.io.IOException JavaDoc {
128         if ((getEditorSupport() != null) && (getEditorSupport().isDocumentLoaded())) {
129             // loading from the memory (Document)
130
return getEditorSupport().getInputStream();
131         }
132         else {
133             return getPrimaryFile().getInputStream();
134         }
135     }
136     
137     /** This method has to be called everytime after prepareInputSource calling.
138      * It is used for closing the stream, because it is not possible to access the
139      * underlayed stream hidden in InputSource.
140      * It is save to call this method without opening.
141      */

142     protected void closeInputSource() {
143         InputStream JavaDoc is = inputStream;
144         if (is != null) {
145             try {
146                 is.close();
147             }
148             catch (IOException JavaDoc e) {
149                 // nothing to do, if exception occurs during saving.
150
}
151             if (is == inputStream) {
152                 inputStream = null;
153             }
154         }
155     }
156     
157     /** This method parses XML document and calls updateNode method which
158     * updates corresponding Node.
159     */

160     public void parsingDocument(){
161         error = null;
162         try {
163             error = updateNode(prepareInputSource());
164         }
165         catch (Exception JavaDoc e) {
166             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
167             setDocumentValid(false);
168             return;
169         }
170         finally {
171             closeInputSource();
172             documentDirty=false;
173         }
174         if (error == null){
175             setDocumentValid(true);
176         }else {
177             setDocumentValid(false);
178         }
179         setNodeDirty(false);
180     }
181     
182     public void setDocumentValid (boolean valid){
183         if (documentValid!=valid) {
184             if (valid)
185                 repairNode();
186             documentValid=valid;
187             firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE);
188         }
189     }
190     
191     /** This method repairs Node Delegate (usually after changing document by property editor)
192     */

193     protected void repairNode(){
194         // PENDING: set the icon in Node
195
// ((DataNode)getNodeDelegate()).setIconBase (getIconBaseForValidDocument());
196
org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); // NOI18N
197
/* if (inOut!=null) {
198             inOut.closeInputOutput();
199             errorAnnotation.detach();
200         }*/

201     }
202     
203     private org.w3c.dom.Document JavaDoc getDomDocument(InputStream JavaDoc inputSource) throws SAXParseException {
204         try {
205             // creating w3c document
206
org.w3c.dom.Document JavaDoc doc = org.netbeans.modules.schema2beans.GraphManager.
207                 createXmlDocument(new org.xml.sax.InputSource JavaDoc(inputSource), false, jsfCatalog,
208                 new J2eeErrorHandler(this));
209             return doc;
210         } catch(Exception JavaDoc e) {
211             // XXX Change that
212
throw new SAXParseException(e.getMessage(), new org.xml.sax.helpers.LocatorImpl JavaDoc());
213         }
214     }
215     
216
217     /** Update the node from document. This method is called after document is changed.
218     * @param is Input source for the document
219     * @return number of the line with error (document is invalid), 0 (xml document is valid)
220     */

221     // TODO is prepared for handling arrors, but not time to finish it.
222
protected SAXParseError updateNode(InputStream JavaDoc is) throws java.io.IOException JavaDoc{
223         try {
224             Document doc = getDomDocument(is);
225             
226             //TODO new api
227
//JSF version = JSFCatalog.extractVersion(doc);
228
//check version, use impl class to create graph
229
//TODO new API
230
// if (FacesConfig.VERSION_1_1.equals(version)) {
231
// lastGoodFacesConfig = org.netbeans.modules.web.jsf.config.model_1_1.FacesConfig.createGraph(doc);
232
// }
233
// if (FacesConfig.VERSION_1_0.equals(version)) {
234
// lastGoodFacesConfig = org.netbeans.modules.web.jsf.config.model_1_1.FacesConfig.createGraph(doc);
235
// }
236
// if (FacesConfig.VERSION_1_2.equals(version)) {
237
// lastGoodFacesConfig = org.netbeans.modules.web.jsf.config.model_1_2.FacesConfig.createGraph(doc);
238
// }
239
}
240         catch(SAXParseException ex) {
241             return new SAXParseError(ex);
242         } catch(SAXException ex) {
243             throw new IOException JavaDoc();
244         }
245         return null;
246     }
247    
248     public boolean isDocumentValid(){
249         return documentValid;
250     }
251     /** setter for property documentDirty. Method updateDocument() usually setsDocumentDirty to false
252     */

253     public void setDocumentDirty(boolean dirty){
254         documentDirty=dirty;
255     }
256
257     /** Getter for property documentDirty.
258     * @return Value of property documentDirty.
259     */

260     public boolean isDocumentDirty(){
261         return documentDirty;
262     }
263     
264     /** Getter for property nodeDirty.
265     * @return Value of property nodeDirty.
266     */

267     public boolean isNodeDirty(){
268         return nodeDirty;
269     }
270
271     /** Setter for property nodeDirty.
272      * @param dirty New value of property nodeDirty.
273      */

274     public void setNodeDirty(boolean dirty){
275         nodeDirty=dirty;
276     }
277     org.openide.nodes.CookieSet getCookieSet0() {
278         return getCookieSet();
279     }
280     
281     public static class J2eeErrorHandler implements ErrorHandler {
282
283         private JSFConfigDataObject dataObject;
284
285         public J2eeErrorHandler(JSFConfigDataObject obj) {
286              dataObject=obj;
287         }
288
289         public void error(SAXParseException exception) throws SAXException {
290             dataObject.createSAXParseError(exception);
291             throw exception;
292         }
293
294         public void fatalError(SAXParseException exception) throws SAXException {
295             dataObject.createSAXParseError(exception);
296             throw exception;
297         }
298
299         public void warning(SAXParseException exception) throws SAXException {
300             dataObject.createSAXParseError(exception);
301             throw exception;
302         }
303     }
304     
305     private void createSAXParseError(SAXParseException error){
306         this.error = new SAXParseError(error);
307     }
308     
309     
310 }
311
Popular Tags