1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import java.util.TreeSet ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 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 ; 50 import org.xml.sax.SAXNotRecognizedException ; 51 import org.xml.sax.SAXParseException ; 52 import org.xml.sax.helpers.DefaultHandler ; 53 54 59 final class ProjectXmlValidator extends DefaultHandler 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) { return; 74 } 75 open(); 76 try { 77 SAXParserFactory f = SAXParserFactory.newInstance(); 79 f.setNamespaceAware(true); 80 f.setValidating(true); 81 SAXParser p = f.newSAXParser(); 82 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas()); p.parse(projectXml.getURL().toString(), this); 86 } catch (SAXParseException e) { 87 log(e); 88 } catch (Exception e) { 89 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 90 } finally { 91 close(); 92 } 93 } 94 95 98 private static String [] getSchemas() { 99 Set <String > schemas = new TreeSet <String >(); 100 schemas.add("nbres:/org/netbeans/modules/project/ant/project.xsd"); schemas.add("nbres:/org/netbeans/modules/ant/freeform/resources/freeform-project-general.xsd"); schemas.add("nbres:/org/netbeans/modules/ant/freeform/resources/freeform-project-general-2.xsd"); for (ProjectNature nature : FreeformProject.PROJECT_NATURES.allInstances()) { 105 schemas.addAll(nature.getSchemas()); 106 } 107 return schemas.toArray(new String [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 e) throws SAXException { 125 log(e); 126 } 127 128 public void error(SAXParseException e) throws SAXException { 129 log(e); 130 } 131 132 public void fatalError(SAXParseException e) throws SAXException { 133 throw e; 134 } 135 136 137 private void open() { 138 if (io != null) { 139 io.closeInputOutput(); 140 io = null; 141 } 142 } 143 144 145 private void log(SAXParseException e) { 146 if (io == null) { 147 String 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 x) { 154 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, x); 155 } 156 } 157 158 159 private void close() { 160 if (io != null) { 161 io.getErr().close(); 162 io.getOut().close(); } 164 } 165 166 private static final class Hyperlink implements OutputListener { 167 168 private final String uri; 169 private final int line, column; 170 171 public Hyperlink(String 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 (uri)); 181 } catch (MalformedURLException 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 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 e) { 209 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 |