corpus-services  1.0
ScoreHTML.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.PrintWriter;
17 import java.io.UnsupportedEncodingException;
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.Iterator;
24 import java.util.List;
25 import java.util.Vector;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.TransformerConfigurationException;
32 import javax.xml.transform.TransformerException;
33 import javax.xml.xpath.XPathExpressionException;
34 import org.exmaralda.common.corpusbuild.FileIO;
35 import org.exmaralda.partitureditor.interlinearText.HTMLParameters;
36 import org.exmaralda.partitureditor.interlinearText.InterlinearText;
37 import org.exmaralda.partitureditor.jexmaralda.BasicTranscription;
38 import org.exmaralda.partitureditor.jexmaralda.TierFormatTable;
39 import org.exmaralda.partitureditor.jexmaralda.convert.ItConverter;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.JDOMException;
43 import org.jdom.filter.ElementFilter;
44 import org.jdom.output.XMLOutputter;
45 import org.jdom.xpath.XPath;
46 import org.xml.sax.SAXException;
47 
55 public class ScoreHTML extends Visualizer {
56 
57  // resources loaded from directory supplied in pom.xml
58  private static final String STYLESHEET_PATH = "/xsl/Score2HTML.xsl";
59  private final String SERVICE_NAME = "ScoreHTML";
60  private Integer width = 900;
61  URL targeturl;
62  CorpusData cd;
63  String corpusname = "";
64 
65  public ScoreHTML() {
66  }
67 
68  public ScoreHTML(String btAsString) throws JDOMException, TransformerException, TransformerConfigurationException, IOException {
69 
70  createFromBasicTranscription(btAsString);
71  }
72 
80  public String createFromBasicTranscription(String btAsString) throws JDOMException, TransformerConfigurationException, TransformerException, IOException {
81  basicTranscriptionString = btAsString;
83 
84  String result = null;
85 
86  BasicTranscription bt = basicTranscription;
87  bt.normalize();
88 
89  TierFormatTable tft = new TierFormatTable(bt);
90 
91  ItConverter itc = new ItConverter();
92  InterlinearText it = itc.BasicTranscriptionToInterlinearText(bt, tft, 0);
93 
94  //setting HTML parameters
95  HTMLParameters param = new HTMLParameters();
96  param.setWidth(width);
97  param.stretchFactor = 1.2;
98  param.smoothRightBoundary = true;
99  param.includeSyncPoints = true;
100  param.putSyncPointsOutside = true;
101  param.outputAnchors = true;
102  param.frame = "lrtb";
103  param.frameStyle = "Solid";
104  param.setFrameColor(new java.awt.Color(153, 153, 153));
105 
106  it.trim(param);
107 
108  String itAsString = it.toXML();
109  String styles = "/* EMTPY TIER FORMAT TABLE!!! */";
110  if (bt.getTierFormatTable() != null) {
111  styles = bt.getTierFormatTable().toTDCSS();
112  }
113 
114  final Document itDocument = FileIO.readDocumentFromString(itAsString);
115  Document btDocument = bt.toJDOMDocument();
116 
117  // remove "line" elements (die stoeren nur)
118  Iterator i = itDocument.getRootElement().getDescendants(new ElementFilter("line"));
119  Vector toBeRemoved = new Vector();
120  while (i.hasNext()) {
121  toBeRemoved.addElement(i.next());
122  }
123  for (int pos = 0; pos < toBeRemoved.size(); pos++) {
124  Element e = (Element) (toBeRemoved.elementAt(pos));
125  e.detach();
126  }
127 
128  XPath xpath1 = XPath.newInstance("//common-timeline");
129  Element timeline = (Element) (xpath1.selectSingleNode(btDocument));
130  timeline.detach();
131 
132  XPath xpath2 = XPath.newInstance("//head");
133  Element head = (Element) (xpath2.selectSingleNode(btDocument));
134  head.detach();
135 
136  XPath xpath3 = XPath.newInstance("//tier");
137  List tiers = xpath3.selectNodes(btDocument);
138  Element tiersElement = new Element("tiers");
139  for (int pos = 0; pos < tiers.size(); pos++) {
140  Element t = (Element) (tiers.get(pos));
141  t.detach();
142  t.removeContent();
143  tiersElement.addContent(t);
144  }
145 
146  Element tableWidthElement = new Element("table-width");
147  tableWidthElement.setAttribute("table-width", Long.toString(Math.round(param.getWidth())));
148 
149  Element btElement = new Element("basic-transcription");
150  // btElement.addContent(nameElement);
151  btElement.addContent(tableWidthElement);
152  btElement.addContent(head);
153  btElement.addContent(timeline);
154  btElement.addContent(tiersElement);
155 
156  itDocument.getRootElement().addContent(btElement);
157 
158  XMLOutputter xmOut = new XMLOutputter();
159  String xml = xmOut.outputString(itDocument);
160 
161  String xsl = TypeConverter.InputStream2String(getClass().getResourceAsStream(STYLESHEET_PATH));
162 
163  XSLTransformer xt = new XSLTransformer();
164  xt.setParameter("EMAIL_ADDRESS", EMAIL_ADDRESS);
165  xt.setParameter("WEBSERVICE_NAME", SERVICE_NAME);
166  xt.setParameter("HZSK_WEBSITE", HZSK_WEBSITE);
167  xt.setParameter("STYLES", styles);
168  xt.setParameter("TRANSCRIPTION_NAME", cd.getFilenameWithoutFileEnding());
169  if (!corpusname.equals("")) {
170  xt.setParameter("CORPUS_NAME", corpusname);
171  }
172 
173  // perform XSLT transformation
174  result = xt.transform(xml, xsl);
175 
176  // insert JavaScript for highlighting
177  // replace JS/CSS placeholders from XSLT output
178  Pattern regex = Pattern.compile("(<hzsk\\-pi:include( xmlns:hzsk\\-pi=\"https://corpora\\.uni\\-hamburg\\.de/hzsk/xmlns/processing\\-instruction\")?>([^<]+)</hzsk\\-pi:include>)", Pattern.DOTALL);
179  Matcher m = regex.matcher(result);
180  StringBuffer sb = new StringBuffer();
181  while (m.find()) {
182  String insertion = TypeConverter.InputStream2String(getClass().getResourceAsStream(m.group(3)));
183  m.appendReplacement(sb, m.group(0).replaceFirst(Pattern.quote(m.group(1)), insertion));
184  }
185  m.appendTail(sb);
186  result = sb.toString();
187  String js = TypeConverter.InputStream2String(getClass().getResourceAsStream(JS_HIGHLIGHTING_PATH));
188  result = result.replace("<!--jsholder-->", js);
189 
190  setHTML(result);
191 
192  return result;
193  }
194 
201  public Integer getWidth() {
202  return width;
203  }
204 
211  public void setWidth(Integer width) {
212  this.width = width;
213  }
214 
215  public static void main(String[] args) {
216  ScoreHTML shtml = new ScoreHTML();
217  Report stats = shtml.doMain(args);
218  System.out.println(stats.getSummaryLines());
219  System.out.println(stats.getErrorReports());
220  }
221 
222  @Override
223  public Report function(CorpusData cod) throws JDOMException, TransformerException, MalformedURLException, TransformerConfigurationException, IOException, ParserConfigurationException, SAXException, XPathExpressionException {
224  Report stats = new Report();
225  cd = cod;
226  String result = createFromBasicTranscription(cd.toSaveableString());
227  targeturl = new URL(cd.getParentURL() + cd.getFilenameWithoutFileEnding() + "_score.html");
228  CorpusIO cio = new CorpusIO();
229  cio.write(result, targeturl);
230  stats.addCorrect(SERVICE_NAME, cd, "Visualization of file was successfully saved at " + targeturl);
231  return stats;
232  }
233 
234  @Override
235  public Report function(Corpus co) throws JDOMException, TransformerException, TransformerConfigurationException, IOException, MalformedURLException, ParserConfigurationException, SAXException, XPathExpressionException {
236  Report stats = new Report();
237  Collection<BasicTranscriptionData> btc = co.getBasicTranscriptionData();
238  for (BasicTranscriptionData bt : btc) {
239  stats.merge(function(bt));
240  }
241  return stats;
242  }
243 
244  @Override
245  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
246  try {
247  Class cl = Class.forName("de.uni_hamburg.corpora.BasicTranscriptionData");
248  IsUsableFor.add(cl);
249  } catch (ClassNotFoundException ex) {
250  report.addException(ex, "Usable class not found.");
251  }
252  return IsUsableFor;
253  }
254 
255  public Report doMain(String[] args) {
256  Report stats = new Report();
257  try {
258  if (args.length == 0) {
259  System.out.println("Usage: " + ScoreHTML.class.getName()
260  + "EXB [HTML]");
261  System.exit(1);
262  } else {
263  byte[] encoded = Files.readAllBytes(Paths.get(args[0]));
264  String btString = new String(encoded, "UTF-8");
265  ScoreHTML score = new ScoreHTML(btString);
266  if (args.length >= 2) {
267  PrintWriter htmlOut = new PrintWriter(args[1]);
268  htmlOut.print(score.getHTML());
269  htmlOut.close();
270  } else {
271  System.out.println(score.getHTML());
272  }
273  }
274  } catch (UnsupportedEncodingException uee) {
275  stats.addException(SERVICE_NAME, uee, "encoding exception");
276  } catch (IOException ioe) {
277  stats.addException(SERVICE_NAME, ioe, "input output exception");
278  } catch (JDOMException ex) {
279  Logger.getLogger(ScoreHTML.class.getName()).log(Level.SEVERE, null, ex);
280  } catch (TransformerException ex) {
281  Logger.getLogger(ScoreHTML.class.getName()).log(Level.SEVERE, null, ex);
282  }
283  return stats;
284  }
285 
286  public URL getTargetURL() {
287  return targeturl;
288  }
289 
290  public void setCorpusName(String s) {
291  corpusname = s;
292  }
293 
294  @Override
295  public String getDescription() {
296  String description = "This class creates an html visualization "
297  + "in the Score format from an exb. ";
298  return description;
299  }
300 }
void setParameter(String parameterName, Object parameterValue)
String createFromBasicTranscription(String btAsString)
Definition: ScoreHTML.java:80
void merge(Report sr)
Definition: Report.java:73
static BasicTranscription String2BasicTranscription(String btAsString)
static String InputStream2String(InputStream is)
Collection< Class<?extends CorpusData > > getIsUsableFor()
Definition: ScoreHTML.java:245
void addCorrect(String statId, String description)
Definition: Report.java:217
void addException(Throwable e, String description)
Definition: Report.java:287