1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.io.OutputStream; |
---|
6 | import java.io.PrintStream; |
---|
7 | |
---|
8 | import javax.xml.parsers.DocumentBuilder; |
---|
9 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
10 | import javax.xml.parsers.FactoryConfigurationError; |
---|
11 | import javax.xml.parsers.ParserConfigurationException; |
---|
12 | |
---|
13 | import org.w3c.dom.Document; |
---|
14 | import org.xml.sax.SAXException; |
---|
15 | |
---|
16 | public class Helpers { |
---|
17 | |
---|
18 | private static PrintStream log; |
---|
19 | |
---|
20 | public static Document readFSLS(String infile) |
---|
21 | { |
---|
22 | Document document = null; |
---|
23 | try { |
---|
24 | DocumentBuilderFactory factory = |
---|
25 | DocumentBuilderFactory.newInstance(); |
---|
26 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
27 | document = builder.parse(infile); |
---|
28 | } |
---|
29 | catch (FactoryConfigurationError e) { |
---|
30 | // unable to get a document builder factory |
---|
31 | notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); |
---|
32 | } |
---|
33 | catch (ParserConfigurationException e) { |
---|
34 | // parser was unable to be configured |
---|
35 | notifyUser(e.getClass() + ": " + e.getLocalizedMessage()); |
---|
36 | } |
---|
37 | catch (IOException e) { |
---|
38 | // i/o error |
---|
39 | notifyUser(e.getClass() + ": io-error " + e.getLocalizedMessage()); |
---|
40 | } catch (SAXException e) { |
---|
41 | // TODO Auto-generated catch block |
---|
42 | e.printStackTrace(); |
---|
43 | } |
---|
44 | |
---|
45 | return document; |
---|
46 | } |
---|
47 | |
---|
48 | public static void notifyUser (String msg) { |
---|
49 | if (log== null) { log = System.err; } |
---|
50 | log.println(msg); |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | public static void copy (InputStream in, OutputStream out) throws IOException, InterruptedException { |
---|
55 | |
---|
56 | byte[] buffer = new byte[1024]; |
---|
57 | int len = in.read(buffer); |
---|
58 | while (len != -1) { |
---|
59 | out.write(buffer, 0, len); |
---|
60 | len = in.read(buffer); |
---|
61 | if (Thread.interrupted()) { |
---|
62 | throw new InterruptedException(); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | } |
---|