Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5126b349a45947d8f4159cbdb9fce4a96a3f605d (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
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST.
 *    
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.common.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

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

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gmf.runtime.common.core.service.AbstractProvider;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.diagram.ui.services.palette.IPaletteProvider;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.part.IPaletteDescription;
import org.eclipse.ui.IEditorPart;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Provider for Palette that uses a XML file for palette definition.
 */
public class LocalPaletteProvider extends AbstractProvider implements IPaletteProvider, IPapyrusPaletteConstant {

	/**
	 * The list of palette provider XML contributions
	 */
	protected NodeList contributions = null;

	/** parser used for the xml file */
	protected XMLDefinitionPaletteParser parser;

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public void contributeToPalette(IEditorPart editor, Object content, PaletteRoot root, Map predefinedEntries) {
		if(contributions ==null) {
			return;
		}
		parser = new XMLDefinitionPaletteParser(new XMLDefinitionPaletteFactory(root, predefinedEntries));
		
		for(int i = 0; i < contributions.getLength(); i++) {
			Node node = contributions.item(i);
			if(PALETTE_DEFINITION.equals(node.getNodeName())) {
				parser.parsePaletteDefinition(node);
			}
		}
	}

	/**
	 * Adds the configuration elements to the list of palette provider XML
	 * contributions
	 * 
	 * @param configElement
	 *        the configuration element from which information are retrieved
	 */
	public void setContributions(IConfigurationElement configElement) {
		// tries to read the XML configuration file
		readXMLDocument(configElement.getAttribute(PATH));
	}

	/**
	 * locally defines palette
	 * 
	 * @param description
	 *        the description of the palette to build
	 */
	public void setContributions(IPaletteDescription description) {
		readXMLDocument(description.getContributions());
	}

	/**
	 * Reads the XML configuration for the specified element
	 * 
	 * @param contribution
	 *        the path for the xml file
	 */
	protected void readXMLDocument(Object contribution) {
		if(contribution instanceof String) {
			readXMLDocument((String)contribution);
		}
	}

	/**
	 * Reads the XML configuration for the specified element
	 * 
	 * @param iConfigurationElement
	 *        the path for the xml file
	 */
	protected void readXMLDocument(String path) {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		try {
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

			InputStream inputStream = getXmlFile(path);
			// the file should never be null in this implementation, but
			// sub-classes could return null
			if(inputStream == null) {
				contributions = new EmptyNodeList();
				Activator.log.debug("Impossible to load file: " + path);
			} else {
				Document document = documentBuilder.parse(inputStream);
				contributions = document.getChildNodes();
				if(contributions == null) {
					contributions = new EmptyNodeList();
				}
			}
		} catch (ParserConfigurationException e) {
			Activator.log.error(e);
			contributions = new EmptyNodeList();
		} catch (IOException e) {
			Activator.log.error(e);
			contributions = new EmptyNodeList();
		} catch (SAXException e) {
			Activator.log.error(e);
			contributions = new EmptyNodeList();
		}
	}

	/**
	 * Returns the file using the specified path in the plugin state location
	 * 
	 * @param path
	 *        the path to the file
	 * @return the file using the specified path in the plugin state location,
	 *         even if it does not exists. In the latter case, the method {@link File#exists()} returns <code>false</code>.
	 */
	public InputStream getXmlFile(String path) throws IOException {
		return new FileInputStream(Activator.getDefault().getStateLocation().append(path).toFile());
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean provides(IOperation operation) {
		return false;
	}

	/**
	 * empty node list implementation
	 */
	public class EmptyNodeList implements NodeList {

		/**
		 * {@inheritDoc}
		 */
		public int getLength() {
			return 0;
		}

		/**
		 * {@inheritDoc}
		 */
		public Node item(int index) {
			return null;
		}

	}

}

Back to the top