KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > PluginLibrary


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.osgi.service.resolver.BundleDescription;
20 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
21 import org.eclipse.pde.core.plugin.IPluginLibrary;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.core.plugin.ISharedPluginModel;
25 import org.eclipse.pde.internal.core.ClasspathUtilCore;
26 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
27 import org.eclipse.pde.internal.core.util.PDEXMLHelper;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31 public class PluginLibrary extends PluginObject implements IPluginLibrary {
32
33     private static final long serialVersionUID = 1L;
34     private String JavaDoc[] fContentFilters;
35     private boolean fExported = false;
36     private String JavaDoc fType;
37
38     public PluginLibrary() {
39     }
40     
41     public boolean isValid() {
42         return fName != null;
43     }
44     
45     public String JavaDoc[] getContentFilters() {
46         IPluginModelBase model = (IPluginModelBase)getModel();
47         if (ClasspathUtilCore.hasBundleStructure(model)) {
48             BundleDescription desc = model.getBundleDescription();
49             if (desc != null) {
50                 ArrayList JavaDoc list = new ArrayList JavaDoc();
51                 ExportPackageDescription[] exports = desc.getExportPackages();
52                 for (int i = 0; i < exports.length; i++) {
53                     list.add(exports[i].getName());
54                 }
55                 return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
56             }
57         }
58         if (!isExported())
59             return new String JavaDoc[0];
60         return isFullyExported() ? new String JavaDoc[] {"**"} : fContentFilters; //$NON-NLS-1$
61
}
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.pde.core.plugin.IPluginLibrary#addContentFilter(java.lang.String)
65      */

66     public void addContentFilter(String JavaDoc filter) throws CoreException {
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.pde.core.plugin.IPluginLibrary#removeContentFilter(java.lang.String)
71      */

72     public void removeContentFilter(String JavaDoc filter) throws CoreException {
73     }
74     
75     public String JavaDoc[] getPackages() {
76         return new String JavaDoc[0];
77     }
78     
79     public boolean isExported() {
80         return fExported;
81     }
82     
83     public boolean isFullyExported() {
84         return fExported
85             && (fContentFilters == null || fContentFilters.length == 0);
86     }
87
88     public String JavaDoc getType() {
89         return fType;
90     }
91
92     
93     public void load(String JavaDoc name) {
94         fName = name;
95         fExported = true;
96     }
97     
98     void load(Node JavaDoc node) {
99         fName = getNodeAttribute(node, "name"); //$NON-NLS-1$
100
fType = getNodeAttribute(node, "type"); //$NON-NLS-1$
101
NodeList JavaDoc children = node.getChildNodes();
102         Vector JavaDoc exports = new Vector JavaDoc();
103         boolean all = false;
104         for (int i = 0; i < children.getLength(); i++) {
105             Node JavaDoc child = children.item(i);
106             if (child.getNodeType() == Node.ELEMENT_NODE) {
107                 String JavaDoc tag = child.getNodeName().toLowerCase(Locale.ENGLISH);
108                 if (tag.equals("export")) { //$NON-NLS-1$
109
String JavaDoc ename = getNodeAttribute(child, "name"); //$NON-NLS-1$
110
if (ename != null) {
111                         ename = ename.trim();
112                         if (ename.equals("*")) { //$NON-NLS-1$
113
all = true;
114                         } else {
115                             exports.add(ename);
116                         }
117                     }
118                 }
119             }
120         }
121         if (exports.size() > 0) {
122             fContentFilters = new String JavaDoc[exports.size()];
123             exports.copyInto(fContentFilters);
124         }
125         fExported = all || exports.size() > 0;
126     }
127     public void setContentFilters(String JavaDoc[] filters) throws CoreException {
128         ensureModelEditable();
129         ArrayList JavaDoc oldValue = createArrayList(fContentFilters);
130         fContentFilters = filters;
131         firePropertyChanged(
132             P_CONTENT_FILTERS,
133             oldValue,
134             createArrayList(filters));
135     }
136
137     public void setPackages(String JavaDoc[] packages) throws CoreException {
138     }
139
140     public void setExported(boolean value) throws CoreException {
141         ensureModelEditable();
142         Boolean JavaDoc oldValue = new Boolean JavaDoc(fExported);
143         fExported = value;
144         firePropertyChanged(P_EXPORTED, oldValue, new Boolean JavaDoc(value));
145     }
146
147     public void setType(String JavaDoc type) throws CoreException {
148         ensureModelEditable();
149         String JavaDoc oldValue = fType;
150         fType = type;
151         firePropertyChanged(P_TYPE, oldValue, type);
152     }
153
154     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
155         throws CoreException {
156         if (name.equals(P_CONTENT_FILTERS)) {
157             ArrayList JavaDoc list = (ArrayList JavaDoc) newValue;
158             if (list != null)
159                 setContentFilters(
160                     (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]));
161             else
162                 setContentFilters(null);
163             return;
164         }
165         if (name.equals(P_EXPORTED)) {
166             setExported(((Boolean JavaDoc) newValue).booleanValue());
167             return;
168         }
169         if (name.equals(P_TYPE)) {
170             setType(newValue != null ? newValue.toString() : null);
171             return;
172         }
173         super.restoreProperty(name, oldValue, newValue);
174     }
175
176     private ArrayList JavaDoc createArrayList(String JavaDoc[] array) {
177         if (array == null)
178             return null;
179         ArrayList JavaDoc list = new ArrayList JavaDoc();
180         for (int i = 0; i < array.length; i++) {
181             list.add(array[i]);
182         }
183         return list;
184     }
185
186     /* (non-Javadoc)
187      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
188      */

189     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
190         // Get the model
191
IPluginModelBase modelBase = getPluginModel();
192         // check to see if the model is a bundle model
193
if ((modelBase instanceof IBundlePluginModelBase) == false) {
194             writer.print(indent);
195             writer.print("<library name=\"" + PDEXMLHelper.getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
196
if (fType != null)
197                 writer.print(" type=\"" + fType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
198
if (!isExported()) {
199                 writer.println("/>"); //$NON-NLS-1$
200
} else {
201                 writer.println(">"); //$NON-NLS-1$
202
String JavaDoc indent2 = indent + " "; //$NON-NLS-1$
203
if (isExported()) {
204                     if (isFullyExported()) {
205                         writer.println(indent2 + "<export name=\"*\"/>"); //$NON-NLS-1$
206
} else {
207                         for (int i = 0; i < fContentFilters.length; i++) {
208                             writer.println(
209                                     indent2
210                                     + "<export name=\"" //$NON-NLS-1$
211
+ fContentFilters[i]
212                                                       + "\"/>"); //$NON-NLS-1$
213
}
214                     }
215                 }
216                 writer.println(indent + "</library>"); //$NON-NLS-1$
217
}
218         } else
219             writer.print(PDEXMLHelper.getWritableString(getName()));
220     }
221     
222     /* (non-Javadoc)
223      * @see org.eclipse.pde.internal.core.plugin.PluginObject#reconnect(org.eclipse.pde.core.plugin.ISharedPluginModel, org.eclipse.pde.core.plugin.IPluginObject)
224      */

225     public void reconnect(ISharedPluginModel model, IPluginObject parent) {
226         super.reconnect(model, parent);
227         // No transient fields
228
}
229     
230     /* (non-Javadoc)
231      * @see org.eclipse.pde.internal.core.plugin.PluginObject#writeDelimeter(java.io.PrintWriter)
232      */

233     public void writeDelimeter(PrintWriter JavaDoc writer) {
234         writer.println(',');
235         writer.print(' ');
236     }
237     
238 }
239
Popular Tags