corpus-services  1.0
RemoveAutoSaveExb.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package de.uni_hamburg.corpora.validation;
7 
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.List;
17 import org.jdom.Document;
18 import org.jdom.Element;
19 import org.jdom.JDOMException;
20 import org.jdom.xpath.XPath;
21 import org.xml.sax.SAXException;
24 import java.net.URISyntaxException;
25 import java.security.NoSuchAlgorithmException;
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.xpath.XPathExpressionException;
29 import org.exmaralda.partitureditor.fsm.FSMException;
30 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
31 
36 public class RemoveAutoSaveExb extends Checker implements CorpusFunction {
37 
38  Document doc = null;
39 
40  public RemoveAutoSaveExb() {
41  //fixing is possible
42  super(true);
43  }
44 
45  @Override
46  public Report function(CorpusData cd, Boolean fix) throws TransformerException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
47  try {
48  XMLData xml = (XMLData) cd;
49  List al = findAllAutoSaveInstances(xml);
50  //if there is no autosave, nothing needs to be done
51  if (al.isEmpty()) {
52  report.addCorrect(function, cd, "there is no autosave info left, nothing to do");
53  } else if (fix) {
54  for (Object o : al) {
55  Element e = (Element) o;
56  System.out.println(e);
57  //remove it
58  e.getParent().removeContent(e);
59  }
60  //then save file
61  //add a report message
62  xml.setJdom(doc);
63  cd = (CorpusData) xml;
64  cd.updateUnformattedString(TypeConverter.JdomDocument2String(doc));
65  CorpusIO cio = new CorpusIO();
66  cio.write(cd, cd.getURL());
67  report.addFix(function, cd, "removed AutoSave info");
68  } else {
69  report.addCritical(function, cd, "autosave info needs to be removed");
70  exmaError.addError("RemoveAutoSaveExb", cd.getURL().getFile(), "", "", false, "autosave info needs to be removed");
71  }
72  } catch (JDOMException ex) {
73  report.addException(ex, function, cd, "Jdom Exception");
74  }
75  return report;
76  }
77 
78  @Override
79  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
80  try {
81  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
82  IsUsableFor.add(cl);
83  Class cl2 = Class.forName("de.uni_hamburg.corpora.SegmentedTranscriptionData");
84  IsUsableFor.add(cl2);
85 
86  } catch (ClassNotFoundException ex) {
87  report.addException(ex, "unknown class not found error");
88  }
89  return IsUsableFor;
90  }
91 
92  public List findAllAutoSaveInstances(XMLData xml) throws JDOMException {
93  doc = xml.getJdom();
94  XPath xp1;
95  //working for exs too
96  xp1 = XPath.newInstance("//head/meta-information/ud-meta-information/ud-information[@attribute-name='AutoSave']");
97  List allAutoSaveInfo = xp1.selectNodes(doc);
98  return allAutoSaveInfo;
99  }
100 
105  @Override
106  public String getDescription() {
107  String description = "This class removes auto save information present in"
108  + " exb and exs files.";
109  return description;
110  }
111 
112  @Override
113  public Report function(Corpus c, Boolean fix) throws SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException, NoSuchAlgorithmException, ClassNotFoundException, FSMException, URISyntaxException, JDOMException {
114  Report stats = new Report();
115  for (CorpusData cdata : c.getBasicTranscriptionData()) {
116  stats.merge(function(cdata, fix));
117  }
118  for (CorpusData sdata : c.getSegmentedTranscriptionData()) {
119  stats.merge(function(sdata, fix));
120  }
121  return stats;
122  }
123 }
Collection< Class<?extends CorpusData > > getIsUsableFor()
void merge(Report sr)
Definition: Report.java:73
void addCritical(String description)
Definition: Report.java:104
void setJdom(Document jdom)
void addCorrect(String statId, String description)
Definition: Report.java:217
static String JdomDocument2String(org.jdom.Document jdomDocument)
void addException(Throwable e, String description)
Definition: Report.java:287
void write(CorpusData cd, URL url)
Definition: CorpusIO.java:66
void addFix(String statId, CorpusData cd, String description)
Definition: Report.java:155