corpus-services  1.0
BasicTranscriptionData.java
Go to the documentation of this file.
1 
9 package de.uni_hamburg.corpora;
10 
12 import org.exmaralda.partitureditor.jexmaralda.BasicTranscription;
13 import java.io.File;
14 import java.io.PrintWriter;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import org.jdom.Document;
18 import org.jdom.input.SAXBuilder;
19 import org.xml.sax.SAXException;
20 import org.jdom.JDOMException;
21 import java.io.IOException;
22 import java.io.FileNotFoundException;
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.xpath.XPathExpressionException;
32 import org.apache.commons.io.FilenameUtils;
33 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
34 
40 public class BasicTranscriptionData implements CorpusData, ContentData, XMLData {
41 
42  private BasicTranscription bt;
43  URL url;
44  Document jdom = new Document();
45  String originalstring;
46  URL parenturl;
47  String filename;
48  String filenamewithoutending;
49 
51  }
52 
53  public BasicTranscriptionData(URL url) {
54  try {
55  this.url = url;
56  SAXBuilder builder = new SAXBuilder();
57  jdom = builder.build(url);
58  File f = new File(url.toURI());
59  loadFile(f);
60  originalstring = new String(Files.readAllBytes(Paths.get(url.toURI())), "UTF-8");
61  URI uri = url.toURI();
62  URI parentURI = uri.getPath().endsWith("/") ? uri.resolve("..") : uri.resolve(".");
63  parenturl = parentURI.toURL();
64  filename = FilenameUtils.getName(url.getPath());
65  filenamewithoutending = FilenameUtils.getBaseName(url.getPath());
66  } catch (JDOMException ex) {
67  Logger.getLogger(UnspecifiedXMLData.class.getName()).log(Level.SEVERE, null, ex);
68  } catch (IOException ex) {
69  Logger.getLogger(UnspecifiedXMLData.class.getName()).log(Level.SEVERE, null, ex);
70  } catch (URISyntaxException ex) {
71  Logger.getLogger(BasicTranscriptionData.class.getName()).log(Level.SEVERE, null, ex);
72  } catch (SAXException ex) {
73  Logger.getLogger(BasicTranscriptionData.class.getName()).log(Level.SEVERE, null, ex);
74  } catch (JexmaraldaException ex) {
75  Logger.getLogger(BasicTranscriptionData.class.getName()).log(Level.SEVERE, null, ex);
76  }
77  }
78 
83  public void loadFile(File f) throws SAXException, JexmaraldaException, MalformedURLException {
84  //we want to read the BasicTranscription as it is without resolving the paths!
85  //bt = new BasicTranscription(f.getAbsolutePath());
86  org.exmaralda.partitureditor.jexmaralda.sax.BasicTranscriptionSaxReader reader = new org.exmaralda.partitureditor.jexmaralda.sax.BasicTranscriptionSaxReader();
87  BasicTranscription t = new BasicTranscription();
88  t = reader.readFromFile(f.getAbsolutePath());
89  bt = t;
90  url = f.toURI().toURL();
91  }
92 
93  /*
94  * uses the field of the Exmaralda Basic transcription to update the jdom field
95  */
96  public void updateJdomDoc() throws SAXException, JexmaraldaException, MalformedURLException, JDOMException, IOException {
97  String xmlString = bt.toXML();
98  SAXBuilder builder = new SAXBuilder();
99  jdom = builder.build(xmlString);
100  }
101 
102  /*
103  private String toPrettyPrintedXML() throws SAXException, JDOMException,
104  IOException, UnsupportedEncodingException {
105  String xmlString = bt.toXML();
106  // this is a bit ugly workaround:
107  SAXBuilder builder = new SAXBuilder();
108  Document xmlDoc = builder.build(new StringReader(xmlString));
109  // FIXME: make HZSK format somewhere
110  Format hzskFormat = Format.getPrettyFormat();
111  hzskFormat.setIndent("\t");
112  XMLOutputter xmlout = new XMLOutputter(hzskFormat);
113  ByteArrayOutputStream baos = new ByteArrayOutputStream();
114  xmlout.output(xmlDoc, baos);
115  return new String(baos.toByteArray(), "UTF-8");
116  }
117  */
118  //I just use the hzsk-corpus-services\src\main\java\de\ uni_hamburg\corpora\
119  //utilities\PrettyPrinter.java here to pretty print the files, so they
120  //will always get pretty printed in the same way
121  //TODO
122  private String toPrettyPrintedXML() throws TransformerException, ParserConfigurationException, SAXException, IOException, XPathExpressionException{
123  PrettyPrinter pp = new PrettyPrinter();
124  String prettyCorpusData = pp.indent(toUnformattedString(), "event");
125  //String prettyCorpusData = pp.indent(bt.toXML(bt.getTierFormatTable()), "event");
126  return prettyCorpusData;
127  }
128 
129  public String toSaveableString() throws TransformerException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
130  return toPrettyPrintedXML();
131  }
132 
133  public static void main(String[] args) {
134  if ((args.length != 2) && (args.length != 1)) {
135  System.out.println("Usage: "
136  + BasicTranscriptionData.class.getName()
137  + " INPUT [OUTPUT]");
138  System.exit(1);
139  }
140  try {
142  btd.loadFile(new File(args[0]));
143  String prettyXML = btd.toSaveableString();
144  boolean emplace = false;
145  PrintWriter output;
146  if (args.length == 2) {
147  output = new PrintWriter(args[1]);
148  } else {
149  // FIXME: reaƶ temp
150  output = new PrintWriter("tempfile.exb");
151  emplace = true;
152  }
153  output.print(prettyXML);
154  output.close();
155  if (emplace) {
156  Files.move(Paths.get("tempfile.exb"), Paths.get(args[0]),
157  java.nio.file.StandardCopyOption.REPLACE_EXISTING);
158  }
159  } catch (SAXException saxe) {
160  saxe.printStackTrace();
161  System.exit(1);
162  } catch (FileNotFoundException fnfe) {
163  fnfe.printStackTrace();
164  System.exit(1);
165  } catch (IOException ioe) {
166  ioe.printStackTrace();
167  System.exit(1);
168  } catch (JexmaraldaException je) {
169  je.printStackTrace();
170  System.exit(1);
171  } catch (TransformerException ex) {
172  ex.printStackTrace();
173  System.exit(1);
174  } catch (ParserConfigurationException ex) {
175  ex.printStackTrace();
176  System.exit(1);
177  } catch (XPathExpressionException ex) {
178  ex.printStackTrace();
179  System.exit(1);
180  }
181  }
182 
183  @Override
184  public URL getURL() {
185  return url;
186  }
187 
188  public Document getReadbtasjdom() {
189  return jdom;
190  }
191 
192  @Override
193  public String toUnformattedString() {
194  return originalstring;
195  }
196 
197  @Override
198  public void updateUnformattedString(String newUnformattedString) {
199  originalstring = newUnformattedString;
200  }
201 
202  public BasicTranscription getEXMARaLDAbt() {
203  return bt;
204  }
205 
206  public void setEXMARaLDAbt(BasicTranscription btn) {
207  bt = btn;
208  }
209 
210  public void setOriginalString(String s) {
211  originalstring = s;
212  }
213 
214  @Override
215  public Document getJdom() {
216  return getReadbtasjdom();
217  }
218 
219  @Override
220  public void setJdom(Document doc) {
221  jdom = doc;
222  }
223 
224  public void setReadbtasjdom(Document doc) {
225  setJdom(doc);
226  }
227 
228  @Override
229  public URL getParentURL() {
230  return parenturl;
231  }
232 
233  @Override
234  public void setURL(URL nurl) {
235  url = nurl;
236  }
237 
238  @Override
239  public void setParentURL(URL url) {
240  parenturl = url;
241  }
242 
243  @Override
244  public String getFilename() {
245  return filename;
246  }
247 
248  @Override
249  public void setFilename(String s) {
250  filename = s;
251  }
252 
253  @Override
255  return filenamewithoutending;
256  }
257 
258  @Override
259  public void setFilenameWithoutFileEnding(String s) {
260  filenamewithoutending = s;
261  }
262 
263 }
String indent(String xml, String suppressedElements)
void updateUnformattedString(String newUnformattedString)