corpus-services  1.0
TypeConverter.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.utilities;
7 
8 import java.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.StringReader;
12 import java.io.StringWriter;
13 import java.io.UnsupportedEncodingException;
14 import java.nio.charset.StandardCharsets;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMSource;
24 import javax.xml.transform.stream.StreamResult;
25 import javax.xml.transform.stream.StreamSource;
26 import org.exmaralda.partitureditor.jexmaralda.BasicTranscription;
27 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
28 import org.jdom.JDOMException;
29 import org.jdom.input.DOMBuilder;
30 import org.jdom.input.SAXBuilder;
31 import org.jdom.output.DOMOutputter;
32 import org.jdom.output.XMLOutputter;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
35 
42 public class TypeConverter {
43 
50  public static String InputStream2String(InputStream is) {
51  java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
52  String result = s.hasNext() ? s.next() : "";
53  return result;
54  }
55 
62  public static InputStream String2InputStream(String s) {
63  InputStream stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
64  return stream;
65  }
66 
74  public static String BasicTranscription2String(BasicTranscription bt) {
75  return bt.toXML();
76  }
77 
85  public static BasicTranscription String2BasicTranscription(String btAsString) {
86  BasicTranscription btResult = null;
87  try {
88  BasicTranscription bt = new BasicTranscription();
89  bt.BasicTranscriptionFromString(btAsString);
90  btResult = bt;
91  } catch (SAXException ex) {
92  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
93  } catch (JexmaraldaException ex) {
94  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
95  }
96  return btResult;
97  }
98 
105  public static StreamSource String2StreamSource(String s) {
106  StreamSource ss = new StreamSource(new StringReader(s));
107  return ss;
108  }
109 
117  public static String JdomDocument2String(org.jdom.Document jdomDocument) {
118  return new XMLOutputter().outputString(jdomDocument);
119 
120  }
121 
129  public static org.jdom.Document String2JdomDocument(String stringRespresentingDocument) {
130  org.jdom.Document newDocument = null;
131  try {
132 
133  InputStream stream = null;
134  SAXBuilder builder = new SAXBuilder();
135  stream = new ByteArrayInputStream(stringRespresentingDocument.getBytes("UTF-8"));
136  newDocument = builder.build(stream);
137  } catch (UnsupportedEncodingException ex) {
138  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
139  } catch (JDOMException ex) {
140  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
141  } catch (IOException ex) {
142  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
143  }
144  return newDocument;
145  }
146 
155  public static org.jdom.Document W3cDocument2JdomDocument(org.w3c.dom.Document input) {
156  org.jdom.Document jdomDoc = null;
157  try {
158  DOMBuilder builder = new DOMBuilder();
159  jdomDoc = builder.build(input);
160  } catch (Exception e) {
161  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, e);
162  }
163  return jdomDoc;
164  }
165 
174  public static org.w3c.dom.Document JdomDocument2W3cDocument(org.jdom.Document jdomDoc) {
175  org.w3c.dom.Document w3cDoc = null;
176  try {
177  DOMOutputter outputter = new DOMOutputter();
178  w3cDoc = outputter.output(jdomDoc);
179  } catch (JDOMException je) {
180  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, je);
181  }
182  return w3cDoc;
183  }
184 
191  public static String W3cDocument2String(org.w3c.dom.Document doc) {
192  DOMSource domSource = new DOMSource(doc);
193  StringWriter writer = new StringWriter();
194  StreamResult result = new StreamResult(writer);
195  TransformerFactory tf = TransformerFactory.newInstance();
196  Transformer transformer;
197  try {
198  transformer = tf.newTransformer();
199  transformer.transform(domSource, result);
200  } catch (TransformerException ex) {
201  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
202  }
203  return writer.toString();
204  }
205 
213  public static org.w3c.dom.Document String2W3cDocument(String stringRespresentingDocument) {
214  org.w3c.dom.Document w3cDoc = null;
215  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
216  try {
217  DocumentBuilder builder = factory.newDocumentBuilder();
218  w3cDoc = builder.parse(new InputSource(new StringReader(stringRespresentingDocument)));
219  } catch (UnsupportedEncodingException ex) {
220  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
221  } catch (IOException ex) {
222  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
223  } catch (ParserConfigurationException ex) {
224  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
225  } catch (SAXException ex) {
226  Logger.getLogger(TypeConverter.class.getName()).log(Level.SEVERE, null, ex);
227  }
228  return w3cDoc;
229  }
230 
231 
232 
233 }
static StreamSource String2StreamSource(String s)
static org.jdom.Document W3cDocument2JdomDocument(org.w3c.dom.Document input)
static org.w3c.dom.Document String2W3cDocument(String stringRespresentingDocument)
static BasicTranscription String2BasicTranscription(String btAsString)
static org.w3c.dom.Document JdomDocument2W3cDocument(org.jdom.Document jdomDoc)
static String InputStream2String(InputStream is)
static String W3cDocument2String(org.w3c.dom.Document doc)
static org.jdom.Document String2JdomDocument(String stringRespresentingDocument)
static InputStream String2InputStream(String s)
static String JdomDocument2String(org.jdom.Document jdomDocument)
static String BasicTranscription2String(BasicTranscription bt)