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