KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.xml.XMLUtil;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32 import org.xml.sax.XMLReader JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34 import org.netbeans.modules.projectimport.ProjectImporterException;
35
36 /**
37  * Parses user library xml document.
38  *
39  * @author mkrauskopf
40  */

41 final class UserLibraryParser extends DefaultHandler JavaDoc {
42     
43     // elements names
44
private static final String JavaDoc USER_LIBRARY = "userlibrary"; // NOI18N
45
private static final String JavaDoc ARCHIVE = "archive"; // NOI18N
46
private static final String JavaDoc ATTRIBUTES = "attributes"; // NOI18N
47
private static final String JavaDoc ATTRIBUTE = "attribute"; // NOI18N
48

49     // attributes names
50
private static final String JavaDoc PATH_ATTR = "path"; // NOI18N
51

52     // indicates current position in a xml document
53
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 JavaDoc chars;
61     
62     private Collection JavaDoc jars;
63     
64     private UserLibraryParser() {/* emtpy constructor */}
65     
66     /** Returns jars contained in the given user library. */
67     static Collection JavaDoc getJars(String JavaDoc xmlDoc) throws ProjectImporterException {
68         UserLibraryParser parser = new UserLibraryParser();
69         parser.load(new InputSource JavaDoc(new StringReader JavaDoc(xmlDoc)));
70         return parser.jars;
71     }
72     
73     /** Parses a given InputSource and fills up jars collection */
74     private void load(InputSource JavaDoc projectIS) throws ProjectImporterException{
75         try {
76             /* parser creation */
77             XMLReader JavaDoc reader = XMLUtil.createXMLReader(false, true);
78             reader.setContentHandler(this);
79             reader.setErrorHandler(this);
80             chars = new StringBuffer JavaDoc(); // initialization
81
reader.parse(projectIS); // start parsing
82
} catch (IOException JavaDoc e) {
83             throw new ProjectImporterException(e);
84         } catch (SAXException JavaDoc e) {
85             throw new ProjectImporterException(e);
86         }
87     }
88     
89     public void characters(char ch[], int offset, int length) throws SAXException JavaDoc {
90         chars.append(ch, offset, length);
91     }
92     
93     public void startElement(String JavaDoc uri, String JavaDoc localName,
94             String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
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 JavaDoc();
102                 } else {
103                     throw (new SAXException JavaDoc("First element has to be " // NOI18N
104
+ USER_LIBRARY + ", but is " + localName)); // NOI18N
105
}
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                     // ignored in the meantime - prepared for future (see #75112)
116
position = POSITION_ATTRIBUTES;
117                 }
118                 break;
119             case POSITION_ATTRIBUTES:
120                 if (localName.equals(ATTRIBUTE)) {
121                     // ignored in the meantime - prepared for future (see #75112)
122
position = POSITION_ATTRIBUTE;
123                 }
124                 break;
125             default:
126                 throw (new SAXException JavaDoc("Unknown element reached: " // NOI18N
127
+ localName));
128         }
129     }
130     
131     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws
132             SAXException JavaDoc {
133         switch (position) {
134             case POSITION_USER_LIBRARY:
135                 // parsing ends
136
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, " + // NOI18N
150
"position: " + position); // NOI18N
151
}
152         chars.setLength(0);
153     }
154     
155     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
156         ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning occurred: " + e);
157     }
158     
159     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
160         ErrorManager.getDefault().log(ErrorManager.WARNING, "Error occurres: " + e);
161         throw e;
162     }
163     
164     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
165         ErrorManager.getDefault().log(ErrorManager.WARNING, "Fatal error occurres: " + e);
166         throw e;
167     }
168 }
169
Popular Tags