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.project.libraries;
043:
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import org.netbeans.spi.project.libraries.LibraryImplementation;
051: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
052: import org.openide.ErrorManager;
053: import org.xml.sax.Attributes;
054: import org.xml.sax.SAXException;
055: import org.xml.sax.SAXParseException;
056:
057: /**
058: * Read content of library declaration XML document.
059: *
060: * @author Petr Kuzel
061: */
062: public class LibraryDeclarationHandlerImpl implements
063: LibraryDeclarationHandler {
064:
065: private LibraryImplementation library;
066:
067: private String libraryType;
068:
069: private String libraryDescription;
070:
071: private String libraryName;
072:
073: private String localizingBundle;
074:
075: private Map<String, List<URL>> contentTypes = new HashMap<String, List<URL>>();
076:
077: // last volume
078: private List<URL> cpEntries;
079:
080: //last volume type
081: private String contentType;
082:
083: //parsing volume?
084: private boolean inVolume = false;
085:
086: public static final boolean DEBUG = false;
087:
088: /**
089: */
090: public LibraryDeclarationHandlerImpl() {
091: }
092:
093: public void start_volume(final Attributes meta) throws SAXException {
094: cpEntries = new ArrayList<URL>();
095: this .inVolume = true;
096: }
097:
098: public void end_volume() throws SAXException {
099: contentTypes.put(contentType, cpEntries);
100: this .inVolume = false;
101: this .contentType = null;
102: }
103:
104: public void handle_type(final String data, final Attributes meta)
105: throws SAXException {
106: if (data == null || data.length() == 0) {
107: throw new SAXException("Empty value of type element"); //NOI18N
108: }
109: if (this .inVolume) {
110: this .contentType = data;
111: } else {
112: this .libraryType = data;
113: }
114: }
115:
116: public void start_library(final Attributes meta)
117: throws SAXException {
118: if ("1.0".equals(meta.getValue("version")) == false) { // NOI18N
119: throw new SAXException("Invalid librray descriptor version"); // NOI18N
120: }
121: cleanUp();
122: }
123:
124: /**
125: * Sets preconditions
126: */
127: private void cleanUp() {
128: this .libraryName = null;
129: this .libraryDescription = null;
130: this .libraryType = null;
131: this .localizingBundle = null;
132: this .contentTypes.clear();
133: }
134:
135: public void end_library() throws SAXException {
136: boolean update;
137: if (this .library != null) {
138: if (this .libraryType == null
139: || !this .libraryType.equals(this .library.getType())) {
140: throw new SAXParseException(
141: "Changing library type of library: "
142: + this .libraryName + " from: "
143: + library.getType() + " to: "
144: + libraryType, null); //NOI18N
145: }
146: update = true;
147: } else {
148: LibraryTypeProvider provider = LibraryTypeRegistry
149: .getDefault().getLibraryTypeProvider(
150: this .libraryType);
151: if (provider == null) {
152: ErrorManager.getDefault().log(
153: ErrorManager.WARNING,
154: "LibraryDeclarationHandlerImpl: Cannot create library: "
155: + this .libraryName
156: + " of unknown type: "
157: + this .libraryType);
158: return;
159: }
160: this .library = provider.createLibrary();
161: update = false;
162: }
163: if (!update
164: || !safeEquals(this .library.getLocalizingBundle(),
165: localizingBundle)) {
166: this .library.setLocalizingBundle(this .localizingBundle);
167: }
168: if (!update || !safeEquals(this .library.getName(), libraryName)) {
169: this .library.setName(this .libraryName);
170: }
171: if (!update
172: || !safeEquals(this .library.getDescription(),
173: libraryDescription)) {
174: this .library.setDescription(this .libraryDescription);
175: }
176: for (Map.Entry<String, List<URL>> entry : contentTypes
177: .entrySet()) {
178: String contentType = entry.getKey();
179: List<URL> cp = entry.getValue();
180: try {
181: if (!update
182: || !safeEquals(this .library
183: .getContent(contentType), cp)) {
184: this .library.setContent(contentType, cp);
185: }
186: } catch (IllegalArgumentException e) {
187: throw (SAXException) new SAXException(e.toString())
188: .initCause(e);
189: }
190: }
191: }
192:
193: public void handle_resource(URL data, final Attributes meta)
194: throws SAXException {
195: if (data != null) {
196: cpEntries.add(data);
197: }
198: }
199:
200: public void handle_name(final String data, final Attributes meta)
201: throws SAXException {
202: this .libraryName = data;
203: }
204:
205: public void handle_description(final String data,
206: final Attributes meta) throws SAXException {
207: libraryDescription = data;
208: }
209:
210: public void handle_localizingBundle(final String data,
211: final Attributes meta) throws SAXException {
212: this .localizingBundle = data;
213: }
214:
215: public void setLibrary(LibraryImplementation library) {
216: this .library = library;
217: }
218:
219: public LibraryImplementation getLibrary() {
220: LibraryImplementation lib = this .library;
221: this .library = null;
222: return lib;
223: }
224:
225: private static boolean safeEquals(Object o1, Object o2) {
226: return o1 == null ? o2 == null : o1.equals(o2);
227: }
228:
229: }
|