KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import org.netbeans.modules.projectimport.ProjectImporterException;
27 import org.openide.ErrorManager;
28 import org.openide.xml.XMLUtil;
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.XMLReader JavaDoc;
34 import org.xml.sax.helpers.DefaultHandler JavaDoc;
35
36 /**
37  * Parses given project's .project file and fills up the project with found
38  * data.
39  *
40  * @author mkrauskopf
41  */

42 final class ProjectParser extends DefaultHandler JavaDoc {
43     
44     private final EclipseProject project;
45     private ClassPath.Link currentLink;
46     
47     private static final String JavaDoc JAVA_NATURE = "org.eclipse.jdt.core.javanature"; // NOI18N
48

49     // elements names
50
private static final String JavaDoc PROJECT_DESCRIPTION = "projectDescription"; // NOI18N
51
private static final String JavaDoc LINKED_RESOURCES = "linkedResources"; // NOI18N
52
private static final String JavaDoc LINK = "link"; // NOI18N
53
private static final String JavaDoc NAME = "name"; // NOI18N
54
private static final String JavaDoc TYPE = "type"; // NOI18N
55
private static final String JavaDoc LOCATION = "location"; // NOI18N
56
private static final String JavaDoc NATURES = "natures"; // NOI18N
57
private static final String JavaDoc NATURE = "nature"; // NOI18N
58

59     // indicates current position in a xml document
60
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 JavaDoc chars;
75     
76     /** Creates a new instance of ProjectParser */
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     /** Parses a given InputSource and fills up a EclipseProject */
87     private void load() throws ProjectImporterException {
88         InputStream JavaDoc projectIS = null;
89         try {
90             projectIS = new BufferedInputStream JavaDoc(
91                     new FileInputStream JavaDoc(project.getProjectFile()));
92             XMLReader JavaDoc reader = XMLUtil.createXMLReader(false, true);
93             reader.setContentHandler(this);
94             reader.setErrorHandler(this);
95             chars = new StringBuffer JavaDoc(); // initialization
96
reader.parse(new InputSource JavaDoc(projectIS)); // start parsing
97
} catch (IOException JavaDoc e) {
98             throw new ProjectImporterException(e);
99         } catch (SAXException JavaDoc e) {
100             throw new ProjectImporterException(e);
101         } finally {
102             if (projectIS != null) {
103                 try {
104                     projectIS.close();
105                 } catch (IOException JavaDoc e) {
106                     ErrorManager.getDefault().log(ErrorManager.WARNING,
107                             "Unable to close projectInputStream: " + e); // NOI18N
108
}
109             }
110         }
111     }
112     
113     public void characters(char ch[], int offset, int length) throws SAXException JavaDoc {
114         chars.append(ch, offset, length);
115     }
116     
117     public void startElement(String JavaDoc uri, String JavaDoc localName,
118             String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
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 JavaDoc("First element has to be " // NOI18N
127
+ PROJECT_DESCRIPTION + ", but is " + localName)); // NOI18N
128
}
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws
169             SAXException JavaDoc {
170         switch (position) {
171             case POSITION_PROJECT_DESCRIPTION:
172                 // parsing ends
173
position = POSITION_NONE;
174                 break;
175             case POSITION_PROJECT_NAME:
176                 if (unusedInner == 0) {
177                     if (localName.equals(NAME)) {
178                         // Project names cannot have leading/trailing whitespace
179
// as they are IResource names.
180
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 JavaDoc 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, " + // NOI18N
220
"position: " + position); // NOI18N
221
}
222         chars.setLength(0);
223     }
224     
225     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
226         ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e);
227     }
228     
229     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
230         ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e);
231         throw e;
232     }
233     
234     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
235         ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e);
236         throw e;
237     }
238     
239     /**
240      * If a link is OK appends it to an <code>EclipseProject</code> links.
241      */

242     private void processLink(String JavaDoc elementName) throws SAXException JavaDoc {
243         if (elementName.equals(LINK)) {
244             position = POSITION_LINKED_RESOURCES;
245             // Make sure that you have something reasonable
246
String JavaDoc name = currentLink.getName();
247             int type = currentLink.getType();
248             String JavaDoc location = currentLink.getLocation();
249             try {
250                 if ((name == null) || name.length() == 0) {
251                     throw new SAXException JavaDoc(
252                             "Link's name cannot be empty"); //NOI18N
253
}
254                 if (type == ClassPath.Link.TYPE_INVALID) {
255                     throw new SAXException JavaDoc(
256                             "Link's type cannot be equal to " + type); //NOI18N
257
}
258                 if ((location == null) || location.length() == 0) {
259                     throw new SAXException JavaDoc(
260                             "Link's location cannot be empty"); //NOI18N
261
}
262                 project.addLink(currentLink);
263             } finally {
264                 currentLink = null;
265             }
266         }
267     }
268     
269     /** Sets location for currently processed link. */
270     private void processLinkLocation(String JavaDoc elementName) throws SAXException JavaDoc {
271         if (elementName.equals(LOCATION)) {
272             String JavaDoc location = chars.toString().trim();
273             if (currentLink.getLocation() != null) {
274                 throw new SAXException JavaDoc(
275                         "Link's location was already set. There can be only " + //NOI18N
276
"one location element inside of link element"); //NOI18N
277
}
278             currentLink.setLocation(location);
279             position = POSITION_LINK;
280         }
281     }
282     
283     /** Sets name for currently processed link. */
284     private void processLinkName(String JavaDoc elementName) throws SAXException JavaDoc {
285         if (elementName.equals(NAME)) {
286             String JavaDoc name = chars.toString().trim();
287             if (currentLink.getName() != null) {
288                 throw new SAXException JavaDoc(
289                         "Link's name was already set. There can be only " + //NOI18N
290
"one name element inside of link element"); //NOI18N
291
}
292             currentLink.setName(name);
293             position = POSITION_LINK;
294         }
295     }
296     
297     /** Sets type for currently processed link. */
298     private void processLinkType(String JavaDoc elementName) throws SAXException JavaDoc {
299         if (elementName.equals(TYPE)) {
300             // make sure that type wasn't set yet
301
if (currentLink.getType() != ClassPath.Link.TYPE_INVALID) {
302                 throw new SAXException JavaDoc(
303                         "Link's type was already set. There can be only " + //NOI18N
304
"one type element inside of link element"); //NOI18N
305
}
306             try {
307                 currentLink.setType(Integer.parseInt(chars.toString().trim()));
308                 position = POSITION_LINK;
309             } catch (NumberFormatException JavaDoc e) {
310                 throw new SAXException JavaDoc("Link's type has to be a " + //NOI18N
311
"number but is: " + chars.toString().trim()); //NOI18N
312
}
313         }
314     }
315     
316 }
317
Popular Tags