34 |
34 |
import java.io.IOException;
|
35 |
35 |
import java.io.InputStream;
|
36 |
36 |
import java.net.URL;
|
|
37 |
import java.util.regex.Matcher;
|
|
38 |
import java.util.regex.Pattern;
|
37 |
39 |
|
|
40 |
import javax.xml.parsers.ParserConfigurationException;
|
38 |
41 |
import javax.xml.stream.XMLInputFactory;
|
39 |
42 |
import javax.xml.stream.XMLStreamConstants;
|
40 |
43 |
import javax.xml.stream.XMLStreamException;
|
... | ... | |
42 |
45 |
|
43 |
46 |
import org.eclipse.osgi.util.NLS;
|
44 |
47 |
import org.txm.core.messages.TXMCoreMessages;
|
|
48 |
import org.txm.utils.logger.Log;
|
|
49 |
import org.xml.sax.SAXException;
|
45 |
50 |
|
46 |
|
// TODO: Auto-generated Javadoc
|
47 |
51 |
/**
|
48 |
|
* Validate a xml file with a stax parser.
|
|
52 |
* Validate a XML file with a Stax parser.
|
49 |
53 |
*
|
50 |
54 |
* @author mdecorde
|
51 |
55 |
*/
|
52 |
56 |
public class ValidateXml {
|
53 |
|
|
|
57 |
|
54 |
58 |
/**
|
55 |
59 |
* @throws XMLStreamException test a String, not implemented.
|
56 |
60 |
*
|
... | ... | |
75 |
79 |
XMLInputFactory factory = XMLInputFactory.newInstance();
|
76 |
80 |
XMLStreamReader parser = factory.createXMLStreamReader(inputData);
|
77 |
81 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
|
78 |
|
|
|
82 |
|
79 |
83 |
}
|
80 |
84 |
parser.close();
|
81 |
85 |
inputData.close();
|
... | ... | |
87 |
91 |
}
|
88 |
92 |
return true;
|
89 |
93 |
}
|
90 |
|
|
|
94 |
|
91 |
95 |
/**
|
92 |
96 |
* test a content stored in a String, not implemented.
|
93 |
97 |
*
|
... | ... | |
98 |
102 |
public static boolean test(String xmldata) {
|
99 |
103 |
return testAndGetError(xmldata).length() == 0;
|
100 |
104 |
}
|
101 |
|
|
|
105 |
|
102 |
106 |
/**
|
103 |
107 |
* test a content stored in a String, not implemented.
|
104 |
108 |
*
|
... | ... | |
108 |
112 |
*/
|
109 |
113 |
public static String testAndGetError(String xmldata) {
|
110 |
114 |
try {
|
111 |
|
Node records = new XmlParser().parseText(xmldata);
|
|
115 |
new XmlParser().parseText(xmldata);
|
112 |
116 |
return "";
|
113 |
117 |
}
|
114 |
|
catch (Exception e) {
|
115 |
|
System.out.println(e.getLocalizedMessage());
|
116 |
|
return e.toString();
|
|
118 |
catch (IOException | ParserConfigurationException e) {
|
|
119 |
return e.getMessage();
|
117 |
120 |
}
|
|
121 |
catch (SAXException ex) {
|
|
122 |
return getXMLError(null, ex);
|
|
123 |
}
|
118 |
124 |
}
|
119 |
|
|
|
125 |
|
|
126 |
private static String getXMLError(String file, Exception e) {
|
|
127 |
String message = e.getMessage();
|
|
128 |
|
|
129 |
Matcher msgParse = null;
|
|
130 |
if (message.startsWith("ParseError at")) {
|
|
131 |
Pattern p = Pattern.compile("(ParseError at \\[row,col\\]:\\[)([0-9]+),([0-9]+)\\]\n" +
|
|
132 |
"Message: (.+).");
|
|
133 |
msgParse = p.matcher(message); // getMessageAndLocation
|
|
134 |
} else {
|
|
135 |
Pattern p = Pattern.compile("^(.*) lineNumber: ([0-9]+); columnNumber: ([0-9]+); (.*).$");
|
|
136 |
msgParse = p.matcher(message); // getMessageAndLocation
|
|
137 |
}
|
|
138 |
|
|
139 |
if (msgParse.matches() && msgParse.groupCount() == 4) {
|
|
140 |
String lineNumber = msgParse.group(2);
|
|
141 |
String colNumber = msgParse.group(3);
|
|
142 |
String errMsg = msgParse.group(4);
|
|
143 |
|
|
144 |
return TXMCoreMessages.bind("** {0} [line {1}, character {2}]: {3}.", file, lineNumber, colNumber, errMsg);
|
|
145 |
} else {
|
|
146 |
return message;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
120 |
150 |
/**
|
121 |
151 |
* test a file.
|
122 |
152 |
*
|
... | ... | |
143 |
173 |
XMLInputFactory factory = XMLInputFactory.newInstance();
|
144 |
174 |
XMLStreamReader parser = factory.createXMLStreamReader(inputData);
|
145 |
175 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
|
146 |
|
|
|
176 |
|
147 |
177 |
}
|
148 |
178 |
parser.close();
|
149 |
179 |
inputData.close();
|
150 |
180 |
return true;
|
151 |
181 |
}
|
152 |
182 |
catch (Exception e) {
|
153 |
|
System.out.println(e.getLocalizedMessage());
|
|
183 |
System.out.println(getXMLError(infile.toString(), e));
|
154 |
184 |
return false;
|
155 |
185 |
}
|
156 |
186 |
}
|
157 |
|
|
|
187 |
|
158 |
188 |
/**
|
159 |
189 |
* small test to verify a file is a XML-TEI file.
|
160 |
190 |
*
|
... | ... | |
200 |
230 |
}
|
201 |
231 |
parser.close();
|
202 |
232 |
inputData.close();
|
203 |
|
|
|
233 |
|
204 |
234 |
boolean test = n == 3 && tei < teiHeader && teiHeader < text;
|
205 |
235 |
if (test) {
|
206 |
236 |
return true;
|
... | ... | |
228 |
258 |
return false;
|
229 |
259 |
}
|
230 |
260 |
}
|
231 |
|
|
|
261 |
|
232 |
262 |
/**
|
233 |
263 |
* The main method.
|
234 |
264 |
*
|
... | ... | |
237 |
267 |
*/
|
238 |
268 |
public static void main(String[] args) {
|
239 |
269 |
for (File file : new File(System.getProperty("user.home"), "xml/teierrors").listFiles()) {
|
240 |
|
if (ValidateXml.teiTest(file)) {
|
|
270 |
if (ValidateXml.test(file)) {
|
241 |
271 |
System.out.println("OK: " + file); //$NON-NLS-1$
|
242 |
272 |
}
|
243 |
273 |
else {
|