Skip to content

Package: PluginXmlUtil

PluginXmlUtil

nameinstructionbranchcomplexitylinemethod
addEditorExtensionPoint(IFile, String, boolean, String, String, String, String)
M: 112 C: 97
46%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 23 C: 27
54%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * mborkowski - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.genmodel.util;
15:
16: import java.io.ByteArrayInputStream;
17: import java.io.ByteArrayOutputStream;
18: import java.io.IOException;
19:
20: import javax.xml.parsers.DocumentBuilder;
21: import javax.xml.parsers.DocumentBuilderFactory;
22: import javax.xml.parsers.ParserConfigurationException;
23: import javax.xml.transform.Transformer;
24: import javax.xml.transform.TransformerConfigurationException;
25: import javax.xml.transform.TransformerException;
26: import javax.xml.transform.TransformerFactory;
27: import javax.xml.transform.dom.DOMSource;
28: import javax.xml.transform.stream.StreamResult;
29:
30: import org.eclipse.core.resources.IFile;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.IStatus;
33: import org.eclipse.core.runtime.NullProgressMonitor;
34: import org.eclipse.core.runtime.Status;
35: import org.eclipse.emf.ecp.view.spi.model.reporting.StatusReport;
36: import org.eclipse.emfforms.internal.editor.genmodel.Activator;
37: import org.w3c.dom.Document;
38: import org.w3c.dom.Element;
39: import org.xml.sax.SAXException;
40:
41: /**
42: * This class is a utility class internally used for modifying plugin.xml files.
43: */
44: public final class PluginXmlUtil {
45:         private PluginXmlUtil() {
46:         }
47:
48:         /**
49:          * Adds an editor extension point to the given {@link IFile}.
50:          *
51:          * @param file the file to modify
52:          * @param editorClass the editor class to register
53:          * @param defaultEditor whether to register it as a default editor
54:          * @param extensions the extensions to register for (comma-separated)
55:          * @param icon the icon the register
56:          * @param id the ID of the extension contribution
57:          * @param name the user-readable name of the editor
58:          */
59:         public static void addEditorExtensionPoint(IFile file, String editorClass, boolean defaultEditor,
60:                 String extensions, String icon, String id, String name) {
61:
62:                 final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
63:                 DocumentBuilder builder;
64:                 try {
65:                         builder = documentBuilderFactory.newDocumentBuilder();
66:                 } catch (final ParserConfigurationException ex) {
67:                         Activator.getDefault().getReportService().report(
68:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
69:                         return;
70:                 }
71:
72:                 Document document;
73:                 try {
74:                         document = builder.parse(file.getContents());
75:                 } catch (final SAXException ex) {
76:                         Activator.getDefault().getReportService().report(
77:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
78:                         return;
79:                 } catch (final IOException ex) {
80:                         Activator.getDefault().getReportService().report(
81:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
82:                         return;
83:                 } catch (final CoreException ex) {
84:                         Activator.getDefault().getReportService().report(
85:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
86:                         return;
87:                 }
88:
89:                 final Element plugin = document.getDocumentElement();
90:                 final Element editor = document.createElement("editor"); //$NON-NLS-1$
91:                 editor.setAttribute("class", editorClass); //$NON-NLS-1$
92:                 editor.setAttribute("default", Boolean.toString(defaultEditor)); //$NON-NLS-1$
93:                 editor.setAttribute("extensions", extensions); //$NON-NLS-1$
94:                 editor.setAttribute("icon", icon); //$NON-NLS-1$
95:                 editor.setAttribute("id", id); //$NON-NLS-1$
96:                 editor.setAttribute("name", name); //$NON-NLS-1$
97:                 final Element extension = document.createElement("extension"); //$NON-NLS-1$
98:                 extension.setAttribute("point", "org.eclipse.ui.editors"); //$NON-NLS-1$ //$NON-NLS-2$
99:                 plugin.appendChild(extension);
100:                 extension.appendChild(editor);
101:
102:                 final TransformerFactory transformerFactory = TransformerFactory.newInstance();
103:                 Transformer transformer;
104:                 try {
105:                         transformer = transformerFactory.newTransformer();
106:                 } catch (final TransformerConfigurationException ex) {
107:                         Activator.getDefault().getReportService().report(
108:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
109:                         return;
110:                 }
111:                 final DOMSource domSource = new DOMSource(document);
112:                 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
113:                 try {
114:                         transformer.transform(domSource, new StreamResult(bos));
115:                 } catch (final TransformerException ex) {
116:                         Activator.getDefault().getReportService().report(
117:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
118:                         return;
119:                 }
120:                 try {
121:                         file.setContents(new ByteArrayInputStream(bos.toByteArray()), false, true, new NullProgressMonitor());
122:                 } catch (final CoreException ex) {
123:                         Activator.getDefault().getReportService().report(
124:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
125:                         return;
126:                 }
127:         }
128: }