View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
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     Utility Class for File manipulation
39   * @author <a href="mailto:gvaleri@imolinfo.it">Giancarlo Valeri</a>
40   */
41  public class HelperFileUtil {
42  
43      
44       /**
45       * The system dependent line separator string.
46       */
47      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
48  
49      
50       /**
51       * Logger.
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       * Import to exclude
61       */
62      private static final String ORBFNAME = "orb.idl";
63      private static final String IRFNAME = "ir.idl";
64  
65      
66      /**
67       * Reads file content as a <code>String</code>.
68       *
69       * @param   f  the file to read. Must be not <code>null</code>.
70       * @return  a <code>String</code> representing <code>f</code> content, with
71       *          lines separated by the system dependent terminator string. The
72       *          returned <code>String</code> is never <code>null</code>, but it
73       *          may be empty (<code>""</code>).
74       * @throws  IOException  if the file does not exist, is a directory rather
75       *                       than a regular file, for some other reason cannot
76       *                       be opened for reading or an I/O error occurs.
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      * Reads WSDL file and return IDL as a <code>String</code>.
101      *
102      * @param   f  the WSDL file to read. Must be not <code>null</code>.
103      * @return  a <code>String</code> representing <code>IDL</code> content. The
104      *          returned <code>String</code> is never <code>null</code>, but it
105      *          may be empty (<code>""</code>).
106      * @throws  IOException  if the file does not exist, is a directory rather
107      *                       than a regular file, for some other reason cannot
108      *                       be opened for reading or an I/O error occurs.
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      * Write Inpustream on File
142      *
143      * @param   InputStream Must be not <code>null</code>.
144      * @return  a <code>String</code> representing <code>IDL</code> content. The
145      *          returned <code>String</code> is never <code>null</code>, but it
146      *          may be empty (<code>""</code>).
147      * @throws  IOException  if the file does not exist, is a directory rather
148      *                       than a regular file, for some other reason cannot
149      *                       be opened for reading or an I/O error occurs.
150      */
151     public  static void writeToFile(InputStream in, File file) throws FileNotFoundException, IOException {
152         
153         OutputStream out = new FileOutputStream(file);
154     
155         // Transfer bytes from in to out
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      * Copy a file. 
168      * @param in
169      * @param out
170      * @throws Exception
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      * Copy a file from an URL 
190      * @param in
191      * @param out
192      * @throws Exception
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      * Remove same specified include from the Idl File passed as argument
213      * Process Idl starting from file URL and comments all #include orb.idl or ir.idl
214      * @param idlUrl
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