corpus-services  1.0
ComaXsdChecker.java
Go to the documentation of this file.
1 
10 package de.uni_hamburg.corpora.validation;
11 
17 import java.io.IOException;
18 import java.net.URL;
19 import java.util.Collection;
20 import javax.xml.transform.Source;
21 import javax.xml.transform.stream.StreamSource;
22 import javax.xml.validation.Schema;
23 import javax.xml.validation.SchemaFactory;
24 import javax.xml.validation.Validator;
25 import javax.xml.XMLConstants;
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.xpath.XPathExpressionException;
29 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
30 import org.jdom.JDOMException;
31 import org.xml.sax.SAXException;
32 
33 
34 
39 public class ComaXsdChecker extends Checker implements CorpusFunction {
40 
41  public ComaXsdChecker() {
42  super(false);
43  }
44 
48  public Report function(CorpusData cd, Boolean fix)
49  throws SAXException, JDOMException, IOException, JexmaraldaException, TransformerException, ParserConfigurationException, XPathExpressionException{
50  System.out.println("Checking COMA file against schema...");
51  URL COMA_XSD = new URL("http://www.exmaralda.org/xml/comacorpus.xsd");
52  Source xmlStream = new StreamSource(TypeConverter.String2InputStream(cd.toSaveableString()));
53  SchemaFactory schemaFactory =
54  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
55  Schema schema = schemaFactory.newSchema(COMA_XSD);
56  Validator validator = schema.newValidator();
58  validator.setErrorHandler(eh);
59  validator.validate(xmlStream);
60  return eh.getErrors();
61  }
62 
67  @Override
68  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
69  try {
70  Class cl = Class.forName("de.uni_hamburg.corpora.ComaData");
71  IsUsableFor.add(cl);
72  } catch (ClassNotFoundException ex) {
73  report.addException(ex, "unknown class not found error");
74  }
75  return IsUsableFor;
76  }
77 
81  @Override
82  public String getDescription() {
83  String description = "This class validates the coma file with the respective XML schema. ";
84  return description;
85  }
86 
87  @Override
88  public Report function(Corpus c, Boolean fix) throws SAXException, JDOMException, IOException, JexmaraldaException, TransformerException, ParserConfigurationException, XPathExpressionException {
89  Report stats;
90  cd = c.getComaData();
91  stats = function(cd, fix);
92  return stats;
93  }
94 
95 }
Collection< Class<?extends CorpusData > > getIsUsableFor()
static InputStream String2InputStream(String s)
void addException(Throwable e, String description)
Definition: Report.java:287