Package: XetexSourceFile

XetexSourceFile

nameinstructionbranchcomplexitylinemethod
XetexSourceFile(File, String)
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
XetexSourceFile(SourceFile)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkFont(String)
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
createCoverageTable(CoverageUtil)
M: 151 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%
getListingEnvironment()
M: 13 C: 0
0%
M: 4 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isOneByte(String)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
latexLabel(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%
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%
markup(String, List)
M: 93 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
print(PrintWriter, boolean, boolean, boolean, boolean, CoverageUtil)
M: 266 C: 0
0%
M: 36 C: 0
0%
M: 19 C: 0
0%
M: 64 C: 0
0%
M: 1 C: 0
0%
printCoverage(PrintWriter, boolean, CoverageUtil)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
printCoverage(PrintWriter, boolean, boolean, boolean, CoverageUtil)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
utfIncludeCheck(String, Boolean)
M: 142 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 24 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.awt.Font;
25: import java.awt.GraphicsEnvironment;
26: import java.io.BufferedReader;
27: import java.io.File;
28: import java.io.FileInputStream;
29: import java.io.IOException;
30: import java.io.InputStreamReader;
31: import java.io.PrintWriter;
32: import java.util.Collections;
33: import java.util.List;
34: import java.util.Map;
35: import java.util.Vector;
36:
37: import org.overture.ast.intf.lex.ILexLocation;
38: import org.overture.ast.intf.lex.ILexNameToken;
39: import org.overture.ast.lex.CoverageUtil;
40: import org.overture.ast.lex.LexLocation;
41: import org.overture.ast.lex.LexNameList;
42: import org.overture.config.Settings;
43: import org.overture.interpreter.runtime.SourceFile;
44: import org.overture.parser.config.Properties;
45:
46: public class XetexSourceFile extends SourceFile
47: {
48:         private static final String CURLY_BRACKET_VDM_AL = "{vdm_al}";
49:         private static final String CURLY_BRACKET_VDM_SL = "{vdmsl}";
50:         private static final String CURLY_BRACKET_VDM_PP = "{vdmpp}";
51:         private static final String CURLY_BRACKET_VDM_RT = "{vdmrt}";
52:         private static final String BEGIN = "\\begin";
53:         private static final String END = "\\end";
54:         public List<String> rawLines = new Vector<String>();
55:         public final boolean hasVdm_al;
56:         // The argument to lstset is: escapeinside={(*@}{@*)}
57:         public final String LST_ESCAPE_BEGIN = "(*@";
58:         public final String LST_ESCAPE_END = "@*)";
59:         private static final String sectionFont = "{\\jmsg{";
60:         private static final String docFont = "{\\jmsm{";
61:         private static final String fontEnd = "}}";
62:
63:         public boolean useJPNFont;
64:
65:         public XetexSourceFile(SourceFile source) throws IOException
66:         {
67:                 this(source.filename, source.charset);
68:         }
69:
70:         public XetexSourceFile(File filename, String charset) throws IOException
71:         {
72:                 super(filename, charset);
73:
74:                 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), charset));
75:
76:                 String line = br.readLine();
77:                 boolean vdm_al = false;
78:
79:•                while (line != null)
80:                 {
81:•                        if (line.startsWith(BEGIN + CURLY_BRACKET_VDM_AL))
82:                         {
83:                                 vdm_al = true;
84:                         }
85:
86:                         rawLines.add(line);
87:                         line = br.readLine();
88:                 }
89:
90:                 hasVdm_al = vdm_al;
91:                 br.close();
92:         }
93:
94:         public void printCoverage(PrintWriter out, boolean headers, CoverageUtil coverageUtil)
95:         {
96:                 printCoverage(out, headers, false, true, coverageUtil);
97:         }
98:
99:         public void printCoverage(PrintWriter out, boolean headers,
100:                         boolean modelOnly, boolean includeCoverageTable, CoverageUtil coverageUtil)
101:         {
102:                 print(out, headers, modelOnly, includeCoverageTable, true, coverageUtil);
103:         }
104:
105:         public void print(PrintWriter out, boolean headers, boolean modelOnly,
106:                         boolean includeCoverageTable, boolean markCoverage, CoverageUtil coverageUtil)
107:         {
108:                 Map<Integer, List<ILexLocation>> hits = coverageUtil.getMissLocations(filename);
109:                 
110:•                if (headers)
111:                 {
112:                         out.println("\\documentclass[a4paper]{article}");
113:                         out.println("\\usepackage{longtable}");
114:                         // out.println("\\input{times}");
115:                         // out.println("\\input{graphicx}");
116:                         out.println("\\usepackage[color]{vdmlisting}");
117:                         out.println("\\usepackage{fullpage}");
118:                         out.println("\\usepackage{hyperref}");
119:                         out.println("%JPNFONT\\usepackage{fontspec}"); // added by his 2013/10/08
120:                         out.println("%JPNFONT\\setmainfont{Times New Roman}"); // added by his 2013/10/24
121:                         out.println("%JPNFONT\\setmonofont[Scale=0.9]{Courier New}"); // added by his 2013/10/24
122:                         out.println("%JPNFONT\\newfontfamily\\jmsm[Scale=0.92]{MS Mincho}"); // added by his 2013/10/24
123:                         out.println("%JPNFONT\\newfontfamily\\jmsg[Scale=0.92]{MS Gothic}"); // added by his 2013/10/24
124:                         out.println("\\begin{document}");
125:                         out.println("\\title{}");
126:                         out.println("\\author{}");
127:                 }
128:
129:                 // move here
130:                 boolean endDocFound = false;
131:                 boolean inVdmAlModelTag = false;
132:                 useJPNFont = checkFont("MS Gothic");
133:
134:•                if (!hasVdm_al)
135:                 {
136:                         out.println(BEGIN + getListingEnvironment());
137:                         inVdmAlModelTag = true; // added
138:                 }
139:
140:                 // boolean endDocFound = false;
141:                 // boolean inVdmAlModelTag = false;
142:                 // useJPNFont = checkFont("MS Gothic");
143:                 LexNameList spans = coverageUtil.getSpanNames(filename);
144:
145:•                for (int lnum = 1; lnum <= rawLines.size(); lnum++)
146:                 {
147:•                        for (ILexNameToken name : spans)
148:                         {
149:•                                if (name.getLocation().getStartLine() == lnum)
150:                                 {
151:                                         out.println(LST_ESCAPE_BEGIN);
152:                                         out.println("\\label{" + latexLabel(name.getName()) + ":"
153:                                                         + name.getLocation().getStartLine() + "}");
154:                                         out.println(LST_ESCAPE_END);
155:                                 }
156:                         }
157:                         String line = rawLines.get(lnum - 1);
158:
159:•                        if (line.contains("\\end{document}"))
160:                         {
161:                                 endDocFound = true;
162:                                 break;
163:                         }
164:
165:•                        if (line.contains(BEGIN + CURLY_BRACKET_VDM_AL))
166:                         {
167:                                 inVdmAlModelTag = true;
168:                         }
169:
170:                         // if (line.contains("\\subsection{")) // added by his 2013/10/16
171:                         // {
172:                         // inVdmAlModelTag = false;
173:                         // }
174:
175:•                        if (hasVdm_al && modelOnly && !inVdmAlModelTag)
176:                         {
177:                                 // continue;
178:                         }
179:
180:                         String spaced = detab(line, Properties.parser_tabstop);
181:                         spaced = spaced.replace(BEGIN + CURLY_BRACKET_VDM_AL, BEGIN
182:                                         + getListingEnvironment()).replace(END
183:                                         + CURLY_BRACKET_VDM_AL, END + getListingEnvironment());
184:
185:•                        if (markCoverage)
186:                         {
187:                                 // List<LexLocation> list = hits.get(lnum);
188:                                 // out.println(markup(spaced, list));
189:•                                if (inVdmAlModelTag)
190:                                 {
191:                                         List<ILexLocation> list = hits.get(lnum);
192:                                         out.println(markup(spaced, list));
193:                                 } else
194:                                 {
195:                                         // List<LexLocation> list = hits.get(lnum);
196:
197:•                                        if (spaced.contains("\\subsection{")
198:•                                                        || spaced.contains("\\subsubsection{"))
199:                                         {
200:                                                 spaced = utfIncludeCheck(spaced, false);
201:                                                 out.println(spaced.replace(docFont, sectionFont));
202:                                         } else
203:                                         {
204:                                                 out.println(utfIncludeCheck(spaced, false));
205:                                         }
206:                                 }
207:                         } else
208:                         {
209:                                 out.println(spaced);
210:                         }
211:
212:•                        if (line.contains(END + CURLY_BRACKET_VDM_AL))
213:                         {
214:                                 inVdmAlModelTag = false;
215:                         }
216:                 }
217:
218:•                if (!hasVdm_al)
219:                 {
220:                         out.println(END + getListingEnvironment());
221:                         inVdmAlModelTag = false; // added
222:                 }
223:
224:•                if (includeCoverageTable)
225:                 {
226:                         out.println("\\bigskip");
227:                         out.println(createCoverageTable(coverageUtil));
228:                 }
229:•                if (headers || endDocFound)
230:                 {
231:                         out.println("\\end{document}");
232:                 }
233:         }
234:
235:         private String getListingEnvironment()
236:         {
237:•                switch (Settings.dialect)
238:                 {
239:                         case VDM_PP:
240:                                 return CURLY_BRACKET_VDM_PP;
241:                         case VDM_RT:
242:                                 return CURLY_BRACKET_VDM_RT;
243:                         case VDM_SL:
244:                                 return CURLY_BRACKET_VDM_SL;
245:                         case CML:
246:                         default:
247:                                 return CURLY_BRACKET_VDM_AL;
248:                 }
249:         }
250:
251:         private String createCoverageTable(CoverageUtil coverageUtil)
252:         {
253:                 StringBuilder sb = new StringBuilder();
254:                 sb.append("\\begin{longtable}{|l|r|r|r|}" + "\n");
255:                 sb.append("\\hline" + "\n");
256:                 sb.append("Function or operation & Line & Coverage & Calls \\\\" + "\n");
257:                 sb.append("\\hline" + "\n");
258:                 sb.append("\\hline" + "\n");
259:
260:                 long total = 0;
261:
262:                 LexNameList spans = coverageUtil.getSpanNames(filename);
263:                 Collections.sort(spans);
264:
265:•                for (ILexNameToken name : spans)
266:                 {
267:                         long calls = coverageUtil.getSpanCalls(name);
268:                         total += calls;
269:
270:                         sb.append("\\hyperref[" + latexLabel(name.getName()) + ":"
271:                                         + name.getLocation().getStartLine() + "]{"
272:                                         + utfIncludeCheck(latexQuote(name.toString()), false)
273:                                         + "}" + " & " + name.getLocation().getStartLine() + " & "
274:                                         + coverageUtil.getSpanPercent(name) + "\\% & " + calls
275:                                         + " \\\\" + "\n");
276:                         sb.append("\\hline" + "\n");
277:                 }
278:
279:                 sb.append("\\hline" + "\n");
280:                 sb.append(latexQuote(filename.getName()) + " & & "
281:                                 + coverageUtil.getHitPercent(filename) + "\\% & " + total
282:                                 + " \\\\" + "\n");
283:
284:                 sb.append("\\hline" + "\n");
285:                 sb.append("\\end{longtable}" + "\n");
286:                 return sb.toString();
287:         }
288:
289:         private String markup(String line, List<ILexLocation> list)
290:         {
291:•                if (list == null)
292:                 {
293:                         // return line;
294:                         return utfIncludeCheck(line, true);
295:                 } else
296:                 {
297:                         StringBuilder sb = new StringBuilder();
298:                         int p = 0;
299:
300:•                        for (ILexLocation m : list)
301:                         {
302:                                 int start = m.getStartPos() - 1;
303:•                                int end = m.getStartLine() == m.getEndLine() ? m.getEndPos() - 1
304:                                                 : line.length();
305:
306:•                                if (start >= p) // Backtracker produces duplicate tokens
307:                                 {
308:                                         // sb.append(line.substring(p, start));
309:                                         sb.append(utfIncludeCheck(line.substring(p, start), true));
310:                                         sb.append(LST_ESCAPE_BEGIN + "\\vdmnotcovered{");
311:                                         // String temp = utfIncludeCheck(latexQuote(line.substring(start, end)), false);
312:                                         // if(temp.charAt(temp.length()-1)==' ') temp=temp.substring(0, temp.length()-1);
313:                                         sb.append(utfIncludeCheck(latexQuote(line.substring(start, end)), false)); // modified by his
314:                                         sb.append("}" + LST_ESCAPE_END); // \u00A3");
315:
316:                                         p = end;
317:                                 }
318:                         }
319:
320:                         // sb.append(line.substring(p));
321:                         sb.append(utfIncludeCheck(line.substring(p), true));
322:                         return sb.toString();
323:                 }
324:         }
325:
326:         private String latexQuote(String s)
327:         {
328:                 // Latex specials: \# \$ \% \^{} \& \_ \{ \} \~{} \\
329:
330:                 return s.replace("\\", "\\textbackslash ").replace("#", "\\#").replace("$", "\\$").replace("%", "\\%").replace("&", "\\&").replace("_", "\\_").replace("{", "\\{").replace("}", "\\}").replace("~", "\\~").replaceAll("\\^{1}", "\\\\^{}");
331:         }
332:
333:         // add by his 2013/10/08
334:         private String utfIncludeCheck(String line, Boolean addatsign)
335:         {
336:                 String checked = "";
337:                 boolean start = false;
338:
339:•                for (int i = 0; i < line.length(); i++)
340:                 {
341:•                        if (isOneByte(line.substring(i, i + 1)))
342:                         {
343:•                                if (start)
344:                                 {
345:                                         start = false;
346:•                                        checked += (addatsign ? useJPNFont ? fontEnd
347:                                                         + LST_ESCAPE_END : "" : fontEnd)
348:                                                         + line.substring(i, i + 1);
349:                                 } else
350:                                 {
351:                                         checked += line.substring(i, i + 1);
352:                                 }
353:                         } else
354:                         {
355:•                                if (!start)
356:                                 {
357:•                                        checked += (addatsign ? useJPNFont ? LST_ESCAPE_BEGIN : ""
358:                                                         : "")
359:•                                                        + (useJPNFont ? docFont : "")
360:                                                         + line.substring(i, i + 1);
361:                                         start = true;
362:                                 } else
363:                                 {
364:                                         checked += line.substring(i, i + 1);
365:                                 }
366:                         }
367:                 }
368:•                if (start)
369:                 {
370:•                        checked += addatsign ? useJPNFont ? fontEnd + LST_ESCAPE_END : ""
371:                                         : fontEnd;
372:                 }
373:                 return checked;
374:         }
375:
376:         private boolean isOneByte(String a_String)
377:         {
378:                 boolean result = false;
379:                 try
380:                 {
381:                         byte[] code = a_String.getBytes("UTF-8");
382:•                        result = code.length == 1 ? true : false;
383:                 } catch (IOException ex)
384:                 {
385:                 }
386:                 return result;
387:         }
388:
389:         private boolean checkFont(String FontName)
390:         {
391:                 boolean checked = false;
392:
393:                 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
394:
395:                 Font fonts[] = ge.getAllFonts();
396:
397:•                for (int i = 0; i < fonts.length; i++)
398:                 {
399:•                        if (fonts[i].getName().toString().equals(FontName))
400:                         {
401:                                 checked = true;
402:                                 break;
403:                         }
404:                 }
405:                 return checked;
406:         }
407:         
408:         private String latexLabel(String s)
409:         {
410:                 // Latex specials: \# \$ \% \^{} \& \_ \{ \} \~{} \\
411:                 return s.replace("\\", ":").replace("#", ":").replace("$", ":").replace("%", ":").replace("&", ":").replace("_", ":").replace("{", ":").replace("}", ":").replace("~", ":").replaceAll("\\^{1}", ":");
412:         }
413: }