~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to projects/libraries/src/org/netbeans/modules/project/libraries/LibraryDeclarationHandlerImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.project.libraries;
 
43
 
 
44
import java.net.URL;
 
45
import java.util.ArrayList;
 
46
import java.util.HashMap;
 
47
import java.util.Iterator;
 
48
import java.util.List;
 
49
import java.util.Map;
 
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;
 
56
 
 
57
/**
 
58
 * Read content of library declaration XML document.
 
59
 *
 
60
 * @author Petr Kuzel
 
61
 */
 
62
public class LibraryDeclarationHandlerImpl implements LibraryDeclarationHandler {
 
63
 
 
64
 
 
65
    private LibraryImplementation library;
 
66
 
 
67
    private String libraryType;
 
68
    
 
69
    private String libraryDescription;
 
70
    
 
71
    private String libraryName;
 
72
 
 
73
    private String localizingBundle;
 
74
 
 
75
    private Map<String,List<URL>> contentTypes = new HashMap<String,List<URL>>();
 
76
    
 
77
    // last volume
 
78
    private List<URL> cpEntries;
 
79
    
 
80
    //last volume type
 
81
    private String contentType;
 
82
    
 
83
    //parsing volume?
 
84
    private boolean inVolume = false;
 
85
    
 
86
    public static final boolean DEBUG = false;
 
87
 
 
88
 
 
89
    /**
 
90
     */
 
91
    public LibraryDeclarationHandlerImpl() {
 
92
    }
 
93
    
 
94
    public void start_volume(final Attributes meta) throws SAXException {
 
95
        cpEntries = new ArrayList<URL>();
 
96
        this.inVolume = true;
 
97
    }
 
98
    
 
99
    public void end_volume() throws SAXException {
 
100
        contentTypes.put (contentType, cpEntries);
 
101
        this.inVolume = false;
 
102
        this.contentType = null;
 
103
    }
 
104
    
 
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
 
108
                }
 
109
        if (this.inVolume) {
 
110
            this.contentType = data;
 
111
        }
 
112
        else {
 
113
            this.libraryType = data;
 
114
        }        
 
115
    }
 
116
        
 
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
 
120
        }
 
121
    }
 
122
    
 
123
    public void end_library() throws SAXException {        
 
124
        boolean update;
 
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
 
129
            }
 
130
            update = true;
 
131
        }
 
132
        else {
 
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);
 
136
                return;
 
137
            }
 
138
            this.library = provider.createLibrary();
 
139
            update = false;
 
140
        }
 
141
        if (!update || !safeEquals(this.library.getLocalizingBundle(), localizingBundle)) {
 
142
            this.library.setLocalizingBundle (this.localizingBundle);
 
143
        }
 
144
        if (!update || !safeEquals(this.library.getName(), libraryName)) {
 
145
            this.library.setName (this.libraryName);
 
146
        }
 
147
        if (!update || !safeEquals(this.library.getDescription(), libraryDescription)) {
 
148
            this.library.setDescription (this.libraryDescription);
 
149
        }
 
150
        for (Map.Entry<String,List<URL>> entry : contentTypes.entrySet()) {
 
151
            String contentType = entry.getKey();
 
152
            List<URL> cp = entry.getValue();
 
153
            try {
 
154
                if (!update || !safeEquals (this.library.getContent(contentType),cp)) {
 
155
                    this.library.setContent(contentType, cp);
 
156
                }
 
157
            } catch (IllegalArgumentException e) {
 
158
                throw (SAXException) new SAXException(e.toString()).initCause(e);
 
159
            }
 
160
        }
 
161
        this.libraryName = null;
 
162
        this.libraryDescription = null;
 
163
        this.libraryType = null;
 
164
        this.localizingBundle = null;
 
165
        this.contentTypes.clear ();
 
166
    }
 
167
 
 
168
    public void handle_resource(URL data, final Attributes meta) throws SAXException {
 
169
        if (data != null) {
 
170
            cpEntries.add(data);
 
171
        }
 
172
    }
 
173
        
 
174
    public void handle_name(final String data, final Attributes meta) throws SAXException {
 
175
        this.libraryName = data;
 
176
    }
 
177
    
 
178
    public void handle_description (final String data, final Attributes meta) throws SAXException {
 
179
        libraryDescription = data;
 
180
    }
 
181
 
 
182
    public void handle_localizingBundle (final String data, final Attributes meta) throws SAXException {
 
183
        this.localizingBundle = data;
 
184
    }
 
185
 
 
186
    public void setLibrary (LibraryImplementation library) {
 
187
        this.library = library;
 
188
    }
 
189
 
 
190
    public LibraryImplementation getLibrary () {
 
191
        LibraryImplementation lib = this.library;
 
192
        this.library = null;
 
193
        return lib;
 
194
    }
 
195
 
 
196
 
 
197
    private static boolean safeEquals (Object o1, Object o2) {
 
198
        return o1 == null ? o2 == null : o1.equals (o2);
 
199
    }
 
200
 
 
201
}