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

Last change on this file was 1496, checked in by gaba, 13 years ago

repoaction created ,
options + repositories moved to workspaceprofile

File size: 3.8 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.io.OutputStream;
8import java.io.PrintStream;
9
10import javax.xml.parsers.DocumentBuilder;
11import javax.xml.parsers.DocumentBuilderFactory;
12import javax.xml.parsers.FactoryConfigurationError;
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.xpath.XPath;
15import javax.xml.xpath.XPathConstants;
16import javax.xml.xpath.XPathExpression;
17import javax.xml.xpath.XPathExpressionException;
18import javax.xml.xpath.XPathFactory;
19
20import org.w3c.dom.Document;
21import org.xml.sax.SAXException;
22
23public 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        }
73       
74
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
148}
Note: See TracBrowser for help on using the repository browser.