package eu.clarin.cmdi.mdservice.action; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Helpers { private static PrintStream log; public static Document readFSLS(String infile) { Document document = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(infile); } catch (FactoryConfigurationError e) { // unable to get a document builder factory notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); } catch (ParserConfigurationException e) { // parser was unable to be configured notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); } catch (IOException e) { // i/o error notifyUser(e.getClass() + ": io-error " + e.getLocalizedMessage()); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } return document; } public static void notifyUser (String msg) { if (log== null) { log = System.err; } log.println(msg); } public static void copy (InputStream in, OutputStream out) throws IOException, InterruptedException { byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len != -1) { out.write(buffer, 0, len); len = in.read(buffer); if (Thread.interrupted()) { throw new InterruptedException(); } } } public static Document getDocument(InputStream is){ Document doc = null; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(is); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return doc; } public static String getDocumentData(InputStream is, String expression){ String data = ""; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr; try { expr = xpath.compile(expression); data = (String) expr.evaluate(getDocument(is), XPathConstants.STRING); } catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } return data; } /* * TODO: this should go to Utilities! */ public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.reset(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sb.toString(); } }