1 52 53 package freemarker.ext.dom; 54 55 56 import java.io.*; 57 import java.util.*; 58 import freemarker.template.*; 59 60 66 67 public class Transform { 68 69 private File inputFile, ftlFile, outputFile; 70 private String encoding; 71 private Locale locale; 72 private Configuration cfg; 73 74 77 static public void main(String [] args) { 78 try { 79 Transform proc = transformFromArgs(args); 80 proc.transform(); 81 } catch (IllegalArgumentException iae) { 82 System.err.println(iae.getMessage()); 83 usage(); 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 87 } 88 89 96 97 Transform(File inputFile, File ftlFile, File outputFile, Locale locale, String encoding) throws IOException { 98 if (encoding == null) { 99 encoding = System.getProperty("file.encoding"); 100 } 101 if (locale == null) { 102 locale = Locale.getDefault(); 103 } 104 this.encoding = encoding; 105 this.locale = locale; 106 this.inputFile = inputFile; 107 this.ftlFile = ftlFile; 108 this.outputFile = outputFile; 109 File ftlDirectory = ftlFile.getAbsoluteFile().getParentFile(); 110 cfg = new Configuration(); 111 cfg.setDirectoryForTemplateLoading(ftlDirectory); 112 } 113 114 117 void transform() throws Exception { 118 String templateName = ftlFile.getName(); 119 Template template = cfg.getTemplate(templateName, locale); 120 NodeModel rootNode = NodeModel.parse(inputFile); 121 OutputStream outputStream = System.out; 122 if (outputFile != null) { 123 outputStream = new FileOutputStream(outputFile); 124 } 125 Writer outputWriter = new OutputStreamWriter(outputStream, encoding); 126 try { 127 template.process(null, outputWriter, null, rootNode); 128 } finally { 129 if (outputFile != null) 130 outputWriter.close(); 131 } 132 } 133 134 static Transform transformFromArgs(String [] args) throws IOException { 135 int i=0; 136 String input = null, output=null, ftl = null, loc = null, enc = null; 137 while (i<args.length) { 138 String dashArg = args[i++]; 139 if (i>=args.length) { 140 throw new IllegalArgumentException (""); 141 } 142 String arg = args[i++]; 143 if (dashArg.equals("-in")) { 144 if (input != null) { 145 throw new IllegalArgumentException ("The input file should only be specified once"); 146 } 147 input = arg; 148 } else if (dashArg.equals("-ftl")) { 149 if (ftl != null) { 150 throw new IllegalArgumentException ("The ftl file should only be specified once"); 151 } 152 ftl = arg; 153 } else if (dashArg.equals("-out")) { 154 if (output != null) { 155 throw new IllegalArgumentException ("The output file should only be specified once"); 156 } 157 output = arg; 158 } else if (dashArg.equals("-locale")) { 159 if (loc != null) { 160 throw new IllegalArgumentException ("The locale should only be specified once"); 161 } 162 loc = arg; 163 } else if (dashArg.equals("-encoding")) { 164 if (enc != null) { 165 throw new IllegalArgumentException ("The encoding should only be specified once"); 166 } 167 enc = arg; 168 } else { 169 throw new IllegalArgumentException ("Unknown input argument: " + dashArg); 170 } 171 } 172 if (input == null) { 173 throw new IllegalArgumentException ("No input file specified."); 174 } 175 if (ftl == null) { 176 throw new IllegalArgumentException ("No ftl file specified."); 177 } 178 File inputFile = new File(input).getAbsoluteFile(); 179 File ftlFile = new File(ftl).getAbsoluteFile(); 180 if (!inputFile.exists()) { 181 throw new IllegalArgumentException ("Input file does not exist: " + input); 182 } 183 if (!ftlFile.exists()) { 184 throw new IllegalArgumentException ("FTL file does not exist: " + ftl); 185 } 186 if (!inputFile.isFile() || !inputFile.canRead()) { 187 throw new IllegalArgumentException ("Input file must be a readable file: " + input); 188 } 189 if (!ftlFile.isFile() || !ftlFile.canRead()) { 190 throw new IllegalArgumentException ("FTL file must be a readable file: " + ftl); 191 } 192 File outputFile = null; 193 if (output != null) { 194 outputFile = new File(output).getAbsoluteFile(); 195 File outputDirectory = outputFile.getParentFile(); 196 if (!outputDirectory.exists() || !outputDirectory.canWrite()) { 197 throw new IllegalArgumentException ("The output directory must exist and be writable: " + outputDirectory); 198 } 199 } 200 Locale locale = Locale.getDefault(); 201 if (loc != null) { 202 locale = localeFromString(loc); 203 } 204 return new Transform(inputFile, ftlFile, outputFile, locale, enc); 205 } 206 207 static Locale localeFromString(String ls) { 208 if (ls == null) ls = ""; 209 String lang="", country="", variant=""; 210 StringTokenizer st = new StringTokenizer(ls, "_-,"); 211 if (st.hasMoreTokens()) { 212 lang = st.nextToken(); 213 if (st.hasMoreTokens()) { 214 country = st.nextToken(); 215 if (st.hasMoreTokens()) { 216 variant = st.nextToken(); 217 } 218 } 219 return new Locale(lang, country, variant); 220 } else { 221 return Locale.getDefault(); 222 } 223 } 224 225 static void usage() { 226 System.err.println("Usage: java freemarker.ext.dom.Transform -in <xmlfile> -ftl <ftlfile> [-out <outfile>] [-locale <locale>] [-encoding <encoding>]"); 227 System.exit(-1); 228 } 229 } 230 | Popular Tags |