1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.BufferedInputStream ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import org.netbeans.modules.projectimport.ProjectImporterException; 27 import org.openide.ErrorManager; 28 import org.openide.xml.XMLUtil; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.XMLReader ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 36 42 final class ProjectParser extends DefaultHandler { 43 44 private final EclipseProject project; 45 private ClassPath.Link currentLink; 46 47 private static final String JAVA_NATURE = "org.eclipse.jdt.core.javanature"; 49 private static final String PROJECT_DESCRIPTION = "projectDescription"; private static final String LINKED_RESOURCES = "linkedResources"; private static final String LINK = "link"; private static final String NAME = "name"; private static final String TYPE = "type"; private static final String LOCATION = "location"; private static final String NATURES = "natures"; private static final String NATURE = "nature"; 59 private static final int POSITION_NONE = 0; 61 private static final int POSITION_PROJECT_DESCRIPTION = 1; 62 private static final int POSITION_PROJECT_NAME = 2; 63 private static final int POSITION_LINKED_RESOURCES = 3; 64 private static final int POSITION_LINK = 4; 65 private static final int POSITION_LINK_NAME = 5; 66 private static final int POSITION_LINK_TYPE = 6; 67 private static final int POSITION_LINK_LOCATION = 7; 68 private static final int POSITION_NATURES = 8; 69 private static final int POSITION_NATURE = 9; 70 private static final int POSITION_UNUSED = 1000; 71 72 private int position = POSITION_NONE; 73 private int unusedInner = 0; 74 private StringBuffer chars; 75 76 77 private ProjectParser(EclipseProject project) { 78 this.project = project; 79 } 80 81 static void parse(EclipseProject project) throws ProjectImporterException { 82 ProjectParser parser = new ProjectParser(project); 83 parser.load(); 84 } 85 86 87 private void load() throws ProjectImporterException { 88 InputStream projectIS = null; 89 try { 90 projectIS = new BufferedInputStream ( 91 new FileInputStream (project.getProjectFile())); 92 XMLReader reader = XMLUtil.createXMLReader(false, true); 93 reader.setContentHandler(this); 94 reader.setErrorHandler(this); 95 chars = new StringBuffer (); reader.parse(new InputSource (projectIS)); } catch (IOException e) { 98 throw new ProjectImporterException(e); 99 } catch (SAXException e) { 100 throw new ProjectImporterException(e); 101 } finally { 102 if (projectIS != null) { 103 try { 104 projectIS.close(); 105 } catch (IOException e) { 106 ErrorManager.getDefault().log(ErrorManager.WARNING, 107 "Unable to close projectInputStream: " + e); } 109 } 110 } 111 } 112 113 public void characters(char ch[], int offset, int length) throws SAXException { 114 chars.append(ch, offset, length); 115 } 116 117 public void startElement(String uri, String localName, 118 String qName, Attributes attributes) throws SAXException { 119 120 chars.setLength(0); 121 switch (position) { 122 case POSITION_NONE: 123 if (localName.equals(PROJECT_DESCRIPTION)) { 124 position = POSITION_PROJECT_DESCRIPTION; 125 } else { 126 throw (new SAXException ("First element has to be " + PROJECT_DESCRIPTION + ", but is " + localName)); } 129 break; 130 case POSITION_PROJECT_DESCRIPTION: 131 if (localName.equals(NAME)) { 132 position = POSITION_PROJECT_NAME; 133 } else if (localName.equals(LINKED_RESOURCES)) { 134 position = POSITION_LINKED_RESOURCES; 135 } else if (localName.equals(NATURES)) { 136 position = POSITION_NATURES; 137 } else { 138 position = POSITION_UNUSED; 139 unusedInner++; 140 } 141 break; 142 case POSITION_NATURES: 143 if (localName.equals(NATURE)) { 144 position = POSITION_NATURE; 145 } 146 break; 147 case POSITION_LINKED_RESOURCES: 148 if (localName.equals(LINK)) { 149 currentLink = new ClassPath.Link(); 150 position = POSITION_LINK; 151 } 152 break; 153 case POSITION_LINK: 154 if (localName.equals(NAME)) { 155 position = POSITION_LINK_NAME; 156 } else if (localName.equals(TYPE)) { 157 position = POSITION_LINK_TYPE; 158 } else if (localName.equals(LOCATION)) { 159 position = POSITION_LINK_LOCATION; 160 } 161 break; 162 default: 163 position = POSITION_UNUSED; 164 unusedInner++; 165 } 166 } 167 168 public void endElement(String uri, String localName, String qName) throws 169 SAXException { 170 switch (position) { 171 case POSITION_PROJECT_DESCRIPTION: 172 position = POSITION_NONE; 174 break; 175 case POSITION_PROJECT_NAME: 176 if (unusedInner == 0) { 177 if (localName.equals(NAME)) { 178 project.setName(chars.toString().trim()); 181 position = POSITION_PROJECT_DESCRIPTION; 182 } 183 break; 184 } 185 case POSITION_LINKED_RESOURCES: 186 case POSITION_NATURES: 187 position = POSITION_PROJECT_DESCRIPTION; 188 break; 189 case POSITION_NATURE: 190 if (localName.equals(NATURE)) { 191 String nature = chars.toString().trim(); 192 if (JAVA_NATURE.equals(nature)) { 193 project.setJavaNature(true); 194 } else { 195 project.addOtherNature(nature); 196 } 197 } 198 position = POSITION_NATURES; 199 break; 200 case POSITION_LINK: 201 processLink(localName); 202 break; 203 case POSITION_LINK_NAME: 204 processLinkName(localName); 205 break; 206 case POSITION_LINK_TYPE: 207 processLinkType(localName); 208 break; 209 case POSITION_LINK_LOCATION: 210 processLinkLocation(localName); 211 break; 212 case POSITION_UNUSED: 213 if (--unusedInner == 0) { 214 position = POSITION_PROJECT_DESCRIPTION; 215 } 216 break; 217 default: 218 ErrorManager.getDefault().log(ErrorManager.WARNING, 219 "Unknown state reached in ProjectParser, " + "position: " + position); } 222 chars.setLength(0); 223 } 224 225 public void warning(SAXParseException e) throws SAXException { 226 ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e); 227 } 228 229 public void error(SAXParseException e) throws SAXException { 230 ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e); 231 throw e; 232 } 233 234 public void fatalError(SAXParseException e) throws SAXException { 235 ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e); 236 throw e; 237 } 238 239 242 private void processLink(String elementName) throws SAXException { 243 if (elementName.equals(LINK)) { 244 position = POSITION_LINKED_RESOURCES; 245 String name = currentLink.getName(); 247 int type = currentLink.getType(); 248 String location = currentLink.getLocation(); 249 try { 250 if ((name == null) || name.length() == 0) { 251 throw new SAXException ( 252 "Link's name cannot be empty"); } 254 if (type == ClassPath.Link.TYPE_INVALID) { 255 throw new SAXException ( 256 "Link's type cannot be equal to " + type); } 258 if ((location == null) || location.length() == 0) { 259 throw new SAXException ( 260 "Link's location cannot be empty"); } 262 project.addLink(currentLink); 263 } finally { 264 currentLink = null; 265 } 266 } 267 } 268 269 270 private void processLinkLocation(String elementName) throws SAXException { 271 if (elementName.equals(LOCATION)) { 272 String location = chars.toString().trim(); 273 if (currentLink.getLocation() != null) { 274 throw new SAXException ( 275 "Link's location was already set. There can be only " + "one location element inside of link element"); } 278 currentLink.setLocation(location); 279 position = POSITION_LINK; 280 } 281 } 282 283 284 private void processLinkName(String elementName) throws SAXException { 285 if (elementName.equals(NAME)) { 286 String name = chars.toString().trim(); 287 if (currentLink.getName() != null) { 288 throw new SAXException ( 289 "Link's name was already set. There can be only " + "one name element inside of link element"); } 292 currentLink.setName(name); 293 position = POSITION_LINK; 294 } 295 } 296 297 298 private void processLinkType(String elementName) throws SAXException { 299 if (elementName.equals(TYPE)) { 300 if (currentLink.getType() != ClassPath.Link.TYPE_INVALID) { 302 throw new SAXException ( 303 "Link's type was already set. There can be only " + "one type element inside of link element"); } 306 try { 307 currentLink.setType(Integer.parseInt(chars.toString().trim())); 308 position = POSITION_LINK; 309 } catch (NumberFormatException e) { 310 throw new SAXException ("Link's type has to be a " + "number but is: " + chars.toString().trim()); } 313 } 314 } 315 316 } 317 | Popular Tags |