1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.utils;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12
13 import java.io.BufferedReader;
14 import java.io.ByteArrayInputStream;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.FileOutputStream;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.LineNumberReader;
23 import java.io.OutputStream;
24 import java.io.Reader;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.logging.Level;
28
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.NodeList;
35 import org.xml.sax.SAXException;
36
37
38
39
40
41 public class HelperFileUtil {
42
43
44
45
46
47 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
48
49
50
51
52
53 private static final Logger LOG = LoggerFactory.getLogger(HelperFileUtil.class);
54
55 public static final int ENABLE=0;
56 public static final int DISABLE=1;
57
58
59
60
61
62 private static final String ORBFNAME = "orb.idl";
63 private static final String IRFNAME = "ir.idl";
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public static String readFileAsString(final File f)
79 throws IOException {
80 BufferedReader file = null;
81
82 try {
83 StringBuilder buf = new StringBuilder();
84 file = new BufferedReader(new FileReader(f));
85
86 buf.append(LINE_SEPARATOR);
87 for (String s = file.readLine(); s != null; s = file.readLine()) {
88 buf.append(s).append(LINE_SEPARATOR);
89 }
90 return buf.toString();
91 } finally {
92 if (file != null) {
93 file.close();
94 }
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public static String readIdlFromFile(final File wsdlFile)
111 throws IOException {
112 String idl="";
113 try {
114
115
116 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
117 DocumentBuilder mBuilder;
118
119 mBuilder = factory.newDocumentBuilder();
120 Document document = mBuilder.parse(wsdlFile);
121
122 NodeList ndl = document.getElementsByTagName( "imolacorba:idl" );
123
124 if (ndl.getLength() !=0 )
125 {
126 idl=ndl.item(0).getTextContent();
127 }
128
129
130
131 } catch (SAXException ex) {
132 java.util.logging.Logger.getLogger(HelperFileUtil.class.getName()).log(Level.SEVERE, null, ex);
133 } catch (ParserConfigurationException ex) {
134 java.util.logging.Logger.getLogger(HelperFileUtil.class.getName()).log(Level.SEVERE, null, ex);
135 }
136
137 return idl;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151 public static void writeToFile(InputStream in, File file) throws FileNotFoundException, IOException {
152
153 OutputStream out = new FileOutputStream(file);
154
155
156 byte[] buf = new byte[1024];
157 int len;
158 while ((len = in.read(buf)) > 0) {
159 out.write(buf, 0, len);
160 }
161 in.close();
162 out.close();
163
164 }
165
166
167
168
169
170
171
172 public static void copyFile(File in, File out) throws IOException {
173 FileInputStream fis = new FileInputStream(in);
174 FileOutputStream fos = new FileOutputStream(out);
175 try {
176 byte[] buf = new byte[1024];
177 int i = 0;
178 while ((i = fis.read(buf)) != -1) {
179 fos.write(buf, 0, i);
180 }
181 }
182 finally {
183 if (fis != null) fis.close();
184 if (fos != null) fos.close();
185 }
186 }
187
188
189
190
191
192
193
194 public static void copyFile(URL in, File out) throws IOException {
195 InputStream fis = in.openStream();
196 FileOutputStream fos = new FileOutputStream(out);
197 try {
198 byte[] buf = new byte[1024];
199 int i = 0;
200 while ((i = fis.read(buf)) != -1) {
201 fos.write(buf, 0, i);
202 }
203 }
204 finally {
205 if (fis != null) fis.close();
206 if (fos != null) fos.close();
207 }
208 }
209
210
211
212
213
214
215
216 public static void changeIDLIncludeStaus(final URL idlUrl,final int type) throws FileNotFoundException, URISyntaxException, IOException{
217 File idlFile=new File(idlUrl.toURI());
218 Reader r=new FileReader(idlFile);
219 LineNumberReader reader = new LineNumberReader(r);
220 StringBuilder buffer=new StringBuilder();
221 String line;
222 while((line=reader.readLine())!=null){
223
224 if(type==DISABLE){
225 if (line.startsWith("#include") && (line.contains(ORBFNAME) || line.contains(IRFNAME))) {
226 buffer.append("//"+line+LINE_SEPARATOR);
227 } else{
228 buffer.append(line+LINE_SEPARATOR);
229 }
230 }else{
231 if (line.startsWith("//#include") && (line.contains(ORBFNAME) || line.contains(IRFNAME))) {
232 buffer.append(line.substring(2)+LINE_SEPARATOR);
233 } else{
234 buffer.append(line+LINE_SEPARATOR);
235 }
236 }
237
238 }
239 reader.close();
240 r.close();
241 ByteArrayInputStream bais=new ByteArrayInputStream(buffer.toString().getBytes());
242 writeToFile(bais, idlFile);
243 }
244
245
246 }
247