Package: Xetex

Xetex

nameinstructionbranchcomplexitylinemethod
Xetex(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 Xetex 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 Xetex(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:                         String argument = "xelatex " + documentName; // modified by his 2013/10/08
97:                         cw.println("Starting: " + argument + "\nIn: "
98:                                         + outputFolder.getAbsolutePath());
99:                         ProcessBuilder pb = new ProcessBuilder(argument);
100:•                        if (currentOS.equals(Platform.OS_MACOSX))
101:                         { // fix for MacOS
102:                                 String osxpath = LatexPlugin.getDefault().getPreferenceStore().getString(ILatexConstants.OSX_LATEX_PATH_PREFERENCE);
103:•                                if (osxpath.equals(""))
104:                                 {
105:                                         pb.command("/Library/TeX/texbin/xelatex", "-interaction=nonstopmode", documentName); // modified by his
106:                                                                                                                                                                                                         // 2013/10/08
107:                                 } else
108:                                 {
109:                                         pb.command(osxpath, "-interaction=nonstopmode", documentName);
110:                                 }
111:                         } else
112:                         {
113:                                 // pb.command("pdflatex", "-interaction=nonstopmode", "\""
114:                                 // + documentName + "\"");
115:                                 pb.command("xelatex", "-interaction=nonstopmode", "\""
116:                                                 + documentName + "\""); // modified by his 2013/10/08
117:                         }
118:
119:                         pb.directory(outputFolder);
120:
121:                         process = pb.start();
122:                         new ProcessConsolePrinter(cw, process.getInputStream(), this).start();
123:                         new ProcessConsolePrinter(cw, process.getErrorStream(), this).start();
124:
125:                         process.waitFor();
126:
127:•                        if (project != null)
128:                         {
129:                                 project.refreshLocal(IResource.DEPTH_INFINITE, null);
130:                         }
131:
132:•                        if (latexFailed)
133:                         {
134:                                 this.failed = true;
135:                         }
136:
137:                         finished = true;
138:                 } catch (IOException e)
139:                 {
140:                         this.failed = true;
141:                         // TODO Auto-generated catch block
142:                         e.printStackTrace();
143:                 } catch (CoreException e)
144:                 {
145:                         this.failed = true;
146:                         // TODO Auto-generated catch block
147:                         e.printStackTrace();
148:                 } catch (InterruptedException e)
149:                 {
150:                         this.failed = true;
151:                         // TODO Auto-generated catch block
152:                         e.printStackTrace();
153:                 }
154:
155:         }
156:
157:         public static String getRelativePath(File file, File relativeTo)
158:                         throws IOException
159:         {
160:                 file = new File(file + File.separator + "89243jmsjigs45u9w43545lkhj7").getParentFile();
161:                 relativeTo = new File(relativeTo + File.separator
162:                                 + "984mvcxbsfgqoykj30487df556").getParentFile();
163:                 File origFile = file;
164:                 File origRelativeTo = relativeTo;
165:                 List<File> filePathStack = new ArrayList<File>();
166:                 List<File> relativeToPathStack = new ArrayList<File>();
167:                 // build the path stack info to compare it afterwards
168:                 file = file.getCanonicalFile();
169:•                while (file != null)
170:                 {
171:                         filePathStack.add(0, file);
172:                         file = file.getParentFile();
173:                 }
174:                 relativeTo = relativeTo.getCanonicalFile();
175:•                while (relativeTo != null)
176:                 {
177:                         relativeToPathStack.add(0, relativeTo);
178:                         relativeTo = relativeTo.getParentFile();
179:                 }
180:                 // compare as long it goes
181:                 int count = 0;
182:                 file = (File) filePathStack.get(count);
183:                 relativeTo = (File) relativeToPathStack.get(count);
184:•                while (count < filePathStack.size() - 1
185:•                                && count < relativeToPathStack.size() - 1
186:•                                && file.equals(relativeTo))
187:                 {
188:                         count++;
189:                         file = (File) filePathStack.get(count);
190:                         relativeTo = (File) relativeToPathStack.get(count);
191:                 }
192:•                if (file.equals(relativeTo))
193:                 {
194:                         count++;
195:                 }
196:                 // up as far as necessary
197:                 StringBuffer relString = new StringBuffer();
198:•                for (int i = count; i < relativeToPathStack.size(); i++)
199:                 {
200:                         relString.append(".." + File.separator);
201:                 }
202:                 // now back down to the file
203:•                for (int i = count; i < filePathStack.size() - 1; i++)
204:                 {
205:                         relString.append(((File) filePathStack.get(i)).getName()
206:                                         + File.separator);
207:                 }
208:                 relString.append(((File) filePathStack.get(filePathStack.size() - 1)).getName());
209:                 // just to test
210:                 File relFile = new File(origRelativeTo.getAbsolutePath()
211:                                 + File.separator + relString.toString());
212:•                if (!relFile.getCanonicalFile().equals(origFile.getCanonicalFile()))
213:                 {
214:                         throw new IOException("Failed to find relative path.");
215:                 }
216:                 return relString.toString();
217:         }
218:
219:         @Override
220:         public boolean isFinished()
221:         {
222:                 return finished;
223:         }
224:
225:         @Override
226:         public boolean hasFailed()
227:         {
228:                 return failed;
229:         }
230: }