001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.plugin;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.Locale;
015: import java.util.Vector;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.osgi.service.resolver.BundleDescription;
019: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
020: import org.eclipse.pde.core.plugin.IPluginLibrary;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.core.plugin.IPluginObject;
023: import org.eclipse.pde.core.plugin.ISharedPluginModel;
024: import org.eclipse.pde.internal.core.ClasspathUtilCore;
025: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
026: import org.eclipse.pde.internal.core.util.PDEXMLHelper;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.NodeList;
029:
030: public class PluginLibrary extends PluginObject implements
031: IPluginLibrary {
032:
033: private static final long serialVersionUID = 1L;
034: private String[] fContentFilters;
035: private boolean fExported = false;
036: private String fType;
037:
038: public PluginLibrary() {
039: }
040:
041: public boolean isValid() {
042: return fName != null;
043: }
044:
045: public String[] getContentFilters() {
046: IPluginModelBase model = (IPluginModelBase) getModel();
047: if (ClasspathUtilCore.hasBundleStructure(model)) {
048: BundleDescription desc = model.getBundleDescription();
049: if (desc != null) {
050: ArrayList list = new ArrayList();
051: ExportPackageDescription[] exports = desc
052: .getExportPackages();
053: for (int i = 0; i < exports.length; i++) {
054: list.add(exports[i].getName());
055: }
056: return (String[]) list.toArray(new String[list.size()]);
057: }
058: }
059: if (!isExported())
060: return new String[0];
061: return isFullyExported() ? new String[] { "**" } : fContentFilters; //$NON-NLS-1$
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.pde.core.plugin.IPluginLibrary#addContentFilter(java.lang.String)
066: */
067: public void addContentFilter(String filter) throws CoreException {
068: }
069:
070: /* (non-Javadoc)
071: * @see org.eclipse.pde.core.plugin.IPluginLibrary#removeContentFilter(java.lang.String)
072: */
073: public void removeContentFilter(String filter) throws CoreException {
074: }
075:
076: public String[] getPackages() {
077: return new String[0];
078: }
079:
080: public boolean isExported() {
081: return fExported;
082: }
083:
084: public boolean isFullyExported() {
085: return fExported
086: && (fContentFilters == null || fContentFilters.length == 0);
087: }
088:
089: public String getType() {
090: return fType;
091: }
092:
093: public void load(String name) {
094: fName = name;
095: fExported = true;
096: }
097:
098: void load(Node node) {
099: fName = getNodeAttribute(node, "name"); //$NON-NLS-1$
100: fType = getNodeAttribute(node, "type"); //$NON-NLS-1$
101: NodeList children = node.getChildNodes();
102: Vector exports = new Vector();
103: boolean all = false;
104: for (int i = 0; i < children.getLength(); i++) {
105: Node child = children.item(i);
106: if (child.getNodeType() == Node.ELEMENT_NODE) {
107: String tag = child.getNodeName().toLowerCase(
108: Locale.ENGLISH);
109: if (tag.equals("export")) { //$NON-NLS-1$
110: String ename = getNodeAttribute(child, "name"); //$NON-NLS-1$
111: if (ename != null) {
112: ename = ename.trim();
113: if (ename.equals("*")) { //$NON-NLS-1$
114: all = true;
115: } else {
116: exports.add(ename);
117: }
118: }
119: }
120: }
121: }
122: if (exports.size() > 0) {
123: fContentFilters = new String[exports.size()];
124: exports.copyInto(fContentFilters);
125: }
126: fExported = all || exports.size() > 0;
127: }
128:
129: public void setContentFilters(String[] filters)
130: throws CoreException {
131: ensureModelEditable();
132: ArrayList oldValue = createArrayList(fContentFilters);
133: fContentFilters = filters;
134: firePropertyChanged(P_CONTENT_FILTERS, oldValue,
135: createArrayList(filters));
136: }
137:
138: public void setPackages(String[] packages) throws CoreException {
139: }
140:
141: public void setExported(boolean value) throws CoreException {
142: ensureModelEditable();
143: Boolean oldValue = new Boolean(fExported);
144: fExported = value;
145: firePropertyChanged(P_EXPORTED, oldValue, new Boolean(value));
146: }
147:
148: public void setType(String type) throws CoreException {
149: ensureModelEditable();
150: String oldValue = fType;
151: fType = type;
152: firePropertyChanged(P_TYPE, oldValue, type);
153: }
154:
155: public void restoreProperty(String name, Object oldValue,
156: Object newValue) throws CoreException {
157: if (name.equals(P_CONTENT_FILTERS)) {
158: ArrayList list = (ArrayList) newValue;
159: if (list != null)
160: setContentFilters((String[]) list
161: .toArray(new String[list.size()]));
162: else
163: setContentFilters(null);
164: return;
165: }
166: if (name.equals(P_EXPORTED)) {
167: setExported(((Boolean) newValue).booleanValue());
168: return;
169: }
170: if (name.equals(P_TYPE)) {
171: setType(newValue != null ? newValue.toString() : null);
172: return;
173: }
174: super .restoreProperty(name, oldValue, newValue);
175: }
176:
177: private ArrayList createArrayList(String[] array) {
178: if (array == null)
179: return null;
180: ArrayList list = new ArrayList();
181: for (int i = 0; i < array.length; i++) {
182: list.add(array[i]);
183: }
184: return list;
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
189: */
190: public void write(String indent, PrintWriter writer) {
191: // Get the model
192: IPluginModelBase modelBase = getPluginModel();
193: // check to see if the model is a bundle model
194: if ((modelBase instanceof IBundlePluginModelBase) == false) {
195: writer.print(indent);
196: writer
197: .print("<library name=\"" + PDEXMLHelper.getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
198: if (fType != null)
199: writer.print(" type=\"" + fType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
200: if (!isExported()) {
201: writer.println("/>"); //$NON-NLS-1$
202: } else {
203: writer.println(">"); //$NON-NLS-1$
204: String indent2 = indent + " "; //$NON-NLS-1$
205: if (isExported()) {
206: if (isFullyExported()) {
207: writer
208: .println(indent2
209: + "<export name=\"*\"/>"); //$NON-NLS-1$
210: } else {
211: for (int i = 0; i < fContentFilters.length; i++) {
212: writer.println(indent2 + "<export name=\"" //$NON-NLS-1$
213: + fContentFilters[i] + "\"/>"); //$NON-NLS-1$
214: }
215: }
216: }
217: writer.println(indent + "</library>"); //$NON-NLS-1$
218: }
219: } else
220: writer.print(PDEXMLHelper.getWritableString(getName()));
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.pde.internal.core.plugin.PluginObject#reconnect(org.eclipse.pde.core.plugin.ISharedPluginModel, org.eclipse.pde.core.plugin.IPluginObject)
225: */
226: public void reconnect(ISharedPluginModel model, IPluginObject parent) {
227: super .reconnect(model, parent);
228: // No transient fields
229: }
230:
231: /* (non-Javadoc)
232: * @see org.eclipse.pde.internal.core.plugin.PluginObject#writeDelimeter(java.io.PrintWriter)
233: */
234: public void writeDelimeter(PrintWriter writer) {
235: writer.println(',');
236: writer.print(' ');
237: }
238:
239: }
|