Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cfc9bef8cac001c4061c6b6ab54e375209975c54 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay 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:
 *     John Krasnay - initial API and implementation
 *     Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
 *******************************************************************************/
package org.eclipse.vex.ui.internal.config;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.vex.core.internal.dom.DocumentWriter;
import org.eclipse.vex.ui.internal.VexPlugin;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * Represents a Vex plugin project in the workspace.
 */
public class PluginProject extends ConfigSource {

	public static final String PLUGIN_XML = "vex-plugin.xml"; //$NON-NLS-1$

	private final IProject project;

	public PluginProject(final IProject project) {
		super(project.getName());
		this.project = project;
	}

	@Override
	public URL getBaseUrl() {
		try {
			return getProject().getLocation().toFile().toURL();
		} catch (final MalformedURLException e) {
			throw new AssertionError(e);
		}
	}

	/**
	 * @return the IProject associated with this plugin project.
	 */
	public IProject getProject() {
		return project;
	}

	public void build() {
		try {
			project.build(IncrementalProjectBuilder.FULL_BUILD, null);
		} catch (final CoreException e) {
			final String message = MessageFormat.format(Messages.getString("PluginProject.buildError"), //$NON-NLS-1$
					new Object[] { project.getName() });
			VexPlugin.getInstance().log(IStatus.ERROR, message, e);
		}
	}

	public void load() throws CoreException {
		checkProject();
		parseConfigXml(null);
		parseResources(null);
	}

	private void checkProject() {
		if (!isOpenPluginProject(project))
			throw new IllegalStateException(MessageFormat.format(Messages.getString("PluginProject.notPluginProject"), //$NON-NLS-1$
					project.getName()));
	}

	public static boolean isOpenPluginProject(final IProject project) {
		return project.isOpen() && hasPluginProjectNature(project);
	}

	public static boolean hasPluginProjectNature(final IProject project) {
		try {
			return project.isOpen() && project.hasNature(PluginProjectNature.ID);
		} catch (final CoreException e) {
			VexPlugin.getInstance().getLog().log(e.getStatus());
			return false;
		}
	}

	/**
	 * Re-parses the vex-plugin.xml file.
	 */
	public void parseConfigXml(final IBuildProblemHandler problemHandler) throws CoreException {
		removeAllItems();

		final Document document = parseConfigXmlDom(problemHandler);
		if (document == null)
			return;

		final NodeList extensions = document.getElementsByTagName("extension"); //$NON-NLS-1$
		for (int i = 0; i < extensions.getLength(); i++) {
			final Element extensionElement = (Element) extensions.item(i);
			final String extensionPointId = extensionElement.getAttribute("point"); //$NON-NLS-1$
			final String id = extensionElement.getAttribute("id"); //$NON-NLS-1$
			final String name = extensionElement.getAttribute("name"); //$NON-NLS-1$
			final IConfigElement[] configElements = parseConfigElements(extensionElement);

			try {
				this.addItem(extensionPointId, id, name, configElements);
			} catch (final IOException e) {
				VexPlugin.getInstance().log(IStatus.ERROR, e.getMessage(), e);
			}
		}
	}

	private Document parseConfigXmlDom(final IBuildProblemHandler problemHandler) throws CoreException {
		final IFile configXml = project.getFile(PLUGIN_XML);
		final DocumentBuilder builder = createDocumentBuilder();
		try {
			final URL url = configXml.getLocation().toFile().toURL();
			return builder.parse(url.toString());
		} catch (final SAXParseException e) {
			if (problemHandler != null) {
				final BuildProblem problem = new BuildProblem(BuildProblem.SEVERITY_ERROR, configXml.getName(), e.getMessage(), e.getLineNumber());
				problemHandler.foundProblem(problem);
				return null;
			} else {
				VexPlugin.getInstance().log(IStatus.ERROR, MessageFormat.format("Cannot load {0}.", configXml.getFullPath()), e);
				return null;
			}
		} catch (final SAXException e) {
			VexPlugin.getInstance().log(IStatus.ERROR, MessageFormat.format("Cannot load {0}.", configXml.getFullPath()), e);
			return null;
		} catch (final IOException e) {
			VexPlugin.getInstance().log(IStatus.ERROR, MessageFormat.format("Cannot load {0}.", configXml.getFullPath()), e);
			return null;
		}
	}

