corpus-services  1.0
GeneralTransformer.java
Go to the documentation of this file.
1 /*
2  * This class runs an xsl transformation on a file
3  */
4 package de.uni_hamburg.corpora.validation;
5 
12 import java.io.IOException;
13 import java.io.UnsupportedEncodingException;
14 import java.net.MalformedURLException;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.Collection;
18 import javax.xml.transform.TransformerConfigurationException;
19 import javax.xml.transform.TransformerException;
20 import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
21 import org.jdom.JDOMException;
22 import org.xml.sax.SAXException;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.xpath.XPathExpressionException;
25 
30 public class GeneralTransformer extends Checker {
31 
32  String pathToXSL = "";
33  String outputFilename = "";
34  URL urlToOutput;
35  boolean overwritefiles = false;
36  boolean coma = false;
37  boolean exb = false;
38  boolean exs = false;
39 
40  public GeneralTransformer() {
41  //fixing is available
42  super(true);
43  }
44 
45  @Override
46  public Report function(CorpusData cd, Boolean fix) throws JDOMException, IOException, URISyntaxException, TransformerConfigurationException, TransformerException, ParserConfigurationException, UnsupportedEncodingException, SAXException, XPathExpressionException {
47  if (fix) {
48  CorpusIO cio = new CorpusIO();
49  String corpusdata = cd.toUnformattedString();
50  String stylesheet = cio.readExternalResourceAsString(pathToXSL);
51  XSLTransformer xslt = new XSLTransformer();
52  String result
53  = xslt.transform(corpusdata, stylesheet);
54  if (result != null) {
55  report.addFix(function, cd,
56  "XSL Transformation was successful");
57 
58  PrettyPrinter pp = new PrettyPrinter();
59  result = pp.indent(result, "event");
60  }
61  if (overwritefiles) {
62  cd.updateUnformattedString(result);
63  cio.write(cd, cd.getURL());
64  } else {
65  cio.write(result, urlToOutput);
66  }
67  } else {
68  report.addCritical(function, cd,
69  "XSL Transformation cannot be checked, only fixed (use -f)");
70  }
71  return report;
72  }
73 
74  @Override
75  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
76  try {
77  if (exb) {
78  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
79  IsUsableFor.add(cl);
80  }
81  if (exs) {
82  Class cl2 = Class.forName("de.uni_hamburg.corpora.UnspecifiedXMLData");
83  IsUsableFor.add(cl2);
84  }
85  if (coma) {
86  Class cl3 = Class.forName("de.uni_hamburg.corpora.ComaData");
87  IsUsableFor.add(cl3);
88  }
89  } catch (ClassNotFoundException ex) {
90  report.addException(ex, " usable class not found");
91  }
92  return IsUsableFor;
93  }
94 
95  public void setPathToXSL(String s) {
96  pathToXSL = s;
97  }
98 
99  public void setOutputFileName(String s) throws MalformedURLException {
100  urlToOutput = new URL(cd.getParentURL() + s);
101  }
102 
103  public void setOverwriteFiles(String s) {
104  if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("wahr") || s.equalsIgnoreCase("ja")) {
105  overwritefiles = true;
106  } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("falsch") || s.equalsIgnoreCase("nein")) {
107  overwritefiles = false;
108  } else {
109  report.addCritical(function, cd, "Parameter coma not recognized: " + escapeHtml4(s));
110  }
111  }
112 
113  public void setComa(String s) {
114  if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("wahr") || s.equalsIgnoreCase("ja")) {
115  coma = true;
116  } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("falsch") || s.equalsIgnoreCase("nein")) {
117  coma = false;
118  } else {
119  report.addCritical(function, cd, "Parameter coma not recognized: " + escapeHtml4(s));
120  }
121  }
122 
123  public void setExb(String s) {
124  if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("wahr") || s.equalsIgnoreCase("ja")) {
125  exb = true;
126  } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("falsch") || s.equalsIgnoreCase("nein")) {
127  exb = false;
128  } else {
129  report.addCritical(function, cd, "Parameter coma not recognized: " + escapeHtml4(s));
130  }
131  }
132 
133  public void setExs(String s) {
134  if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("wahr") || s.equalsIgnoreCase("ja")) {
135  exs = true;
136  } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("falsch") || s.equalsIgnoreCase("nein")) {
137  exs = false;
138  } else {
139  report.addCritical(function, cd, "Parameter coma not recognized: " + escapeHtml4(s));
140  }
141  }
142 
147  @Override
148  public String getDescription() {
149  String description = "This class runs an xsl transformation on files. ";
150  return description;
151  }
152 
153  @Override
154  public Report function(Corpus c, Boolean fix) throws SAXException, IOException, ParserConfigurationException, URISyntaxException, JDOMException, TransformerException, TransformerConfigurationException, UnsupportedEncodingException, XPathExpressionException {
155  Report stats = new Report();
156  if (exb) {
157  for (CorpusData cdata : c.getBasicTranscriptionData()) {
158  stats.merge(function(cdata, fix));
159  }
160  }
161  if (exs) {
162  for (CorpusData scdata : c.getSegmentedTranscriptionData()) {
163  stats.merge(function(scdata, fix));
164  }
165  }
166  if (coma) {
167  cd = c.getComaData();
168  stats = function(cd, fix);
169  }
170  return stats;
171  }
172 
173 }
void merge(Report sr)
Definition: Report.java:73
String indent(String xml, String suppressedElements)
Collection< Class<?extends CorpusData > > getIsUsableFor()
String readExternalResourceAsString(String path2resource)
Definition: CorpusIO.java:201
void addCritical(String description)
Definition: Report.java:104
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