1 17 18 package org.pentaho.core.util; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileWriter ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.StringReader ; 26 import java.io.StringWriter ; 27 import java.math.BigDecimal ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.text.ParseException ; 31 import java.text.SimpleDateFormat ; 32 import java.util.ArrayList ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Locale ; 38 import java.util.Map ; 39 import java.util.ResourceBundle ; 40 import java.util.Set ; 41 import java.util.TimeZone ; 42 import javax.xml.transform.Transformer ; 43 import javax.xml.transform.TransformerFactory ; 44 import javax.xml.transform.URIResolver ; 45 import javax.xml.transform.stream.StreamResult ; 46 import javax.xml.transform.stream.StreamSource ; 47 import org.dom4j.Document; 48 import org.dom4j.DocumentException; 49 import org.dom4j.DocumentHelper; 50 import org.dom4j.Element; 51 import org.dom4j.Node; 52 import org.pentaho.core.session.IPentahoSession; 53 import org.pentaho.core.system.PentahoSystem; 54 import org.pentaho.messages.Messages; 55 import org.pentaho.messages.util.LocaleHelper; 56 import org.pentaho.util.PentahoChainedException; 57 import org.pentaho.util.logging.Logger; 58 59 65 public class XmlHelper { 66 67 public static Document getDocFromString(String str) { 68 Document document = null; 69 try { 70 document = DocumentHelper.parseText(str); 71 } catch (Exception e) { 72 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0001_GET_DOC_FROM_STRING_ERROR", e.getMessage()), e); } 74 return document; 75 } 76 77 public static Document getDocFromFile(File f) { 78 Document document = null; 79 try { 80 FileInputStream fis = new FileInputStream (f); 81 try { 82 document = getDocFromStream( fis ); 83 } finally { 84 fis.close(); 85 } 86 } catch (Exception e) { 87 String fName = null; 88 try { 89 fName = f.getCanonicalPath(); 90 } catch (Exception ignored) { 91 } 92 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0002_GET_DOC_FROM_FILE_ERROR", fName), e); } 94 return document; 95 } 96 97 public static Document getDocFromStream(InputStream inStream) throws Exception { 98 Document document = null; 99 InputStreamReader reader = new InputStreamReader (inStream, LocaleHelper.getSystemEncoding()); 100 try { 101 StringBuffer sb = new StringBuffer (); 102 if (reader != null) { 103 char buffer[] = new char[1000]; 104 int bytesRead; 105 while ((bytesRead = reader.read(buffer)) > 0) { 106 sb.append(buffer, 0, bytesRead); 107 } 108 } 109 document = DocumentHelper.parseText(sb.toString()); 110 return document; 111 } finally { 112 try { 113 inStream.close(); 114 } catch (Exception ex) { 115 Logger.error(XmlHelper.class.getName(), ex.toString()); 117 } 118 } 119 } 120 121 protected static final StringBuffer transform(StreamSource xslSrc, StreamSource docSrc, Map params, IPentahoSession session) throws Exception { 122 123 StringBuffer sb = new StringBuffer (); 125 StringWriter writer = new StringWriter (); 126 if ((xslSrc == null) || (docSrc == null)) { 127 if (xslSrc == null) 128 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0003_NULL_XSL_SOURCE")); if (docSrc == null) 130 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0004_NULL_DOCUMENT")); } else { 132 URIResolver resolver = new SolutionURIResolver(session); 133 TransformerFactory tf = TransformerFactory.newInstance(); 134 tf.setURIResolver(resolver); 135 Transformer t = tf.newTransformer(xslSrc); 137 if (params != null) { 139 Set keys = params.keySet(); 140 Iterator it = keys.iterator(); 141 String key, val; 142 while (it.hasNext()) { 143 key = (String ) it.next(); 144 val = (String ) params.get(key); 145 t.setParameter(key, val); 146 } 147 } 148 t.transform(docSrc, new StreamResult (writer)); 149 sb = writer.getBuffer(); 150 } 151 return sb; 152 } 153 154 public static final StringBuffer transformXml(String xslName, String path, Document document, Map params, IPentahoSession session) { 155 156 try { 157 if (params == null) { 159 params = new HashMap (); 160 } 161 params.put("output-encoding", LocaleHelper.getSystemEncoding()); File localeXsl = getlocalizedXsl(path, xslName); 165 if (localeXsl == null || !localeXsl.exists()) { 166 return null; 167 } 168 FileInputStream xslIS = new FileInputStream (localeXsl); 169 StreamSource xslSrc = new StreamSource (xslIS); 170 StringReader xmlReader = new StringReader (document.asXML()); 171 StreamSource docSrc = new StreamSource (xmlReader); 172 return transform(xslSrc, docSrc, params, session); 173 } catch (Exception e) { 174 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0006_TRASFORM_XML_ERROR", e.getMessage(), xslName), e); } 176 return null; 177 } 178 179 public static final StringBuffer transformXml(String xslName, Document document, Map params, IPentahoSession session) { 180 181 try { 182 if (params == null) { 184 params = new HashMap (); 185 } 186 params.put("output-encoding", LocaleHelper.getSystemEncoding()); File localeXsl = getlocalizedXsl(xslName); 188 if (localeXsl == null || !localeXsl.exists()) { 189 return null; 190 } 191 FileInputStream xslIS = new FileInputStream (localeXsl); 192 StreamSource xslSrc = new StreamSource (xslIS); 193 StringReader xmlReader = new StringReader (document.asXML()); 194 StreamSource docSrc = new StreamSource (xmlReader); 195 return transform(xslSrc, docSrc, params, session); 196 } catch (Exception e) { 197 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0006_TRASFORM_XML_ERROR", e.getMessage(), xslName), e); } 199 return null; 200 } 201 202 public static final StringBuffer transformXmlUrl(String xslName, String path, String uri, Map params, IPentahoSession session) { 203 try { 204 if (params == null) { 206 params = new HashMap (); 207 } 208 params.put("output-encoding", LocaleHelper.getSystemEncoding()); File localeXsl = getlocalizedXsl(path, xslName); 210 FileInputStream xslIS = new FileInputStream (localeXsl); 211 StreamSource xslSrc = new StreamSource (xslIS); 212 URL url = new URL (uri); 213 InputStream is = url.openStream(); 214 StreamSource docSrc = new StreamSource (is); 215 return transform(xslSrc, docSrc, params, session); 216 } catch (Exception e) { 217 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0007_TRANSFORM_XML_URL", e.getMessage(), xslName), e); } 219 return null; 220 } 221 222 public static final StringBuffer transformXml(String xslName, String path, String document, Map params, IPentahoSession session) { 223 224 try { 225 if (params == null) { 227 params = new HashMap (); 228 } 229 params.put("output-encoding", LocaleHelper.getSystemEncoding()); File localeXsl = getlocalizedXsl(path, xslName); 231 FileInputStream xslIS = new FileInputStream (localeXsl); 232 StreamSource xslSrc = new StreamSource (xslIS); 233 StringReader xmlReader = new StringReader (document); 234 StreamSource docSrc = new StreamSource (xmlReader); 235 return transform(xslSrc, docSrc, params, session); 236 } catch (Exception e) { 237 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0006_TRASFORM_XML_ERROR", e.getMessage(), xslName), e); } 239 return null; 240 } 241 242 public static ResourceBundle getBundle(String baseName, File localeDir, Locale locale) { 243 try { 244 245 URL localeDirUrl = localeDir.getParentFile().toURL(); 246 URLClassLoader loader = new URLClassLoader (new URL [] { localeDirUrl }); 247 248 ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, loader); 249 return bundle; 250 251 } catch (Exception e) { 252 Logger.error("XmlHelper", Messages.getErrorString("XmlHelper.ERROR_0012_COULD_NOT_READ_PROPERTIES", localeDir.getAbsolutePath() + File.separator + baseName), e); } 254 return null; 255 } 256 257 public static void localizeDoc(Node document, File file, Locale locale, boolean xmlEncode) { 258 259 String fileName = file.getName(); 260 int dotIndex = fileName.indexOf('.'); 261 String baseName = fileName.substring(0, dotIndex); 262 ResourceBundle bundle = null; 263 bundle = getBundle(baseName, file, locale); 264 if (bundle == null) { 265 return; 266 } 267 localizeDoc(document, bundle, locale, file.getAbsolutePath(), xmlEncode); 268 269 } 270 271 public static String getXmlEncodedString(String rawValue) { 272 StringBuffer value = new StringBuffer (); 273 for (int n = 0; n < rawValue.length(); n++) { 274 int charValue = rawValue.charAt(n); 275 if (charValue >= 0x80) { 276 value.append("&#x"); value.append(Integer.toString(charValue, 0x10)); 278 value.append(";"); } else { 280 value.append((char) charValue); 281 } 282 } 283 return value.toString(); 284 285 } 286 287 public static void localizeDoc(Node document, ResourceBundle bundle, Locale locale, String bundlePath, boolean xmlEncode) { 288 289 if (bundle == null) { 290 return; 291 } 292 try { 293 294 List nodes = document.selectNodes("descendant::*"); Iterator nodeIterator = nodes.iterator(); 296 while (nodeIterator.hasNext()) { 297 Node node = (Node) nodeIterator.next(); 298 String name = node.getText(); 299 if (name.startsWith("%") && !node.getPath().endsWith("/text()")) { try { 301 if (bundle != null) { 302 String localeText = bundle.getString(name.substring(1)); 303 if (localeText != null) { 304 if (xmlEncode) { 305 node.setText(getXmlEncodedString(localeText)); 306 } else { 307 node.setText(localeText); 308 } 309 } 310 } 311 } catch (Exception e) { 312 Logger.warn("XmlHelper", Messages.getString("XmlHelper.WARN_MISSING_RESOURCE_PROPERTY", name.substring(1), bundlePath, locale.toString())); } 314 } 315 } 316 317 } catch (Exception e) { 318 Logger.error("XmlHelper", Messages.getErrorString("XmlHelper.ERROR_0012_COULD_NOT_READ_PROPERTIES", bundlePath), e); } 320 321 } 322 323 public static final File getlocalizedFile(String solutionPath, String fileName, String extension) { 324 325 Locale locale = LocaleHelper.getLocale(); 326 327 String language = locale.getLanguage(); 328 String country = locale.getCountry(); 329 String variant = locale.getVariant(); 330 331 String fullPath = PentahoSystem.getApplicationContext().getSolutionPath(solutionPath + File.separator + fileName + extension); 332 File file = new File (fullPath); 333 334 fileName = file.getName(); 335 int dotIndex = fileName.indexOf('.'); 336 String baseName = fileName.substring(0, dotIndex); 337 338 File directory = file.getParentFile(); 339 File localeFile = null; 340 if (!variant.equals("")) { localeFile = new File (directory, baseName + "_" + language + "_" + country + "_" + variant + extension); } 343 if (localeFile == null || !localeFile.exists()) { 344 localeFile = new File (directory, baseName + "_" + language + "_" + country + extension); } 346 if (!localeFile.exists()) { 347 localeFile = new File (directory, baseName + "_" + language + extension); } 349 if (!localeFile.exists()) { 350 localeFile = new File (directory, baseName + extension); 351 } 352 if (localeFile.exists()) { 353 return localeFile; 354 } 355 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0011_TRASFORM_XSL_DOES_NOT_EXIST", fullPath)); return null; 358 359 } 360 361 public static final File getlocalizedFile(File file) { 362 363 Locale locale = LocaleHelper.getLocale(); 364 365 String language = locale.getLanguage(); 366 String country = locale.getCountry(); 367 String variant = locale.getVariant(); 368 369 String fileName = file.getName(); 370 int dotIndex = fileName.indexOf('.'); 371 String baseName = fileName.substring(0, dotIndex); 372 String extension = fileName.substring(fileName.lastIndexOf('.')); 373 374 File directory = file.getParentFile(); 375 File localeFile = null; 376 if (!variant.equals("")) { localeFile = new File (directory, baseName + "_" + language + "_" + country + "_" + variant + extension); } 379 if (localeFile == null || !localeFile.exists()) { 380 localeFile = new File (directory, baseName + "_" + language + "_" + country + extension); } 382 if (!localeFile.exists()) { 383 localeFile = new File (directory, baseName + "_" + language + extension); } 385 if (!localeFile.exists()) { 386 localeFile = new File (directory, baseName + extension); 387 } 388 if (localeFile.exists()) { 389 return localeFile; 390 } 391 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0011_TRASFORM_XSL_DOES_NOT_EXIST", file.getAbsolutePath())); return null; 394 395 } 396 397 public static final File getlocalizedXsl(String path, String xslName) { 398 399 Locale locale = LocaleHelper.getLocale(); 400 401 String language = locale.getLanguage(); 402 String country = locale.getCountry(); 403 String variant = locale.getVariant(); 404 405 if (path == null) { 406 path = "system/custom/xsl"; } 408 409 String fullPath = PentahoSystem.getApplicationContext().getSolutionPath(path + File.separator + xslName).replace('\\', '/'); 410 File file = new File (fullPath); 411 if (!file.exists()) { 412 fullPath = PentahoSystem.getApplicationContext().getSolutionPath("system/custom/xsl/" + xslName).replace('\\', '/'); file = new File (fullPath); 414 if (!file.exists()) { 415 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0011_TRASFORM_XSL_DOES_NOT_EXIST", fullPath)); return null; 417 } 418 } 419 420 String fileName = file.getName(); 421 int dotIndex = fileName.indexOf('.'); 422 String baseName = fileName.substring(0, dotIndex); 423 String extension = ".xsl"; 425 File directory = file.getParentFile(); 426 File localeFile = null; 427 if (!variant.equals("")) { localeFile = new File (directory, baseName + "_" + language + "_" + country + "_" + variant + extension); } 430 if (localeFile == null || !localeFile.exists()) { 431 localeFile = new File (directory, baseName + "_" + language + "_" + country + extension); } 433 if (!localeFile.exists()) { 434 localeFile = new File (directory, baseName + "_" + language + extension); } 436 if (!localeFile.exists()) { 437 localeFile = new File (directory, baseName + extension); 438 } 439 if (localeFile.exists()) { 440 return localeFile; 441 } 442 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0011_TRASFORM_XSL_DOES_NOT_EXIST", fullPath)); return null; 445 446 } 447 448 public static final File getlocalizedXsl(String xslName) { 449 450 Locale locale = LocaleHelper.getLocale(); 451 452 String language = locale.getLanguage(); 453 String country = locale.getCountry(); 454 String variant = locale.getVariant(); 455 456 String fullPath = PentahoSystem.getApplicationContext().getSolutionPath("system/custom/xsl/" + xslName); File file = new File (fullPath); 458 459 String fileName = file.getName(); 460 int dotIndex = fileName.indexOf('.'); 461 String baseName = fileName.substring(0, dotIndex); 462 String extension = ".xsl"; 464 File directory = file.getParentFile(); 465 File localeFile = null; 466 if (!variant.equals("")) { localeFile = new File (directory, baseName + "_" + language + "_" + country + "_" + variant + extension); } 469 if (localeFile == null || !localeFile.exists()) { 470 localeFile = new File (directory, baseName + "_" + language + "_" + country + extension); } 472 if (!localeFile.exists()) { 473 localeFile = new File (directory, baseName + "_" + language + extension); } 475 if (!localeFile.exists()) { 476 localeFile = new File (directory, baseName + extension); 477 } 478 if (localeFile.exists()) { 479 return localeFile; 480 } 481 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0011_TRASFORM_XSL_DOES_NOT_EXIST", fullPath)); return null; 484 485 } 486 487 public static Document getDomFromString(String str) { 488 Document document = null; 489 try { 490 document = DocumentHelper.parseText(str); 491 } catch (Exception e) { 492 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0008_GET_DOM_FROM_STRING_ERROR", e.getMessage()), e); } 494 return document; 495 } 496 497 public static void saveDomToFile(Document doc, File file) { 498 String xml = doc.asXML(); 499 try { 500 FileWriter fos = new FileWriter (file); 501 try { 502 fos.write(xml); 503 } finally { 504 try { 505 fos.close(); 506 } catch (Exception ex) { 507 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0009_SAVE_DOMoTO_FILE_ERROR", ex.getMessage()), ex); } 509 } 510 } catch (Exception e) { 511 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0009_SAVE_DOMoTO_FILE_ERROR", e.getMessage()), e); } 513 } 514 515 public static void saveDomToFile(Document doc, String fullPath) { 516 String xml = doc.asXML(); 517 File file = new File (fullPath); 518 try { 519 FileWriter fos = new FileWriter (file); 520 try { 521 fos.write(xml); 522 } finally { 523 try { 524 fos.close(); 525 } catch (Exception ex) { 526 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0009_SAVE_DOMoTO_FILE_ERROR", ex.getMessage()), ex); } 528 } 529 } catch (Exception e) { 530 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0009_SAVE_DOMoTO_FILE_ERROR", e.getMessage()), e); } 532 } 533 534 public static Document getDomFromResource(String path) { 535 String str = getContentFromSolutionResource(path); 536 return getDomFromString(str); 537 } 538 539 public static String getContentFromResource(String fullPath) { 540 541 StringBuffer strBuf = new StringBuffer (); 542 try { 543 FileInputStream fin = new FileInputStream (fullPath); 544 InputStreamReader reader = new InputStreamReader (fin, LocaleHelper.getSystemEncoding()); 545 try { 546 if (reader != null) { 548 boolean reading = true; 549 int bytesRead; 550 char buffer[] = new char[1000]; 551 while (reading) { 552 bytesRead = reader.read(buffer); 553 reading = bytesRead == buffer.length; 554 strBuf.append(buffer, 0, bytesRead); 555 } 556 } 557 } finally { 558 try { 559 reader.close(); 560 } catch (Exception ex) { 561 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0010_GET_CONTENT_FROM_RESOURCE_ERROR", ex.getMessage()), ex); } 563 } 564 } catch (Exception e) { 565 Logger.error(XmlHelper.class.getName(), Messages.getErrorString("XmlHelper.ERROR_0010_GET_CONTENT_FROM_RESOURCE_ERROR", e.getMessage()), e); 567 } 568 return strBuf.toString(); 569 } 570 571 public static String getContentFromSolutionResource(String path) { 572 String fullPath = PentahoSystem.getApplicationContext().getSolutionPath(path); 573 return getContentFromResource(fullPath); 574 } 575 576 579 580 public static String listToXML(List l) throws UnsupportedOperationException { 581 return listToXML(l, ""); } 583 584 public static String listToXML(List l, String indent) throws UnsupportedOperationException { 585 StringBuffer sb = new StringBuffer (); 586 sb.append(indent).append("<list>\r"); String newIndent = indent + " "; Object obj; 589 for (int i = 0; i < l.size(); i++) { 590 obj = l.get(i); 591 sb.append(newIndent).append("<list-element>\r"); objToXML(obj, sb, newIndent); 593 sb.append(newIndent).append("</list-element>\r"); } 595 sb.append(indent).append("</list>\r"); return sb.toString(); 597 } 598 599 public static String mapToXML(Map m) throws UnsupportedOperationException { 600 return mapToXML(m, ""); } 602 603 public static String mapToXML(Map mp, String indent) throws UnsupportedOperationException { 604 StringBuffer sb = new StringBuffer (); 605 sb.append(indent).append("<map>\r"); String newIndent = indent + " "; Iterator it = mp.entrySet().iterator(); 608 Map.Entry ent; 609 Object obj; 610 while (it.hasNext()) { 611 ent = (Map.Entry ) it.next(); 612 if (!(ent.getKey() instanceof String )) { 613 throw new UnsupportedOperationException (Messages.getErrorString("XMLUTL.ERROR_0011_MAP_KEYS")); } 615 sb.append(newIndent).append("<map-entry>\r"); sb.append(newIndent).append("<key>\r"); sb.append(newIndent).append("<![CDATA[").append(ent.getKey().toString()).append("]]>\r"); sb.append(newIndent).append("</key>\r"); obj = ent.getValue(); 620 objToXML(obj, sb, newIndent); 621 sb.append(newIndent).append("</map-entry>\r"); } 623 sb.append(indent).append("</map>\r"); return sb.toString(); 625 } 626 627 private static void objToXML(Object obj, StringBuffer sb, String newIndent) { 628 if (obj instanceof String ) { 629 sb.append(newIndent).append("<string-value>\r"); sb.append(newIndent).append("<![CDATA[").append((String ) obj).append("]]>\r"); sb.append(newIndent).append("</string-value>\r"); } else if (obj instanceof StringBuffer ) { 633 sb.append(newIndent).append("<stringbuffer-value>\r"); sb.append(newIndent).append("<![CDATA[").append(obj.toString()).append("]]>\r"); sb.append(newIndent).append("</stringbuffer-value>\r"); } else if (obj instanceof BigDecimal ) { 637 sb.append(newIndent).append("<bigdecimal-value>").append(obj.toString()).append("</bigdecimal-value>\r"); } else if (obj instanceof Date ) { 639 SimpleDateFormat fmt = new SimpleDateFormat (); 640 fmt.setTimeZone(TimeZone.getTimeZone("GMT")); sb.append(newIndent).append("<date-value>"); sb.append(fmt.format((Date ) obj)).append("</date-value>\r"); } else if (obj instanceof Long ) { 644 sb.append(newIndent).append("<long-value>"); sb.append(obj.toString()).append("</long-value>\r"); } else if (obj instanceof Map ) { 647 sb.append(newIndent).append("<map-value>\r"); sb.append(mapToXML((Map ) obj, newIndent)); 649 sb.append(newIndent).append("</map-value>\r"); } else if (obj instanceof List ) { 651 sb.append(newIndent).append("<list-value>\r"); sb.append(listToXML((List ) obj, newIndent)); 653 sb.append(newIndent).append("</list-value>\r"); } else { 655 throw new UnsupportedOperationException (Messages.getErrorString("XMLUTL.ERROR_0012_DATA_TYPE", obj.getClass().getName())); } 657 658 } 659 660 public static List xmlToList(String s) throws PentahoChainedException { 661 try { 662 Document doc = DocumentHelper.parseText(s); 663 Element root = doc.getRootElement(); 664 return processList(root); 665 } catch (DocumentException ex) { 666 throw new PentahoChainedException(Messages.getErrorString("XMLUTL.ERROR_0013_PARSING"), ex); } catch (ParseException pex) { 668 throw new PentahoChainedException(Messages.getErrorString("XMLUTL.ERROR_0013_PARSING"), pex); } 670 } 671 672 public static Map xmlToMap(String s) throws PentahoChainedException { 673 try { 674 Document doc = DocumentHelper.parseText(s); 675 Element root = doc.getRootElement(); 676 return processMap(root); 677 } catch (DocumentException ex) { 678 throw new PentahoChainedException(Messages.getErrorString("XMLUTL.ERROR_0013_PARSING"), ex); } catch (ParseException pex) { 680 throw new PentahoChainedException(Messages.getErrorString("XMLUTL.ERROR_0013_PARSING"), pex); } 682 } 683 684 public static List processList(Element ele) throws ParseException { 685 List rtn = new ArrayList (); 686 Iterator it = ele.elementIterator(); 687 Element anElement; 688 while (it.hasNext()) { 689 anElement = (Element) it.next(); 690 processListElement(anElement, rtn); 691 } 692 return rtn; 693 } 694 695 public static Map processMap(Element ele) throws ParseException { 696 Map rtn = new HashMap (); 697 Iterator it = ele.elementIterator(); 698 Element anElement; 699 while (it.hasNext()) { 700 anElement = (Element) it.next(); 701 processMapElement(anElement, rtn); 702 } 703 return rtn; 704 } 705 706 public static Object elementToObject(Element anElement) throws ParseException { 707 if (anElement.getName().equals("string-value")) { return anElement.getTextTrim(); 709 } else if (anElement.getName().equals("stringbuffer-value")) { return new StringBuffer (anElement.getTextTrim()); 711 } else if (anElement.getName().equals("bigdecimal-value")) { return new BigDecimal (anElement.getTextTrim()); 713 } else if (anElement.getName().equals("long-value")) { return new Long (anElement.getTextTrim()); 715 } else if (anElement.getName().equals("date-value")) { SimpleDateFormat fmt = new SimpleDateFormat (); 717 fmt.setTimeZone(TimeZone.getTimeZone("GMT")); return fmt.parse(anElement.getTextTrim()); 719 } else if (anElement.getName().equals("list-value")) { return processList(anElement.element("list")); } else if (anElement.getName().equals("map-value")) { return processMap(anElement.element("map")); } else { 724 throw new ParseException (Messages.getErrorString("XMLUTL.ERROR_0014_UNKNOWN_TYPE", anElement.getName()), 0); } 726 } 727 728 public static void processListElement(Element ele, List rtn) throws ParseException { 729 Iterator it = ele.elementIterator(); 730 Element anElement; 731 while (it.hasNext()) { 732 anElement = (Element) it.next(); 733 Object anObj = elementToObject(anElement); 734 rtn.add(anObj); 735 } 736 } 737 738 public static void processMapElement(Element ele, Map rtn) throws ParseException { 739 Iterator it = ele.elementIterator(); 740 Element keyElement = null; 741 Element valueElement = null; 742 Element anElement = null; 743 while (it.hasNext()) { 744 anElement = (Element) it.next(); 745 if (anElement.getName().equals("key")) { keyElement = anElement; 747 } else { 748 valueElement = anElement; 749 } 750 } 751 Object anObj = elementToObject(valueElement); 754 rtn.put(keyElement.getTextTrim(), anObj); 755 } 756 757 public static String getNodeText(String path, Node rootNode) { 758 return (getNodeText(path, rootNode, null)); 759 } 760 761 public static long getNodeText(String path, Node rootNode, long defaultValue) { 762 String valueStr = getNodeText(path, rootNode, Long.toString(defaultValue)); 763 try { 764 return Long.parseLong(valueStr); 765 } catch (Exception e) { 766 } 767 return defaultValue; 768 } 769 770 public static double getNodeText(String path, Node rootNode, double defaultValue) { 771 String valueStr = getNodeText(path, rootNode, null); 772 if ( valueStr == null ) { 773 return defaultValue; 774 } 775 try { 776 return Double.parseDouble(valueStr); 777 } catch (Exception e) { 778 } 779 return defaultValue; 780 } 781 782 public static String getNodeText(String path, Node rootNode, String defaultValue) { 783 if ( rootNode == null ) { 784 return( defaultValue ); 785 } 786 Node node = rootNode.selectSingleNode(path); 787 if (node == null) { 788 return defaultValue; 789 } 790 return node.getText(); 791 } 792 793 public static void decode(String [] strings ) { 794 if ( strings != null ) { 795 for ( int i = 0; i < strings.length; ++i ) { 796 strings[i] = decode( strings[i] ); 797 } 798 } 799 } 800 801 public static String decode(String string) { 802 if (string == null) { 804 return null; 805 } 806 string = string.replaceAll("<", "<"); string = string.replaceAll(">", ">"); string = string.replaceAll("&", "&"); return string; 810 } 811 812 public static void encode(String [] strings ) { 813 if ( strings != null ) { 814 for ( int i = 0; i < strings.length; ++i ) { 815 strings[i] = encode( strings[i] ); 816 } 817 } 818 } 819 820 public static String encode(String string) { 821 if (string == null) { 823 return null; 824 } 825 string = string.replaceAll("<", "<"); string = string.replaceAll(">", ">"); string = string.replaceAll("&", "&"); return string; 829 } 830 831 } 832 | Popular Tags |