-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.java
More file actions
153 lines (138 loc) · 3.88 KB
/
Validate.java
File metadata and controls
153 lines (138 loc) · 3.88 KB
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
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* Checks the well-formedness and validity of a given XML document.
* Invoke from command line as:
*
* java -v document.xml
* java -s document.xml
* java document.xml
*
* Without the -v option only well-formedness is checked. With it, both
* well-formedness and validity are checked.
*
* The -s option does validation with respect to an XML Schema.
*
* @author jaf@it.uc3m.es
*
*/
public class Validate {
class ValidationErrorHandler implements ErrorHandler {
protected boolean errors = false;
@Override
public void error(SAXParseException exception) throws SAXException {
System.err.println("Error: " + exception.getMessage());
errors = true;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.err.println("Fatal error: " + exception.getMessage());
errors = true;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
// Do nothing...
}
}
public boolean validate(String fileName, boolean xmlSchema) {
boolean valid = false;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
if (xmlSchema) {
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setNamespaceAware(true);
}
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
return false;
}
try {
ValidationErrorHandler handler = new ValidationErrorHandler();
builder.setErrorHandler(handler);
Document document = builder.parse(new InputSource(fileName));
valid = !handler.errors;
} catch (SAXException e) {
valid = false;
} catch (IOException e) {
valid = false;
e.printStackTrace();
}
return valid;
}
public boolean checkWellFormedness(String fileName) {
boolean wellFormed = false;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
return false;
}
try {
Document document = builder.parse(new InputSource(fileName));
wellFormed = true;
} catch (SAXException e) {
wellFormed = false;
} catch (IOException e) {
wellFormed = false;
e.printStackTrace();
}
return wellFormed;
}
public static void main(String[] args) {
boolean validate;
boolean schema;
String xmlFileName;
if (args.length < 1 || args.length > 2 ||
(args.length == 2 && !("-v".equals(args[0])
|| "-s".equals(args[0])))) {
printHelp();
System.exit(1);
}
if (args.length == 2) {
validate = true;
xmlFileName = args[1];
if ("-s".equals(args[0])) {
schema = true;
} else {
schema = false;
}
} else {
validate = false;
schema = false;
xmlFileName = args[0];
}
Validate validator = new Validate();
if (validate) {
if (validator.validate(xmlFileName, schema)) {
System.out.println("The document is well-formed and valid");
} else {
System.out.println("The document is not valid");
}
} else {
if (validator.checkWellFormedness(xmlFileName)) {
System.out.println("The document is well-formed");
} else {
System.out.println("The document is not well-formed");
}
}
}
private static void printHelp() {
System.err.println("Error: wrong command-line arguments");
System.err.println("java Validate [-v|-s] file");
}
}