1 19 20 package org.netbeans.core.filesystems; 21 22 import java.io.*; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 import org.xml.sax.*; 27 import org.xml.sax.helpers.*; 28 import org.openide.filesystems.*; 29 import org.openide.util.*; 30 import org.openide.xml.*; 31 import org.openide.*; 32 33 34 41 abstract class DefaultParser extends DefaultHandler { 42 43 protected FileObject fo; 44 private Locator locator = null; 45 46 protected short state = INIT; 47 48 protected static final short PARSED = 1000; 49 protected static final short ERROR = -1; 50 protected static final short INIT = 0; 51 52 protected DefaultParser() { 53 } 54 55 protected DefaultParser(FileObject fo) { 56 this.fo = fo; 57 } 58 59 62 protected XMLReader createXMLReader() throws IOException, SAXException { 63 return XMLUtil.createXMLReader(false); 64 } 65 66 70 protected boolean isStopException(Exception e) { 71 return false; 72 } 73 74 77 protected short getState() { 78 return state; 79 } 80 81 protected final Locator getLocator() { 82 return locator; 83 } 84 85 89 protected void parse(FileObject fo) { 90 state = INIT; InputStream is = null; 92 this.fo = fo; 93 try { 94 XMLReader parser = createXMLReader(); 95 parser.setEntityResolver(this); 96 parser.setErrorHandler(this); 97 parser.setContentHandler(this); 98 99 106 InputSource in = new InputSource(); 107 is = fo.getInputStream(); 108 in.setByteStream(is); 109 in.setSystemId(fo.getURL().toExternalForm()); 110 customizeInputSource(in); 111 112 parser.parse(in); 113 114 } catch (IOException io) { 115 if (!isStopException(io)) { 116 if (fo.isValid() && fo.canRead()) { 117 Exceptions.attachMessage(io, "While parsing: " + fo); Logger.getLogger(DefaultParser.class.getName()).log(Level.INFO, null, io); 119 state = ERROR; 120 } 121 } 122 } catch (SAXException sex) { 123 if (!isStopException(sex)) { 124 Exceptions.attachMessage(sex, "While parsing: " + fo); Logger.getLogger(DefaultParser.class.getName()).log(Level.INFO, null, sex); 126 state = ERROR; 127 } 128 } finally { 129 if (is != null) { 130 try { 131 is.close(); 132 } catch (IOException ex) { 133 } 135 } 136 } 137 } 138 139 140 protected void customizeInputSource(InputSource in) { 141 } 142 143 146 protected final void parse() { 147 if (fo == null) throw new NullPointerException (); 148 parse(fo); 149 } 150 151 152 protected void error() throws SAXException { 153 String reason = org.openide.util.NbBundle.getMessage(DefaultParser.class, "Invalid_XML_document"); 154 error(reason); 155 } 156 157 158 protected void error(String reason) throws SAXException { 159 StringBuffer buf = new StringBuffer (reason).append(": ").append(fo.toString()); if (locator != null) { 161 buf.append(" line: ").append(locator.getLineNumber()); buf.append(" column: ").append(locator.getColumnNumber()); } 164 String msg = buf.toString(); SAXException sex = new SAXException(msg); 166 throw sex; 167 } 168 169 public void error(SAXParseException exception) throws SAXException { 170 throw exception; 171 } 172 173 public void fatalError(SAXParseException exception) throws SAXException { 174 throw exception; 175 } 176 177 public void endDocument() throws SAXException { 178 state = PARSED; 179 } 180 181 public void setDocumentLocator(Locator locator) { 182 this.locator = locator; 183 } 184 185 public InputSource resolveEntity (String publicID, String systemID) { 186 return new InputSource (new ByteArrayInputStream (new byte[] { })); 188 } 189 190 } 191 | Popular Tags |