corpus-services  1.0
ExbFileReferenceChecker.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.io.File;
19 import java.util.Collection;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.xpath.XPathExpressionException;
28 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
29 import org.jdom.JDOMException;
30 import org.xml.sax.SAXException;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.NodeList;
34 
38 public class ExbFileReferenceChecker extends Checker implements CorpusFunction {
39 
41  //no fixing option available
42  super(false);
43  }
44 
49  @Override
50  public Report function(CorpusData cd, Boolean fix)
51  throws SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException, URISyntaxException {
52  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53  DocumentBuilder db = dbf.newDocumentBuilder();
54  Document doc = db.parse(TypeConverter.String2InputStream(cd.toSaveableString())); // get the file as a document
55  NodeList reffiles = doc.getElementsByTagName("referenced-file");
56  int reffilesFound = 0;
57  int reffilesMissing = 0;
58  Report stats = new Report();
59  for (int i = 0; i < reffiles.getLength(); i++) {
60  Element reffile = (Element) reffiles.item(i);
61  String url = reffile.getAttribute("url");
62  if (!url.isEmpty()) {
63  if (url.startsWith("file:///C:") || url.startsWith("file:/C:")) {
64  stats.addCritical(function, cd, "Referenced-file " + url
65  + " points to absolute local path, fix to relative path first");
66  }
67  boolean found = false;
68  File justFile = new File(url);
69  if (justFile.exists()) {
70  found = true;
71  }
72  URL referencePath = cd.getParentURL();
73  URL absPath = new URL(referencePath + "/" + url);
74  File absFile = new File(absPath.toURI());
75  if (absFile.exists()) {
76  found = true;
77  }
78  if (!found) {
79  reffilesMissing++;
80  stats.addCritical(function, cd, "File in referenced-file NOT found: " + url);
81  exmaError.addError(function, cd.getURL().getFile(), "", "", false, "Error: File in referenced-file NOT found: " + url);
82  } else {
83  reffilesFound++;
84  stats.addCorrect(function, cd, "File in referenced-file was found: " + url);
85  }
86  } else {
87  stats.addCorrect(function, cd, "No file was referenced in this transcription");
88  }
89  }
90  return stats;
91  }
92 
98  @Override
99  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
100  try {
101  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
102  IsUsableFor.add(cl);
103  } catch (ClassNotFoundException ex) {
104  report.addException(ex, "Usable class not found.");
105  }
106  return IsUsableFor;
107  }
108 
112  @Override
113  public String getDescription() {
114  String description = "This class is a validator for EXB-file's references;"
115  + " it checks Exmaralda .exb file for file references if a referenced "
116  + "file does not exist, issues a warning;";
117  return description;
118  }
119 
120  @Override
121  public Report function(Corpus c, Boolean fix) throws SAXException, IOException, ParserConfigurationException, URISyntaxException, JDOMException, TransformerException, XPathExpressionException, JexmaraldaException {
122  Report stats = new Report();
123  for (CorpusData cdata : c.getBasicTranscriptionData()) {
124  stats.merge(function(cdata, fix));
125  }
126  return stats;
127  }
128 
129 }
void merge(Report sr)
Definition: Report.java:73
void addCritical(String description)
Definition: Report.java:104
void addCorrect(String statId, String description)
Definition: Report.java:217
Collection< Class<?extends CorpusData > > getIsUsableFor()
static InputStream String2InputStream(String s)
void addException(Throwable e, String description)
Definition: Report.java:287