corpus-services  1.0
HScoreHTML.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.io.UnsupportedEncodingException;
17 import java.io.PrintWriter;
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.nio.file.Files;
21 import java.nio.file.Paths;
22 import java.util.Collection;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.transform.TransformerConfigurationException;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.xpath.XPathExpressionException;
29 import org.exmaralda.partitureditor.jexmaralda.BasicTranscription;
30 import org.jdom.JDOMException;
31 import org.xml.sax.SAXException;
32 
37 public class HScoreHTML extends Visualizer {
38 
39  // resources loaded from directory supplied in pom.xml
40  private static final String STYLESHEET_PATH = "/xsl/EXB2hScoreHTML.xsl";
41  private final String SERVICE_NAME = "HScoreHTML";
42  URL targeturl;
43  CorpusData cd;
44  String corpusname = "";
45 
46  public HScoreHTML() {
47  }
48 
49  public HScoreHTML(String btAsString) {
50  try {
51  createFromBasicTranscription(btAsString);
52  } catch (TransformerException ex) {
53  Logger.getLogger(HScoreHTML.class.getName()).log(Level.SEVERE, null, ex);
54  }
55  }
56 
64  public String createFromBasicTranscription(String btAsString) throws TransformerConfigurationException, TransformerException {
65 
66  basicTranscriptionString = btAsString;
68 
69  String result = null;
70 
71  BasicTranscription bt = basicTranscription;
72  bt.normalize();
73  basicTranscriptionString = bt.toXML();
74  String xsl = TypeConverter.InputStream2String(getClass().getResourceAsStream(STYLESHEET_PATH));
75 
76  // perform XSLT transformation
77  XSLTransformer xt = new XSLTransformer();
78  xt.setParameter("EMAIL_ADDRESS", EMAIL_ADDRESS);
79  xt.setParameter("WEBSERVICE_NAME", SERVICE_NAME);
80  xt.setParameter("HZSK_WEBSITE", HZSK_WEBSITE);
81  String referencedRecording = bt.getHead().getMetaInformation().getReferencedFile("wav");
82  if (referencedRecording != null) {
83  System.out.println("not null " + referencedRecording);
84  xt.setParameter("RECORDING_PATH", referencedRecording);
85  xt.setParameter("RECORDING_TYPE", "wav");
86  }
87  result = xt.transform(basicTranscriptionString, xsl);
88 
89  setHTML(result);
90 
91  return result;
92  }
93 
94  public static void main(String[] args) {
95  try {
96  if (args.length == 0) {
97  System.out.println("Usage: " + HScoreHTML.class.getName()
98  + "EXB [HTML]");
99  System.exit(1);
100  } else {
101  byte[] encoded = Files.readAllBytes(Paths.get(args[0]));
102  String btString = new String(encoded, "UTF-8");
103  HScoreHTML score = new HScoreHTML(btString);
104  if (args.length >= 2) {
105  PrintWriter htmlOut = new PrintWriter(args[1]);
106  htmlOut.print(score.getHTML());
107  htmlOut.close();
108  } else {
109  System.out.println(score.getHTML());
110  }
111  }
112  } catch (UnsupportedEncodingException uee) {
113  uee.printStackTrace();
114  } catch (IOException ioe) {
115  ioe.printStackTrace();
116  }
117  }
118 
119  @Override
120  public Report function(CorpusData cod) {
121  Report stats = new Report();
122  try {
123  cd = cod;
124  stats = new Report();
125  String result = createFromBasicTranscription(cd.toSaveableString());
126  targeturl = new URL(cd.getParentURL() + cd.getFilenameWithoutFileEnding() + "_hscore.html");
127  CorpusIO cio = new CorpusIO();
128  cio.write(result, targeturl);
129  stats.addCorrect(SERVICE_NAME, cd, "Visualization of file was successfully saved at " + targeturl);
130  } catch (MalformedURLException ex) {
131  stats.addException(SERVICE_NAME, ex, "Malformed URL used");
132  } catch (IOException ex) {
133  stats.addException(SERVICE_NAME, ex, "Input Output Exception");
134  } catch (TransformerException ex) {
135  stats.addException(SERVICE_NAME, ex, "Transformer Exception");
136  } catch (ParserConfigurationException ex) {
137  stats.addException(SERVICE_NAME, ex, "Parser Exception");
138  } catch (SAXException ex) {
139  stats.addException(SERVICE_NAME, ex, "XML Exception");
140  } catch (XPathExpressionException ex) {
141  stats.addException(SERVICE_NAME, ex, "XPath Exception");
142  }
143  return stats;
144  }
145 
146  @Override
147  public Report function(Corpus co) throws TransformerException, TransformerConfigurationException, IOException, SAXException {
148  Report stats = new Report();
149  Collection<BasicTranscriptionData> btc = co.getBasicTranscriptionData();
150  for (BasicTranscriptionData bt : btc) {
151  stats.merge(function(bt));
152  }
153  return stats;
154  }
155 
156  @Override
157  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
158  try {
159  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
160  IsUsableFor.add(cl);
161  } catch (ClassNotFoundException ex) {
162  report.addException(ex, "Usable class not found.");
163  }
164  return IsUsableFor;
165  }
166 
167  public Report doMain(String[] args) {
168  try {
169  if (args.length == 0) {
170  System.out.println("Usage: " + ScoreHTML.class.getName()
171  + "EXB [HTML]");
172  System.exit(1);
173  } else {
174  byte[] encoded = Files.readAllBytes(Paths.get(args[0]));
175  String btString = new String(encoded, "UTF-8");
176  ScoreHTML score = new ScoreHTML(btString);
177  if (args.length >= 2) {
178  PrintWriter htmlOut = new PrintWriter(args[1]);
179  htmlOut.print(score.getHTML());
180  htmlOut.close();
181  } else {
182  System.out.println(score.getHTML());
183  }
184  }
185  } catch (UnsupportedEncodingException uee) {
186  report.addException(SERVICE_NAME, uee, "encoding exception");
187  } catch (IOException ioe) {
188  report.addException(SERVICE_NAME, ioe, "input output exception");
189  } catch (JDOMException ex) {
190  Logger.getLogger(HScoreHTML.class.getName()).log(Level.SEVERE, null, ex);
191  } catch (TransformerException ex) {
192  Logger.getLogger(HScoreHTML.class.getName()).log(Level.SEVERE, null, ex);
193  }
194  return report;
195  }
196 
197  @Override
198  public String getDescription() {
199  String description = "This class creates an html visualization "
200  + "in the HScore format from an exb. ";
201  return description;
202  }
203 
204 }
void setParameter(String parameterName, Object parameterValue)
void merge(Report sr)
Definition: Report.java:73
static BasicTranscription String2BasicTranscription(String btAsString)
static String InputStream2String(InputStream is)
void addCorrect(String statId, String description)
Definition: Report.java:217
Collection< Class<?extends CorpusData > > getIsUsableFor()
void addException(Throwable e, String description)
Definition: Report.java:287
String createFromBasicTranscription(String btAsString)
Definition: HScoreHTML.java:64