source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/Helpers.java @ 466

Last change on this file since 466 was 466, checked in by vronk, 14 years ago

initial import

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