6 package de.uni_hamburg.corpora.visualization;
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 import java.io.UnsupportedEncodingException;
18 import java.net.MalformedURLException;
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;
58 private static final String STYLESHEET_PATH =
"/xsl/Score2HTML.xsl";
59 private final String SERVICE_NAME =
"ScoreHTML";
60 private Integer width = 900;
63 String corpusname =
"";
68 public ScoreHTML(String btAsString)
throws JDOMException, TransformerException, TransformerConfigurationException, IOException {
80 public String
createFromBasicTranscription(String btAsString)
throws JDOMException, TransformerConfigurationException, TransformerException, IOException {
89 TierFormatTable tft =
new TierFormatTable(bt);
91 ItConverter itc =
new ItConverter();
92 InterlinearText it = itc.BasicTranscriptionToInterlinearText(bt, tft, 0);
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));
108 String itAsString = it.toXML();
109 String styles =
"/* EMTPY TIER FORMAT TABLE!!! */";
110 if (bt.getTierFormatTable() != null) {
111 styles = bt.getTierFormatTable().toTDCSS();
114 final Document itDocument = FileIO.readDocumentFromString(itAsString);
115 Document btDocument = bt.toJDOMDocument();
118 Iterator i = itDocument.getRootElement().getDescendants(
new ElementFilter(
"line"));
119 Vector toBeRemoved =
new Vector();
120 while (i.hasNext()) {
121 toBeRemoved.addElement(i.next());
123 for (
int pos = 0; pos < toBeRemoved.size(); pos++) {
124 Element e = (Element) (toBeRemoved.elementAt(pos));
128 XPath xpath1 = XPath.newInstance(
"//common-timeline");
129 Element timeline = (Element) (xpath1.selectSingleNode(btDocument));
132 XPath xpath2 = XPath.newInstance(
"//head");
133 Element head = (Element) (xpath2.selectSingleNode(btDocument));
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));
143 tiersElement.addContent(t);
146 Element tableWidthElement =
new Element(
"table-width");
147 tableWidthElement.setAttribute(
"table-width", Long.toString(Math.round(param.getWidth())));
149 Element btElement =
new Element(
"basic-transcription");
151 btElement.addContent(tableWidthElement);
152 btElement.addContent(head);
153 btElement.addContent(timeline);
154 btElement.addContent(tiersElement);
156 itDocument.getRootElement().addContent(btElement);
158 XMLOutputter xmOut =
new XMLOutputter();
159 String xml = xmOut.outputString(itDocument);
169 if (!corpusname.equals(
"")) {
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();
183 m.appendReplacement(sb, m.group(0).replaceFirst(Pattern.quote(m.group(1)), insertion));
186 result = sb.toString();
188 result = result.replace(
"<!--jsholder-->", js);
215 public static void main(String[] args) {
223 public Report function(
CorpusData cod)
throws JDOMException, TransformerException, MalformedURLException, TransformerConfigurationException, IOException, ParserConfigurationException, SAXException, XPathExpressionException {
229 cio.write(result, targeturl);
230 stats.
addCorrect(SERVICE_NAME, cd,
"Visualization of file was successfully saved at " + targeturl);
235 public Report function(
Corpus co)
throws JDOMException, TransformerException, TransformerConfigurationException, IOException, MalformedURLException, ParserConfigurationException, SAXException, XPathExpressionException {
237 Collection<BasicTranscriptionData> btc = co.getBasicTranscriptionData();
239 stats.
merge(
function(bt));
247 Class cl = Class.forName(
"de.uni_hamburg.corpora.BasicTranscriptionData");
249 }
catch (ClassNotFoundException ex) {
258 if (args.length == 0) {
259 System.out.println(
"Usage: " +
ScoreHTML.class.getName()
263 byte[] encoded = Files.readAllBytes(Paths.get(args[0]));
264 String btString =
new String(encoded,
"UTF-8");
266 if (args.length >= 2) {
267 PrintWriter htmlOut =
new PrintWriter(args[1]);
268 htmlOut.print(score.
getHTML());
271 System.out.println(score.
getHTML());
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);
296 String description =
"This class creates an html visualization " 297 +
"in the Score format from an exb. ";
String getFilenameWithoutFileEnding()
String toSaveableString()
String createFromBasicTranscription(String btAsString)
ScoreHTML(String btAsString)
String basicTranscriptionString
static void main(String[] args)
static BasicTranscription String2BasicTranscription(String btAsString)
static String InputStream2String(InputStream is)
void setCorpusName(String s)
Collection< Class<?extends CorpusData > > getIsUsableFor()
void addCorrect(String statId, String description)
String JS_HIGHLIGHTING_PATH
BasicTranscription basicTranscription
Report doMain(String[] args)
void addException(Throwable e, String description)
void setWidth(Integer width)