KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ProjectXmlValidator


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.ant.freeform;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30 import org.netbeans.modules.ant.freeform.spi.ProjectNature;
31 import org.openide.ErrorManager;
32 import org.openide.cookies.EditorCookie;
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.filesystems.FileUtil;
39 import org.openide.filesystems.URLMapper;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.openide.text.Line;
43 import org.openide.util.Lookup;
44 import org.openide.util.NbBundle;
45 import org.openide.windows.IOProvider;
46 import org.openide.windows.InputOutput;
47 import org.openide.windows.OutputEvent;
48 import org.openide.windows.OutputListener;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.SAXNotRecognizedException JavaDoc;
51 import org.xml.sax.SAXParseException JavaDoc;
52 import org.xml.sax.helpers.DefaultHandler JavaDoc;
53
54 /**
55  * Checks validity of freeform project.xml files.
56  * @see "#47288"
57  * @author Jesse Glick
58  */

59 final class ProjectXmlValidator extends DefaultHandler JavaDoc implements FileChangeListener {
60     
61     private final FileObject projectXml;
62     private InputOutput io;
63     
64     public ProjectXmlValidator(FileObject projectXml) {
65         this.projectXml = projectXml;
66         projectXml.addFileChangeListener(this);
67         validateProjectXml();
68     }
69     
70     private void validateProjectXml() {
71         if (System.getProperty("netbeans.user") == null) { // NOI18N
72
// Probably in a unit test; skip it.
73
return;
74         }
75         open();
76         try {
77             // XXX may want to preinitialize the desired SAXParserFactory and keep it statically, for speed
78
SAXParserFactory JavaDoc f = SAXParserFactory.newInstance();
79             f.setNamespaceAware(true);
80             f.setValidating(true);
81             SAXParser JavaDoc p = f.newSAXParser();
82             p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", // NOI18N
83
"http://www.w3.org/2001/XMLSchema"); // NOI18N
84
p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas()); // NOI18N
85
p.parse(projectXml.getURL().toString(), this);
86         } catch (SAXParseException JavaDoc e) {
87             log(e);
88         } catch (Exception JavaDoc e) {
89             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
90         } finally {
91             close();
92         }
93     }
94
95     /**
96      * Compute a list of XML schema locations to be used for validating project.xml files.
97      */

98     private static String JavaDoc[] getSchemas() {
99         Set JavaDoc<String JavaDoc> schemas = new TreeSet JavaDoc<String JavaDoc>();
100         // XXX should not refer to schema in another module; wait for #42686 to solve properly
101
schemas.add("nbres:/org/netbeans/modules/project/ant/project.xsd"); // NOI18N
102
schemas.add("nbres:/org/netbeans/modules/ant/freeform/resources/freeform-project-general.xsd"); // NOI18N
103
schemas.add("nbres:/org/netbeans/modules/ant/freeform/resources/freeform-project-general-2.xsd"); // NOI18N
104
for (ProjectNature nature : FreeformProject.PROJECT_NATURES.allInstances()) {
105             schemas.addAll(nature.getSchemas());
106         }
107         return schemas.toArray(new String JavaDoc[schemas.size()]);
108     }
109     
110     public void fileChanged(FileEvent fe) {
111         validateProjectXml();
112     }
113
114     public void fileRenamed(FileRenameEvent fe) {}
115
116     public void fileAttributeChanged(FileAttributeEvent fe) {}
117
118     public void fileFolderCreated(FileEvent fe) {}
119
120     public void fileDeleted(FileEvent fe) {}
121
122     public void fileDataCreated(FileEvent fe) {}
123
124     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
125         log(e);
126     }
127
128     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
129         log(e);
130     }
131
132     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
133         throw e;
134     }
135     
136     /** Close any old error tab. */
137     private void open() {
138         if (io != null) {
139             io.closeInputOutput();
140             io = null;
141         }
142     }
143
144     /** Log a parse error, opening error tab as needed. */
145     private void log(SAXParseException JavaDoc e) {
146         if (io == null) {
147             String JavaDoc title = NbBundle.getMessage(ProjectXmlValidator.class, "LBL_project.xml_errors", FileUtil.getFileDisplayName(projectXml));
148             io = IOProvider.getDefault().getIO(title, true);
149             io.select();
150         }
151         try {
152             io.getErr().println(e.getLocalizedMessage(), new Hyperlink(e.getSystemId(), e.getLineNumber(), e.getColumnNumber()));
153         } catch (IOException JavaDoc x) {
154             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, x);
155         }
156     }
157
158     /** Close the stream for the error tab, if one is open, but leave it visible. */
159     private void close() {
160         if (io != null) {
161             io.getErr().close();
162             io.getOut().close(); // XXX why is this necessary?
163
}
164     }
165
166     private static final class Hyperlink implements OutputListener {
167         
168         private final String JavaDoc uri;
169         private final int line, column;
170         
171         public Hyperlink(String JavaDoc uri, int line, int column) {
172             this.uri = uri;
173             this.line = line;
174             this.column = column;
175         }
176         
177         public void outputLineAction(OutputEvent ev) {
178             FileObject fo;
179             try {
180                 fo = URLMapper.findFileObject(new URL JavaDoc(uri));
181             } catch (MalformedURLException JavaDoc e) {
182                 assert false : e;
183                 return;
184             }
185             if (fo == null) {
186                 return;
187             }
188             DataObject d;
189             try {
190                 d = DataObject.find(fo);
191             } catch (DataObjectNotFoundException e) {
192                 assert false : e;
193                 return;
194             }
195             EditorCookie ec = d.getCookie(EditorCookie.class);
196             if (ec == null) {
197                 return;
198             }
199             if (line != -1) {
200                 try {
201                     // XXX do we need to call ec.openDocument as in org.apache.tools.ant.module.run.Hyperlink?
202
Line l = ec.getLineSet().getOriginal(line - 1);
203                     if (column != -1) {
204                         l.show(Line.SHOW_GOTO, column - 1);
205                     } else {
206                         l.show(Line.SHOW_GOTO);
207                     }
208                 } catch (IndexOutOfBoundsException JavaDoc e) {
209                     // forget it
210
ec.open();
211                 }
212             } else {
213                 ec.open();
214             }
215         }
216         
217         public void outputLineSelected(OutputEvent ev) {}
218         
219         public void outputLineCleared(OutputEvent ev) {}
220         
221     }
222     
223 }
224
Popular Tags