1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.StringReader ; 29 import org.netbeans.modules.projectimport.ProjectImporterException; 30 import org.openide.ErrorManager; 31 import org.openide.xml.XMLUtil; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 44 final class ClassPathParser extends DefaultHandler { 45 46 private static final String CLASSPATH = "classpath"; private static final String CLASSPATH_ENTRY = "classpathentry"; private static final String ATTRIBUTES = "attributes"; private static final String ATTRIBUTE = "attribute"; private static final String ACCESSRULES = "accessrules"; private static final String ACCESSRULE = "accessrule"; 54 private static final String KIND_ATTR = "kind"; private static final String PATH_ATTR = "path"; 58 private static final int POSITION_NONE = 0; 60 private static final int POSITION_CLASSPATH = 1; 61 private static final int POSITION_CLASSPATH_ENTRY = 2; 62 private static final int POSITION_ATTRIBUTES = 3; 63 private static final int POSITION_ATTRIBUTE = 4; 64 private static final int POSITION_ACCESSRULES = 5; 65 private static final int POSITION_ACCESSRULE = 6; 66 67 private int position = POSITION_NONE; 68 private StringBuffer chars; 69 70 private ClassPath classPath; 71 72 private ClassPathParser() {} 73 74 79 static ClassPath parse(File classPathFile) throws ProjectImporterException { 80 81 ClassPathParser parser = new ClassPathParser(); 82 InputStream classPathIS = null; 83 try { 84 classPathIS = new BufferedInputStream ( 85 new FileInputStream (classPathFile)); 86 parser.load(new InputSource (classPathIS)); 87 } catch (FileNotFoundException e) { 88 throw new ProjectImporterException(e); 89 } finally { 90 if (classPathIS != null) { 91 try { 92 classPathIS.close(); 93 } catch (IOException e) { 94 ErrorManager.getDefault().log(ErrorManager.WARNING, 95 "Unable to close classPathStream: " + e); } 97 } 98 } 99 return parser.classPath; 100 } 101 102 108 static ClassPath parse(String classPath) throws ProjectImporterException { 109 ClassPathParser parser = new ClassPathParser(); 110 parser.load(new InputSource (new StringReader (classPath))); 111 return parser.classPath; 112 } 113 114 115 private void load(InputSource projectIS) throws ProjectImporterException{ 116 try { 117 118 XMLReader reader = XMLUtil.createXMLReader(false, true); 119 reader.setContentHandler(this); 120 reader.setErrorHandler(this); 121 chars = new StringBuffer (); reader.parse(projectIS); } catch (IOException e) { 124 throw new ProjectImporterException(e); 125 } catch (SAXException e) { 126 throw new ProjectImporterException(e); 127 } 128 } 129 130 public void characters(char ch[], int offset, int length) throws SAXException { 131 chars.append(ch, offset, length); 132 } 133 134 public void startElement(String uri, String localName, 135 String qName, Attributes attributes) throws SAXException { 136 137 chars.setLength(0); 138 switch (position) { 139 case POSITION_NONE: 140 if (localName.equals(CLASSPATH)) { 141 position = POSITION_CLASSPATH; 142 classPath = new ClassPath(); 143 } else { 144 unknownElementReached(position, localName); 145 } 146 break; 147 case POSITION_CLASSPATH: 148 if (localName.equals(CLASSPATH_ENTRY)) { 149 ClassPathEntry entry = new ClassPathEntry( 150 attributes.getValue(KIND_ATTR), 151 attributes.getValue(PATH_ATTR)); 152 classPath.addEntry(entry); 153 position = POSITION_CLASSPATH_ENTRY; 154 } else { 155 unknownElementReached(position, localName); 156 } 157 break; 158 case POSITION_CLASSPATH_ENTRY: 159 if (localName.equals(ATTRIBUTES)) { 160 position = POSITION_ATTRIBUTES; 163 } else if (localName.equals(ACCESSRULES)) { 164 position = POSITION_ACCESSRULES; 166 } else { 167 unknownElementReached(position, localName); 168 } 169 break; 170 case POSITION_ATTRIBUTES: 171 if (localName.equals(ATTRIBUTE)) { 172 position = POSITION_ATTRIBUTE; 175 } else { 176 unknownElementReached(position, localName); 177 } 178 break; 179 case POSITION_ACCESSRULES: 180 if (localName.equals(ACCESSRULE)) { 181 position = POSITION_ACCESSRULE; 183 } else { 184 unknownElementReached(position, localName); 185 } 186 break; 187 default: 188 unknownElementReached(position, localName); 189 } 190 } 191 192 private void unknownElementReached(int position, String localName) throws SAXException { 193 throw new SAXException ("Unknown element reached: " + "position: " + position + ", element: " + localName); } 196 197 public void endElement(String uri, String localName, String qName) throws 198 SAXException { 199 switch (position) { 200 case POSITION_CLASSPATH: 201 position = POSITION_NONE; 203 break; 204 case POSITION_CLASSPATH_ENTRY: 205 position = POSITION_CLASSPATH; 206 break; 207 case POSITION_ATTRIBUTES: 208 position = POSITION_CLASSPATH_ENTRY; 209 break; 210 case POSITION_ATTRIBUTE: 211 position = POSITION_ATTRIBUTES; 212 break; 213 case POSITION_ACCESSRULES: 214 position = POSITION_CLASSPATH_ENTRY; 215 break; 216 case POSITION_ACCESSRULE: 217 position = POSITION_ACCESSRULES; 218 break; 219 default: 220 ErrorManager.getDefault().log(ErrorManager.WARNING, 221 "Unknown state reached in ClassPathParser, " + "position: " + position + ", element: " + localName); } 224 chars.setLength(0); 225 } 226 227 public void warning(SAXParseException e) throws SAXException { 228 ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e); 229 } 230 231 public void error(SAXParseException e) throws SAXException { 232 ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurred: " + e); 233 throw e; 234 } 235 236 public void fatalError(SAXParseException e) throws SAXException { 237 ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurred: " + e); 238 throw e; 239 } 240 241 } 242 | Popular Tags |