## Package: XetexBuilder

### XetexBuilder

nameinstructionbranchcomplexitylinemethod
XetexBuilder()
 M: 17 C: 0 0%
 M: 0 C: 0 100%
 M: 1 C: 0 0%
 M: 5 C: 0 0%
 M: 1 C: 0 0%
 M: 11 C: 0 0%
 M: 2 C: 0 0%
 M: 2 C: 0 0%
 M: 3 C: 0 0%
 M: 1 C: 0 0%
getResource(String, String)
 M: 25 C: 0 0%
 M: 4 C: 0 0%
 M: 3 C: 0 0%
 M: 9 C: 0 0%
 M: 1 C: 0 0%
latexQuote(String)
 M: 32 C: 0 0%
 M: 0 C: 0 100%
 M: 1 C: 0 0%
 M: 1 C: 0 0%
 M: 1 C: 0 0%
makeOutputFolder(IProject)
 M: 31 C: 0 0%
 M: 4 C: 0 0%
 M: 3 C: 0 0%
 M: 8 C: 0 0%
 M: 1 C: 0 0%
prepare(IProject, Dialect)
 M: 5 C: 0 0%
 M: 0 C: 0 100%
 M: 1 C: 0 0%
 M: 2 C: 0 0%
 M: 1 C: 0 0%
 M: 29 C: 0 0%
 M: 2 C: 0 0%
 M: 2 C: 0 0%
 M: 6 C: 0 0%
 M: 1 C: 0 0%
saveDocument(IProject, File, String, boolean)
 M: 144 C: 0 0%
 M: 6 C: 0 0%
 M: 4 C: 0 0%
 M: 24 C: 0 0%
 M: 1 C: 0 0%
writeFile(File, String, String)
 M: 23 C: 0 0%
 M: 0 C: 0 100%
 M: 1 C: 0 0%
 M: 6 C: 0 0%
 M: 1 C: 0 0%

## Coverage

