1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.logging.Logger ; 27 import org.netbeans.modules.projectimport.LoggerFactory; 28 import org.openide.ErrorManager; 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.helpers.DefaultHandler ; 34 import org.netbeans.modules.projectimport.ProjectImporterException; 35 import org.openide.xml.XMLUtil; 36 import org.xml.sax.XMLReader ; 37 38 43 final class PreferredVMParser extends DefaultHandler { 44 45 46 private static final Logger logger = 47 LoggerFactory.getDefault().createLogger(PreferredVMParser.class); 48 49 private static final String VM_SETTINGS = "vmSettings"; private static final String VM_TYPE = "vmType"; private static final String VM = "vm"; private static final String LIBRARY_LOCATIONS = "libraryLocations"; private static final String LIBRARY_LOCATION = "libraryLocation"; 56 private static final String DEFAULT_VM_ATTR = "defaultVM"; private static final String ID_ATTR = "id"; private static final String NAME_ATTR = "name"; private static final String PATH_ATTR = "path"; 62 private static final int POSITION_NONE = 0; 64 private static final int POSITION_VM_SETTINGS = 1; 65 private static final int POSITION_VM_TYPE = 2; 66 private static final int POSITION_VM = 3; 67 private static final int POSITION_LIBRARY_LOCATIONS = 4; 68 private static final int POSITION_LIBRARY_LOCATION = 5; 69 70 private int position = POSITION_NONE; 71 private StringBuffer chars; 72 private String defaultId; 73 74 private Map jdks; 75 76 private PreferredVMParser() {} 77 78 79 static Map parse(String vmXML) throws ProjectImporterException { 80 PreferredVMParser parser = new PreferredVMParser(); 81 parser.load(new InputSource (new StringReader (vmXML))); 82 return parser.jdks; 83 } 84 85 86 private void load(InputSource vmXMLIS) throws ProjectImporterException{ 87 try { 88 XMLReader reader = XMLUtil.createXMLReader(false, true); 89 reader.setContentHandler(this); 90 reader.setErrorHandler(this); 91 chars = new StringBuffer (); reader.parse(vmXMLIS); } catch (IOException e) { 94 throw new ProjectImporterException(e); 95 } catch (SAXException e) { 96 throw new ProjectImporterException(e); 97 } 98 } 99 100 public void characters(char ch[], int offset, int length) throws SAXException { 101 chars.append(ch, offset, length); 102 } 103 104 public void startElement(String uri, String localName, 105 String qName, Attributes attributes) throws SAXException { 106 107 chars.setLength(0); 108 switch (position) { 109 case POSITION_NONE: 110 if (localName.equals(VM_SETTINGS)) { 111 position = POSITION_VM_SETTINGS; 112 String defaultVMAttr = attributes.getValue(DEFAULT_VM_ATTR); 114 defaultId = defaultVMAttr.substring(defaultVMAttr.lastIndexOf(',') + 1); 115 jdks = new HashMap (); 116 } else { 117 throw (new SAXException ("First element has to be " + VM_SETTINGS + ", but is " + localName)); } 120 break; 121 case POSITION_VM_SETTINGS: 122 if (localName.equals(VM_TYPE)) { 123 position = POSITION_VM_TYPE; 124 } 125 break; 126 case POSITION_VM_TYPE: 127 if (localName.equals(VM)) { 128 position = POSITION_VM; 129 addJDK(attributes.getValue(ID_ATTR), 130 attributes.getValue(NAME_ATTR), 131 attributes.getValue(PATH_ATTR)); 132 } 133 break; 134 case POSITION_VM: 135 if (localName.equals(LIBRARY_LOCATIONS)) { 136 position = POSITION_LIBRARY_LOCATIONS; 137 logger.info("JRE used by your project presuambly contains additional jars. This is not supported (imported) yet. " + "CC yourself to issue http://www.netbeans.org/issues/show_bug.cgi?id=70733 to watch a progress."); } 146 break; 147 case POSITION_LIBRARY_LOCATIONS: 148 if (localName.equals(LIBRARY_LOCATION)) { 149 position = POSITION_LIBRARY_LOCATION; 150 } 152 break; 153 default: 154 throw (new SAXException ("Unknown position reached: " + position + " (element: " + localName + ")")); } 157 } 158 159 public void endElement(String uri, String localName, String qName) throws 162 SAXException { 163 switch (position) { 164 case POSITION_VM_SETTINGS: 165 position = POSITION_NONE; 167 break; 168 case POSITION_VM_TYPE: 169 position = POSITION_VM_SETTINGS; 170 break; 171 case POSITION_VM: 172 position = POSITION_VM_TYPE; 173 break; 174 case POSITION_LIBRARY_LOCATIONS: 175 position = POSITION_VM; 176 break; 177 case POSITION_LIBRARY_LOCATION: 178 position = POSITION_LIBRARY_LOCATIONS; 179 break; 180 default: 181 ErrorManager.getDefault().log(ErrorManager.WARNING, 182 "Unknown state reached in ClassPathParser, " + "position: " + position); } 185 chars.setLength(0); 186 } 187 188 public void error(SAXParseException e) throws SAXException { 189 ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e); throw e; 191 } 192 193 public void fatalError(SAXParseException e) throws SAXException { 194 ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e); throw e; 196 } 197 198 private void addJDK(String id, String name, String value) { 199 if (id.equals(defaultId)) { 200 jdks.put(Workspace.DEFAULT_JRE_CONTAINER, value); 204 } 205 jdks.put(name, value); 206 } 207 } 208 | Popular Tags |