Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 package org.knowrob.report_card;
00035
00036 import java.io.File;
00037 import java.io.IOException;
00038
00039 import org.knowrob.utils.ros.RosUtilities;
00040 import de.nixosoft.jlr.*;
00041
00042 public class Generator {
00043
00044
00045 private static File sourceDir = new File(resolveRelativePath("package://" +
00046 "report_card/report_card/tex_templates"));
00047 private static File template = new File(sourceDir.getAbsolutePath() +
00048 File.separator + "reportCard.tex");
00049
00055 public static void main(String[] args) {
00056 System.out.println("Hello from org.knowrob.report_card.Generator package");
00057
00058 JLRGenerator pdfGen = new JLRGenerator();
00059 }
00060
00070 private static String resolveRelativePath(String strRelativePath) {
00071 String strReturn = strRelativePath;
00072
00073 if(strRelativePath.substring(0, 10).equals("package://")) {
00074 int nNextSlash = strRelativePath.indexOf('/', 11);
00075 String strPkg = strRelativePath.substring(10, nNextSlash);
00076
00077 String strAbs = RosUtilities.rospackFind(strPkg);
00078
00079 strReturn = strAbs + strRelativePath.substring(nNextSlash);
00080 }
00081
00082 return strReturn;
00083 }
00084
00091 private static String[] latexTheStrings(String[] strings) {
00092
00093 String[] latexedStrings = new String[strings.length];
00094
00095 for(int i = 0; i < strings.length; ++i) {
00096 latexedStrings[i] = escapeLatexSpecialCharacters(strings[i]);
00097 }
00098
00099 return latexedStrings;
00100
00101 }
00102
00109 private static String escapeLatexSpecialCharacters(String laText) {
00110
00111
00112 String backSlash = laText.replace("\\", "\\\\");
00113 String openBrace = backSlash.replace("{", "\\{");
00114 String closeBrace = openBrace.replace("}", "\\}");
00115 String underscore = closeBrace.replace("_", "\\_");
00116 String caret = underscore.replace("^", "\\^");
00117 String pound = caret.replace("#", "\\#");
00118 String and = pound.replace("&", "\\&");
00119 String dollar = and.replace("$", "\\$");
00120 String percent = dollar.replace("%", "\\%");
00121 String tilde = percent.replace("~", "\\~");
00122
00123 return tilde;
00124
00125 }
00126
00137 public static String rc(String tempPath, String trialID, String[] arguments) {
00138
00139
00140 String[] escapedArguments = latexTheStrings(arguments);
00141
00142
00143 File tempDir = new File(tempPath);
00144 File texOutput = new File(tempDir.getAbsolutePath() + File.separator +
00145 "rc.tex");
00146
00147
00148 if (!tempDir.isDirectory()) {
00149 System.out.println("Couldn't find temp dir: " + tempPath + "; creating!");
00150 tempDir.mkdir();
00151 }
00152
00153 try {
00154
00155 JLRConverter converter = new JLRConverter(sourceDir);
00156
00157
00158 if(escapedArguments.length > 10) {
00159 System.err.println("Up to 10 sections are supported at the moment; " +
00160 "please extend Java and LaTeX code if you need such feature.");
00161 }
00162
00163
00164 converter.replace("trialID", trialID);
00165 for(int i = 0; i < escapedArguments.length; ++i) {
00166 converter.replace("section" + Integer.toString(i), escapedArguments[i]);
00167 }
00168
00169 for(int i = escapedArguments.length; i < 10; ++i) {
00170 converter.replace("section" + Integer.toString(i), "");
00171 }
00172
00173 if (!converter.parse(template, texOutput)) {
00174 System.err.println(converter.getErrorMessage());
00175 }
00176
00177
00178 JLRGenerator pdfGen = new JLRGenerator();
00179
00180 if (!pdfGen.generate(texOutput, tempDir, sourceDir)) {
00181 System.err.println(pdfGen.getErrorMessage());
00182 }
00183
00184
00185
00186 } catch (IOException ex) {
00187 System.err.println(ex.getMessage());
00188 }
00189
00190
00191 return tempDir.getAbsolutePath() + File.separator + "rc.pdf";
00192
00193 }
00194
00203 public static String section(String tempPath, String section,
00204 String[] arguments) {
00205
00206
00207 String[] escapedArguments = latexTheStrings(arguments);
00208
00209
00210 File tempDir = new File(tempPath);
00211 File texOutput = new File(tempDir.getAbsolutePath() + File.separator +
00212 section + ".tex");
00213 File sectionTemplate = new File(sourceDir.getAbsolutePath() + File.separator
00214 + section + ".tex");
00215
00216
00217
00218 if (!tempDir.isDirectory()) {
00219 System.out.println("Couldn't find temp dir: " + tempPath + "; creating!");
00220 tempDir.mkdir();
00221 }
00222
00223 try {
00224
00225 JLRConverter converter = new JLRConverter(sourceDir);
00226
00227
00228 if(escapedArguments.length > 20) {
00229 System.err.println("Up to 20 arguments per section are supported at " +
00230 "the moment; please extend Java and LaTeX code if you need such " +
00231 "feature.");
00232 }
00233
00234
00235 for(int i = 0; i < escapedArguments.length; ++i) {
00236 converter.replace("argument" + Integer.toString(i), escapedArguments[i]);
00237 }
00238
00239 if (!converter.parse(sectionTemplate, texOutput)) {
00240 System.err.println(converter.getErrorMessage());
00241 }
00242
00243 } catch (IOException ex) {
00244 System.err.println(ex.getMessage());
00245 }
00246
00247
00248 return tempDir.getAbsolutePath() + File.separator + section + ".tex";
00249
00250 }
00251
00252 }