Generator.java
Go to the documentation of this file.
00001 /*
00002  * Generator.java
00003  * Copyright (c) 2015, Kacper Sokol
00004  *
00005  * All rights reserved.
00006  *
00007  * Software License Agreement (BSD License)
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  *     * Redistributions of source code must retain the above copyright
00013  *       notice, this list of conditions and the following disclaimer.
00014  *     * Redistributions in binary form must reproduce the above copyright
00015  *       notice, this list of conditions and the following disclaimer in the
00016  *       documentation and/or other materials provided with the distribution.
00017  *     * Neither the name of Universitaet Bremen nor the names of its
00018  *       contributors may be used to endorse or promote products derived from
00019  *       this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
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   // Get all necessary paths
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     // test JLR
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     // the order must be preserved
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     // escape all LaTeX characters
00140     String[] escapedArguments = latexTheStrings(arguments);
00141 
00142     // create absolute paths
00143     File tempDir = new File(tempPath);
00144     File texOutput = new File(tempDir.getAbsolutePath() + File.separator +
00145       "rc.tex");
00146 
00147     // check if temporary directory exists
00148     if (!tempDir.isDirectory()) {
00149       System.out.println("Couldn't find temp dir: " + tempPath + "; creating!");
00150       tempDir.mkdir();
00151     }
00152 
00153     try {
00154       // Create LaTeX conversion engine
00155       JLRConverter converter = new JLRConverter(sourceDir);
00156 
00157       // up to 10 sections are supported ATM
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       // Substitute LaTeX variables
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       // fill with blank conversion up to 10 LaTeX variables
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       // Generate PDF in temp folder
00178       JLRGenerator pdfGen = new JLRGenerator();
00179 
00180       if (!pdfGen.generate(texOutput, tempDir, sourceDir)) {
00181         System.err.println(pdfGen.getErrorMessage());
00182       }
00183 
00184       // JLROpener.open(pdfGen.getPDF());
00185 
00186     } catch (IOException ex) {
00187       System.err.println(ex.getMessage());
00188     }
00189 
00190     // return the path to the created report card
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     // escape all LaTeX characters
00207     String[] escapedArguments = latexTheStrings(arguments);
00208 
00209     // create absolute paths
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     // check if temporary directory exists
00218     if (!tempDir.isDirectory()) {
00219       System.out.println("Couldn't find temp dir: " + tempPath + "; creating!");
00220       tempDir.mkdir();
00221     }
00222 
00223     try {
00224       // Create LaTeX conversion engine
00225       JLRConverter converter = new JLRConverter(sourceDir);
00226 
00227       // up to 20 arguments per section are supported ATM
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       // Substitute LaTeX variables
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     // return the path to the created section
00248     return tempDir.getAbsolutePath() + File.separator + section + ".tex";
00249 
00250   }
00251 
00252 }


report_card
Author(s): Kacper Sokol
autogenerated on Thu Jun 6 2019 22:05:49