`1: /*2: * #%~3: * org.overture.ide.plugins.latex4: * %%5: * Copyright (C) 2008 - 2014 Overture6: * %%7: * This program is free software: you can redistribute it and/or modify8: * it under the terms of the GNU General Public License as9: * published by the Free Software Foundation, either version 3 of the10: * License, or (at your option) any later version.11: * 12: * This program is distributed in the hope that it will be useful,13: * but WITHOUT ANY WARRANTY; without even the implied warranty of14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15: * GNU General Public License for more details.16: * 17: * You should have received a copy of the GNU General Public18: * License along with this program. If not, see19: * <http://www.gnu.org/licenses/gpl-3.0.html>.20: * #~%21: */22: package org.overture.ide.plugins.latex.utility;23: 24: import java.io.BufferedWriter;25: import java.io.File;26: import java.io.FileWriter;27: import java.io.IOException;28: import java.io.InputStreamReader;29: import java.net.MalformedURLException;30: import java.net.URL;31: import java.util.List;32: import java.util.Vector;33: 34: import org.eclipse.core.resources.IProject;35: import org.eclipse.core.runtime.Platform;36: import org.eclipse.ui.internal.util.BundleUtility;37: import org.osgi.framework.Bundle;38: import org.overture.ast.lex.Dialect;39: import org.overture.ide.core.resources.IVdmProject;40: import org.overture.ide.plugins.latex.LatexPlugin;41: 42: @SuppressWarnings("restriction")43: public class XetexBuilder implements PdfBuilder44: {45:         final static String OUTPUT_FOLDER_NAME = "latex";46:         final String PROJECT_INCLUDE_MODEL_FILES = "%PROJECT_INCLUDE_MODEL_FILES";47:         final String TITLE = "%TITLE";48:         File outputFolder = null;49:         List<String> includes = new Vector<String>();50: 51:         public void prepare(IProject project, Dialect dialect) throws IOException52:         {53:                 outputFolder = makeOutputFolder(project);54:                 // String overturesty = readFile("latex/overture.sty");55:                 //56:                 // String overturelanguagedef = readFile("latex/overturelanguagedef.sty");57:                 //58:                 // overturesty = overturesty.replaceAll("OVERTURE_LANGUAGE", dialect.toString());59:                 //60:                 // writeFile(outputFolder, "overture.sty", overturesty);61:                 // writeFile(outputFolder, "overturelanguagedef.sty", overturelanguagedef);62: 63:         }64: 65:         public void saveDocument(IProject project, File projectRoot, String name, boolean modelOnly)66:                         throws IOException67:         {68:                 String document = readFile("xetex/document.tex");69:                 String documentFileName = name;// + ".tex";70:                 File latexRoot = makeOutputFolder(project);71:                 StringBuilder sb = new StringBuilder();72:                 String title = // "Coverage Report: " +73:                 projectRoot.getName().replace('\\', '/').substring(0, projectRoot.getName().length());74: 75:•                for (String path : includes)76:                 {77: 78:                         String includeName = path;79:                         includeName = includeName.substring(0, includeName.lastIndexOf('.'));80:                         includeName = includeName.substring(0, includeName.lastIndexOf('.'));81:                         String tmp = includeName.replace('\\', '/');82:                         includeName = tmp.substring(tmp.lastIndexOf('/') + 1);83: 84:•                        if(modelOnly){85:                         sb.append("\n" + "\\section{" + latexQuote(includeName) + "}");86:                         }87: 88:•                        if (path.contains(latexRoot.getAbsolutePath()))89:                         {90:                                 path = path.substring(latexRoot.getAbsolutePath().length());91:                                 // sb.append("\n" + "\\input{" + (".." + path).replace('\\',92:                                 // '/')93:                                 // + "}");94:                                 sb.append("\n" + "\\input{"95:                                                 + path.replace('\\', '/').substring(1, path.length())96:                                                 + "}");97:                         } else98:                         {99:                                 sb.append("\n" + "\\input{" + path.replace('\\', '/') + "}");100:                         }101: 102:                 }103:                 document = document.replace(TITLE, latexQuote(title)).replace(PROJECT_INCLUDE_MODEL_FILES, sb.toString());104: 105:                 writeFile(outputFolder, documentFileName, document);106:         }107: 108:         private String latexQuote(String s)109:         {110:                 // Latex specials: \# \\$ \% \^{} \& \_ \{ \} \~{} \\111: 112:                 return s.replace("\\", "\\textbackslash ").replace("#", "\\#").replace("\$", "\\\$").replace("%", "\\%").replace("&", "\\&").replace("_", "\\_").replace("{", "\\{").replace("}", "\\}").replace("~", "\\~").replaceAll("\\^{1}", "\\\\^{}");113:         }114: 115:         public void addInclude(String path)116:         {117:•                if (!includes.contains(path))118:                 {119:                         includes.add(path);120:                 }121:         }122: 123:         public static File makeOutputFolder(IProject project)124:         {125:                 // File projectRoot = project.getLocation().toFile();126:                 // return makeOutputFolder(project);127:                 // }128:                 //129:                 // public static File makeOutputFolder(IProject project)130:                 // {131: 132:                 IVdmProject p = (IVdmProject) project.getAdapter(IVdmProject.class);133:                 File outputFolder = p.getModelBuildPath().getOutput().getLocation().toFile();// new File(projectRoot,134:                                                                                                                                                                                 // "generated");135:•                if (!outputFolder.exists())136:                 {137:                         outputFolder.mkdirs();138:                 }139: 140:                 File latexoutput = new File(outputFolder, OUTPUT_FOLDER_NAME);141:•                if (!latexoutput.exists())142:                 {143:                         latexoutput.mkdirs();144:                 }145: 146:                 return latexoutput;147:         }148: 149:         private static String readFile(String relativePath) throws IOException150:         {151:                 URL tmp = getResource(LatexPlugin.PLUGIN_ID, relativePath);152: 153:                 InputStreamReader reader = new InputStreamReader(tmp.openStream());154:                 // Create Buffered/PrintWriter Objects155:                 // BufferedReader inputStream = new BufferedReader(bin);156:                 StringBuilder sb = new StringBuilder();157: 158:                 int inLine;159:•                while ((inLine = reader.read()) != -1)160:                 {161:                         sb.append((char) inLine);162:                 }163:                 return sb.toString();164:         }165: 166:         private void writeFile(File outputFolder, String fileName, String content)167:                         throws IOException168:         {169:                 FileWriter outputFileReader = new FileWriter(new File(outputFolder, fileName), false);170:                 BufferedWriter outputStream = new BufferedWriter(outputFileReader);171:                 outputStream.write(content);172:                 outputStream.close();173:                 outputFileReader.close();174:         }175: 176:         public static URL getResource(String pluginId, String path)177:         {178:                 // if the bundle is not ready then there is no image179:                 Bundle bundle = Platform.getBundle(pluginId);180:•                if (!BundleUtility.isReady(bundle))181:                 {182:                         return null;183:                 }184: 185:                 // look for the image (this will check both the plugin and fragment186:                 // folders187:                 URL fullPathString = BundleUtility.find(bundle, path);188:•                if (fullPathString == null)189:                 {190:                         try191:                         {192:                                 fullPathString = new URL(path);193:                         } catch (MalformedURLException e)194:                         {195:                                 return null;196:                         }197:                 }198: 199:                 return fullPathString;200: 201:         }202: }`