Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79904a2f52311d6cc2f233498887a92ece969cec (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*******************************************************************************
 * Copyright (c) 2007 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.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.Bundle;

/**
 * Acts as an Helper class for Template Engine
 * 
 * @since 4.0
 */
public class TemplateEngineHelper {
	public static final String US = "_";  //$NON-NLS-1$
	public static final String OPEN_MARKER = "$("; //$NON-NLS-1$
	public static final String CLOSE_MARKER = ")"; //$NON-NLS-1$
	public static final String STRING_EXTERNALIZATION_MARKER = "%"; //$NON-NLS-1$
	public static final String LOGGER_FILE_NAME = "Process"; //$NON-NLS-1$
	// This is used while getting the Plugin Path.
	public static final String PROJRESOURCE = "plugin.xml"; //$NON-NLS-1$
	public static final String PLUGIN_ID = "pluginId"; //$NON-NLS-1$
	public static final String PLUGIN_PROPERTIES = "plugin.properties"; //$NON-NLS-1$
	public static final String TEMPLATE_PROPERTIES = "templates.properties"; //$NON-NLS-1$
	public static final String BOOLTRUE = "true"; //$NON-NLS-1$
	public static final String ID = "id"; //$NON-NLS-1$
	public static final String VALUE = "value"; //$NON-NLS-1$
	public static final String SDLOG_FILE_NAME = "sharedDefaults"; //$NON-NLS-1$
	public static final String LOCATION = "location"; //$NON-NLS-1$
	public static final String WIZARD_ID = "wizardId"; //$NON-NLS-1$
	public static final String FILTER_PATTERN = "filterPattern"; //$NON-NLS-1$
	public static final String USAGE_DESCRIPTION = "usageDescription"; //$NON-NLS-1$
	public static final String PROJECT_TYPE = "projectType"; //$NON-NLS-1$
	public static final String TOOL_CHAIN = "toolChain"; //$NON-NLS-1$
	public static final String EXTRA_PAGES_PROVIDER = "pagesAfterTemplateSelectionProvider"; //$NON-NLS-1$
	public static final String IS_CATEGORY = "isCategory"; //$NON-NLS-1$
	public static final String CONFIGURATIONS = "Configurations"; //$NON-NLS-1$

	/**
	 * Gets the backup shareddefaults XML file. Presence of the file indicates
	 * that the template engine or the application underwent some crash or
	 * destruction.
	 * 
	 * @param sharedLocation
	 * @return sharedXMLFile
     * 
     * @since 4.0
	 */

	public static File getSharedDefaultLocation(String sharedLocation) {

		File sharedXMLFile = findLocation(sharedLocation);
		return sharedXMLFile;
	}

	/**
	 * Finds the location of the shareddefaults backup and original xml file.
	 * 
	 * @param fileLocation
	 * @return file
     * 
     * @since 4.0
	 */

	private static File findLocation(String fileLocation) {

		Plugin plugin = CCorePlugin.getDefault();
		IPath stateLoc = plugin.getStateLocation();
		fileLocation = stateLoc.toString() + File.separator + fileLocation;
		File file = new File(fileLocation);

		return file;
	}

	/**
	 * Stores the shareddefaults xml file in
	 * "${workspace}/.metadata/.plugins/${plugin.name}/shareddefaults.xml" path.
	 * 
	 * @param sharedLocation
	 * @return sharedXMLFile
     * 
     * @since 4.0
	 */

	public static File storeSharedDefaultLocation(String sharedLocation) {

		File sharedXMLFile = findLocation(sharedLocation);

		try {
			createNewFile(sharedXMLFile.getPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sharedXMLFile;
	}

	/**
	 * This creates a new File, in the Absolute Path given as argument. The File
	 * name will be part of the path argument.
	 * 
	 * @param aFileName,
	 *            absoulute File path(including File name) to be created.
     * 
     * @since 4.0
	 */
	public static void createNewFile(String fileName) throws IOException {

		(new File(fileName)).createNewFile();
	}

	/**
	 * This method returns the workspace path present in the workspace
	 * 
	 * @return String Example : file:/C:/eclipse/workspace/
     * 
     * @since 4.0
	 */
	public static IPath getWorkspacePath() {

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		IPath workSpacePath = new Path(root.getLocation().toString() + File.separator);
		return workSpacePath;

	}

	/**
	 * given a String of the form $(ID), return ID.
	 * 
	 * @param markerString
	 * @return
     * 
     * @since 4.0
	 */
	public static String getFirstMarkerID(String markerString) {
		String key = null;
		if (markerString.indexOf(OPEN_MARKER) != -1) {
			key = markerString.substring(markerString.indexOf(OPEN_MARKER) + OPEN_MARKER.length(), markerString
					.indexOf(CLOSE_MARKER));
		}
		return key;
	}

	/**
	 * Check whether there is a directory existing in present workspace, with
	 * the given name.
	 * 
	 * @param directoryName
	 * @return true, if directory exists.
     * 
     * @since 4.0
	 */
	public static boolean checkDirectoryInWorkspace(String directoryName) {

		boolean retVal = false;
		File file = null;

		try {
			file = new File(getWorkspacePath() + directoryName);
		} catch (Exception exp) {

		}
		if ((file != null) && (file.exists()) && (file.isDirectory())) {
			retVal = true;
		}
		return retVal;
	}
	/**
	 * Return Template Source path as URL
	 * @param pluginId
	 * @param resourcePath
	 * @return URL, of the Template Resource
	 * @throws IOException
     * 
     * @since 4.0
	 */

	public static URL getTemplateResourceURL(String pluginId, String resourcePath) throws IOException {
		return FileLocator.toFileURL(Platform.getBundle(pluginId).getEntry(resourcePath));
	}

	/**
	 * 
	 * Returns the Template Resource Relative Path as URL 
	 * @param template
	 * @param resourcePath
	 * @return URL, of the Template Resource
	 * @throws IOException
     * 
     * @since 4.0
	 */
	public static URL getTemplateResourceURLRelativeToTemplate(TemplateCore template, String resourcePath) throws IOException {
		TemplateInfo templateInfo = template.getTemplateInfo();
		String path = templateInfo.getTemplatePath();
		int slash = path.lastIndexOf("/"); //$NON-NLS-1$
		if (slash == -1) {
			path = resourcePath;
		} else {
			path = path.substring(0, slash + 1) + resourcePath;
		}
		URL entry = Platform.getBundle(templateInfo.getPluginId()).getEntry(path);
		if (entry == null) {
			return null;
		}
		return FileLocator.toFileURL(entry);
	}

	public static String externalizeTemplateString(TemplateInfo ti, String key) {
		if (key.startsWith(STRING_EXTERNALIZATION_MARKER)) {
			String pluginId = ti.getPluginId();
			String path = ti.getTemplatePath();
			IPath p = new Path(path);
			String propertiesPath = TEMPLATE_PROPERTIES;
			if(p.segmentCount() != 0){
				p = p.removeLastSegments(1);
				propertiesPath = p.append(propertiesPath).toString();
			}
			return externalizeTemplateString(pluginId, propertiesPath, key);
		}
		return key;
	}

	public static String externalizeTemplateString(String pluginId, String location, String key) {
		String value = key;
		if (key.startsWith(STRING_EXTERNALIZATION_MARKER)) {
			try {
				value = location != null ? getValueFromProperties(pluginId, location, key.substring(1)) : null;
				if (value == null) {
					value = getValueFromProperties(pluginId, PLUGIN_PROPERTIES, key.substring(1));
				}
			} catch (IOException e) {
				value = key;
				e.printStackTrace();
			}

			if (value == null) {
				value = key;
			}
		}
		return value;
	}
	
	private static String getValueFromProperties(String pluginId, String propertiesFile, String key) throws IOException {
		String value = null;
		Bundle b = Platform.getBundle(pluginId);
//		URL url= b.getResource(propertiesFile);
		URL url= getResourceURL(b, propertiesFile);
		if (url != null) {
			InputStream in= url.openStream();
			Properties p = new Properties();
			p.load(in);
			value = (String) p.get(key);
		}
		return value;
		
	}
	
	private static URL getResourceURL(Bundle bundle, String propertiesFile) {
		// Get the properties in the following order
		// propertiesFile_lang_country_variant
		// propertiesFile_lang_country
		// propertiesFile_lang
		// propertiesFile
	
		URL url = null;
		Locale locale = Locale.getDefault();
		String lang = locale.getLanguage();
		String country = locale.getCountry();
		String variant = locale.getVariant();
		
		url = bundle.getResource(propertiesFile + US + lang + US + country + US + variant);
		
		if (url == null) {
			url = bundle.getResource(propertiesFile + US + lang + US + country);
		}
		if (url == null) {
			url = bundle.getResource(propertiesFile + US + lang);
		}
		if (url == null) {
			url = bundle.getResource(propertiesFile);
		}
		
		return url;
	}
}

Back to the top