Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0b4946de7c5c8d928511a6cd2907dd629ebfbbb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.core.plugin;

import java.io.PrintWriter;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.HostSpecification;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.eclipse.pde.core.plugin.IFragment;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPluginExtension;
import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
import org.eclipse.pde.internal.core.PDEState;
import org.w3c.dom.Node;

public class Fragment extends PluginBase implements IFragment {
	private static final long serialVersionUID = 1L;

	private String fPluginId = ""; //$NON-NLS-1$

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

	private int fMatchRule = IMatchRules.NONE;
	
	private boolean fPatch;

	public String getPluginId() {
		return fPluginId;
	}

	public String getPluginVersion() {
		return fPluginVersion;
	}

	public int getRule() {
		return fMatchRule;
	}

	protected boolean hasRequiredAttributes() {
		if (fPluginId == null || fPluginVersion == null)
			return false;
		return super.hasRequiredAttributes();
	}

	void load(BundleDescription bundleDescription, PDEState state) {
		HostSpecification host = bundleDescription.getHost();
		fPluginId = host.getName();
		VersionRange versionRange = host.getVersionRange();
		if (versionRange != null) {
			fPluginVersion = versionRange.getMinimum() != null ? versionRange
					.getMinimum().toString()
					: null;
			fMatchRule = PluginBase.getMatchRule(versionRange);
		}
		fPatch = state.isPatchFragment(bundleDescription.getBundleId());
		super.load(bundleDescription, state);
	}

	void load(Node node, String schemaVersion) {
		fPluginId = getNodeAttribute(node, "plugin-id"); //$NON-NLS-1$
		fPluginVersion = getNodeAttribute(node, "plugin-version"); //$NON-NLS-1$
		String match = getNodeAttribute(node, "match"); //$NON-NLS-1$
		if (match != null) {
			String[] table = IMatchRules.RULE_NAME_TABLE;
			for (int i = 0; i < table.length; i++) {
				if (match.equalsIgnoreCase(table[i])) {
					fMatchRule = i;
					break;
				}
			}
		}
		super.load(node, schemaVersion);
	}

	public void reset() {
		fPluginId = ""; //$NON-NLS-1$
		fPluginVersion = ""; //$NON-NLS-1$
		fMatchRule = IMatchRules.NONE;
		super.reset();
	}

	public void setPluginId(String newPluginId) throws CoreException {
		ensureModelEditable();
		String oldValue = this.fPluginId;
		fPluginId = newPluginId;
		firePropertyChanged(P_PLUGIN_ID, oldValue, fPluginId);
	}

	public void setPluginVersion(String newPluginVersion) throws CoreException {
		ensureModelEditable();
		String oldValue = this.fPluginVersion;
		fPluginVersion = newPluginVersion;
		firePropertyChanged(P_PLUGIN_VERSION, oldValue, fPluginVersion);
	}

	public void setRule(int rule) throws CoreException {
		ensureModelEditable();
		Integer oldValue = new Integer(this.fMatchRule);
		fMatchRule = rule;
		firePropertyChanged(P_RULE, oldValue, new Integer(rule));
	}

	public void restoreProperty(String name, Object oldValue, Object newValue)
			throws CoreException {
		if (name.equals(P_PLUGIN_ID)) {
			setPluginId(newValue != null ? newValue.toString() : null);
			return;
		}
		if (name.equals(P_PLUGIN_VERSION)) {
			setPluginVersion(newValue != null ? newValue.toString() : null);
			return;
		}
		if (name.equals(P_RULE)) {
			setRule(((Integer) newValue).intValue());
			return;
		}
		super.restoreProperty(name, oldValue, newValue);
	}

	public void write(String indent, PrintWriter writer) {
		writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
		if (getSchemaVersion() != null) {
			writer.println("<?eclipse version=\"" + getSchemaVersion() + "\"?>"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		writer.print("<fragment"); //$NON-NLS-1$
		if (getId() != null) {
			writer.println();
			writer.print("   id=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (getName() != null) {
			writer.println();
			writer.print("   name=\"" + getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (getVersion() != null) {
			writer.println();
			writer.print("   version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (getProviderName() != null) {
			writer.println();
			writer.print("   provider-name=\"" + getWritableString(getProviderName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String pid = getPluginId();
		if (pid != null && pid.length() > 0) {
			writer.println();
			writer.print("   plugin-id=\"" + getPluginId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String pver = getPluginVersion(); 
		if (pver != null && pver.length() > 0) {
			writer.println();
			writer.print("   plugin-version=\"" + getPluginVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (getRule() != IMatchRules.NONE) {
			writer.println();
			writer.print("   match=\"" + IMatchRules.RULE_NAME_TABLE[getRule()] + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		writer.println(">"); //$NON-NLS-1$
		writer.println();

		String firstIndent = "   "; //$NON-NLS-1$

		// add runtime
		Object[] children = getLibraries();
		if (children.length > 0) {
			writeChildren(firstIndent, "runtime", children, writer); //$NON-NLS-1$
			writer.println();
		}

		// add requires
		children = getImports();
		if (children.length > 0) {
			writeChildren(firstIndent, "requires", children, writer); //$NON-NLS-1$
			writer.println();
		}

		children = getExtensionPoints();
		if (children.length > 0) {
			for (int i = 0; i < children.length; i++) {
				((IPluginExtensionPoint) children[i]).write(firstIndent, writer);
			}
			writer.println();
		}

		// add extensions
		children = getExtensions();
		for (int i = 0; i < children.length; i++) {
			((IPluginExtension) children[i]).write(firstIndent, writer);
		}
		writer.println("</fragment>"); //$NON-NLS-1$
	}
	
	public boolean isPatch() {
		return fPatch;
	}
}

Back to the top