587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 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 the License "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.helium.core;
|
|
19 |
|
628
|
20 |
import java.io.File;
|
587
|
21 |
import java.io.FileWriter;
|
628
|
22 |
import java.util.HashMap;
|
587
|
23 |
import java.util.List;
|
|
24 |
import java.util.Map;
|
628
|
25 |
|
587
|
26 |
import org.apache.log4j.Logger;
|
|
27 |
|
628
|
28 |
import freemarker.template.Configuration;
|
|
29 |
import freemarker.template.Template;
|
|
30 |
|
587
|
31 |
/**
|
|
32 |
* Template processor.
|
|
33 |
*
|
|
34 |
*/
|
|
35 |
public class TemplateProcessor {
|
|
36 |
|
|
37 |
private Logger log = Logger.getLogger(TemplateProcessor.class);
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Create a Map of FreeMarker compatible data from the list of input source.
|
|
41 |
*
|
|
42 |
* @param inputSources
|
|
43 |
* @return
|
|
44 |
* @throws TemplateProcessorException
|
|
45 |
*/
|
|
46 |
private HashMap<String, Object> getTemplateMap(
|
|
47 |
List<TemplateInputSource> inputSources) {
|
|
48 |
HashMap<String, Object> templateMap = new HashMap<String, Object>();
|
|
49 |
try {
|
|
50 |
for (TemplateInputSource source : inputSources) {
|
|
51 |
if (source instanceof XMLTemplateSource) {
|
|
52 |
XMLTemplateSource xmlSource = (XMLTemplateSource) source;
|
|
53 |
File inputFile = xmlSource.getSourceLocation();
|
|
54 |
if (inputFile.exists()) {
|
|
55 |
templateMap.put(xmlSource.getSourceName(),
|
|
56 |
freemarker.ext.dom.NodeModel.parse(inputFile));
|
|
57 |
} else {
|
|
58 |
log.debug("TemplateProcessor: input file " + inputFile
|
|
59 |
+ " for the template does not exists");
|
|
60 |
}
|
|
61 |
} else if (source instanceof PropertiesSource) {
|
|
62 |
PropertiesSource propSource = (PropertiesSource) source;
|
|
63 |
templateMap.put(propSource.getSourceName(), propSource
|
|
64 |
.getProperties());
|
|
65 |
}
|
|
66 |
}
|
|
67 |
} catch (java.io.IOException e) {
|
|
68 |
throw new TemplateProcessorException(
|
|
69 |
"I/O Error during template conversion: " + e.getMessage());
|
|
70 |
} catch (org.xml.sax.SAXException e1) {
|
|
71 |
throw new TemplateProcessorException("XML parser error: "
|
|
72 |
+ e1.getMessage());
|
|
73 |
} catch (javax.xml.parsers.ParserConfigurationException e3) {
|
|
74 |
throw new TemplateProcessorException("Parser error: "
|
|
75 |
+ e3.getMessage());
|
|
76 |
}
|
|
77 |
return templateMap;
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Convert a template.
|
|
82 |
*
|
|
83 |
* @param templateFile
|
|
84 |
* @param outputFile
|
|
85 |
* @param sourceList
|
|
86 |
* @throws TemplateProcessorException
|
|
87 |
*/
|
|
88 |
public void convertTemplate(String templateFile, String outputFile,
|
|
89 |
List<TemplateInputSource> sourceList) {
|
|
90 |
convertTemplate(new File(templateFile), new File(outputFile),
|
|
91 |
sourceList);
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Convert a template.
|
|
96 |
*
|
|
97 |
* @param templateFile
|
|
98 |
* @param outputFile
|
|
99 |
* @param sourceList
|
|
100 |
* @throws TemplateProcessorException
|
|
101 |
*/
|
|
102 |
public void convertTemplate(File templateFile, File outputFile,
|
|
103 |
List<TemplateInputSource> sourceList) {
|
|
104 |
if (templateFile != null) {
|
|
105 |
convertTemplate(templateFile.getParent(), templateFile.getName(),
|
|
106 |
outputFile.toString(), sourceList);
|
|
107 |
} else {
|
|
108 |
throw new TemplateProcessorException("Template file not defined.");
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Converts the template to generate xml file to be sent to the server.
|
|
114 |
*
|
|
115 |
* @param templateFile
|
|
116 |
* - template file to be converted
|
|
117 |
* @param input
|
|
118 |
* - input source file.
|
|
119 |
* @param outputFile
|
|
120 |
* - location to store the converted file
|
|
121 |
* @param antProperties
|
|
122 |
* - used as one of the input for conversion.
|
|
123 |
* @param inputSourceType
|
|
124 |
* - source input type (xml / properties from file). Currently
|
|
125 |
* xml input source is the only one supported.
|
|
126 |
* @return - location of the input source.
|
|
127 |
* @throws TemplateProcessorException
|
|
128 |
*/
|
|
129 |
public void convertTemplate(String templateDir, String templateFile,
|
|
130 |
String outputFile, List<TemplateInputSource> sourceList) {
|
|
131 |
|
|
132 |
Configuration cfg = new Configuration();
|
|
133 |
File templateDirFile = new File(templateDir);
|
|
134 |
if (templateDir != null && templateDirFile.exists()) {
|
|
135 |
try {
|
|
136 |
cfg.setDirectoryForTemplateLoading(templateDirFile);
|
|
137 |
log
|
|
138 |
.debug("diamonds:TemplateProcessor:adding template directory loader: "
|
|
139 |
+ templateDir);
|
|
140 |
} catch (java.io.IOException ie) {
|
|
141 |
throw new TemplateProcessorException(
|
|
142 |
"Template directory configuring error: " + ie);
|
|
143 |
}
|
|
144 |
} else {
|
|
145 |
throw new TemplateProcessorException(
|
|
146 |
"Template directory does not exist: "
|
|
147 |
+ templateDirFile.getAbsolutePath());
|
|
148 |
}
|
|
149 |
try {
|
|
150 |
Template template = cfg.getTemplate(templateFile);
|
|
151 |
Map<String, Object> templateMap = getTemplateMap(sourceList);
|
|
152 |
template.process(templateMap, new FileWriter(outputFile));
|
|
153 |
} catch (freemarker.core.InvalidReferenceException ivx) {
|
|
154 |
throw new TemplateProcessorException(
|
|
155 |
"Invalid reference in config: " + ivx);
|
|
156 |
} catch (freemarker.template.TemplateException e2) {
|
|
157 |
throw new TemplateProcessorException("TemplateException: " + e2);
|
|
158 |
} catch (java.io.IOException e) {
|
|
159 |
throw new TemplateProcessorException(
|
|
160 |
"I/O Error during template conversion: " + e);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|