1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.tei;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Hashtable;
24 import java.util.List;
25
26 import javax.xml.XMLConstants;
27 import javax.xml.transform.Source;
28 import javax.xml.validation.Schema;
29 import javax.xml.validation.SchemaFactory;
30 import javax.xml.validation.Validator;
31
32 import org.xml.sax.ErrorHandler;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.SAXParseException;
35
36 public class MCRTEIValidator implements ErrorHandler {
37
38 public static final String MCR_TRANSCRIPTION_SCHEMA = "xsd/mcrtranscr.xsd";
39
40 public static final String FATAL_ERROR = "fatalError";
41
42 public static final String ERROR = "error";
43
44 public static final String WARNING = "warning";
45
46 private Hashtable<String, List<SAXParseException>> exceptionMap;
47
48 private Source teiSource;
49
50 public MCRTEIValidator(Source teiSource) {
51 this.teiSource = teiSource;
52
53 this.exceptionMap = new Hashtable<>();
54 this.exceptionMap.put("warning", new ArrayList<>());
55 this.exceptionMap.put("error", new ArrayList<>());
56 this.exceptionMap.put("fatalError", new ArrayList<>());
57 }
58
59 private Schema getSchema(String path) throws SAXException {
60 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
61 schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
62 schemaFactory.setErrorHandler(this);
63 return schemaFactory.newSchema(MCRTEIValidator.class.getClassLoader().getResource(
64 path));
65 }
66
67 public void validate() throws IOException, SAXException {
68 Validator validator = this.getSchema(MCR_TRANSCRIPTION_SCHEMA).newValidator();
69 validator.setErrorHandler(this);
70 validator.validate(this.teiSource);
71 }
72
73 @Override
74 public void warning(SAXParseException exception) throws SAXException {
75 this.exceptionMap.get(WARNING).add(exception);
76 }
77
78 @Override
79 public void error(SAXParseException exception) throws SAXException {
80 this.exceptionMap.get(ERROR).add(exception);
81 }
82
83 @Override
84 public void fatalError(SAXParseException exception) throws SAXException {
85 this.exceptionMap.get(FATAL_ERROR).add(exception);
86 }
87
88 public List<SAXParseException> getWarnings() {
89 return this.exceptionMap.get(WARNING);
90 }
91
92 public List<SAXParseException> getErrors() {
93 return this.exceptionMap.get(ERROR);
94 }
95
96 public List<SAXParseException> getFatals() {
97 return this.exceptionMap.get(FATAL_ERROR);
98 }
99
100 }