KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > xmlutils > XMLJ2eeDataObject


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.xmlutils;
21
22 import org.openide.cookies.*;
23 import org.openide.nodes.CookieSet;
24 import org.openide.filesystems.FileObject;
25 import org.openide.loaders.MultiFileLoader;
26 import org.openide.loaders.XMLDataObject;
27 import org.openide.ErrorManager;
28 import org.openide.text.Line;
29 import org.openide.windows.*;
30 import org.openide.util.NbBundle;
31
32 import java.text.MessageFormat JavaDoc;
33 import java.io.*;
34 import org.xml.sax.*;
35 import org.openide.xml.*;
36 import org.netbeans.api.xml.cookies.CheckXMLCookie;
37 import org.netbeans.spi.xml.cookies.*;
38
39 /** Represents a XMLJ2eeDataObject in the Repository.
40  *
41  * @author mkuchtiak
42  */

43 public abstract class XMLJ2eeDataObject extends XMLDataObject implements CookieSet.Factory {
44
45     protected boolean nodeDirty = false;
46     private boolean documentDirty = true;
47     private boolean savingDocument;
48     private InputStream inputStream;
49     private InputOutput inOut;
50     protected XMLJ2eeEditorSupport editor;
51     private boolean documentValid=true;
52     private SAXParseError error;
53     private org.openide.text.Annotation errorAnnotation;
54
55     private static final long serialVersionUID = -515751072013886985L;
56
57     /** Property name for property documentValid */
58     public static final String JavaDoc PROP_DOC_VALID = "documentValid"; // NOI18N
59

60     public XMLJ2eeDataObject(FileObject pf, MultiFileLoader loader)
61         throws org.openide.loaders.DataObjectExistsException {
62         super(pf,loader);
63
64         getCookieSet().add(XMLJ2eeEditorSupport.class, this);
65         getCookieSet().add(EditCookie.class, this);
66         getCookieSet().add(EditorCookie.class, this);
67         getCookieSet().add(LineCookie.class, this);
68         getCookieSet().add(PrintCookie.class, this);
69         getCookieSet().add(CloseCookie.class, this);
70         // added CheckXMLCookie
71
InputSource in = DataObjectAdapters.inputSource(this);
72         CheckXMLCookie checkCookie = new CheckXMLSupport(in);
73         getCookieSet().add(checkCookie);
74     }
75     // Issuezilla 23493 - this is the way how to disable the OpenCoookie from this data object
76
protected EditorCookie createEditorCookie () {
77         return null;
78     }
79     /** Update the node from document. This method is called after document is changed.
80     * @param is Input source for the document
81     * @return number of the line with error (document is invalid), 0 (xml document is valid)
82     */

83     protected abstract SAXParseError updateNode(org.xml.sax.InputSource JavaDoc is) throws java.io.IOException JavaDoc;
84     /** gets the Icon Base for node delegate when parser accepts the xml document as valid
85     * @return Icon Base for node delegate
86     */

87     protected abstract String JavaDoc getIconBaseForValidDocument();
88
89     /** gets the Icon Base for node delegate when parser finds error(s) in xml document
90     * @return Icon Base for node delegate
91     */

92     protected abstract String JavaDoc getIconBaseForInvalidDocument();
93
94     /** Implements <code>CookieSet.Factory</code> interface. */
95     public org.openide.nodes.Node.Cookie createCookie(Class JavaDoc clazz) {
96         if(clazz.isAssignableFrom(XMLJ2eeEditorSupport.class))
97             return getEditorSupport();
98         else
99             return null;
100     }
101
102     /** Gets editor support for this data object. */
103     protected synchronized XMLJ2eeEditorSupport getEditorSupport() {
104         if(editor == null) {
105             editor = new XMLJ2eeEditorSupport(this);
106         }
107         return editor;
108     }
109
110     /** !PW Added this method to make this String available to derived classes.
111      * I'm not sure why it wasn't done this way to begin with and I'm trying
112      * to keep the diffs to a minimum.
113      * @return String indicating error and location, or empty string if there is no error.
114      */

115     public String JavaDoc getStringForInvalidDocument() {
116         if(error != null) {
117             return getOutputStringForInvalidDocument(error);
118         } else {
119             return "No errors";
120         }
121     }
122
123     /** gets the String for status line when parser finds error(s) in xml document
124     * @param error info about an error
125     * @return String for status line
126     */

127     public String JavaDoc getOutputStringForInvalidDocument(SAXParseError error){
128         //return error.getErrorText()+" ["+error.getErrorLine()+","+error.getErrorColumn()+"]";
129
String JavaDoc mes = MessageFormat.format (NbBundle.getMessage (XMLJ2eeDataObject.class, "TXT_errorMessage"),
130             new Object JavaDoc [] { error.getErrorText(),
131             new Integer JavaDoc(error.getErrorLine()),
132             new Integer JavaDoc(error.getErrorColumn()) });
133         return mes;
134     }
135
136     /** Getter for property nodeDirty.
137     * @return Value of property nodeDirty.
138     */

139     public boolean isNodeDirty(){
140         return nodeDirty;
141     }
142
143     /** setter for property documentDirty. Method updateDocument() usually setsDocumentDirty to false
144     */

145     public void setDocumentDirty(boolean dirty){
146         documentDirty=dirty;
147     }
148
149     /** Getter for property documentDirty.
150     * @return Value of property documentDirty.
151     */

152     public boolean isDocumentDirty(){
153         return documentDirty;
154     }
155
156     /** Setter for property nodeDirty.
157      * @param dirty New value of property nodeDirty.
158      */

159     public void setNodeDirty(boolean dirty){
160         nodeDirty=dirty;
161     }
162
163     /** This method repairs Node Delegate (usually after changing document by property editor)
164     */

165     protected void repairNode(){
166         // PENDING: set the icon in Node
167
// ((DataNode)getNodeDelegate()).setIconBase (getIconBaseForValidDocument());
168
org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); // NOI18N
169
if (inOut!=null) {
170             inOut.closeInputOutput();
171             errorAnnotation.detach();
172         }
173     }
174
175     /** This method parses XML document and calls abstract updateNode method which
176     * updates corresponding Node.
177     */

