KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > eclipse > PreferredVMParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.eclipse;
21
22 import java.io.IOException JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import org.netbeans.modules.projectimport.LoggerFactory;
28 import org.openide.ErrorManager;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34 import org.netbeans.modules.projectimport.ProjectImporterException;
35 import org.openide.xml.XMLUtil;
36 import org.xml.sax.XMLReader JavaDoc;
37
38 /**
39  * Parses default JRE containers from Eclipse workspace.
40  *
41  * @author mkrauskopf
42  */

43 final class PreferredVMParser extends DefaultHandler JavaDoc {
44     
45     /** Logger for this class. */
46     private static final Logger JavaDoc logger =
47             LoggerFactory.getDefault().createLogger(PreferredVMParser.class);
48     
49     // elements names
50
private static final String JavaDoc VM_SETTINGS = "vmSettings"; // NOI18N
51
private static final String JavaDoc VM_TYPE = "vmType"; // NOI18N
52
private static final String JavaDoc VM = "vm"; // NOI18N
53
private static final String JavaDoc LIBRARY_LOCATIONS = "libraryLocations"; // NOI18N
54
private static final String JavaDoc LIBRARY_LOCATION = "libraryLocation"; // NOI18N
55

56     // attributes names
57
private static final String JavaDoc DEFAULT_VM_ATTR = "defaultVM"; // NOI18N
58
private static final String JavaDoc ID_ATTR = "id"; // NOI18N
59
private static final String JavaDoc NAME_ATTR = "name"; // NOI18N
60
private static final String JavaDoc PATH_ATTR = "path"; // NOI18N
61

62     // indicates current position in a xml document
63
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 JavaDoc chars;
72     private String JavaDoc defaultId;
73     
74     private Map JavaDoc jdks;
75     
76     private PreferredVMParser() {/* emtpy constructor */}
77     
78     /** Returns vmMap of JDKs */
79     static Map JavaDoc parse(String JavaDoc vmXML) throws ProjectImporterException {
80         PreferredVMParser parser = new PreferredVMParser();
81         parser.load(new InputSource JavaDoc(new StringReader JavaDoc(vmXML)));
82         return parser.jdks;
83     }
84     
85     /** Parses a given InputSource and fills up jdk vmMap */
86     private void load(InputSource JavaDoc vmXMLIS) throws ProjectImporterException{
87         try {
88             XMLReader JavaDoc reader = XMLUtil.createXMLReader(false, true);
89             reader.setContentHandler(this);
90             reader.setErrorHandler(this);
91             chars = new StringBuffer JavaDoc(); // initialization
92
reader.parse(vmXMLIS); // start parsing
93
} catch (IOException JavaDoc e) {
94             throw new ProjectImporterException(e);
95         } catch (SAXException JavaDoc e) {
96             throw new ProjectImporterException(e);
97         }
98     }
99     
100     public void characters(char ch[], int offset, int length) throws SAXException JavaDoc {
101         chars.append(ch, offset, length);
102     }
103     
104     public void startElement(String JavaDoc uri, String JavaDoc localName,
105             String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
106         
107         chars.setLength(0);
108         switch (position) {
109             case POSITION_NONE:
110                 if (localName.equals(VM_SETTINGS)) {
111                     position = POSITION_VM_SETTINGS;
112                     // default vm id seems to be after the last comma
113
String JavaDoc defaultVMAttr = attributes.getValue(DEFAULT_VM_ATTR);
114                     defaultId = defaultVMAttr.substring(defaultVMAttr.lastIndexOf(',') + 1);
115                     jdks = new HashMap JavaDoc();
116                 } else {
117                     throw (new SAXException JavaDoc("First element has to be " // NOI18N
118
+ VM_SETTINGS + ", but is " + localName)); // NOI18N
119
}
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. " + // NOI18N
138
"CC yourself to issue http://www.netbeans.org/issues/show_bug.cgi?id=70733 to watch a progress."); // NOI18N
139
// XXX this means that additional jars were added to the
140
// JDK used by a project.
141
// See Preferences --> Java --> Installed JREs --> Choose
142
// JDK --> Edit --> Uncheck "Use default system libraries"
143
// --> Add External Jar
144
// Than take a look at .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs
145
}
146                 break;
147             case POSITION_LIBRARY_LOCATIONS:
148                 if (localName.equals(LIBRARY_LOCATION)) {
149                     position = POSITION_LIBRARY_LOCATION;
150                     // XXX See comment above - "case POSITION_VM"
151
}
152                 break;
153             default:
154                 throw (new SAXException JavaDoc("Unknown position reached: " // NOI18N
155
+ position + " (element: " + localName + ")")); // NOI18N
156
}
157     }
158     
159     // XXX use array[x] array[x-1] or 1.5 enumerations(?) here and for similar
160
// cases or consider DOM
161
public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws
162             SAXException JavaDoc {
163         switch (position) {
164             case POSITION_VM_SETTINGS:
165                 // parsing ends
166
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, " + // NOI18N
183
"position: " + position); // NOI18N
184
}
185         chars.setLength(0);
186     }
187     
188     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
189         ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e); // NOI18N
190
throw e;
191     }
192     
193     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
194         ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e); // NOI18N
195
throw e;
196     }
197     
198     private void addJDK(String JavaDoc id, String JavaDoc name, String JavaDoc value) {
199         if (id.equals(defaultId)) {
200             // put the default twice under two names. It seems that under some
201
// circumstances there is full name in .classpath con entry even
202
// if the currently used container is default one.
203
jdks.put(Workspace.DEFAULT_JRE_CONTAINER, value);
204         }
205         jdks.put(name, value);
206     }
207 }
208
Popular Tags