2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.project.libraries;
45
import java.util.ArrayList;
46
import java.util.HashMap;
47
import java.util.Iterator;
48
import java.util.List;
50
import org.netbeans.spi.project.libraries.LibraryImplementation;
51
import org.netbeans.spi.project.libraries.LibraryTypeProvider;
52
import org.openide.ErrorManager;
53
import org.xml.sax.Attributes;
54
import org.xml.sax.SAXException;
55
import org.xml.sax.SAXParseException;
58
* Read content of library declaration XML document.
62
public class LibraryDeclarationHandlerImpl implements LibraryDeclarationHandler {
65
private LibraryImplementation library;
67
private String libraryType;
69
private String libraryDescription;
71
private String libraryName;
73
private String localizingBundle;
75
private Map<String,List<URL>> contentTypes = new HashMap<String,List<URL>>();
78
private List<URL> cpEntries;
81
private String contentType;
84
private boolean inVolume = false;
86
public static final boolean DEBUG = false;
91
public LibraryDeclarationHandlerImpl() {
94
public void start_volume(final Attributes meta) throws SAXException {
95
cpEntries = new ArrayList<URL>();
99
public void end_volume() throws SAXException {
100
contentTypes.put (contentType, cpEntries);
101
this.inVolume = false;
102
this.contentType = null;
105
public void handle_type(final String data, final Attributes meta) throws SAXException {
106
if (data == null || data.length () == 0) {
107
throw new SAXException ("Empty value of type element"); //NOI18N
110
this.contentType = data;
113
this.libraryType = data;
117
public void start_library(final Attributes meta) throws SAXException {
118
if ("1.0".equals(meta.getValue("version")) == false) { // NOI18N
119
throw new SAXException("Invalid librray descriptor version"); // NOI18N
123
public void end_library() throws SAXException {
125
if (this.library != null) {
126
if (this.libraryType == null || !this.libraryType.equals(this.library.getType())) {
127
throw new SAXParseException("Changing library type of library: "+this.libraryName+" from: "+
128
library.getType()+" to: " + libraryType, null); //NOI18N
133
LibraryTypeProvider provider = LibraryTypeRegistry.getDefault().getLibraryTypeProvider(this.libraryType);
134
if (provider == null) {
135
ErrorManager.getDefault().log (ErrorManager.WARNING, "LibraryDeclarationHandlerImpl: Cannot create library: "+this.libraryName+" of unknown type: " + this.libraryType);
138
this.library = provider.createLibrary();
141
if (!update || !safeEquals(this.library.getLocalizingBundle(), localizingBundle)) {
142
this.library.setLocalizingBundle (this.localizingBundle);
144
if (!update || !safeEquals(this.library.getName(), libraryName)) {
145
this.library.setName (this.libraryName);
147
if (!update || !safeEquals(this.library.getDescription(), libraryDescription)) {
148
this.library.setDescription (this.libraryDescription);
150
for (Map.Entry<String,List<URL>> entry : contentTypes.entrySet()) {
151
String contentType = entry.getKey();
152
List<URL> cp = entry.getValue();
154
if (!update || !safeEquals (this.library.getContent(contentType),cp)) {
155
this.library.setContent(contentType, cp);
157
} catch (IllegalArgumentException e) {
158
throw (SAXException) new SAXException(e.toString()).initCause(e);
161
this.libraryName = null;
162
this.libraryDescription = null;
163
this.libraryType = null;
164
this.localizingBundle = null;
165
this.contentTypes.clear ();
168
public void handle_resource(URL data, final Attributes meta) throws SAXException {
174
public void handle_name(final String data, final Attributes meta) throws SAXException {
175
this.libraryName = data;
178
public void handle_description (final String data, final Attributes meta) throws SAXException {
179
libraryDescription = data;
182
public void handle_localizingBundle (final String data, final Attributes meta) throws SAXException {
183
this.localizingBundle = data;
186
public void setLibrary (LibraryImplementation library) {
187
this.library = library;
190
public LibraryImplementation getLibrary () {
191
LibraryImplementation lib = this.library;
197
private static boolean safeEquals (Object o1, Object o2) {
198
return o1 == null ? o2 == null : o1.equals (o2);