1 25 26 package org.jrobin.core; 27 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Document ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.DocumentBuilder ; 38 import java.text.DecimalFormat ; 39 import java.text.NumberFormat ; 40 import java.text.SimpleDateFormat ; 41 import java.text.ParseException ; 42 import java.util.Date ; 43 import java.util.Locale ; 44 import java.util.GregorianCalendar ; 45 import java.util.ArrayList ; 46 import java.io.*; 47 48 53 public class Util { 54 55 static final String PATTERN = "0.0000000000E00"; 57 static final String JROBIN_DIR = "jrobin-demo"; 59 60 static final DecimalFormat df; 61 static { 62 df = (DecimalFormat ) NumberFormat.getNumberInstance(Locale.ENGLISH); 63 df.applyPattern(PATTERN); 64 df.setPositivePrefix("+"); 65 } 66 67 74 public static long getTime() { 75 return (System.currentTimeMillis() + 500L) / 1000L; 76 } 77 78 82 public static long getTimestamp() { 83 return getTime(); 84 } 85 86 94 public static long normalize(long timestamp, long step) { 95 return timestamp - timestamp % step; 96 } 97 98 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 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 formatDouble(double x, String 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 formatDouble(double x, boolean forceExponents) { 137 return formatDouble(x, "" + Double.NaN, forceExponents); 138 } 139 140 146 public static Date getDate(long timestamp) { 147 return new Date (timestamp * 1000L); 148 } 149 150 156 public static GregorianCalendar getGregorianCalendar(long timestamp) { 157 GregorianCalendar gc = new GregorianCalendar (); 158 gc.setTimeInMillis(timestamp * 1000L); 159 return gc; 160 } 161 162 167 public static GregorianCalendar getGregorianCalendar(Date date) { 168 GregorianCalendar gc = new GregorianCalendar (); 169 gc.setTime(date); 170 return gc; 171 } 172 173 178 public static long getTimestamp(Date date) { 179 return (date.getTime() + 499L) / 1000L; 181 } 182 183 188 public static long getTimestamp(GregorianCalendar gc) { 189 return getTimestamp(gc.getTime()); 190 } 191 192 201 public static long getTimestamp(int year, int month, int day, int hour, int min) { 202 GregorianCalendar gc = new GregorianCalendar (year, month, day, hour, min); 203 return Util.getTimestamp(gc); 204 } 205 206 213 public static long getTimestamp(int year, int month, int day) { 214 return Util.getTimestamp(year, month, day, 0, 0); 215 } 216 217 223 public static double parseDouble(String valueStr) { 224 double value; 225 try { 226 value = Double.parseDouble(valueStr); 227 } 228 catch(NumberFormatException nfe) { 229 value = Double.NaN; 230 } 231 return value; 232 } 233 234 240 public static boolean parseBoolean(String 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 252 public static String getFileSeparator() { 253 return System.getProperty("file.separator"); 254 } 255 256 260 public static String getUserHomeDirectory() { 261 return System.getProperty("user.home") + getFileSeparator(); 262 } 263 264 private static final File homeDirFile; 265 private static final String homeDirPath; 266 267 static { 268 homeDirPath = getUserHomeDirectory() + JROBIN_DIR + getFileSeparator(); 269 homeDirFile = new File(homeDirPath); 270 } 271 272 278 public static String getJRobinDemoDirectory() { 279 return (homeDirFile.exists() || homeDirFile.mkdirs())? homeDirPath: null; 280 } 281 282 288 public static String getJRobinDemoPath(String filename) { 289 String demoDir = getJRobinDemoDirectory(); 290 if(demoDir != null) { 291 return demoDir + filename; 292 } 293 else { 294 return null; 295 } 296 } 297 298 static boolean sameFilePath(String path1, String 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 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 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 getTmpFilename() throws IOException { 326 return File.createTempFile("JROBIN_", ".tmp").getCanonicalPath(); 327 } 328 329 static final String ISO_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 331 339 public static GregorianCalendar getGregorianCalendar(String timeStr) { 340 try { 342 long timestamp = Long.parseLong(timeStr); 343 return Util.getGregorianCalendar(timestamp); 344 } catch (NumberFormatException e) { } 345 SimpleDateFormat df = new SimpleDateFormat (ISO_DATE_FORMAT); 347 df.setLenient(false); 348 try { 349 Date date = df.parse(timeStr); 350 return Util.getGregorianCalendar(date); 351 } catch (ParseException e) { 352 throw new IllegalArgumentException ("Time/date not in " + ISO_DATE_FORMAT + 353 " format: " + timeStr); 354 } 355 } 356 357 360 public static class Xml { 361 public static Node [] getChildNodes(Node parentNode) { 362 return getChildNodes(parentNode, null); 363 } 364 365 public static Node [] getChildNodes(Node parentNode, String childName) { 366 ArrayList nodes = new ArrayList (); 367 NodeList nodeList = parentNode.getChildNodes(); 368 for (int i = 0; i < nodeList.getLength(); i++) { 369 Node node = nodeList.item(i); 370 if (childName == null || node.getNodeName().equals(childName)) { 371 nodes.add(node); 372 } 373 } 374 return (Node []) nodes.toArray(new Node [0]); 375 } 376 377 public static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { 378 Node [] 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 parentNode, String childName) { 386 Node [] childs = getChildNodes(parentNode, childName); 387 return childs.length > 0; 388 } 389 390 public static String getChildValue( Node parentNode, String childName ) throws RrdException { 392 return getChildValue( parentNode, childName, true ); 393 } 394 395 public static String getChildValue( Node parentNode, String childName, boolean trim ) throws RrdException { 396 NodeList children = parentNode.getChildNodes(); 397 for (int i = 0; i < children.getLength(); i++) { 398 Node 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 public static String getValue(Node node) { 408 return getValue( node, true ); 409 } 410 411 public static String getValue(Node node, boolean trimValue ) { 412 String value = null; 413 Node 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 parentNode, String childName) throws RrdException { 424 String valueStr = getChildValue(parentNode, childName); 425 return Integer.parseInt(valueStr); 426 } 427 428 public static int getValueAsInt(Node node) { 429 String valueStr = getValue(node); 430 return Integer.parseInt(valueStr); 431 } 432 433 public static long getChildValueAsLong(Node parentNode, String childName) throws RrdException { 434 String valueStr = getChildValue(parentNode, childName); 435 return Long.parseLong(valueStr); 436 } 437 438 public static long getValueAsLong(Node node) { 439 String valueStr = getValue(node); 440 return Long.parseLong(valueStr); 441 } 442 443 public static double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { 444 String valueStr = getChildValue(parentNode, childName); 445 return Util.parseDouble(valueStr); 446 } 447 448 public static double getValueAsDouble(Node node) { 449 String valueStr = getValue(node); 450 return Util.parseDouble(valueStr); 451 } 452 453 public static boolean getChildValueAsBoolean(Node parentNode, String childName) throws RrdException { 454 String valueStr = getChildValue(parentNode, childName); 455 return Util.parseBoolean(valueStr); 456 } 457 458 public static boolean getValueAsBoolean(Node node) { 459 String valueStr = getValue(node); 460 return Util.parseBoolean(valueStr); 461 } 462 463 public static Element getRootElement(InputSource inputSource) throws RrdException, IOException { 464 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 465 factory.setValidating(false); 466 factory.setNamespaceAware(false); 467 try { 468 DocumentBuilder builder = factory.newDocumentBuilder(); 469 Document doc = builder.parse(inputSource); 470 return doc.getDocumentElement(); 471 } catch (ParserConfigurationException e) { 472 throw new RrdException(e); 473 } catch (SAXException e) { 474 throw new RrdException(e); 475 } 476 } 477 478 public static Element getRootElement(String xmlString) throws RrdException, IOException { 479 return getRootElement(new InputSource (new StringReader(xmlString))); 480 } 481 482 public static Element getRootElement(File xmlFile) throws RrdException, IOException { 483 Reader reader = null; 484 try { 485 reader = new FileReader(xmlFile); 486 return getRootElement(new InputSource (reader)); 487 } 488 finally { 489 if(reader != null) { 490 reader.close(); 491 } 492 } 493 } 494 } 495 496 private static Date lastLap = new Date (); 497 498 504 public static String getLapTime() { 505 Date newLap = new Date (); 506 double seconds = (newLap.getTime() - lastLap.getTime()) / 1000.0; 507 lastLap = newLap; 508 return "[" + seconds + " sec]"; 509 } 510 511 521 public static String getJRobinHomeDirectory() { 522 String className = Util.class.getName().replace('.', '/'); 523 String uri = Util.class.getResource("/" + className + ".class").toString(); 524 if(uri.startsWith("file:/")) { 525 uri = uri.substring(6); 526 File file = new File(uri); 527 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 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 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 |