KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > LibraryDeclarationHandlerImpl


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.project.libraries;
21
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.netbeans.spi.project.libraries.LibraryImplementation;
29 import org.netbeans.spi.project.libraries.LibraryTypeProvider;
30 import org.openide.ErrorManager;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35 /**
36  * Read content of library declaration XML document.
37  *
38  * @author Petr Kuzel
39  */

40 public class LibraryDeclarationHandlerImpl implements LibraryDeclarationHandler {
41
42
43     private LibraryImplementation library;
44
45     private String JavaDoc libraryType;
46     
47     private String JavaDoc libraryDescription;
48     
49     private String JavaDoc libraryName;
50
51     private String JavaDoc localizingBundle;
52
53     private Map JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>> contentTypes = new HashMap JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>>();
54     
55     // last volume
56
private List JavaDoc<URL JavaDoc> cpEntries;
57     
58     //last volume type
59
private String JavaDoc contentType;
60     
61     //parsing volume?
62
private boolean inVolume = false;
63     
64     public static final boolean DEBUG = false;
65
66
67     /**
68      */

69     public LibraryDeclarationHandlerImpl() {
70     }
71     
72     public void start_volume(final Attributes JavaDoc meta) throws SAXException JavaDoc {
73         cpEntries = new ArrayList JavaDoc<URL JavaDoc>();
74         this.inVolume = true;
75     }
76     
77     public void end_volume() throws SAXException JavaDoc {
78         contentTypes.put (contentType, cpEntries);
79         this.inVolume = false;
80         this.contentType = null;
81     }
82     
83     public void handle_type(final String JavaDoc data, final Attributes JavaDoc meta) throws SAXException JavaDoc {
84         if (data == null || data.length () == 0) {
85             throw new SAXException JavaDoc ("Empty value of type element"); //NOI18N
86
}
87         if (this.inVolume) {
88             this.contentType = data;
89         }
90         else {
91             this.libraryType = data;
92         }
93     }
94         
95     public void start_library(final Attributes JavaDoc meta) throws SAXException JavaDoc {
96         if ("1.0".equals(meta.getValue("version")) == false) { // NOI18N
97
throw new SAXException JavaDoc("Invalid librray descriptor version"); // NOI18N
98
}
99     }
100     
101     public void end_library() throws SAXException JavaDoc {
102         boolean update;
103         if (this.library != null) {
104             if (this.libraryType == null || !this.libraryType.equals(this.library.getType())) {
105                 throw new SAXParseException JavaDoc("Changing library type of library: "+this.libraryName+" from: "+
106                         library.getType()+" to: " + libraryType, null); //NOI18N
107
}
108             update = true;
109         }
110         else {
111             LibraryTypeProvider provider = LibraryTypeRegistry.getDefault().getLibraryTypeProvider(this.libraryType);
112             if (provider == null) {
113                 ErrorManager.getDefault().log (ErrorManager.WARNING, "LibraryDeclarationHandlerImpl: Cannot create library: "+this.libraryName+" of unknown type: " + this.libraryType);
114                 return;
115             }
116             this.library = provider.createLibrary();
117             update = false;
118         }
119         if (!update || !safeEquals(this.library.getLocalizingBundle(), localizingBundle)) {
120             this.library.setLocalizingBundle (this.localizingBundle);
121         }
122         if (!update || !safeEquals(this.library.getName(), libraryName)) {
123             this.library.setName (this.libraryName);
124         }
125         if (!update || !safeEquals(this.library.getDescription(), libraryDescription)) {
126             this.library.setDescription (this.libraryDescription);
127         }
128         for (Map.Entry JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>> entry : contentTypes.entrySet()) {
129             String JavaDoc contentType = entry.getKey();
130             List JavaDoc<URL JavaDoc> cp = entry.getValue();
131             try {
132                 if (!update || !safeEquals (this.library.getContent(contentType),cp)) {
133                     this.library.setContent(contentType, cp);
134                 }
135             } catch (IllegalArgumentException JavaDoc e) {
136                 throw (SAXException JavaDoc) new SAXException JavaDoc(e.toString()).initCause(e);
137             }
138         }
139         this.libraryName = null;
140         this.libraryDescription = null;
141         this.libraryType = null;
142         this.localizingBundle = null;
143         this.contentTypes.clear ();
144     }
145
146     public void handle_resource(URL JavaDoc data, final Attributes JavaDoc meta) throws SAXException JavaDoc {
147         if (data != null) {
148             cpEntries.add(data);
149         }
150     }
151         
152     public void handle_name(final String JavaDoc data, final Attributes JavaDoc meta) throws SAXException JavaDoc {
153         this.libraryName = data;
154     }
155     
156     public void handle_description (final String JavaDoc data, final Attributes JavaDoc meta) throws SAXException JavaDoc {
157         libraryDescription = data;
158     }
159
160     public void handle_localizingBundle (final String JavaDoc data, final Attributes JavaDoc meta) throws SAXException JavaDoc {
161         this.localizingBundle = data;
162     }
163
164     public void setLibrary (LibraryImplementation library) {
165         this.library = library;
166     }
167
168     public LibraryImplementation getLibrary () {
169         LibraryImplementation lib = this.library;
170         this.library = null;
171         return lib;
172     }
173
174
175     private static boolean safeEquals (Object JavaDoc o1, Object JavaDoc o2) {
176         return o1 == null ? o2 == null : o1.equals (o2);
177     }
178
179 }
180
Popular Tags