KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > Util


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.core;
27
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.parsers.DocumentBuilder JavaDoc;
38 import java.text.DecimalFormat JavaDoc;
39 import java.text.NumberFormat JavaDoc;
40 import java.text.SimpleDateFormat JavaDoc;
41 import java.text.ParseException JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.GregorianCalendar JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.io.*;
47
48 /**
49  * Class defines various utility functions used in JRobin.
50  *
51  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>
52  */

53 public class Util {
54
55     // pattern RRDTool uses to format doubles in XML files
56
static final String JavaDoc PATTERN = "0.0000000000E00";
57     // directory under $USER_HOME used for demo graphs storing
58
static final String JavaDoc JROBIN_DIR = "jrobin-demo";
59
60     static final DecimalFormat JavaDoc df;
61     static {
62         df = (DecimalFormat JavaDoc) NumberFormat.getNumberInstance(Locale.ENGLISH);
63         df.applyPattern(PATTERN);
64         df.setPositivePrefix("+");
65     }
66
67     /**
68      * Returns current timestamp in seconds (without milliseconds). Returned timestamp
69      * is obtained with the following expression: <p>
70      *
71      * <code>(System.currentTimeMillis() + 500L) / 1000L</code>
72      * @return Current timestamp
73      */

74     public static long getTime() {
75         return (System.currentTimeMillis() + 500L) / 1000L;
76     }
77
78     /**
79      * Just an alias for {@link #getTime()} method.
80      * @return Current timestamp (without milliseconds)
81      */

82     public static long getTimestamp() {
83         return getTime();
84     }
85
86     /**
87      * Rounds the given timestamp to the nearest whole &quote;step&quote;. Rounded value is obtained
88      * from the following expression:<p>
89      * <code>timestamp - timestamp % step;</code>
90      * @param timestamp Timestamp in seconds
91      * @param step Step in seconds
92      * @return "Rounded" timestamp
93      */

94     public static long normalize(long timestamp, long step) {
95         return timestamp - timestamp % step;
96     }
97
98     /**
99      * Returns the greater of two double values, but treats NaN as the smallest possible
100      * value. Note that <code>Math.max()</code> behaves differently for NaN arguments.
101      *
102      * @param x an argument
103      * @param y another argument
104      * @return the lager of arguments
105      */

106     public static double max(double x, double y) {
107         return Double.isNaN(x)? y: Double.isNaN(y)? x: Math.max(x, y);
108     }
109
110     /**
111      * Returns the smaller of two double values, but treats NaN as the greatest possible
112      * value. Note that <code>Math.min()</code> behaves differently for NaN arguments.
113      *
114      * @param x an argument
115      * @param y another argument
116      * @return the smaller of arguments
117      */

118     public static double min(double x, double y) {
119         return Double.isNaN(x)? y: Double.isNaN(y)? x: Math.min(x, y);
120     }
121
122     static double sum(double x, double y) {
123         return Double.isNaN(x)? y: Double.isNaN(y)? x: x + y;
124     }
125
126     static String JavaDoc formatDouble(double x, String JavaDoc nanString, boolean forceExponents) {
127         if(Double.isNaN(x)) {
128             return nanString;
129         }
130         if(forceExponents) {
131             return df.format(x);
132         }
133         return "" + x;
134     }
135
136     static String JavaDoc formatDouble(double x, boolean forceExponents) {
137         return formatDouble(x, "" + Double.NaN, forceExponents);
138     }
139
140     /**
141      * Returns <code>Date</code> object for the given timestamp (in seconds, without
142      * milliseconds)
143      * @param timestamp Timestamp in seconds.
144      * @return Corresponding Date object.
145      */

146     public static Date JavaDoc getDate(long timestamp) {
147         return new Date JavaDoc(timestamp * 1000L);
148     }
149
150     /**
151      * Returns <code>GregorianCalendar</code> object for the given timestamp
152      * (in seconds, without milliseconds)
153      * @param timestamp Timestamp in seconds.
154      * @return Corresponding GregorianCalendar object.
155      */

156     public static GregorianCalendar JavaDoc getGregorianCalendar(long timestamp) {
157         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
158         gc.setTimeInMillis(timestamp * 1000L);
159         return gc;
160     }
161
162     /**
163      * Returns <code>GregorianCalendar</code> object for the given Date object
164      * @param date Date object
165      * @return Corresponding GregorianCalendar object.
166      */

167     public static GregorianCalendar JavaDoc getGregorianCalendar(Date JavaDoc date) {
168         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
169         gc.setTime(date);
170         return gc;
171     }
172
173     /**
174      * Returns timestamp (unix epoch) for the given Date object
175      * @param date Date object
176      * @return Corresponding timestamp (without milliseconds)
177      */

178     public static long getTimestamp(Date JavaDoc date) {
179         // round to whole seconds, ignore milliseconds
180
return (date.getTime() + 499L) / 1000L;
181     }
182
183     /**
184      * Returns timestamp (unix epoch) for the given GregorianCalendar object
185      * @param gc GregorianCalendar object
186      * @return Corresponding timestamp (without milliseconds)
187      */

188     public static long getTimestamp(GregorianCalendar JavaDoc gc) {
189         return getTimestamp(gc.getTime());
190     }
191
192     /**
193      * Returns timestamp (unix epoch) for the given year, month, day, hour and minute.
194      * @param year Year
195      * @param month Month (zero-based)
196      * @param day Day in month
197      * @param hour Hour
198      * @param min Minute
199      * @return Corresponding timestamp
200      */

201     public static long getTimestamp(int year, int month, int day, int hour, int min) {
202         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc(year, month, day, hour, min);
203         return Util.getTimestamp(gc);
204     }
205
206     /**
207      * Returns timestamp (unix epoch) for the given year, month and day.
208      * @param year Year
209      * @param month Month (zero-based)
210      * @param day Day in month
211      * @return Corresponding timestamp
212      */

213     public static long getTimestamp(int year, int month, int day) {
214         return Util.getTimestamp(year, month, day, 0, 0);
215     }
216
217     /**
218      * Parses input string as a double value. If the value cannot be parsed, Double.NaN
219      * is returned (NumberFormatException is never thrown).
220      * @param valueStr String representing double value
221      * @return a double corresponding to the input string
222      */

223     public static double parseDouble(String JavaDoc valueStr) {
224         double value;
225         try {
226             value = Double.parseDouble(valueStr);
227         }
228         catch(NumberFormatException JavaDoc nfe) {
229             value = Double.NaN;
230         }
231         return value;
232     }
233
234     /**
235      * Parses input string as a boolean value. The parser is case insensitive.
236      * @param valueStr String representing boolean value
237      * @return <code>true</code>, if valueStr equals to 'true', 'on', 'yes', 'y' or '1';
238      * <code>false</code> in all other cases.
239      */

240     public static boolean parseBoolean(String JavaDoc valueStr) {
241         return valueStr.equalsIgnoreCase("true") ||
242             valueStr.equalsIgnoreCase("on") ||
243             valueStr.equalsIgnoreCase("yes") ||
244             valueStr.equalsIgnoreCase("y") ||
245             valueStr.equalsIgnoreCase("1");
246     }
247
248     /**
249      * Returns file system separator string.
250      * @return File system separator ("/" on Unix, "\" on Windows)
251      */

252     public static String JavaDoc getFileSeparator() {
253         return System.getProperty("file.separator");
254     }
255
256     /**
257      * Returns path to user's home directory.
258      * @return Path to users home directory, with file separator appended.
259      */

260     public static String JavaDoc getUserHomeDirectory() {
261         return System.getProperty("user.home") + getFileSeparator();
262     }
263
264     private static final File homeDirFile;
265     private static final String JavaDoc homeDirPath;
266
267     static {
268         homeDirPath = getUserHomeDirectory() + JROBIN_DIR + getFileSeparator();
269         homeDirFile = new File(homeDirPath);
270     }
271
272     /**
273      * Returns path to directory used for placement of JRobin demo graphs and creates it
274      * if necessary.
275      * @return Path to demo directory (defaults to $HOME/jrobin/) if directory exists or
276      * was successfully created. Null if such directory could not be created.
277      */

278     public static String JavaDoc getJRobinDemoDirectory() {
279         return (homeDirFile.exists() || homeDirFile.mkdirs())? homeDirPath: null;
280     }
281
282     /**
283      * Returns full path to the file stored in the demo directory of JRobin
284      * @param filename Partial path to the file stored in the demo directory of JRobin
285      * (just name and extension, without parent directories)
286      * @return Full path to the file
287      */

288     public static String JavaDoc getJRobinDemoPath(String JavaDoc filename) {
289         String JavaDoc demoDir = getJRobinDemoDirectory();
290         if(demoDir != null) {
291             return demoDir + filename;
292         }
293         else {
294             return null;
295         }
296     }
297
298     static boolean sameFilePath(String JavaDoc path1, String JavaDoc path2) throws IOException {
299         File file1 = new File(path1);
300         File file2 = new File(path2);
301         return file1.getCanonicalPath().equals(file2.getCanonicalPath());
302     }
303
304     static int getMatchingDatasourceIndex(RrdDb rrd1, int dsIndex, RrdDb rrd2) throws IOException {
305         String JavaDoc dsName = rrd1.getDatasource(dsIndex).getDsName();
306         try {
307             return rrd2.getDsIndex(dsName);
308         } catch (RrdException e) {
309             return -1;
310         }
311     }
312
313     static int getMatchingArchiveIndex(RrdDb rrd1, int arcIndex, RrdDb rrd2)
314         throws IOException {
315         Archive archive = rrd1.getArchive(arcIndex);
316         String JavaDoc consolFun = archive.getConsolFun();
317         int steps = archive.getSteps();
318         try {
319             return rrd2.getArcIndex(consolFun, steps);
320         } catch (RrdException e) {
321             return -1;
322         }
323     }
324
325     static String JavaDoc getTmpFilename() throws IOException {
326         return File.createTempFile("JROBIN_", ".tmp").getCanonicalPath();
327     }
328
329     static final String JavaDoc ISO_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; // ISO
330

331     /**
332      * Creates GregorianCalendar object from a string. The string should represent
333      * either a long integer (UNIX timestamp in seconds without milliseconds,
334      * like "1002354657") or a human readable date string in the format "yyyy-MM-dd HH:mm:ss"
335      * (like "2004-02-25 12:23:45").
336      * @param timeStr Input string
337      * @return GregorianCalendar object
338      */

339     public static GregorianCalendar JavaDoc getGregorianCalendar(String JavaDoc timeStr) {
340         // try to parse it as long
341
try {
342             long timestamp = Long.parseLong(timeStr);
343             return Util.getGregorianCalendar(timestamp);
344         } catch (NumberFormatException JavaDoc e) { }
345         // not a long timestamp, try to parse it as data
346
SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(ISO_DATE_FORMAT);
347         df.setLenient(false);
348         try {
349             Date JavaDoc date = df.parse(timeStr);
350             return Util.getGregorianCalendar(date);
351         } catch (ParseException JavaDoc e) {
352             throw new IllegalArgumentException JavaDoc("Time/date not in " + ISO_DATE_FORMAT +
353                 " format: " + timeStr);
354         }
355     }
356
357     /**
358      * Various DOM utility functions
359      */

360     public static class Xml {
361         public static Node JavaDoc[] getChildNodes(Node JavaDoc parentNode) {
362             return getChildNodes(parentNode, null);
363         }
364
365         public static Node JavaDoc[] getChildNodes(Node JavaDoc parentNode, String JavaDoc childName) {
366             ArrayList JavaDoc nodes = new ArrayList JavaDoc();
367             NodeList JavaDoc nodeList = parentNode.getChildNodes();
368             for (int i = 0; i < nodeList.getLength(); i++) {
369                 Node JavaDoc node = nodeList.item(i);
370                 if (childName == null || node.getNodeName().equals(childName)) {
371                     nodes.add(node);
372                 }
373             }
374             return (Node JavaDoc[]) nodes.toArray(new Node JavaDoc[0]);
375         }
376
377         public static Node JavaDoc getFirstChildNode(Node JavaDoc parentNode, String JavaDoc childName) throws RrdException {
378             Node JavaDoc[] childs = getChildNodes(parentNode, childName);
379             if (childs.length > 0) {
380                 return childs[0];
381             }
382             throw new RrdException("XML Error, no such child: " + childName);
383         }
384
385         public static boolean hasChildNode(Node JavaDoc parentNode, String JavaDoc childName) {
386             Node JavaDoc[] childs = getChildNodes(parentNode, childName);
387             return childs.length > 0;
388         }
389
390         // -- Wrapper around getChildValue with trim
391
public static String JavaDoc getChildValue( Node JavaDoc parentNode, String JavaDoc childName ) throws RrdException {
392             return getChildValue( parentNode, childName, true );
393         }
394
395         public static String JavaDoc getChildValue( Node JavaDoc parentNode, String JavaDoc childName, boolean trim ) throws RrdException {
396             NodeList JavaDoc children = parentNode.getChildNodes();
397             for (int i = 0; i < children.getLength(); i++) {
398                 Node JavaDoc child = children.item(i);
399                 if (child.getNodeName().equals(childName)) {
400                     return getValue(child, trim);
401                 }
402             }
403             throw new RrdException("XML Error, no such child: " + childName);
404         }
405
406         // -- Wrapper around getValue with trim
407
public static String JavaDoc getValue(Node JavaDoc node) {
408             return getValue( node, true );
409         }
410
411         public static String JavaDoc getValue(Node JavaDoc node, boolean trimValue ) {
412             String JavaDoc value = null;
413             Node JavaDoc child = node.getFirstChild();
414             if(child != null) {
415                 value = child.getNodeValue();
416                 if( value != null && trimValue ) {
417                     value = value.trim();
418                 }
419             }
420             return value;
421         }
422
423         public static int getChildValueAsInt(Node JavaDoc parentNode, String JavaDoc childName) throws RrdException {
424             String JavaDoc valueStr = getChildValue(parentNode, childName);
425             return Integer.parseInt(valueStr);
426         }
427
428         public static int getValueAsInt(Node JavaDoc node) {
429             String JavaDoc valueStr = getValue(node);
430             return Integer.parseInt(valueStr);
431         }
432
433         public static long getChildValueAsLong(Node JavaDoc parentNode, String JavaDoc childName) throws RrdException {
434             String JavaDoc valueStr = getChildValue(parentNode, childName);
435             return Long.parseLong(valueStr);
436         }
437
438         public static long getValueAsLong(Node JavaDoc node) {
439             String JavaDoc valueStr = getValue(node);
440             return Long.parseLong(valueStr);
441         }
442
443         public static double getChildValueAsDouble(Node JavaDoc parentNode, String JavaDoc childName) throws RrdException {
444             String JavaDoc valueStr = getChildValue(parentNode, childName);
445             return Util.parseDouble(valueStr);
446         }
447
448         public static double getValueAsDouble(Node JavaDoc node) {
449             String JavaDoc valueStr = getValue(node);
450             return Util.parseDouble(valueStr);
451         }
452
453         public static boolean getChildValueAsBoolean(Node JavaDoc parentNode, String JavaDoc childName) throws RrdException {
454             String JavaDoc valueStr = getChildValue(parentNode, childName);
455             return Util.parseBoolean(valueStr);
456         }
457
458         public static boolean getValueAsBoolean(Node JavaDoc node) {
459             String JavaDoc valueStr = getValue(node);
460             return Util.parseBoolean(valueStr);
461         }
462
463         public static Element JavaDoc getRootElement(InputSource JavaDoc inputSource) throws RrdException, IOException {
464             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
465             factory.setValidating(false);
466             factory.setNamespaceAware(false);
467             try {
468                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
469                 Document JavaDoc doc = builder.parse(inputSource);
470                 return doc.getDocumentElement();
471             } catch (ParserConfigurationException JavaDoc e) {
472                 throw new RrdException(e);
473             } catch (SAXException JavaDoc e) {
474                 throw new RrdException(e);
475             }
476         }
477
478         public static Element JavaDoc getRootElement(String JavaDoc xmlString) throws RrdException, IOException {
479             return getRootElement(new InputSource JavaDoc(new StringReader(xmlString)));
480         }
481
482         public static Element JavaDoc getRootElement(File xmlFile) throws RrdException, IOException {
483             Reader reader = null;
484             try {
485                 reader = new FileReader(xmlFile);
486                 return getRootElement(new InputSource JavaDoc(reader));
487             }
488             finally {
489                 if(reader != null) {
490                     reader.close();
491                 }
492             }
493         }
494     }
495
496     private static Date JavaDoc lastLap = new Date JavaDoc();
497
498     /**
499      * Function used for debugging purposes and performance bottlenecks detection.
500      * Probably of no use for end users of JRobin.
501      * @return String representing time in seconds since last
502      * <code>getLapTime()</code> method call.
503      */

504     public static String JavaDoc getLapTime() {
505         Date JavaDoc newLap = new Date JavaDoc();
506         double seconds = (newLap.getTime() - lastLap.getTime()) / 1000.0;
507         lastLap = newLap;
508         return "[" + seconds + " sec]";
509     }
510
511     /**
512      * Returns the root directory of the JRobin distribution. Useful in some demo applications,
513      * probably of no use anywhere else.<p>
514      *
515      * The function assumes that all JRobin .class files are placed under
516      * the &lt;root&gt;/classes subdirectory and that all jars (libraries) are placed in the
517      * &lt;root&gt;/lib subdirectory (the original JRobin directory structure).<p>
518      *
519      * @return absolute path to JRobin's home directory
520      */

521     public static String JavaDoc getJRobinHomeDirectory() {
522         String JavaDoc className = Util.class.getName().replace('.', '/');
523         String JavaDoc uri = Util.class.getResource("/" + className + ".class").toString();
524         if(uri.startsWith("file:/")) {
525             uri = uri.substring(6);
526             File file = new File(uri);
527             // let's go 5 steps backwards
528
for(int i = 0; i < 5; i++) {
529                 file = file.getParentFile();
530             }
531             uri = file.getAbsolutePath();
532         }
533         else if(uri.startsWith("jar:file:/")) {
534             uri = uri.substring(10, uri.lastIndexOf('!'));
535             File file = new File(uri);
536             // let's go 2 steps backwards
537
for(int i = 0; i < 2; i++) {
538                 file = file.getParentFile();
539             }
540             uri = file.getAbsolutePath();
541         }
542         else {
543             uri = null;
544         }
545         return uri;
546     }
547
548     /**
549      * Compares two doubles, but returns true if x = y = Double.NaN
550      * @param x First double
551      * @param y Second double
552      * @return true, if doubles are equal, false otherwise.
553      */

554     public static boolean equal(double x, double y) {
555         if(Double.isNaN(x) && Double.isNaN(y)) {
556             return true;
557         }
558         else {
559             return x == y;
560         }
561     }
562
563 }
Popular Tags