	private IConfigElement[] parseConfigElements(final Element extensionElement) {
		final List<IConfigElement> result = new ArrayList<IConfigElement>();
		final NodeList childList = extensionElement.getChildNodes();
		for (int j = 0; j < childList.getLength(); j++) {
			final Node child = childList.item(j);
			if (child instanceof Element)
				result.add(new DomConfigurationElement((Element) child));
		}
		return result.toArray(new IConfigElement[result.size()]);
	}

	private static DocumentBuilder createDocumentBuilder() {
		try {
			return DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (final ParserConfigurationException e) {
			throw new AssertionError(e);
		} catch (final FactoryConfigurationError e) {
			throw new AssertionError(e);
		}
	}

	/**
	 * Writes this configuraton to the file vex-config.xml in the project.
	 */
	public void writeConfigXml() throws CoreException, IOException {
		final ByteArrayOutputStream baos = new ByteArrayOutputStream();
		final PrintWriter out = new PrintWriter(new OutputStreamWriter(baos, "utf-8")); //$NON-NLS-1$

		final ConfigurationElement root = new ConfigurationElement("plugin"); //$NON-NLS-1$
		for (final ConfigItem item : getAllItems()) {
			final ConfigurationElement extElement = new ConfigurationElement("extension"); //$NON-NLS-1$
			extElement.setAttribute("id", item.getSimpleId()); //$NON-NLS-1$
			extElement.setAttribute("name", item.getName()); //$NON-NLS-1$
			extElement.setAttribute("point", item.getExtensionPointId()); //$NON-NLS-1$
			final IConfigItemFactory factory = getConfigItemFactory(item.getExtensionPointId());
			if (factory != null) {
				extElement.setChildren(factory.createConfigurationElements(item));
				root.addChild(extElement);
			}
		}
		writeElement(root, out, 0);

		out.close();

		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

		final IFile file = getProject().getFile(PLUGIN_XML);
		if (file.exists())
			file.setContents(inputStream, true, false, null);
		else
			file.create(inputStream, true, null);
	}

	private static void writeElement(final IConfigElement element, final PrintWriter out, final int level) {
		final StringBuffer elementIndent = new StringBuffer();
		for (int i = 0; i < level; i++)
			elementIndent.append("  "); //$NON-NLS-1$
		final StringBuffer elementPrefix = new StringBuffer();
		elementPrefix.append("<"); //$NON-NLS-1$
		elementPrefix.append(element.getName());

		final StringBuffer attributeIndent = new StringBuffer(elementIndent.toString());
		for (int i = 0; i < elementPrefix.length() + 1; i++)
			attributeIndent.append(" "); //$NON-NLS-1$

		out.print(elementIndent.toString());
		out.print(elementPrefix.toString());
		final String[] attributeNames = element.getAttributeNames();
		for (int i = 0; i < attributeNames.length; i++) {
			final String attributeName = attributeNames[i];
			if (i > 0) {
				out.println();
				out.print(attributeIndent);
			} else
				out.print(" "); //$NON-NLS-1$

			out.print(attributeName);
			out.print("=\""); //$NON-NLS-1$
			out.print(DocumentWriter.escape(element.getAttribute(attributeName)));
			out.print("\""); //$NON-NLS-1$
		}
		out.println(">"); //$NON-NLS-1$

		final IConfigElement[] children = element.getChildren();
		for (final IConfigElement element2 : children)
			writeElement(element2, out, level + 1);

		out.print(elementIndent);
		out.print("</"); //$NON-NLS-1$
		out.print(element.getName());
		out.println(">"); //$NON-NLS-1$
	}

}

Back to the top