1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import org.openide.ErrorManager; 27 import org.openide.xml.XMLUtil; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.SAXParseException ; 32 import org.xml.sax.XMLReader ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 import org.netbeans.modules.projectimport.ProjectImporterException; 35 36 41 final class UserLibraryParser extends DefaultHandler { 42 43 private static final String USER_LIBRARY = "userlibrary"; private static final String ARCHIVE = "archive"; private static final String ATTRIBUTES = "attributes"; private static final String ATTRIBUTE = "attribute"; 49 private static final String PATH_ATTR = "path"; 52 private static final int POSITION_NONE = 0; 54 private static final int POSITION_USER_LIBRARY = 1; 55 private static final int POSITION_ARCHIVE = 2; 56 private static final int POSITION_ATTRIBUTES = 3; 57 private static final int POSITION_ATTRIBUTE = 4; 58 59 private int position = POSITION_NONE; 60 private StringBuffer chars; 61 62 private Collection jars; 63 64 private UserLibraryParser() {} 65 66 67 static Collection getJars(String xmlDoc) throws ProjectImporterException { 68 UserLibraryParser parser = new UserLibraryParser(); 69 parser.load(new InputSource (new StringReader (xmlDoc))); 70 return parser.jars; 71 } 72 73 74 private void load(InputSource projectIS) throws ProjectImporterException{ 75 try { 76 77 XMLReader reader = XMLUtil.createXMLReader(false, true); 78 reader.setContentHandler(this); 79 reader.setErrorHandler(this); 80 chars = new StringBuffer (); reader.parse(projectIS); } catch (IOException e) { 83 throw new ProjectImporterException(e); 84 } catch (SAXException e) { 85 throw new ProjectImporterException(e); 86 } 87 } 88 89 public void characters(char ch[], int offset, int length) throws SAXException { 90 chars.append(ch, offset, length); 91 } 92 93 public void startElement(String uri, String localName, 94 String qName, Attributes attributes) throws SAXException { 95 96 chars.setLength(0); 97 switch (position) { 98 case POSITION_NONE: 99 if (localName.equals(USER_LIBRARY)) { 100 position = POSITION_USER_LIBRARY; 101 jars = new HashSet (); 102 } else { 103 throw (new SAXException ("First element has to be " + USER_LIBRARY + ", but is " + localName)); } 106 break; 107 case POSITION_USER_LIBRARY: 108 if (localName.equals(ARCHIVE)) { 109 jars.add(attributes.getValue(PATH_ATTR)); 110 position = POSITION_ARCHIVE; 111 } 112 break; 113 case POSITION_ARCHIVE: 114 if (localName.equals(ATTRIBUTES)) { 115 position = POSITION_ATTRIBUTES; 117 } 118 break; 119 case POSITION_ATTRIBUTES: 120 if (localName.equals(ATTRIBUTE)) { 121 position = POSITION_ATTRIBUTE; 123 } 124 break; 125 default: 126 throw (new SAXException ("Unknown element reached: " + localName)); 128 } 129 } 130 131 public void endElement(String uri, String localName, String qName) throws 132 SAXException { 133 switch (position) { 134 case POSITION_USER_LIBRARY: 135 position = POSITION_NONE; 137 break; 138 case POSITION_ARCHIVE: 139 position = POSITION_USER_LIBRARY; 140 break; 141 case POSITION_ATTRIBUTES: 142 position = POSITION_ARCHIVE; 143 break; 144 case POSITION_ATTRIBUTE: 145 position = POSITION_ATTRIBUTES; 146 break; 147 default: 148 ErrorManager.getDefault().log(ErrorManager.WARNING, 149 "Unknown state reached in UserLibraryParser, " + "position: " + position); } 152 chars.setLength(0); 153 } 154 155 public void warning(SAXParseException e) throws SAXException { 156 ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e); 157 } 158 159 public void error(SAXParseException e) throws SAXException { 160 ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e); 161 throw e; 162 } 163 164 public void fatalError(SAXParseException e) throws SAXException { 165 ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e); 166 throw e; 167 } 168 } 169 | Popular Tags |