178     public void parsingDocument(){
179         //System.out.println("background parsing "); // NOI18N
180
//Thread.dumpStack();
181
// SAXParseError err=null;
182
error = null;
183         try {
184             error = updateNode(prepareInputSource());
185         }
186         catch (Exception JavaDoc e) {
187             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
188             setDocumentValid(false);
189             return;
190         }
191         finally {
192             closeInputSource();
193             documentDirty=false;
194         }
195         if (error == null){
196             setDocumentValid(true);
197         }else {
198             setDocumentValid(false);
199         }
200     }
201
202     /** This method is used for obtaining the current source of xml document.
203     * First try if document is in the memory. If not, provide the input from
204     * underlayed file object.
205     * @return The input source from memory or from file
206     * @exception IOException if some problem occurs
207     */

208     protected org.xml.sax.InputSource JavaDoc prepareInputSource() throws java.io.IOException JavaDoc {
209         if ((editor != null) && (editor.isDocumentLoaded())) {
210             // loading from the memory (Document)
211
final javax.swing.text.Document JavaDoc doc = editor.getDocument();
212             final String JavaDoc[] str = new String JavaDoc[1];
213             // safely take the text from the document
214
Runnable JavaDoc run = new Runnable JavaDoc() {
215                 public void run() {
216                     try {
217                         str[0] = doc.getText(0, doc.getLength());
218                     }
219                     catch (javax.swing.text.BadLocationException JavaDoc e) {
220                         // impossible
221
}
222                 }
223             };
224
225             doc.render(run);
226             // TODO: this StringReader should be also closed.
227
StringReader reader = new StringReader(str[0]);
228             return new org.xml.sax.InputSource JavaDoc(reader);
229         }
230         else {
231             // loading from the file
232
inputStream = new BufferedInputStream(getPrimaryFile().getInputStream());
233             return new org.xml.sax.InputSource JavaDoc(inputStream);
234         }
235     }
236
237     /** This method has to be called everytime after prepareInputSource calling.
238      * It is used for closing the stream, because it is not possible to access the
239      * underlayed stream hidden in InputSource.
240      * It is save to call this method without opening.
241      */

