corpus-services  1.0
ExbSchemaChecker.java
Go to the documentation of this file.
1 
10 package de.uni_hamburg.corpora.validation;
11 
17 import java.io.File;
18 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.net.URL;
21 import java.nio.file.Paths;
22 import java.util.Collection;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.stream.StreamSource;
25 import javax.xml.validation.Schema;
26 import javax.xml.validation.SchemaFactory;
27 import javax.xml.validation.Validator;
28 import javax.xml.XMLConstants;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.xpath.XPathExpressionException;
32 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
33 import org.jdom.JDOMException;
34 import org.xml.sax.SAXException;
35 
40 public class ExbSchemaChecker extends Checker implements CorpusFunction {
41 
42 
43  public ExbSchemaChecker() {
44  super(false);
45  }
46 
54  @Override
55  public Report function(CorpusData cd, Boolean fix)
56  throws SAXException, JDOMException, IOException, JexmaraldaException, TransformerException, ParserConfigurationException, XPathExpressionException{
57  System.out.println("Checking the exb file against DTD...");
58  String exbSchemaPath = new File("src\\test\\java\\de\\uni_hamburg\\corpora\\resources\\schemas\\exb_schema.xsd").getAbsolutePath();
59  URL exbSchema = Paths.get(exbSchemaPath).toUri().toURL();//;new URL(exbSchemaPath);
60  Source xmlStream = new StreamSource(TypeConverter.String2InputStream(cd.toSaveableString()));
61  SchemaFactory schemaFactory =
62  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
63  Schema schema = schemaFactory.newSchema(exbSchema);
64  Validator validator = schema.newValidator();
65  ExbErrorReportGenerator eh = new ExbErrorReportGenerator(cd.getFilename());
66  validator.setErrorHandler(eh);
67  validator.validate(xmlStream);
68  return eh.getErrors();
69  }
70 
75  @Override
76  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
77  try {
78  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
79  IsUsableFor.add(cl);
80  } catch (ClassNotFoundException ex) {
81  report.addException(ex, " usable class not found");
82  }
83  return IsUsableFor;
84  }
85 
89  @Override
90  public String getDescription() {
91  String description = "This class validates a exb file with its DTD file. ";
92  return description;
93  }
94 
95  @Override
96  public Report function(Corpus c, Boolean fix) throws SAXException, IOException, ParserConfigurationException, URISyntaxException, JDOMException, TransformerException, XPathExpressionException, JexmaraldaException {
97  Report stats = new Report();
98  for (CorpusData cdata : c.getBasicTranscriptionData()) {
99  stats.merge(function(cdata, fix));
100  }
101  return stats;
102  }
103 }
104 
void merge(Report sr)
Definition: Report.java:73
Collection< Class<?extends CorpusData > > getIsUsableFor()
static InputStream String2InputStream(String s)
void addException(Throwable e, String description)
Definition: Report.java:287