KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > Util


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util;
38
39 import net.sourceforge.cruisecontrol.CruiseControlException;
40 import org.jdom.Element;
41 import org.jdom.input.SAXBuilder;
42
43 import java.io.BufferedInputStream JavaDoc;
44 import java.io.BufferedOutputStream JavaDoc;
45 import java.io.BufferedReader JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.FileInputStream JavaDoc;
48 import java.io.FileNotFoundException JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.FileReader JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.InputStream JavaDoc;
53 import java.util.Properties JavaDoc;
54
55 public final class Util {
56
57     private Util() {
58     }
59
60     public static Element loadConfigFile(File JavaDoc configFile) throws CruiseControlException {
61         try {
62             SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
63             return builder.build(configFile).getRootElement();
64         } catch (Exception JavaDoc e) {
65             throw new CruiseControlException(
66                     "failed to load config file [" + (configFile != null
67                     ? configFile.getName()
68                     : "") + "]",
69                     e);
70         }
71     }
72     
73     public static Element parseConfig(InputStream JavaDoc in) throws CruiseControlException {
74         try {
75             SAXBuilder builder = new SAXBuilder();
76             return builder.build(in).getRootElement();
77         } catch (Exception JavaDoc e) {
78             throw new CruiseControlException("failed to parse configuration", e);
79         }
80     }
81     
82     /**
83      * Deletes a File instance. If the file represents a directory, all
84      * the subdirectories and files within.
85      */

86     public static void deleteFile(File JavaDoc file) {
87         if (file == null || !file.exists()) {
88             return;
89         }
90         if (file.isDirectory()) {
91             File JavaDoc[] children = file.listFiles();
92             for (int i = 0; i < children.length; i++) {
93                 File JavaDoc child = children[i];
94                 deleteFile(child);
95             }
96         }
97         file.delete();
98     }
99
100     public static boolean isWindows() {
101         String JavaDoc osName = Util.getOsName();
102         return osName.indexOf("Windows") > -1;
103     }
104
105     public static String JavaDoc getOsName() {
106         return System.getProperty("os.name");
107     }
108
109     /**
110      * Loads a set of properties from the specified properties file. The file
111      * must exist and be in the proper format. If not, a
112      * <code>CruiseControlException</code> is thrown.
113      *
114      * @param file
115      * The <code>File</code> from which to load the properties
116      * @return A <code>Properties</code> object which contains all properties
117      * defined in the file.
118      * @throws CruiseControlException
119      * @throws IOException
120      */

121     public static Properties JavaDoc loadPropertiesFromFile(File JavaDoc file)
122             throws CruiseControlException, IOException JavaDoc {
123         Properties JavaDoc properties = new Properties JavaDoc();
124
125         // Load the properties from file
126
BufferedInputStream JavaDoc bis = null;
127         try {
128             bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
129             properties.load(bis);
130         } catch (FileNotFoundException JavaDoc e) {
131             throw new CruiseControlException(
132                     "Could not load properties from file "
133                             + file.getAbsolutePath() + ". It does not exist.",
134                     e);
135         } finally {
136             if (bis != null) {
137                 bis.close();
138             }
139         }
140
141         return properties;
142     }
143     
144     /**
145      * Stores the contents of a <code>Properties</code> object to the specifed
146      * file. If the file does not exist, it will be created (if possible).
147      *
148      * @param properties
149      * The <code>Properties</code> object which will be stored to
150      * file
151      * @param header
152      * A string which will be written to the first line of the
153      * properties file as a comment. Can be <code>null</code>.
154      * @param file
155      * The properties file to which the properties will be written.
156      *
157      * @throws CruiseControlException
158      * @throws IOException
159      */

160     public static void storePropertiesToFile(Properties JavaDoc properties,
161             String JavaDoc header, File JavaDoc file) throws CruiseControlException,
162             IOException JavaDoc {
163         BufferedOutputStream JavaDoc bos = null;
164
165         try {
166             bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
167             properties.store(bos, header);
168         } catch (FileNotFoundException JavaDoc e) {
169             throw new CruiseControlException(
170                     "Could not store properties to file "
171                             + file.getAbsolutePath() + ". It does not exist.",
172                     e);
173         } finally {
174             if (bos != null) {
175                 bos.close();
176             }
177         }
178     }
179
180     /**
181      * Return the content of the file specified by its path into a <code>String</code>
182      */

183     public static String JavaDoc readFileToString(String JavaDoc fileName) throws IOException JavaDoc {
184         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
185         appendFileToBuffer(fileName, out);
186         return out.toString();
187     }
188
189     public static String JavaDoc readFileToString(File JavaDoc file) throws IOException JavaDoc {
190         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
191         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
192
193         String JavaDoc s = reader.readLine();
194         while (s != null) {
195             result.append(s.trim());
196             s = reader.readLine();
197         }
198         reader.close();
199
200         return result.toString();
201     }
202
203     /**
204      * Append the content of the file specified by its path into a <code>StringBuffer</code>
205      */

206     public static void appendFileToBuffer(String JavaDoc fileName, StringBuffer JavaDoc out) throws IOException JavaDoc {
207         FileReader JavaDoc fr = null;
208         try {
209             fr = new FileReader JavaDoc(fileName);
210             char[] buff = new char[4096];
211             int size = fr.read(buff, 0, 4096);
212             while (size > 0) {
213                 out.append(buff, 0, size);
214                 size = fr.read(buff, 0, 4096);
215             }
216         } finally {
217             if (fr != null) {
218                 try {
219                     fr.close();
220                 } catch (IOException JavaDoc ioe) {
221                     // ignore
222
}
223             }
224         }
225     }
226 }
227
Popular Tags