001 package net.sf.jolene.util; 002 003 import net.sf.jolene.dom.*; 004 005 import java.io.*; 006 import java.util.Properties; 007 008 009 /** 010 * Not used. 011 * User: Dan 012 * Date: Jun 1, 2008 013 * Time: 5:22:33 PM 014 * 015 * todo dumb class name - it's not translating anything. 016 */ 017 public class Translator { 018 019 private final static PrintStream ps = System.out; 020 021 /** 022 * java -jar jolene.jar net.sf.jolene.util.Translator 023 * 024 * @param args 2 params are expected: full path to the FOLDER where the html files reside - full path to the output resources file. 025 */ 026 public static void main(String args[]) { 027 028 if (args.length < 2) { 029 ps.println("Translator requires 2 parameters"); 030 ps.println("\t 1) full path to the FOLDER where the html files reside"); 031 ps.println("\t 2) full path to the output resources file"); 032 ps.println(); 033 ps.println("Example:"); 034 ps.println("java -jar jolene.jar d:/projects/myproject/html d:/projects/myproject/resources/application.properties"); 035 System.exit(0); 036 } 037 038 new Translator(args[0], args[1]).translate(); 039 } 040 041 042 private String sourceFolder; 043 private String targetPropertiesFile; 044 private Properties properties; 045 046 Translator(String sourceFolder, String targetPropertiesFile) { 047 this.sourceFolder = sourceFolder; 048 this.targetPropertiesFile = targetPropertiesFile; 049 050 properties = new Properties(); 051 try { 052 053 File f = new File(targetPropertiesFile); 054 f.createNewFile(); 055 InputStream in = new FileInputStream(f); 056 properties.load(in); 057 058 } catch (IOException e) { 059 ps.println(e.getMessage() + "\n" + e.getStackTrace()); 060 System.exit(1); 061 } 062 063 } 064 065 private void translate() { 066 File dir = new File(sourceFolder); 067 ps.println("DIR: " + dir.getAbsolutePath()); 068 translate(dir); 069 070 try { 071 File f = new File(targetPropertiesFile); 072 f.createNewFile(); 073 OutputStream out = new FileOutputStream(f); 074 properties.store(out, "---Locale strings written by jolene---"); 075 } catch (IOException e) { 076 ps.println(e.getMessage() + "\n" + e.getStackTrace()); 077 System.exit(1); 078 } 079 } 080 081 private void translate(File dir) { 082 File[] files = dir.listFiles(); 083 if (files == null) { 084 ps.println("No files found in " + sourceFolder); 085 return; 086 087 } else { 088 for (int i = 0; i < files.length; i++) { 089 if (files[i].isDirectory()) { 090 if (!files[i].getName().equalsIgnoreCase(".svn")) { 091 translate(files[i]); 092 } 093 } else { 094 //String filename = files[i].getAbsolutePath(); 095 translateDocument(files[i]); 096 } 097 } 098 } 099 100 } 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 String filename = file.getAbsolutePath(); 116 String sourcefolder = new File(sourceFolder).getAbsolutePath(); 117 118 ps.println("file: " + filename); 119 ps.println("sourceFolder: " + sourcefolder); 120 121 String docname = filename.substring(sourcefolder.length() + 1); 122 ps.println(docname); 123 int n = docname.indexOf("."); 124 if (n > -1) { 125 docname = docname.substring(0, n); 126 } 127 ps.println(docname); 128 docname = docname.replace("/", "."); 129 docname = docname.replace("\\", "."); 130 ps.println(docname); 131 132 try { 133 Document document = new Document(filename); 134 HTMLElement element; 135 StringBuilder docAndFormName; 136 for (int j = 0; j < document.getFormCount(); j++) { 137 docAndFormName = new StringBuilder(); 138 docAndFormName.append(docname).append('.'); 139 Form form = (Form) document.forms(j); 140 if (form.getName().trim().length() > 0) { 141 docAndFormName.append(form.getName()).append('.'); 142 } else { 143 docAndFormName.append("form").append(j + 1).append('.'); 144 } 145 146 String keyName; 147 for (int k = 0; k < document.forms(j).getElementCount(); k++) { 148 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 keyName = element.getName(); 167 keyName = docAndFormName + keyName; 168 169 if (!(element instanceof Text) && element instanceof Label) { 170 ps.println("object: " + keyName + " value: " + element.getValue()); 171 if (element.getValue() != null) { 172 properties.put(keyName, element.getValue()); 173 } 174 } else 175 if (element.hasAttribute("type") && (element.getAttribute("type").equalsIgnoreCase("button") || element.getAttribute("type").equalsIgnoreCase("submit"))) { 176 ps.println("object: " + keyName + " value: " + element.getValue()); 177 if (element.getValue() != null) { 178 properties.put(keyName, element.getValue()); 179 } 180 } 181 } 182 } 183 184 } catch (IOException e) { 185 ps.println(e.getMessage() + "\n" + e.getStackTrace()); 186 System.exit(1); 187 } 188 189 } 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 }