[466] | 1 | package eu.clarin.cmdi.mdservice.action; |
---|
| 2 | |
---|
[1496] | 3 | import java.io.BufferedReader; |
---|
[466] | 4 | import java.io.IOException; |
---|
| 5 | import java.io.InputStream; |
---|
[1496] | 6 | import java.io.InputStreamReader; |
---|
[466] | 7 | import java.io.OutputStream; |
---|
| 8 | import java.io.PrintStream; |
---|
| 9 | |
---|
| 10 | import javax.xml.parsers.DocumentBuilder; |
---|
| 11 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
| 12 | import javax.xml.parsers.FactoryConfigurationError; |
---|
| 13 | import javax.xml.parsers.ParserConfigurationException; |
---|
[1496] | 14 | import javax.xml.xpath.XPath; |
---|
| 15 | import javax.xml.xpath.XPathConstants; |
---|
| 16 | import javax.xml.xpath.XPathExpression; |
---|
| 17 | import javax.xml.xpath.XPathExpressionException; |
---|
| 18 | import javax.xml.xpath.XPathFactory; |
---|
[466] | 19 | |
---|
| 20 | import org.w3c.dom.Document; |
---|
| 21 | import org.xml.sax.SAXException; |
---|
| 22 | |
---|
| 23 | public class Helpers { |
---|
| 24 | |
---|
| 25 | private static PrintStream log; |
---|
| 26 | |
---|
| 27 | public static Document readFSLS(String infile) |
---|
| 28 | { |
---|
| 29 | Document document = null; |
---|
| 30 | try { |
---|
| 31 | DocumentBuilderFactory factory = |
---|
| 32 | DocumentBuilderFactory.newInstance(); |
---|
| 33 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
| 34 | document = builder.parse(infile); |
---|
| 35 | } |
---|
| 36 | catch (FactoryConfigurationError e) { |
---|
| 37 | // unable to get a document builder factory |
---|
| 38 | notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); |
---|
| 39 | } |
---|
| 40 | catch (ParserConfigurationException e) { |
---|
| 41 | // parser was unable to be configured |
---|
| 42 | notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); |
---|
| 43 | } |
---|
| 44 | catch (IOException e) { |
---|
| 45 | // i/o error |
---|
| 46 | notifyUser(e.getClass() + ": io-error " + e.getLocalizedMessage()); |
---|
| 47 | } catch (SAXException e) { |
---|
| 48 | // TODO Auto-generated catch block |
---|
| 49 | e.printStackTrace(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | return document; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | public static void notifyUser (String msg) { |
---|
| 56 | if (log== null) { log = System.err; } |
---|
| 57 | log.println(msg); |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public static void copy (InputStream in, OutputStream out) throws IOException, InterruptedException { |
---|
| 62 | |
---|
| 63 | byte[] buffer = new byte[1024]; |
---|
| 64 | int len = in.read(buffer); |
---|
| 65 | while (len != -1) { |
---|
| 66 | out.write(buffer, 0, len); |
---|
| 67 | len = in.read(buffer); |
---|
| 68 | if (Thread.interrupted()) { |
---|
| 69 | throw new InterruptedException(); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
[1496] | 73 | |
---|
[466] | 74 | |
---|
[1496] | 75 | public static Document getDocument(InputStream is){ |
---|
| 76 | Document doc = null; |
---|
| 77 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
| 78 | DocumentBuilder docBuilder; |
---|
| 79 | try { |
---|
| 80 | docBuilder = docFactory.newDocumentBuilder(); |
---|
| 81 | doc = docBuilder.parse(is); |
---|
| 82 | } catch (ParserConfigurationException e) { |
---|
| 83 | // TODO Auto-generated catch block |
---|
| 84 | e.printStackTrace(); |
---|
| 85 | } catch (SAXException e) { |
---|
| 86 | // TODO Auto-generated catch block |
---|
| 87 | e.printStackTrace(); |
---|
| 88 | } catch (IOException e) { |
---|
| 89 | // TODO Auto-generated catch block |
---|
| 90 | e.printStackTrace(); |
---|
| 91 | } |
---|
| 92 | return doc; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public static String getDocumentData(InputStream is, String expression){ |
---|
| 96 | String data = ""; |
---|
| 97 | |
---|
| 98 | XPathFactory factory = XPathFactory.newInstance(); |
---|
| 99 | XPath xpath = factory.newXPath(); |
---|
| 100 | XPathExpression expr; |
---|
| 101 | try { |
---|
| 102 | expr = xpath.compile(expression); |
---|
| 103 | data = (String) expr.evaluate(getDocument(is), XPathConstants.STRING); |
---|
| 104 | } catch (XPathExpressionException e) { |
---|
| 105 | // TODO Auto-generated catch block |
---|
| 106 | e.printStackTrace(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return data; |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | /* |
---|
| 113 | * TODO: this should go to Utilities! |
---|
| 114 | */ |
---|
| 115 | public static String convertStreamToString(InputStream is) { |
---|
| 116 | |
---|
| 117 | BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
---|
| 118 | StringBuilder sb = new StringBuilder(); |
---|
| 119 | String line = null; |
---|
| 120 | |
---|
| 121 | try { |
---|
| 122 | while ((line = reader.readLine()) != null) { |
---|
| 123 | sb.append(line + "\n"); |
---|
| 124 | } |
---|
| 125 | } catch (IOException e) { |
---|
| 126 | // TODO Auto-generated catch block |
---|
| 127 | e.printStackTrace(); |
---|
| 128 | } finally { |
---|
| 129 | try { |
---|
| 130 | is.close(); |
---|
| 131 | } catch (IOException e) { |
---|
| 132 | // TODO Auto-generated catch block |
---|
| 133 | e.printStackTrace(); |
---|
| 134 | } |
---|
| 135 | try { |
---|
| 136 | is.reset(); |
---|
| 137 | } catch (IOException e) { |
---|
| 138 | // TODO Auto-generated catch block |
---|
| 139 | e.printStackTrace(); |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | return sb.toString(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[466] | 148 | } |
---|