Package: LatexStreamReader

LatexStreamReader

nameinstructionbranchcomplexitylinemethod
LatexStreamReader(InputStream, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
read(char[])
M: 73 C: 127
64%
M: 21 C: 29
58%
M: 13 C: 13
50%
M: 17 C: 35
67%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.parser.lex;
25:
26: import java.io.BufferedReader;
27: import java.io.IOException;
28: import java.io.InputStream;
29: import java.io.InputStreamReader;
30: import java.io.UnsupportedEncodingException;
31: import java.util.Stack;
32:
33: /**
34: * A class to read LaTeX encoded VDM files.
35: */
36:
37: public class LatexStreamReader extends InputStreamReader
38: {
39:         private Stack<Boolean> ifstack = new Stack<Boolean>();
40:
41:         public LatexStreamReader(InputStream in, String charsetName)
42:                         throws UnsupportedEncodingException
43:         {
44:                 super(in, charsetName);
45:         }
46:
47:         @Override
48:         public int read(char[] array) throws IOException
49:         {
50:                 BufferedReader br = new BufferedReader(this);
51:                 String line = br.readLine();
52:
53:                 boolean supress = false;
54:                 boolean suppressLine = false;
55:                 int pos = 0;
56:
57:•                while (line != null)
58:                 {
59:                         String trimmed = line.trim();
60:
61:•                        if (trimmed.startsWith("%"))
62:                         {
63:                                 supress = true;
64:                                 // line = "";
65:                         }
66:•                        else if (trimmed.startsWith("\\"))
67:                         {
68:•                                if (trimmed.startsWith("\\begin{vdm_al}"))
69:                                 {
70:                                         supress = false;
71:                                         suppressLine = true;
72:                                 }
73:•                                else if (trimmed.startsWith("\\end{vdm_al}")
74:•                                                || trimmed.startsWith("\\section")
75:•                                                || trimmed.startsWith("\\subsection")
76:•                                                || trimmed.startsWith("\\document"))
77:                                 {
78:                                         supress = true;
79:                                 }
80:                         }
81:•                        else if (trimmed.startsWith("#"))
82:                         {
83:•                                if (trimmed.startsWith("#ifdef"))
84:                                 {
85:                                         String label = trimmed.substring(6).trim();
86:                                         ifstack.push(supress);
87:
88:•                                        if (!supress && System.getProperty(label) == null)
89:                                         {
90:                                                 supress = true;
91:                                         }
92:
93:                                         suppressLine = true;
94:                                 }
95:•                                else if (trimmed.startsWith("#ifndef"))
96:                                 {
97:                                         String label = trimmed.substring(7).trim();
98:                                         ifstack.push(supress);
99:
100:•                                        if (!supress && System.getProperty(label) != null)
101:                                         {
102:                                                 supress = true;
103:                                         }
104:
105:                                         suppressLine = true;
106:                                 }
107:•                                else if (trimmed.startsWith("#else") && !ifstack.isEmpty())
108:                                 {
109:•                                        if (!ifstack.peek())
110:                                         {
111:•                                                supress = !supress;
112:                                                 suppressLine = true;
113:                                         }
114:                                 }
115:•                                else if (trimmed.startsWith("#endif") && !ifstack.isEmpty())
116:                                 {
117:                                         supress = ifstack.pop();
118:                                         suppressLine = true;
119:                                 }
120:                         }
121:
122:•                        if (!supress && !suppressLine)
123:                         {
124:                                 line.getChars(0, line.length(), array, pos);
125:                                 pos += line.length();
126:                         }
127:                         else
128:                         {
129:•                                for (int i = 0; i < line.length(); i++)
130:                                 {
131:                                         array[pos++] = ' ';
132:                                 }
133:                         }
134:
135:                         suppressLine = false;
136:•                        if (pos < array.length)
137:                         {
138:                                 array[pos++] = '\n';
139:                         }
140:                         line = br.readLine();
141:                 }
142:                 br.close();
143:
144:                 return pos;
145:         }
146:
147: }