Package: XMLStreamReader

XMLStreamReader

nameinstructionbranchcomplexitylinemethod
XMLStreamReader(InputStream, String)
M: 96 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%
dequote(String)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
length()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
read(char[])
M: 97 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2011 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.IOException;
27: import java.io.InputStream;
28: import java.io.InputStreamReader;
29: import java.util.Arrays;
30: import java.util.regex.Matcher;
31: import java.util.regex.Pattern;
32: import java.util.zip.ZipEntry;
33: import java.util.zip.ZipInputStream;
34:
35: //import org.overture.vdmj.VDMJ;
36:
37: /**
38: * A class to read an XML encoded VDM file, such as .docx or ODF
39: */
40:
41: abstract public class XMLStreamReader extends InputStreamReader
42: {
43:         protected String fileText = null;
44:         private final static int ARRAYCHUNK = 10000;
45:
46:         public XMLStreamReader(InputStream in, String partName) throws IOException
47:         {
48:                 super(in);
49:
50:                 ZipInputStream zis = new ZipInputStream(in);
51:                 ZipEntry ze = zis.getNextEntry();
52:
53:•                while (ze != null)
54:                 {
55:•                        if (ze.getName().equals(partName))
56:                         {
57:                                 byte[] bytes = new byte[ARRAYCHUNK];
58:                                 int space = ARRAYCHUNK;
59:                                 int p = 0;
60:                                 int r = 0;
61:
62:                                 do
63:                                 {
64:                                         r = zis.read(bytes, p, 1000);
65:
66:•                                        if (r > 0)
67:                                         {
68:                                                 p += r;
69:
70:•                                                if (space - p < 1000)
71:                                                 {
72:                                                         space += ARRAYCHUNK;
73:                                                         bytes = Arrays.copyOf(bytes, space);
74:                                                 }
75:                                         }
76:•                                } while (r > 0);
77:
78:                                 // Look for the XML encoding
79:                                 String encoding = "";// VDMJ.filecharset;//TODO
80:                                 String firstLine = new String(bytes, 0, 100);
81:                                 Pattern epattern = Pattern.compile("encoding=\"([\\w-]+)\"");
82:                                 Matcher ematch = epattern.matcher(firstLine);
83:
84:•                                if (ematch.find())
85:                                 {
86:                                         encoding = ematch.group(1);
87:                                 }
88:
89:                                 fileText = despace(new String(bytes, 0, p, encoding));
90:                                 break;
91:                         }
92:
93:                         ze = zis.getNextEntry();
94:                 }
95:
96:                 zis.close();
97:         }
98:
99:         protected final static String MARKER = "%%VDM%%";
100:
101:         @Override
102:         public int read(char[] array) throws IOException
103:         {
104:                 int start = fileText.indexOf(MARKER);
105:                 int ap = 0;
106:
107:•                while (start > 0)
108:                 {
109:                         start += MARKER.length();
110:                         char[] clean = new char[fileText.length() - start];
111:                         int end = fileText.indexOf(MARKER, start);
112:                         boolean capturing = true;
113:                         int cp = 0;
114:
115:•                        for (int p = start; p < end; p++)
116:                         {
117:                                 char c = fileText.charAt(p);
118:
119:•                                if (capturing)
120:                                 {
121:•                                        if (c == '<')
122:                                         {
123:                                                 capturing = false;
124:                                         } else
125:                                         {
126:                                                 clean[cp++] = c;
127:                                         }
128:                                 } else
129:                                 {
130:•                                        if (c == '>')
131:                                         {
132:                                                 capturing = true;
133:                                         }
134:                                 }
135:                         }
136:
137:                         String fixed = dequote(new String(clean, 0, cp));
138:                         char[] chars = fixed.toCharArray();
139:                         System.arraycopy(chars, 0, array, ap, chars.length);
140:                         ap += chars.length;
141:
142:                         start = fileText.indexOf(MARKER, end + 1);
143:                 }
144:
145:                 return ap;
146:         }
147:
148:         public int length()
149:         {
150:                 return fileText.length();
151:         }
152:
153:         abstract protected String despace(String in);
154:
155:         protected String dequote(String in)
156:         {
157:                 return in.replaceAll("&", "&").replaceAll("'", "\'").replaceAll("<", "<").replaceAll(">", ">").replaceAll(""", "\\\"");
158:         }
159: }