|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.testfw.codegen.ui.templates; |
|
19 |
|
20 import java.io.IOException; |
|
21 import java.util.HashMap; |
|
22 import java.util.Map; |
|
23 |
|
24 import org.eclipse.core.internal.content.ContentTypeManager; |
|
25 import org.eclipse.core.runtime.IStatus; |
|
26 import org.eclipse.core.runtime.Status; |
|
27 import org.eclipse.core.runtime.content.IContentType; |
|
28 import org.eclipse.jface.text.templates.Template; |
|
29 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; |
|
30 import org.eclipse.jface.text.templates.persistence.TemplateStore; |
|
31 |
|
32 import com.nokia.testfw.codegen.templates.TemplateBuilder; |
|
33 import com.nokia.testfw.codegen.templates.TemplateLoader; |
|
34 import com.nokia.testfw.codegen.ui.CodegenUIPlugin; |
|
35 |
|
36 @SuppressWarnings("restriction") |
|
37 public class EclipseTemplateBuilder implements TemplateBuilder { |
|
38 |
|
39 private static Map<String, Template> iPath2TemplateMap; |
|
40 |
|
41 public static Map<String, Template> getPathTemplateMap(String template) { |
|
42 Map<String, Template> lPathTemplateMap = new HashMap<String, Template>(); |
|
43 String lTemplatePath = template + "/"; |
|
44 |
|
45 TemplateStore lTemplateStore = CodegenUIPlugin.getDefault() |
|
46 .getTemplateStore(); |
|
47 |
|
48 TemplatePersistenceData[] lTemplateDataArray = lTemplateStore |
|
49 .getTemplateData(false); |
|
50 |
|
51 for (TemplatePersistenceData data : lTemplateDataArray) { |
|
52 String path = data.getId(); |
|
53 if (path.startsWith(lTemplatePath)) { |
|
54 if (data.isEnabled()) { |
|
55 lPathTemplateMap.put( |
|
56 path.substring(lTemplatePath.length()), data |
|
57 .getTemplate()); |
|
58 } |
|
59 } |
|
60 } |
|
61 |
|
62 return lPathTemplateMap; |
|
63 } |
|
64 |
|
65 public static Map<String, Template> findAllOriTemplates() { |
|
66 if (iPath2TemplateMap == null) { |
|
67 iPath2TemplateMap = new HashMap<String, Template>(); |
|
68 |
|
69 try { |
|
70 Map<String, String> lPath2PatternMap = TemplateLoader.load(""); |
|
71 for (String lpath : lPath2PatternMap.keySet()) { |
|
72 String name = lpath.substring(lpath.lastIndexOf("/") + 1); |
|
73 String description = lpath; |
|
74 String pattern = lPath2PatternMap.get(lpath); |
|
75 |
|
76 ContentTypeManager lContentTypeManager = ContentTypeManager |
|
77 .getInstance(); |
|
78 IContentType type = lContentTypeManager |
|
79 .findContentTypeFor(name); |
|
80 String contextTypeId; |
|
81 if (type != null) { |
|
82 contextTypeId = (new StringBuilder(String.valueOf(type |
|
83 .getId()))).append(".contenttype_context") |
|
84 .toString(); |
|
85 } else { |
|
86 contextTypeId = CodeGenTemplateContextType.FILE_TEMPLATE_CONTEXT_TYPE; |
|
87 } |
|
88 |
|
89 Template lTemplate = new Template(name, description, |
|
90 contextTypeId, pattern, true); |
|
91 iPath2TemplateMap.put(lpath, lTemplate); |
|
92 } |
|
93 } catch (IOException e) { |
|
94 IStatus lStatus = new Status(IStatus.ERROR, |
|
95 EclipseTemplateBuilder.class.getName(), |
|
96 "Exception was thrown while loading templates.", e); |
|
97 CodegenUIPlugin.getDefault().getLog().log(lStatus); |
|
98 } |
|
99 } |
|
100 return iPath2TemplateMap; |
|
101 |
|
102 } |
|
103 |
|
104 public Map<String, String> build(String path) { |
|
105 Map<String, String> result = new HashMap<String, String>(); |
|
106 Map<String, org.eclipse.jface.text.templates.Template> lTemplatePathMap = getPathTemplateMap(path); |
|
107 |
|
108 for (String lpath : lTemplatePathMap.keySet()) { |
|
109 org.eclipse.jface.text.templates.Template lEclipseTemplate = lTemplatePathMap |
|
110 .get(lpath); |
|
111 String pattern = lEclipseTemplate.getPattern(); |
|
112 result.put(lpath, pattern); |
|
113 } |
|
114 return result; |
|
115 } |
|
116 } |