Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 10f658ebbbc8b2e8dc2fa8e053263bf4d2d53741 (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
/*******************************************************************************
 * Copyright (c) 2007, 2009 Symbian Software Limited 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:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.templateengine;

import java.io.IOException;
import java.net.URL;
import com.ibm.icu.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


/**
 * This class contains methods to get first process block element, next process block 
 * element and checks for next process block element.
 */
public class TemplateDescriptor {
	public static final String PROPERTY_GROUP = "property-group"; //$NON-NLS-1$
	public static final String PROCESS = "process"; //$NON-NLS-1$
	public static final String IF = "if"; //$NON-NLS-1$
	public static final String ID = "id"; //$NON-NLS-1$
	public static final String DEFAULT = "default"; //$NON-NLS-1$
    public static final String PERSIST = "persist";                 //$NON-NLS-1$
    public static final String BOOL_TRUE = "true";                   //$NON-NLS-1$

    private Document document;
	private Element rootElement;
	private List<String> persistVector;
	private String pluginId;

	/**
	 * Constructor  which construct the Document based the URL
	 * @param descriptorURL
	 * @throws TemplateInitializationException
	 */
	public TemplateDescriptor(URL descriptorURL, String pluginId) throws TemplateInitializationException {
		String msg= MessageFormat.format(TemplateEngineMessages.getString("TemplateCore.InitFailed"), new Object[]{descriptorURL}); //$NON-NLS-1$
		try {
			this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(descriptorURL.openStream());
		} catch(ParserConfigurationException pce) {
			throw new TemplateInitializationException(msg, pce);
		} catch(IOException ioe) {
			throw new TemplateInitializationException(msg, ioe);
		} catch(SAXException se) {
			throw new TemplateInitializationException(msg, se);
		}
		this.rootElement = document.getDocumentElement();
		this.persistVector = new ArrayList<String>();
		this.pluginId = pluginId;
	}

	/**
	 * This method is to get the default key value pair (HashMap) form template
	 * descriptor root element.
	 * 
	 * @return default values with keys
	 */
	public Map<String, String> getTemplateDefaults(Element element) {
		Map<String, String> templateDefaults = new HashMap<String, String>();
		Element propertyGroupElement;
		List<Element> children = TemplateEngine.getChildrenOfElement(element);
		for (int i = 0, l = children.size(); i < l; i++) {
			propertyGroupElement = children.get(i);
			if (isNestedElement(propertyGroupElement)) {
				templateDefaults = getTemplateDefaults(propertyGroupElement);
			}
			propertyElements(templateDefaults, propertyGroupElement);
		}
		return templateDefaults;
	}

	/**
	 * Checks whether element nested or not
	 * @param element
	 * @return
	 */
	private boolean isNestedElement(Element element){
		boolean result=false;
		if (element!=null){
			List<Element> children = TemplateEngine.getChildrenOfElement(element);
			String elementName = element.getNodeName();
			Element testElement;
			String testElementName = null;
			if (children.size() > 0){
				testElement = children.get(0);
				testElementName=testElement.getNodeName();
			}
			if(elementName.equals(testElementName))
				result=true;
			else result=false;
		}
		return result;
	}

	/**
	 * This method is to get the list of property-group elements from template
	 * descriptor root element.
	 * 
	 * @return list of property-group elements
	 */
	public List<Element> getPropertyGroupList() {
		List<Element> resultList = null;
		List<Element> list = new ArrayList<Element>();
		resultList = list;
		if (rootElement != null) {
			List<Element> tempList = TemplateEngine.getChildrenOfElement(rootElement);
			for (int i = 0, l = tempList.size(); i < l; i++) {
				Element nextPropertyGroup = tempList.get(i);
				String nextPGName = nextPropertyGroup.getNodeName();
				if (nextPGName.equalsIgnoreCase(PROPERTY_GROUP)) {
					list.add(nextPropertyGroup);
				}
			}
		}
		return resultList;
	}

	/**
	 * This method is to get the complex property-group from template descriptor
	 * root element. complex means a property-group contains other
	 * property-group(s)
	 * 
	 * @param element
	 *            root element of type JDOM Element
	 * @return property-group root element of type JDOM Element
	 */
	public Element getRootPropertyGroup(Element element) {
		if (element != null) {
			String rootElementName = element.getNodeName();
			if (rootElementName.equalsIgnoreCase(PROPERTY_GROUP) && isNestedElement(element)) {
				return element;
			}
			return element;
		} else {
			String nextPGElementName = null;
			List<Element> propertyGroupList = TemplateEngine.getChildrenOfElement(element);
			for (int i = 0, l = propertyGroupList.size(); i < l; i++) {
				Element nextPGElement = propertyGroupList.get(i);
				if (isNestedElement(nextPGElement))
					nextPGElementName = nextPGElement.getNodeName();
				if (PROPERTY_GROUP.equalsIgnoreCase(nextPGElementName) && isNestedElement(nextPGElement)) {
					return nextPGElement;
				}
			}
		}
		return null;
	}

	/**
	 * This private method is used in getTemplateDefaults() to get defaults from
	 * property elements
	 * 
	 * @param defaults
	 *            HashMap to store deraults
	 * @param propertyGroupElement
	 *            traverse the complex property-group element
	 */
	private void propertyElements(Map<String, String> defaults, Element propertyGroupElement) {
		List<Element> children = TemplateEngine.getChildrenOfElement(propertyGroupElement);
		for (int i = 0, l = children.size(); i < l; i++) {
			Element propertyElement = children.get(i);
			String key = propertyElement.getAttribute(ID);
			String value = propertyElement.getAttribute(DEFAULT);
			if (key != null && !key.equals("")) { //$NON-NLS-1$
				defaults.put(key, value);
			}

			String persist = propertyElement.getAttribute(PERSIST);
			if ((persist != null) && (persist.trim().equalsIgnoreCase(BOOL_TRUE))) {
				persistVector.add(key);
			}
		}
	}

	/**
	 * added to return root of this document.
	 */
	public Element getRootElement() {
		return rootElement;
	}

	/**
	 * return the list of IDs whose Persist attribute is true.
	 * 
	 * @return Vector.
	 */
	public List<String> getPersistTrueIDs() {
		return persistVector;
	}
	
	public String getPluginId() {
		return pluginId;
	}
}

Back to the top