KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 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.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import org.netbeans.modules.projectimport.ProjectImporterException;
30 import org.openide.ErrorManager;
31 import org.openide.xml.XMLUtil;
32 import org.xml.sax.Attributes JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39 /**
40  * Parses a content of the .classpath file.
41  *
42  * @author mkrauskopf
43  */

44 final class ClassPathParser extends DefaultHandler JavaDoc {
45     
46     // elements names
47
private static final String JavaDoc CLASSPATH = "classpath"; // NOI18N
48
private static final String JavaDoc CLASSPATH_ENTRY = "classpathentry"; // NOI18N
49
private static final String JavaDoc ATTRIBUTES = "attributes"; // NOI18N
50
private static final String JavaDoc ATTRIBUTE = "attribute"; // NOI18N
51
private static final String JavaDoc ACCESSRULES = "accessrules"; // NOI18N
52
private static final String JavaDoc ACCESSRULE = "accessrule"; // NOI18N
53

54     // attributes names
55
private static final String JavaDoc KIND_ATTR = "kind"; // NOI18N
56
private static final String JavaDoc PATH_ATTR = "path"; // NOI18N
57

58     // indicates current position in a xml document
59
private static final int POSITION_NONE = 0;
60     private static final int POSITION_CLASSPATH = 1;
61     private static final int POSITION_CLASSPATH_ENTRY = 2;
62     private static final int POSITION_ATTRIBUTES = 3;
63     private static final int POSITION_ATTRIBUTE = 4;
64     private static final int POSITION_ACCESSRULES = 5;
65     private static final int POSITION_ACCESSRULE = 6;
66     
67     private int position = POSITION_NONE;
68     private StringBuffer JavaDoc chars;
69     
70     private ClassPath classPath;
71     
72     private ClassPathParser() {/* emtpy constructor */}
73     
74     /**
75      * Returns classpath content from a project's .classpath file.
76      *
77      * @param classPathFile the .classpath file
78      */

79     static ClassPath parse(File JavaDoc classPathFile) throws ProjectImporterException {
80         
81         ClassPathParser parser = new ClassPathParser();
82         InputStream JavaDoc classPathIS = null;
83         try {
84             classPathIS = new BufferedInputStream JavaDoc(
85                     new FileInputStream JavaDoc(classPathFile));
86             parser.load(new InputSource JavaDoc(classPathIS));
87         } catch (FileNotFoundException JavaDoc e) {
88             throw new ProjectImporterException(e);
89         } finally {
90             if (classPathIS != null) {
91                 try {
92                     classPathIS.close();
93                 } catch (IOException JavaDoc e) {
94                     ErrorManager.getDefault().log(ErrorManager.WARNING,
95                             "Unable to close classPathStream: " + e); // NOI18N
96
}
97             }
98         }
99         return parser.classPath;
100     }
101     
102     /**
103      * Returns classpath content as it is represented in the project's
104      * .classpath file.
105      *
106      * @param classPath string as it is in the .classpath file
107      */

108     static ClassPath parse(String JavaDoc classPath) throws ProjectImporterException {
109         ClassPathParser parser = new ClassPathParser();
110         parser.load(new InputSource JavaDoc(new StringReader JavaDoc(classPath)));
111         return parser.classPath;
112     }
113     
114     /** Parses a given InputSource and fills up a EclipseProject */
115     private void load(InputSource JavaDoc projectIS) throws ProjectImporterException{
116         try {
117             /* parser creation */
118             XMLReader JavaDoc reader = XMLUtil.createXMLReader(false, true);
119             reader.setContentHandler(this);
120             reader.setErrorHandler(this);
121             chars = new StringBuffer JavaDoc(); // initialization
122
reader.parse(projectIS); // start parsing
123
} catch (IOException JavaDoc e) {
124             throw new ProjectImporterException(e);
125         } catch (SAXException JavaDoc e) {
126             throw new ProjectImporterException(e);
127         }
128     }
129     
130     public void characters(char ch[], int offset, int length) throws SAXException JavaDoc {
131         chars.append(ch, offset, length);
132     }
133     
134     public void startElement(String JavaDoc uri, String JavaDoc localName,
135             String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
136         
137         chars.setLength(0);
138         switch (position) {
139             case POSITION_NONE:
140                 if (localName.equals(CLASSPATH)) {
141                     position = POSITION_CLASSPATH;
142                     classPath = new ClassPath();
143                 } else {
144                     unknownElementReached(position, localName);
145                 }
146                 break;
147             case POSITION_CLASSPATH:
148                 if (localName.equals(CLASSPATH_ENTRY)) {
149                     ClassPathEntry entry = new ClassPathEntry(
150                             attributes.getValue(KIND_ATTR),
151                             attributes.getValue(PATH_ATTR));
152                     classPath.addEntry(entry);
153                     position = POSITION_CLASSPATH_ENTRY;
154                 } else {
155                     unknownElementReached(position, localName);
156                 }
157                 break;
158             case POSITION_CLASSPATH_ENTRY:
159                 if (localName.equals(ATTRIBUTES)) {
160                     // ignored in the meantime - prepared for future
161
// (probably since elicpse 3.1M6 - see #57661)
162
position = POSITION_ATTRIBUTES;
163                 } else if (localName.equals(ACCESSRULES)) {
164                     // ignored in the meantime - prepared for future
165
position = POSITION_ACCESSRULES;
166                 } else {
167                     unknownElementReached(position, localName);
168                 }
169                 break;
170             case POSITION_ATTRIBUTES:
171                 if (localName.equals(ATTRIBUTE)) {
172                     // ignored in the meantime - prepared for future
173
// (probably since elicpse 3.1M6 - see #57661)
174
position = POSITION_ATTRIBUTE;
175                 } else {
176                     unknownElementReached(position, localName);
177                 }
178                 break;
179             case POSITION_ACCESSRULES:
180                 if (localName.equals(ACCESSRULE)) {
181                     // ignored in the meantime - prepared for future
182
position = POSITION_ACCESSRULE;
183                 } else {
184                     unknownElementReached(position, localName);
185                 }
186                 break;
187             default:
188                 unknownElementReached(position, localName);
189         }
190     }
191     
192     private void unknownElementReached(int position, String JavaDoc localName) throws SAXException JavaDoc {
193         throw new SAXException JavaDoc("Unknown element reached: " + // NOI18N
194
"position: " + position + ", element: " + localName); // NOI18N
195
}
196     
197     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws
198             SAXException JavaDoc {
199         switch (position) {
200             case POSITION_CLASSPATH:
201                 // parsing ends
202
position = POSITION_NONE;
203                 break;
204             case POSITION_CLASSPATH_ENTRY:
205                 position = POSITION_CLASSPATH;
206                 break;
207             case POSITION_ATTRIBUTES:
208                 position = POSITION_CLASSPATH_ENTRY;
209                 break;
210             case POSITION_ATTRIBUTE:
211                 position = POSITION_ATTRIBUTES;
212                 break;
213             case POSITION_ACCESSRULES:
214                 position = POSITION_CLASSPATH_ENTRY;
215                 break;
216             case POSITION_ACCESSRULE:
217                 position = POSITION_ACCESSRULES;
218                 break;
219             default:
220                 ErrorManager.getDefault().log(ErrorManager.WARNING,
221                         "Unknown state reached in ClassPathParser, " + // NOI18N
222
"position: " + position + ", element: " + localName); // NOI18N
223
}
224         chars.setLength(0);
225     }
226     
227     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
228         ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e);
229     }
230     
231     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
232         ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurred: " + e);
233         throw e;
234     }
235     
236     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
237         ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurred: " + e);
238         throw e;
239     }
240     
241 }
242
Popular Tags