1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/* LanguageTool, a natural language style checker
* Copyright (C) 2007 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package de.danielnaber.languagetool;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import de.danielnaber.languagetool.tools.StringTools;
/**
* Validate XML files with a given DTD.
*
* @author Daniel Naber
*/
public final class XMLValidator {
public XMLValidator() {
}
/**
* Check some limits of our simplified XML output.
*/
public void checkSimpleXMLString(String xml) throws IOException {
Pattern p = Pattern.compile("(<error.*?/>)", Pattern.DOTALL|Pattern.MULTILINE);
Matcher matcher = p.matcher(xml);
int pos = 0;
while (matcher.find(pos)) {
String errorElement = matcher.group();
pos = matcher.end();
if (errorElement.contains("\n") || errorElement.contains("\r"))
throw new IOException("<error ...> may not contain line breaks");
char beforeError = xml.charAt(matcher.start()-1);
if (beforeError != '\n' && beforeError != '\r')
throw new IOException("Each <error ...> must start on a new line");
}
}
/**
* Validate XML with the given DTD. Throws exception on error.
*/
public void validateXMLString(String xml, String dtdFile, String docType) throws SAXException, IOException, ParserConfigurationException {
validateInternal(xml, dtdFile, docType);
}
/**
* Validate XML file with the given DTD. Throws exception on error.
*/
public final void validate(String filename, String dtdFile, String docType) throws IOException {
try {
String xml = StringTools.readFile(this.getClass().getResourceAsStream(filename), "utf-8");
validateInternal(xml, dtdFile, docType);
} catch (Exception e) {
IOException ioe = new IOException("Cannot load or parse '"+filename+"'");
ioe.initCause(e);
throw ioe;
}
}
/**
* Validate XML file using the given XSD. Throws an exception on error
* @param filename File to validate.
* @param xmlSchema Schema to use.
* @throws IOException Thrown on error.
*/
public final void validate(String filename, String xmlSchema) throws IOException {
try {
validateInternal(this.getClass().getResourceAsStream(filename),
this.getClass().getResource(xmlSchema));
} catch (Exception e) {
IOException ioe = new IOException("Cannot load or parse '"+filename+"'");
ioe.initCause(e);
throw ioe;
}
}
private void validateInternal(String xml, String dtdFile, String doctype) throws SAXException, IOException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
//used for removing existing DOCTYPE from grammar.xml files
xml = xml.replaceAll("<!DOCTYPE.+>", "");
final String decl = "<?xml version=\"1.0\"";
final String endDecl = "?>";
final String dtd = "<!DOCTYPE "+doctype+" PUBLIC \"-//W3C//DTD Rules 0.1//EN\" \"" +this.getClass().getResource(dtdFile)+ "\">";
int pos = xml.indexOf(decl);
int endPos = xml.indexOf(endDecl);
if (pos == -1)
throw new IOException("No XML declaration found in '" + xml.substring(0, Math.min(100, xml.length())) + "...'");
String newXML = xml.substring(0, endPos+endDecl.length()) + "\r\n" + dtd + xml.substring(endPos+endDecl.length());
//System.err.println(newXML);
InputSource is = new InputSource(new StringReader(newXML));
saxParser.parse(is, new ErrorHandler());
}
private void validateInternal(InputStream xml, URL xmlSchema) throws SAXException, IOException, ParserConfigurationException {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(xmlSchema);
Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler());
validator.validate(new StreamSource(xml));
}
}
/**
* XML handler that throws exception on error and warning, does nothing otherwise.
*/
class ErrorHandler extends DefaultHandler {
public void warning (SAXParseException e) throws SAXException {
System.err.println(e.getMessage()
+ " Problem found at line " + e.getLineNumber()
+ ", column " + e.getColumnNumber() + ".");
throw e;
}
public void error (SAXParseException e) throws SAXException {
System.err.println(e.getMessage()
+ " Problem found at line " + e.getLineNumber()
+ ", column " + e.getColumnNumber() + ".");
throw e;
}
}
|