corpus-services  1.0
Visualizer.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 
12 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.security.NoSuchAlgorithmException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.regex.Pattern;
18 import javax.xml.parsers.ParserConfigurationException;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.xpath.XPathExpressionException;
21 import org.exmaralda.partitureditor.fsm.FSMException;
22 import org.exmaralda.partitureditor.jexmaralda.BasicTranscription;
23 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
24 import org.jdom.JDOMException;
25 import org.xml.sax.SAXException;
26 
36 public abstract class Visualizer implements CorpusFunction {
37 
38  private String html = null;
39  protected BasicTranscription basicTranscription = null;
40  protected String basicTranscriptionString = null;
41 
42  // resources loaded from directory supplied in pom.xml
43  protected String STYLESHEET_PATH = null;
44  protected String JS_HIGHLIGHTING_PATH = "/js/timelight-0.1.min.js";
45 
46  protected String EMAIL_ADDRESS = "corpora@uni-hamburg.de";
47  protected String SERVICE_NAME = null;
48  protected String HZSK_WEBSITE = "https://corpora.uni-hamburg.de/";
49  protected String RECORDING_PATH = null;
50  protected String RECORDING_TYPE = null;
51 
52  CorpusData cd;
53  Report report;
54  Collection<Class<? extends CorpusData>> IsUsableFor = new ArrayList<Class<? extends CorpusData>>();
55  final String function;
56  Boolean canfix = false;
57 
58  public Visualizer() {
59  function = this.getClass().getSimpleName();
60  }
61 
68  public void setHTML(String c) {
69  html = c;
70  }
71 
78  public String getHTML() {
79  return html;
80  }
81 
90  public void setMedia(String recordingId, String recordingType) {
91 
92  String newMediaElem = "";
93 
94  recordingType = recordingType.toLowerCase();
95 
96  if (recordingType.matches("^(wav|mp3|ogg)$")) {
97 
98  newMediaElem = "<audio controls=\"controls\" data-tlid=\"media\">\n"
99  + " <source src=\"" + recordingId + "\" type=\"audio/" + recordingType + "\"/>\n"
100  + "</audio>";
101  }
102 
103  if (recordingType.matches("^(mpeg|mpg|webm)$")) {
104  newMediaElem = "<video controls=\"controls\" data-tlid=\"media\">\n"
105  + " <source src=\"" + recordingId + "\" type=\"video/" + recordingType + "\"/>\n"
106  + "</video>";
107  }
108 
109  setHTML(Pattern.compile("<div[^>]*id=\"mediaplayer\".*?</div>", Pattern.DOTALL).matcher(html).replaceAll("<div id=\"mediaplayer\" class=\"sidebarcontrol\">" + newMediaElem + "</div>"));
110 
111  }
112 
119  public void removeMedia() {
120 
121  setHTML(Pattern.compile("<div[^>]*id=\"mediaplayer\".*?</div>", Pattern.DOTALL).matcher(html).replaceAll("<div id=\"mediaplayer\" class=\"sidebarcontrol\"></div>"));
122 
123  }
124 
125  public Report execute(CorpusData cd) {
126  report = new Report();
127  try {
128  report = function(cd);
129  } catch (JDOMException jdome) {
130  report.addException(jdome, function, cd, "Unknown parsing error");
131  } catch (SAXException saxe) {
132  report.addException(saxe, function, cd, "Unknown parsing error");
133  } catch (IOException ioe) {
134  report.addException(ioe, function, cd, "File reading error");
135  } catch (FSMException ex) {
136  report.addException(ex, function, cd, "File reading error");
137  } catch (URISyntaxException ex) {
138  report.addException(ex, function, cd, "File reading erro");
139  } catch (ParserConfigurationException ex) {
140  report.addException(ex, function, cd, "File reading error");
141  } catch (TransformerException ex) {
142  report.addException(ex, function, cd, "File reading error");
143  } catch (XPathExpressionException ex) {
144  report.addException(ex, function, cd, "File reading error");
145  } catch (ClassNotFoundException ex) {
146  report.addException(ex, function, cd, "File reading error");
147  } catch (NoSuchAlgorithmException ex) {
148  report.addException(ex, function, cd, "File reading error");
149  } catch (JexmaraldaException ex) {
150  report.addException(ex, function, cd, "File reading error");
151  }
152 
153  return report;
154  }
155 
156  //no fix boolean needed
157  public Report execute(CorpusData cd, boolean fix) {
158  return execute(cd);
159  }
160 
161  public Report execute(Corpus c) {
162  report = new Report();
163  try {
164  report = function(c);
165  } catch (JexmaraldaException je) {
166  report.addException(je, function, cd, "Unknown parsing error");
167  } catch (JDOMException jdome) {
168  report.addException(jdome, function, cd, "Unknown parsing error");
169  } catch (SAXException saxe) {
170  report.addException(saxe, function, cd, "Unknown parsing error");
171  } catch (IOException ioe) {
172  report.addException(ioe, function, cd, "File reading error");
173  } catch (FSMException ex) {
174  report.addException(ex, function, cd, "File reading error");
175  } catch (URISyntaxException ex) {
176  report.addException(ex, function, cd, "File reading erro");
177  } catch (ParserConfigurationException ex) {
178  report.addException(ex, function, cd, "File reading error");
179  } catch (TransformerException ex) {
180  report.addException(ex, function, cd, "File reading error");
181  } catch (XPathExpressionException ex) {
182  report.addException(ex, function, cd, "File reading error");
183  } catch (ClassNotFoundException ex) {
184  report.addException(ex, function, cd, "File reading error");
185  } catch (NoSuchAlgorithmException ex) {
186  report.addException(ex, function, cd, "File reading error");
187  }
188 
189  return report;
190  }
191 
192  //no fix boolean needed
193  public Report execute(Corpus c, boolean fix) {
194  return execute(c);
195  }
196 
197  //TODO
198  public abstract Report function(CorpusData cd) throws NoSuchAlgorithmException, ClassNotFoundException, FSMException, URISyntaxException, SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException, JDOMException;
199 
200  ;
201 
202  public abstract Report function(Corpus c) throws NoSuchAlgorithmException, ClassNotFoundException, FSMException, URISyntaxException, SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException, JDOMException;
203 
204  ;
205 
206  public abstract Collection<Class<? extends CorpusData>> getIsUsableFor();
207 
208  public void setIsUsableFor(Collection<Class<? extends CorpusData>> cdc) {
209  for (Class<? extends CorpusData> cl : cdc) {
210  IsUsableFor.add(cl);
211  }
212  }
213 
214  public String getFunction() {
215  return function;
216  }
217 
218  public Boolean getCanFix() {
219  return canfix;
220  }
221 
222 }
abstract Collection< Class<?extends CorpusData > > getIsUsableFor()
void setMedia(String recordingId, String recordingType)
Definition: Visualizer.java:90
Report execute(CorpusData cd, boolean fix)
void setIsUsableFor(Collection< Class<?extends CorpusData >> cdc)
void addException(Throwable e, String description)
Definition: Report.java:287