Package: LatexBuilder

LatexBuilder

nameinstructionbranchcomplexitylinemethod
LatexBuilder()
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%
addInclude(String)
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%
readFile(String)
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.latex
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * 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 of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <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 LatexBuilder implements PdfBuilder
44: {
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:         /*
52:          * (non-Javadoc)
53:          * @see org.overture.ide.plugins.latex.utility.PdfBuilder#prepare(org.eclipse.core.resources.IProject,
54:          * org.overture.ast.lex.Dialect)
55:          */
56:         @Override
57:         public void prepare(IProject project, Dialect dialect) throws IOException
58:         {
59:                 outputFolder = makeOutputFolder(project);
60:                 // String overturesty = readFile("latex/overture.sty");
61:                 //
62:                 // String overturelanguagedef = readFile("latex/overturelanguagedef.sty");
63:                 //
64:                 // overturesty = overturesty.replaceAll("OVERTURE_LANGUAGE", dialect.toString());
65:                 //
66:                 // writeFile(outputFolder, "overture.sty", overturesty);
67:                 // writeFile(outputFolder, "overturelanguagedef.sty", overturelanguagedef);
68:
69:         }
70:
71:         /*
72:          * (non-Javadoc)
73:          * @see org.overture.ide.plugins.latex.utility.PdfBuilder#saveDocument(org.eclipse.core.resources.IProject,
74:          * java.io.File, java.lang.String)
75:          */
76:         @Override
77:         public void saveDocument(IProject project, File projectRoot, String name, boolean modelOnly)
78:                         throws IOException
79:         {
80:                 String document = readFile("latex/document.tex");
81:                 String documentFileName = name;// + ".tex";
82:                 File latexRoot = makeOutputFolder(project);
83:                 StringBuilder sb = new StringBuilder();
84:                 String title = // "Coverage Report: " +
85:                 projectRoot.getName().replace('\\', '/').substring(0, projectRoot.getName().length());
86:
87:•                for (String path : includes)
88:                 {
89:
90:                         String includeName = path;
91:                         includeName = includeName.substring(0, includeName.lastIndexOf('.'));
92:                         includeName = includeName.substring(0, includeName.lastIndexOf('.'));
93:                         String tmp = includeName.replace('\\', '/');
94:                         includeName = tmp.substring(tmp.lastIndexOf('/') + 1);
95:
96:•                        if(modelOnly){
97:                         sb.append("\n" + "\\section{" + latexQuote(includeName) + "}");
98:                         }
99:
100:•                        if (path.contains(latexRoot.getAbsolutePath()))
101:                         {
102:                                 path = path.substring(latexRoot.getAbsolutePath().length());
103:                                 // sb.append("\n" + "\\input{" + (".." + path).replace('\\',
104:                                 // '/')
105:                                 // + "}");
106:                                 sb.append("\n" + "\\input{"
107:                                                 + path.replace('\\', '/').substring(1, path.length())
108:                                                 + "}");
109:                         } else
110:                         {
111:                                 sb.append("\n" + "\\input{" + path.replace('\\', '/') + "}");
112:                         }
113:
114:                 }
115:                 document = document.replace(TITLE, latexQuote(title)).replace(PROJECT_INCLUDE_MODEL_FILES, sb.toString());
116:
117:                 writeFile(outputFolder, documentFileName, document);
118:         }
119:
120:         private String latexQuote(String s)
121:         {
122:                 // Latex specials: \# \$ \% \^{} \& \_ \{ \} \~{} \\
123:
124:                 return s.replace("\\", "\\textbackslash ").replace("#", "\\#").replace("$", "\\$").replace("%", "\\%").replace("&", "\\&").replace("_", "\\_").replace("{", "\\{").replace("}", "\\}").replace("~", "\\~").replaceAll("\\^{1}", "\\\\^{}");
125:         }
126:
127:         /*
128:          * (non-Javadoc)
129:          * @see org.overture.ide.plugins.latex.utility.PdfBuilder#addInclude(java.lang.String)
130:          */
131:         @Override
132:         public void addInclude(String path)
133:         {
134:•                if (!includes.contains(path))
135:                 {
136:                         includes.add(path);
137:                 }
138:         }
139:
140:         public static File makeOutputFolder(IProject project)
141:         {
142:                 // File projectRoot = project.getLocation().toFile();
143:                 // return makeOutputFolder(project);
144:                 // }
145:                 //
146:                 // public static File makeOutputFolder(IProject project)
147:                 // {
148:
149:                 IVdmProject p = (IVdmProject) project.getAdapter(IVdmProject.class);
150:                 File outputFolder = p.getModelBuildPath().getOutput().getLocation().toFile();// new File(projectRoot,
151:                                                                                                                                                                                 // "generated");
152:•                if (!outputFolder.exists())
153:                 {
154:                         outputFolder.mkdirs();
155:                 }
156:
157:                 File latexoutput = new File(outputFolder, OUTPUT_FOLDER_NAME);
158:•                if (!latexoutput.exists())
159:                 {
160:                         latexoutput.mkdirs();
161:                 }
162:
163:                 return latexoutput;
164:         }
165:
166:         private static String readFile(String relativePath) throws IOException
167:         {
168:                 URL tmp = getResource(LatexPlugin.PLUGIN_ID, relativePath);
169:
170:                 InputStreamReader reader = new InputStreamReader(tmp.openStream());
171:                 // Create Buffered/PrintWriter Objects
172:                 // BufferedReader inputStream = new BufferedReader(bin);
173:                 StringBuilder sb = new StringBuilder();
174:
175:                 int inLine;
176:•                while ((inLine = reader.read()) != -1)
177:                 {
178:                         sb.append((char) inLine);
179:                 }
180:                 return sb.toString();
181:         }
182:
183:         private void writeFile(File outputFolder, String fileName, String content)
184:                         throws IOException
185:         {
186:                 FileWriter outputFileReader = new FileWriter(new File(outputFolder, fileName), false);
187:                 BufferedWriter outputStream = new BufferedWriter(outputFileReader);
188:                 outputStream.write(content);
189:                 outputStream.close();
190:                 outputFileReader.close();
191:         }
192:
193:         public static URL getResource(String pluginId, String path)
194:         {
195:                 // if the bundle is not ready then there is no image
196:                 Bundle bundle = Platform.getBundle(pluginId);
197:•                if (!BundleUtility.isReady(bundle))
198:                 {
199:                         return null;
200:                 }
201:
202:                 // look for the image (this will check both the plugin and fragment
203:                 // folders
204:                 URL fullPathString = BundleUtility.find(bundle, path);
205:•                if (fullPathString == null)
206:                 {
207:                         try
208:                         {
209:                                 fullPathString = new URL(path);
210:                         } catch (MalformedURLException e)
211:                         {
212:                                 return null;
213:                         }
214:                 }
215:
216:                 return fullPathString;
217:
218:         }
219: }