corpus-services  1.0
CorpusHTML.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.visualization;
7 
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.util.Collection;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.transform.TransformerConfigurationException;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.xpath.XPathExpressionException;
23 import org.xml.sax.SAXException;
24 
29 public class CorpusHTML extends Visualizer {
30 
31  // resources loaded from directory supplied in pom.xml
32  static final String STYLESHEET_PATH = "/xsl/Coma2HTML.xsl";
33  private static final String SERVICE_NAME = "ComaHTML";
34  URL targeturl;
35  CorpusData cod;
36 
37  public CorpusHTML() {
38  }
39 
40  public String createFromComa(String coma) {
41  //TODO this report is never used anywhere
42  Report stats = new Report();
43  String result = null;
44 
45  try {
46 
47  String corpusPrefix = coma.split("<Key Name=\"hzsk:corpusPrefix\">")[1].split("</Key>")[0];
48  String corpusVersion = coma.split("<Key Name=\"hzsk:corpusVersion\">")[1].split("</Key>")[0];
49 
50  // read the XSL stylesheet into a String
51  String xsl = TypeConverter.InputStream2String(getClass().getResourceAsStream(STYLESHEET_PATH));
52 
53  XSLTransformer xt = new XSLTransformer();
54  xt.setParameter("identifier", "spoken-corpus:" + corpusPrefix + "-" + corpusVersion);
55  result = xt.transform(coma, xsl);
56  } catch (TransformerConfigurationException ex) {
57  stats.addException(ex, SERVICE_NAME, cd, "Unknown TransformerConfigurationException");
58  } catch (TransformerException ex) {
59  stats.addException(ex, SERVICE_NAME, cd, "Unknown TransformerException");
60  }
61 
62  return result;
63  }
64 
65  @Override
66  public Report function(CorpusData cd) {
67  Report stats = new Report();
68  try {
69  cod = cd;
70  String result = createFromComa(cd.toSaveableString());
71  CorpusIO cio = new CorpusIO();
72  targeturl = new URL(cd.getParentURL() + "/resources/" + cd.getFilenameWithoutFileEnding() + ".html");
73  cio.write(result, targeturl);
74  } catch (MalformedURLException ex) {
75  stats.addException(ex, SERVICE_NAME, cd, "Malformed URL used");
76  } catch (IOException ex) {
77  stats.addException(ex, SERVICE_NAME, cd, "Unknown Input Output error");
78  } catch (TransformerException ex) {
79  stats.addException(SERVICE_NAME, ex, "Transformer Exception");
80  } catch (ParserConfigurationException ex) {
81  stats.addException(SERVICE_NAME, ex, "Parser Exception");
82  } catch (SAXException ex) {
83  stats.addException(SERVICE_NAME, ex, "XML Exception");
84  } catch (XPathExpressionException ex) {
85  stats.addException(SERVICE_NAME, ex, "XPath Exception");
86  }
87  return stats;
88  }
89 
90  @Override
91  public Report function(Corpus co) throws TransformerException, TransformerConfigurationException, IOException, SAXException {
92  Report stats = new Report();
93  Collection<BasicTranscriptionData> btc = co.getBasicTranscriptionData();
94  for (BasicTranscriptionData bt : btc) {
95  stats.merge(function(bt));
96  }
97  return stats;
98  }
99 
100  @Override
101  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
102  try {
103  Class cl = Class.forName("de.uni_hamburg.corpora.ComaData");
104  IsUsableFor.add(cl);
105  } catch (ClassNotFoundException ex) {
106  report.addException(ex, "Usable class not found.");
107  }
108  return IsUsableFor;
109  }
110 
111  public Report doMain(String[] args) {
112  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
113  }
114 
115  public void setURL(URL url) {
116  targeturl = url;
117  }
118 
119  public URL getTargetURL() throws MalformedURLException {
120  return targeturl;
121  }
122 
123  @Override
124  public String getDescription() {
125  String description = "This class creates an html overview of the corpus "
126  + "needed for the ingest into the repository. ";
127  return description;
128 
129  }
130 
131 }
void setParameter(String parameterName, Object parameterValue)
void merge(Report sr)
Definition: Report.java:73
Collection< Class<?extends CorpusData > > getIsUsableFor()
static String InputStream2String(InputStream is)
void addException(Throwable e, String description)
Definition: Report.java:287