KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.osgi.service.resolver.BundleDescription;
17 import org.eclipse.osgi.service.resolver.HostSpecification;
18 import org.eclipse.osgi.service.resolver.VersionRange;
19 import org.eclipse.pde.core.plugin.IFragment;
20 import org.eclipse.pde.core.plugin.IMatchRules;
21 import org.eclipse.pde.core.plugin.IPluginExtension;
22 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
23 import org.eclipse.pde.internal.core.PDEState;
24 import org.w3c.dom.Node JavaDoc;
25
26 public class Fragment extends PluginBase implements IFragment {
27     private static final long serialVersionUID = 1L;
28
29     private String JavaDoc fPluginId = ""; //$NON-NLS-1$
30

31     private String JavaDoc fPluginVersion = ""; //$NON-NLS-1$
32

33     private int fMatchRule = IMatchRules.NONE;
34     
35     private boolean fPatch;
36
37     public String JavaDoc getPluginId() {
38         return fPluginId;
39     }
40
41     public String JavaDoc getPluginVersion() {
42         return fPluginVersion;
43     }
44
45     public int getRule() {
46         return fMatchRule;
47     }
48
49     protected boolean hasRequiredAttributes() {
50         if (fPluginId == null || fPluginVersion == null)
51             return false;
52         return super.hasRequiredAttributes();
53     }
54
55     void load(BundleDescription bundleDescription, PDEState state) {
56         HostSpecification host = bundleDescription.getHost();
57         fPluginId = host.getName();
58         VersionRange versionRange = host.getVersionRange();
59         if (versionRange != null) {
60             fPluginVersion = versionRange.getMinimum() != null ? versionRange
61                     .getMinimum().toString()
62                     : null;
63             fMatchRule = PluginBase.getMatchRule(versionRange);
64         }
65         fPatch = state.isPatchFragment(bundleDescription.getBundleId());
66         super.load(bundleDescription, state);
67     }
68
69     void load(Node JavaDoc node, String JavaDoc schemaVersion) {
70         fPluginId = getNodeAttribute(node, "plugin-id"); //$NON-NLS-1$
71
fPluginVersion = getNodeAttribute(node, "plugin-version"); //$NON-NLS-1$
72
String JavaDoc match = getNodeAttribute(node, "match"); //$NON-NLS-1$
73
if (match != null) {
74             String JavaDoc[] table = IMatchRules.RULE_NAME_TABLE;
75             for (int i = 0; i < table.length; i++) {
76                 if (match.equalsIgnoreCase(table[i])) {
77                     fMatchRule = i;
78                     break;
79                 }
80             }
81         }
82         super.load(node, schemaVersion);
83     }
84
85     public void reset() {
86         fPluginId = ""; //$NON-NLS-1$
87
fPluginVersion = ""; //$NON-NLS-1$
88
fMatchRule = IMatchRules.NONE;
89         super.reset();
90     }
91
92     public void setPluginId(String JavaDoc newPluginId) throws CoreException {
93         ensureModelEditable();
94         String JavaDoc oldValue = this.fPluginId;
95         fPluginId = newPluginId;
96         firePropertyChanged(P_PLUGIN_ID, oldValue, fPluginId);
97     }
98
99     public void setPluginVersion(String JavaDoc newPluginVersion) throws CoreException {
100         ensureModelEditable();
101         String JavaDoc oldValue = this.fPluginVersion;
102         fPluginVersion = newPluginVersion;
103         firePropertyChanged(P_PLUGIN_VERSION, oldValue, fPluginVersion);
104     }
105
106     public void setRule(int rule) throws CoreException {
107         ensureModelEditable();
108         Integer JavaDoc oldValue = new Integer JavaDoc(this.fMatchRule);
109         fMatchRule = rule;
110         firePropertyChanged(P_RULE, oldValue, new Integer JavaDoc(rule));
111     }
112
113     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
114             throws CoreException {
115         if (name.equals(P_PLUGIN_ID)) {
116             setPluginId(newValue != null ? newValue.toString() : null);
117             return;
118         }
119         if (name.equals(P_PLUGIN_VERSION)) {
120             setPluginVersion(newValue != null ? newValue.toString() : null);
121             return;
122         }
123         if (name.equals(P_RULE)) {
124             setRule(((Integer JavaDoc) newValue).intValue());
125             return;
126         }
127         super.restoreProperty(name, oldValue, newValue);
128     }
129
130     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
131         writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
132
if (getSchemaVersion() != null) {
133             writer.println("<?eclipse version=\"" + getSchemaVersion() + "\"?>"); //$NON-NLS-1$ //$NON-NLS-2$
134
}
135         writer.print("<fragment"); //$NON-NLS-1$
136
if (getId() != null) {
137             writer.println();
138             writer.print(" id=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
139
}
140         if (getName() != null) {
141             writer.println();
142             writer.print(" name=\"" + getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
143
}
144         if (getVersion() != null) {
145             writer.println();
146             writer.print(" version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
147
}
148         if (getProviderName() != null) {
149             writer.println();
150             writer.print(" provider-name=\"" + getWritableString(getProviderName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
151
}
152         String JavaDoc pid = getPluginId();
153         if (pid != null && pid.length() > 0) {
154             writer.println();
155             writer.print(" plugin-id=\"" + getPluginId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
156
}
157         String JavaDoc pver = getPluginVersion();
158         if (pver != null && pver.length() > 0) {
159             writer.println();
160             writer.print(" plugin-version=\"" + getPluginVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
161
}
162         if (getRule() != IMatchRules.NONE) {
163             writer.println();
164             writer.print(" match=\"" + IMatchRules.RULE_NAME_TABLE[getRule()] + "\""); //$NON-NLS-1$ //$NON-NLS-2$
165
}
166         writer.println(">"); //$NON-NLS-1$
167
writer.println();
168
169         String JavaDoc firstIndent = " "; //$NON-NLS-1$
170

171         // add runtime
172
Object JavaDoc[] children = getLibraries();
173         if (children.length > 0) {
174             writeChildren(firstIndent, "runtime", children, writer); //$NON-NLS-1$
175
writer.println();
176         }
177
178         // add requires
179
children = getImports();
180         if (children.length > 0) {
181             writeChildren(firstIndent, "requires", children, writer); //$NON-NLS-1$
182
writer.println();
183         }
184
185         children = getExtensionPoints();
186         if (children.length > 0) {
187             for (int i = 0; i < children.length; i++) {
188                 ((IPluginExtensionPoint) children[i]).write(firstIndent, writer);
189             }
190             writer.println();
191         }
192
193         // add extensions
194
children = getExtensions();
195         for (int i = 0; i < children.length; i++) {
196             ((IPluginExtension) children[i]).write(firstIndent, writer);
197         }
198         writer.println("</fragment>"); //$NON-NLS-1$
199
}
200     
201     public boolean isPatch() {
202         return fPatch;
203     }
204 }
205
Popular Tags