001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.projectimport.eclipse;
043:
044: import java.io.IOException;
045: import java.io.StringReader;
046: import java.util.Collection;
047: import java.util.HashSet;
048: import org.openide.ErrorManager;
049: import org.openide.xml.XMLUtil;
050: import org.xml.sax.Attributes;
051: import org.xml.sax.InputSource;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.SAXParseException;
054: import org.xml.sax.XMLReader;
055: import org.xml.sax.helpers.DefaultHandler;
056: import org.netbeans.modules.projectimport.ProjectImporterException;
057:
058: /**
059: * Parses user library xml document.
060: *
061: * @author mkrauskopf
062: */
063: final class UserLibraryParser extends DefaultHandler {
064:
065: // elements names
066: private static final String USER_LIBRARY = "userlibrary"; // NOI18N
067: private static final String ARCHIVE = "archive"; // NOI18N
068: private static final String ATTRIBUTES = "attributes"; // NOI18N
069: private static final String ATTRIBUTE = "attribute"; // NOI18N
070:
071: // attributes names
072: private static final String PATH_ATTR = "path"; // NOI18N
073:
074: // indicates current position in a xml document
075: private static final int POSITION_NONE = 0;
076: private static final int POSITION_USER_LIBRARY = 1;
077: private static final int POSITION_ARCHIVE = 2;
078: private static final int POSITION_ATTRIBUTES = 3;
079: private static final int POSITION_ATTRIBUTE = 4;
080:
081: private int position = POSITION_NONE;
082: private StringBuffer chars;
083:
084: private Collection jars;
085:
086: private UserLibraryParser() {/* emtpy constructor */
087: }
088:
089: /** Returns jars contained in the given user library. */
090: static Collection getJars(String xmlDoc)
091: throws ProjectImporterException {
092: UserLibraryParser parser = new UserLibraryParser();
093: parser.load(new InputSource(new StringReader(xmlDoc)));
094: return parser.jars;
095: }
096:
097: /** Parses a given InputSource and fills up jars collection */
098: private void load(InputSource projectIS)
099: throws ProjectImporterException {
100: try {
101: /* parser creation */
102: XMLReader reader = XMLUtil.createXMLReader(false, true);
103: reader.setContentHandler(this );
104: reader.setErrorHandler(this );
105: chars = new StringBuffer(); // initialization
106: reader.parse(projectIS); // start parsing
107: } catch (IOException e) {
108: throw new ProjectImporterException(e);
109: } catch (SAXException e) {
110: throw new ProjectImporterException(e);
111: }
112: }
113:
114: public void characters(char ch[], int offset, int length)
115: throws SAXException {
116: chars.append(ch, offset, length);
117: }
118:
119: public void startElement(String uri, String localName,
120: String qName, Attributes attributes) throws SAXException {
121:
122: chars.setLength(0);
123: switch (position) {
124: case POSITION_NONE:
125: if (localName.equals(USER_LIBRARY)) {
126: position = POSITION_USER_LIBRARY;
127: jars = new HashSet();
128: } else {
129: throw (new SAXException("First element has to be " // NOI18N
130: + USER_LIBRARY + ", but is " + localName)); // NOI18N
131: }
132: break;
133: case POSITION_USER_LIBRARY:
134: if (localName.equals(ARCHIVE)) {
135: jars.add(attributes.getValue(PATH_ATTR));
136: position = POSITION_ARCHIVE;
137: }
138: break;
139: case POSITION_ARCHIVE:
140: if (localName.equals(ATTRIBUTES)) {
141: // ignored in the meantime - prepared for future (see #75112)
142: position = POSITION_ATTRIBUTES;
143: }
144: break;
145: case POSITION_ATTRIBUTES:
146: if (localName.equals(ATTRIBUTE)) {
147: // ignored in the meantime - prepared for future (see #75112)
148: position = POSITION_ATTRIBUTE;
149: }
150: break;
151: default:
152: throw (new SAXException("Unknown element reached: " // NOI18N
153: + localName));
154: }
155: }
156:
157: public void endElement(String uri, String localName, String qName)
158: throws SAXException {
159: switch (position) {
160: case POSITION_USER_LIBRARY:
161: // parsing ends
162: position = POSITION_NONE;
163: break;
164: case POSITION_ARCHIVE:
165: position = POSITION_USER_LIBRARY;
166: break;
167: case POSITION_ATTRIBUTES:
168: position = POSITION_ARCHIVE;
169: break;
170: case POSITION_ATTRIBUTE:
171: position = POSITION_ATTRIBUTES;
172: break;
173: default:
174: ErrorManager.getDefault().log(ErrorManager.WARNING,
175: "Unknown state reached in UserLibraryParser, " + // NOI18N
176: "position: " + position); // NOI18N
177: }
178: chars.setLength(0);
179: }
180:
181: public void warning(SAXParseException e) throws SAXException {
182: ErrorManager.getDefault().log(ErrorManager.WARNING,
183: "Warning occurred: " + e);
184: }
185:
186: public void error(SAXParseException e) throws SAXException {
187: ErrorManager.getDefault().log(ErrorManager.WARNING,
188: "Error occurres: " + e);
189: throw e;
190: }
191:
192: public void fatalError(SAXParseException e) throws SAXException {
193: ErrorManager.getDefault().log(ErrorManager.WARNING,
194: "Fatal error occurres: " + e);
195: throw e;
196: }
197: }
|