Package: PdfLatex

PdfLatex

nameinstructionbranchcomplexitylinemethod
PdfLatex(IProject, File, String)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getRelativePath(File, File)
M: 201 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 38 C: 0
0%
M: 1 C: 0
0%
hasFailed()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isFinished()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
kill()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
run()
M: 208 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 37 C: 0
0%
M: 1 C: 0
0%
setFail(boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.File;
25: import java.io.IOException;
26: import java.util.ArrayList;
27: import java.util.List;
28:
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.Platform;
33: import org.overture.ide.plugins.latex.ILatexConstants;
34: import org.overture.ide.plugins.latex.LatexPlugin;
35: import org.overture.ide.ui.internal.util.ConsoleWriter;
36:
37: public class PdfLatex extends Thread implements PdfGenerator
38: {
39:
40:         private String documentName;
41:         private IProject project;
42:         private File outputFolder;
43:
44:         private Process process;
45:         public boolean finished = false;
46:         public boolean failed = false;
47:         private String currentOS = null;
48:         private boolean latexFailed = false;
49:
50:         public PdfLatex(IProject project, File outputFolder, String documentName)
51:         {
52:                 this.documentName = documentName;
53:                 this.outputFolder = outputFolder;
54:                 this.project = project;
55:                 currentOS = Platform.getOS();
56:         }
57:
58:         public void kill()
59:         {
60:                 try
61:                 {
62:                         process.destroy();
63:                 } catch (Exception e)
64:                 {
65:                 }
66:         }
67:
68:         public void setFail(boolean b)
69:         {
70:                 this.latexFailed = b;
71:         }
72:
73:         @Override
74:         public void run()
75:         {
76:
77:                 ConsoleWriter cw = new ConsoleWriter("PDF LATEX");
78:                 cw.show();
79:                 try
80:                 {
81:
82:•                        if (documentName.contains(new Character(File.separatorChar).toString()))
83:                         {
84:                                 // Map<String, String> env = pb.environment();
85:                                 // // watch out here! this could be �PATH� or �path�
86:                                 // // Windows doesn�t care, but Java will
87:                                 // String path = (String) env.get("PATH");
88:                                 // env.put("PATH", path + File.pathSeparator
89:                                 // + new File(documentName).getParentFile().getAbsolutePath());
90:                                 // path = (String) env.get("PATH");
91:
92:                                 documentName = getRelativePath(new File(documentName), outputFolder);
93:                         }
94:
95:                         String argument = "pdflatex " + documentName;
96:                         cw.println("Starting: " + argument + "\nIn: "
97:                                         + outputFolder.getAbsolutePath());
98:                         ProcessBuilder pb = new ProcessBuilder(argument);
99:•                        if (currentOS.equals(Platform.OS_MACOSX))
100:                         { // fix for MacOS
101:                                 String osxpath = LatexPlugin.getDefault().getPreferenceStore().getString(ILatexConstants.OSX_LATEX_PATH_PREFERENCE);
102:•                                if (osxpath.equals(""))
103:                                 {
104:                                         pb.command(ILatexConstants.DEFAULT_OSX_LATEX_PATH, "-interaction=nonstopmode", documentName);
105:                                 } else
106:                                 {
107:                                         pb.command(osxpath, "-interaction=nonstopmode", documentName);
108:                                 }
109:                         } else
110:                         {
111:                                 pb.command("pdflatex", "-interaction=nonstopmode", "\""
112:                                                 + documentName + "\"");
113:                         }
114:
115:                         pb.directory(outputFolder);
116:
117:                         process = pb.start();
118:                         new ProcessConsolePrinter(cw, process.getInputStream(), this).start();
119:                         new ProcessConsolePrinter(cw, process.getErrorStream(), this).start();
120:
121:                         process.waitFor();
122:
123:•                        if (project != null)
124:                         {
125:                                 project.refreshLocal(IResource.DEPTH_INFINITE, null);
126:                         }
127:
128:•                        if (latexFailed)
129:                         {
130:                                 this.failed = true;
131:                         }
132:
133:                         finished = true;
134:                 } catch (IOException e)
135:                 {
136:                         this.failed = true;
137:                         // TODO Auto-generated catch block
138:                         e.printStackTrace();
139:                 } catch (CoreException e)
140:                 {
141:                         this.failed = true;
142:                         // TODO Auto-generated catch block
143:                         e.printStackTrace();
144:                 } catch (InterruptedException e)
145:                 {
146:                         this.failed = true;
147:                         // TODO Auto-generated catch block
148:                         e.printStackTrace();
149:                 }
150:
151:         }
152:
153:         public static String getRelativePath(File file, File relativeTo)
154:                         throws IOException
155:         {
156:                 file = new File(file + File.separator + "89243jmsjigs45u9w43545lkhj7").getParentFile();
157:                 relativeTo = new File(relativeTo + File.separator
158:                                 + "984mvcxbsfgqoykj30487df556").getParentFile();
159:                 File origFile = file;
160:                 File origRelativeTo = relativeTo;
161:                 List<File> filePathStack = new ArrayList<File>();
162:                 List<File> relativeToPathStack = new ArrayList<File>();
163:                 // build the path stack info to compare it afterwards
164:                 file = file.getCanonicalFile();
165:•                while (file != null)
166:                 {
167:                         filePathStack.add(0, file);
168:                         file = file.getParentFile();
169:                 }
170:                 relativeTo = relativeTo.getCanonicalFile();
171:•                while (relativeTo != null)
172:                 {
173:                         relativeToPathStack.add(0, relativeTo);
174:                         relativeTo = relativeTo.getParentFile();
175:                 }
176:                 // compare as long it goes
177:                 int count = 0;
178:                 file = (File) filePathStack.get(count);
179:                 relativeTo = (File) relativeToPathStack.get(count);
180:•                while (count < filePathStack.size() - 1
181:•                                && count < relativeToPathStack.size() - 1
182:•                                && file.equals(relativeTo))
183:                 {
184:                         count++;
185:                         file = (File) filePathStack.get(count);
186:                         relativeTo = (File) relativeToPathStack.get(count);
187:                 }
188:•                if (file.equals(relativeTo))
189:                 {
190:                         count++;
191:                 }
192:                 // up as far as necessary
193:                 StringBuffer relString = new StringBuffer();
194:•                for (int i = count; i < relativeToPathStack.size(); i++)
195:                 {
196:                         relString.append(".." + File.separator);
197:                 }
198:                 // now back down to the file
199:•                for (int i = count; i < filePathStack.size() - 1; i++)
200:                 {
201:                         relString.append(((File) filePathStack.get(i)).getName()
202:                                         + File.separator);
203:                 }
204:                 relString.append(((File) filePathStack.get(filePathStack.size() - 1)).getName());
205:                 // just to test
206:                 File relFile = new File(origRelativeTo.getAbsolutePath()
207:                                 + File.separator + relString.toString());
208:•                if (!relFile.getCanonicalFile().equals(origFile.getCanonicalFile()))
209:                 {
210:                         throw new IOException("Failed to find relative path.");
211:                 }
212:                 return relString.toString();
213:         }
214:
215:         @Override
216:         public boolean isFinished()
217:         {
218:                 return finished;
219:         }
220:
221:         @Override
222:         public boolean hasFailed()
223:         {
224:                 return failed;
225:         }
226: }