242     protected void closeInputSource() {
243         InputStream is = inputStream;
244         if (is != null) {
245             try {
246                 is.close();
247             }
248             catch (IOException e) {
249                 // nothing to do, if exception occurs during saving.
250
}
251             if (is == inputStream) {
252                 inputStream = null;
253             }
254         }
255     }
256     public boolean isDocumentValid(){
257         return documentValid;
258     }
259     public void setDocumentValid (boolean valid){
260         if (documentValid!=valid) {
261             if (valid)
262                 repairNode();
263             documentValid=valid;
264             firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE);
265         }
266     }
267     public void addSaveCookie(SaveCookie cookie){
268         getCookieSet().add(cookie);
269     }
270     public void removeSaveCookie(){
271         org.openide.nodes.Node.Cookie cookie = getCookie(SaveCookie.class);
272         if (cookie!=null) getCookieSet().remove(cookie);
273     }
274
275     public void setSavingDocument(boolean saving){
276         savingDocument=saving;
277     }
278     public boolean isSavingDocument(){
279         return savingDocument;
280     }
281     public void displayErrorMessage() {
282         if (error==null) return;
283         if (errorAnnotation==null)
284              errorAnnotation = new org.openide.text.Annotation() {
285              public String JavaDoc getAnnotationType() {
286                return "xml-j2ee-annotation"; // NOI18N
287
}
288              String JavaDoc desc = NbBundle.getMessage(XMLJ2eeDataObject.class, "HINT_XMLErrorDescription");
289              public String JavaDoc getShortDescription() {
290                return desc;
291              }
292         };
293         if (inOut==null)
294             inOut=org.openide.windows.IOProvider.getDefault().getIO(NbBundle.getMessage(XMLJ2eeDataObject.class, "TXT_parser"), false);
295         inOut.setFocusTaken (false);
296         OutputWriter outputWriter = inOut.getOut();
297         int line = Math.max(0,error.getErrorLine());
298         int column = Math.max(0,error.getErrorColumn());
299
300         LineCookie cookie = (LineCookie)getCookie(LineCookie.class);
301         // getting Line object
302
Line xline = cookie.getLineSet ().getCurrent(line==0?0:line-1);
303         // attaching Annotation
304
errorAnnotation.attach(xline);
305
306         try {
307             outputWriter.reset();
308             // defining of new OutputListener
309
IOCtl outList= new IOCtl(xline);
310             outputWriter.println(this.getOutputStringForInvalidDocument(error),outList);
311         } catch(IOException e){
312             ErrorManager.getDefault().notify(e);
313         }
314     }
315
316     public void setValid(boolean valid) throws java.beans.PropertyVetoException JavaDoc {
317         if (!valid && inOut!=null) inOut.closeInputOutput();
318         super.setValid(valid);
319     }
320
321     final class IOCtl implements OutputListener {
322         /** line we check */
323         Line xline;
324
325         public IOCtl (Line xline) {
326             this.xline=xline;
327         }
328
329         public void outputLineSelected (OutputEvent ev) {
330             errorAnnotation.attach(xline);
331             xline.show(Line.SHOW_TRY_SHOW);
332         }
333
334         public void outputLineAction (OutputEvent ev) {
335             errorAnnotation.attach(xline);
336             xline.show(Line.SHOW_TRY_SHOW);
337         }
338
339         public void outputLineCleared (OutputEvent ev) {
340             errorAnnotation.detach();
341         }
342     }
343
344     public static class J2eeErrorHandler implements ErrorHandler {
345
346         private XMLJ2eeDataObject xmlJ2eeDataObject;
347
348         public J2eeErrorHandler(XMLJ2eeDataObject obj) {
349              xmlJ2eeDataObject=obj;
350         }
351
352         public void error(SAXParseException exception) throws SAXException {
353             xmlJ2eeDataObject.createSAXParseError(exception);
354             throw exception;
355         }
356
357         public void fatalError(SAXParseException exception) throws SAXException {
358             xmlJ2eeDataObject.createSAXParseError(exception);
359             throw exception;
360         }
361
362         public void warning(SAXParseException exception) throws SAXException {
363             xmlJ2eeDataObject.createSAXParseError(exception);
364             throw exception;
365         }
366     }
367
368     private void createSAXParseError(SAXParseException error){
369         this.error = new SAXParseError(error);
370     }
371
372 }
373
374
Popular Tags