Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Translator |
|
| 4.4;4.4 |
1 | package net.sf.jolene.util; | |
2 | ||
3 | import net.sf.jolene.dom.*; | |
4 | ||
5 | import java.io.*; | |
6 | import java.util.Properties; | |
7 | ||
8 | ||
9 | /** | |
10 | * Not used. | |
11 | * User: Dan | |
12 | * Date: Jun 1, 2008 | |
13 | * Time: 5:22:33 PM | |
14 | * | |
15 | * todo dumb class name - it's not translating anything. | |
16 | */ | |
17 | public class Translator { | |
18 | ||
19 | 0 | private final static PrintStream ps = System.out; |
20 | ||
21 | /** | |
22 | * java -jar jolene.jar net.sf.jolene.util.Translator | |
23 | * | |
24 | * @param args 2 params are expected: full path to the FOLDER where the html files reside - full path to the output resources file. | |
25 | */ | |
26 | public static void main(String args[]) { | |
27 | ||
28 | 0 | if (args.length < 2) { |
29 | 0 | ps.println("Translator requires 2 parameters"); |
30 | 0 | ps.println("\t 1) full path to the FOLDER where the html files reside"); |
31 | 0 | ps.println("\t 2) full path to the output resources file"); |
32 | 0 | ps.println(); |
33 | 0 | ps.println("Example:"); |
34 | 0 | ps.println("java -jar jolene.jar d:/projects/myproject/html d:/projects/myproject/resources/application.properties"); |
35 | 0 | System.exit(0); |
36 | } | |
37 | ||
38 | 0 | new Translator(args[0], args[1]).translate(); |
39 | 0 | } |
40 | ||
41 | ||
42 | private String sourceFolder; | |
43 | private String targetPropertiesFile; | |
44 | private Properties properties; | |
45 | ||
46 | 0 | Translator(String sourceFolder, String targetPropertiesFile) { |
47 | 0 | this.sourceFolder = sourceFolder; |
48 | 0 | this.targetPropertiesFile = targetPropertiesFile; |
49 | ||
50 | 0 | properties = new Properties(); |
51 | try { | |
52 | ||
53 | 0 | File f = new File(targetPropertiesFile); |
54 | 0 | f.createNewFile(); |
55 | 0 | InputStream in = new FileInputStream(f); |
56 | 0 | properties.load(in); |
57 | ||
58 | 0 | } catch (IOException e) { |
59 | 0 | ps.println(e.getMessage() + "\n" + e.getStackTrace()); |
60 | 0 | System.exit(1); |
61 | 0 | } |
62 | ||
63 | 0 | } |
64 | ||
65 | private void translate() { | |
66 | 0 | File dir = new File(sourceFolder); |
67 | 0 | ps.println("DIR: " + dir.getAbsolutePath()); |
68 | 0 | translate(dir); |
69 | ||
70 | try { | |
71 | 0 | File f = new File(targetPropertiesFile); |
72 | 0 | f.createNewFile(); |
73 | 0 | OutputStream out = new FileOutputStream(f); |
74 | 0 | properties.store(out, "---Locale strings written by jolene---"); |
75 | 0 | } catch (IOException e) { |
76 | 0 | ps.println(e.getMessage() + "\n" + e.getStackTrace()); |
77 | 0 | System.exit(1); |
78 | 0 | } |
79 | 0 | } |
80 | ||
81 | private void translate(File dir) { | |
82 | 0 | File[] files = dir.listFiles(); |
83 | 0 | if (files == null) { |
84 | 0 | ps.println("No files found in " + sourceFolder); |
85 | 0 | return; |
86 | ||
87 | } else { | |
88 | 0 | for (int i = 0; i < files.length; i++) { |
89 | 0 | if (files[i].isDirectory()) { |
90 | 0 | if (!files[i].getName().equalsIgnoreCase(".svn")) { |
91 | 0 | translate(files[i]); |
92 | } | |
93 | } else { | |
94 | //String filename = files[i].getAbsolutePath(); | |
95 | 0 | translateDocument(files[i]); |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | 0 | } |
101 | ||
102 | /** | |
103 | * @param file File object of full path to a document. | |
104 | * In order to get the message name, we use the document name which is derived from the file name with | |
105 | * the sourceFolder path removed. Example: | |
106 | * c:/proj/html/abc.html ==> abc | |
107 | * c:/proj/html/invoice/invoiceEdit.html ==> invoice.invoiceEdit | |
108 | * c:/proj/html/invoice/invoiceList.html ==> invoice.invoiceList | |
109 | * This is to ensure uniqueness of the document names. | |
110 | * @todo generalize this - unit test this. | |
111 | * @todo we could also look for special attributes like TITLE which have text in them. | |
112 | */ | |
113 | private void translateDocument(File file) { | |
114 | ||
115 | 0 | String filename = file.getAbsolutePath(); |
116 | 0 | String sourcefolder = new File(sourceFolder).getAbsolutePath(); |
117 | ||
118 | 0 | ps.println("file: " + filename); |
119 | 0 | ps.println("sourceFolder: " + sourcefolder); |
120 | ||
121 | 0 | String docname = filename.substring(sourcefolder.length() + 1); |
122 | 0 | ps.println(docname); |
123 | 0 | int n = docname.indexOf("."); |
124 | 0 | if (n > -1) { |
125 | 0 | docname = docname.substring(0, n); |
126 | } | |
127 | 0 | ps.println(docname); |
128 | 0 | docname = docname.replace("/", "."); |
129 | 0 | docname = docname.replace("\\", "."); |
130 | 0 | ps.println(docname); |
131 | ||
132 | try { | |
133 | 0 | Document document = new Document(filename); |
134 | HTMLElement element; | |
135 | StringBuilder docAndFormName; | |
136 | 0 | for (int j = 0; j < document.getFormCount(); j++) { |
137 | 0 | docAndFormName = new StringBuilder(); |
138 | 0 | docAndFormName.append(docname).append('.'); |
139 | 0 | Form form = (Form) document.forms(j); |
140 | 0 | if (form.getName().trim().length() > 0) { |
141 | 0 | docAndFormName.append(form.getName()).append('.'); |
142 | } else { | |
143 | 0 | docAndFormName.append("form").append(j + 1).append('.'); |
144 | } | |
145 | ||
146 | String keyName; | |
147 | 0 | for (int k = 0; k < document.forms(j).getElementCount(); k++) { |
148 | 0 | element = document.forms(j).elements(k); |
149 | //@todo the code below often falls under the ELSE keyName = "element" + k; for some reason | |
150 | //@todo decide - just use getName since it's ID or NAME and if empty don't create a property | |
151 | /* | |
152 | if (element.getName().trim().length() == 0) { | |
153 | keyName = element.getAttribute("id"); | |
154 | } else { | |
155 | keyName = element.getName(); | |
156 | } | |
157 | ||
158 | if (keyName == null || keyName.trim().length() == 0) { | |
159 | if (element.hasAttribute("type") && element.getAttribute("type").equalsIgnoreCase("submit")) { | |
160 | keyName = "Submit"; | |
161 | } | |
162 | } else { | |
163 | keyName = "element" + k; | |
164 | } | |
165 | */ | |
166 | 0 | keyName = element.getName(); |
167 | 0 | keyName = docAndFormName + keyName; |
168 | ||
169 | 0 | if (!(element instanceof Text) && element instanceof Label) { |
170 | 0 | ps.println("object: " + keyName + " value: " + element.getValue()); |
171 | 0 | if (element.getValue() != null) { |
172 | 0 | properties.put(keyName, element.getValue()); |
173 | } | |
174 | } else | |
175 | 0 | if (element.hasAttribute("type") && (element.getAttribute("type").equalsIgnoreCase("button") || element.getAttribute("type").equalsIgnoreCase("submit"))) { |
176 | 0 | ps.println("object: " + keyName + " value: " + element.getValue()); |
177 | 0 | if (element.getValue() != null) { |
178 | 0 | properties.put(keyName, element.getValue()); |
179 | } | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | 0 | } catch (IOException e) { |
185 | 0 | ps.println(e.getMessage() + "\n" + e.getStackTrace()); |
186 | 0 | System.exit(1); |
187 | 0 | } |
188 | ||
189 | 0 | } |
190 | ||
191 | /* | |
192 | private void x() { | |
193 | Properties properties = new Properties(); | |
194 | //@todo - so many issues here. Null pointer possibility, What value for objects? Which objects? Should they include html? NO! Wrong place for the code. Where to create the file? | |
195 | ||
196 | FileOutputStream out = null; | |
197 | FileInputStream in = null; | |
198 | try { | |
199 | File f = new File("resources/application.properties"); | |
200 | f.createNewFile(); | |
201 | in = new FileInputStream(f); | |
202 | properties.load(in); | |
203 | out = new FileOutputStream(f); | |
204 | ||
205 | Form ff; | |
206 | StringBuilder objectName; | |
207 | ||
208 | log.info("FILENAME: " + this.fileName); | |
209 | String docName; | |
210 | docName = this.fileName.substring(this.fileName.lastIndexOf("/") + 1); | |
211 | docName = docName.substring(0, docName.indexOf(".")); | |
212 | log.info("DOC NAME: " + docName); | |
213 | for (int j = 0; j < forms.size(); j++) { | |
214 | objectName = new StringBuilder(); | |
215 | objectName.append(docName).append('.'); | |
216 | ||
217 | ff = (Form) forms(j); | |
218 | if (ff.name.trim().length() > 0) { | |
219 | objectName.append(ff.name).append('.'); | |
220 | } else { | |
221 | objectName.append("form").append(j + 1).append('.'); | |
222 | } | |
223 | ||
224 | String elementName; | |
225 | HTMLElement element; | |
226 | for (int k = 0; k < ff.elements.size(); k++) { | |
227 | element = ff.elements(k); | |
228 | if (element.hasAttribute("name")) { | |
229 | elementName = element.getAttribute("name"); | |
230 | } else if (element.hasAttribute("id")) { | |
231 | elementName = element.getAttribute("id"); | |
232 | } else { | |
233 | elementName = element.getTag().toString() + k; | |
234 | } | |
235 | if (element.getValue() != null) { | |
236 | properties.put(objectName + elementName, element.getValue()); | |
237 | } | |
238 | } | |
239 | } | |
240 | properties.store(out, "---Locale strings written by jolene---"); | |
241 | out.close(); | |
242 | in.close(); | |
243 | } finally { | |
244 | if (out != null) { | |
245 | try { | |
246 | out.close(); | |
247 | } catch (IOException e) { | |
248 | log.warn(e.getMessage(), e); | |
249 | } | |
250 | try { | |
251 | in.close(); | |
252 | } catch (IOException e) { | |
253 | log.warn(e.getMessage(), e); | |
254 | } | |
255 | ||
256 | } | |
257 | } | |
258 | ||
259 | } | |
260 | */ | |
261 | ||
262 | ||
263 | } |