/razpub/src/com/razie/pub/base/data/RazElement.scala

http://razpub.googlecode.com/ · Scala · 156 lines · 61 code · 36 blank · 59 comment · 4 complexity · 523ffdebd473d2a5651aa5970fddbf45 MD5 · raw file

  1. /**
  2. * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
  3. * details. No warranty implied nor any liability assumed for this code.
  4. */
  5. package com.razie.pub.base.data
  6. import org.w3c.dom._
  7. // TODO need to optimize these - i convert to RazE stuff all the time, no caching - should use a map for caching? is that useful?
  8. /** xml conversions.
  9. *
  10. * This stuff here is more from my learning scala days, but i got used to the shortcuts...
  11. */
  12. object RazElement {
  13. implicit def toraz (e:org.w3c.dom.Element) : RazElement = new RazElementJava (e)
  14. implicit def torazdoc (e:XmlDoc) : RazElement = new RazElementJava (e e)
  15. implicit def torazse (e:scala.xml.Elem) : RazElement = new RazElementScala (e)
  16. }
  17. /** simplify xpath access to dom. conversions in RJX */
  18. trait RazElement {
  19. /** get attribute value */
  20. def a (name:String) : String
  21. /** has attribute populated? */
  22. def ha (name:String) : Boolean
  23. /**
  24. * get attribute by path
  25. *
  26. * i.e. "/config/mutant/@someattribute"
  27. *
  28. * @param path identifies the xpath
  29. * @return never null
  30. */
  31. def xpa (path:String) : String
  32. /** get child element by path */
  33. def xpe (path:String) : RazElement
  34. /** get child elements by path */
  35. def xpl (path:String) : List[RazElement]
  36. /** get tag's name */
  37. def name : String
  38. /** return the list of children */
  39. def children : List[RazElement]
  40. /** node's value - if any, for CDATA or alike */
  41. def nodeVal : String
  42. }
  43. /** simplify xpath access to dom. conversions in RJX */
  44. class RazElementJava (val e:org.w3c.dom.Element) extends RazElement {
  45. override def a (name:String) : String = e.getAttribute (name)
  46. override def ha (name:String) = e.hasAttribute (name)
  47. /**
  48. * i.e. "/config/mutant/@someattribute"
  49. *
  50. * @param path identifies the xpath
  51. * @name identifies the name attribute of the element - could also be part of xpath instead
  52. * @return never null
  53. */
  54. def xpa (path:String) : String = XmlDoc.getAttr (e, path)
  55. // TODO 3-2 optimize...not sure how...
  56. def xpe (path:String) : RazElement = new RazElementJava(XmlDoc.getEntity (e, path))
  57. /**
  58. * get a specific element, by "name"
  59. *
  60. * @param path identifies the xpath
  61. * @name identifies the name attribute of the element - could also be part of xpath instead
  62. * @return never null
  63. */
  64. // public static Element getEntity(Element e, String path) {
  65. // return (Element) RiXmlUtils.getNode(e, path, null);
  66. // }
  67. def xpl (path:String) : List[RazElement] = {
  68. // val l = XmlDoc.listEntities (e, path)
  69. // val lre : scala.collection.mutable.Buffer[Element] = scala.collection.JavaConversions.asBuffer(l)
  70. for (val x <- razie.RJS list XmlDoc.listEntities (e, path))
  71. yield new RazElementJava(x)
  72. }
  73. def name : String = e.getNodeName
  74. def children : List[RazElement] = {
  75. val children = e.getChildNodes()
  76. val k = for {
  77. i <- 0 to children.getLength()-1
  78. x = children.item(i)
  79. if x.isInstanceOf[RazElement]
  80. }
  81. yield x.asInstanceOf[RazElement]
  82. // TODO i don't like this syntax
  83. k.toList
  84. }
  85. def nodeVal : String = RiXmlUtils.getOptNodeVal (e)
  86. }
  87. /** simplify xpath access to dom . conversions in RJX */
  88. class RazElementScala (val e:scala.xml.Elem) extends RazElement {
  89. def a (name:String) : String =
  90. (e \ ((if (name.startsWith("@")) "" else "@")+name)).text
  91. def ha (name:String) = e attribute name match {
  92. case None => false
  93. case _ => true
  94. }
  95. /** TODO implement full xpath - for now use XP instead */
  96. def xpa (path:String) : String = this a path
  97. def xpe (path:String) : RazElement =
  98. xpl (path).head
  99. /** TODO implement full xpath - for now use XP instead */
  100. def xpl (path:String) : List[RazElement] =
  101. e.elements.filter(_.label==(path.replaceFirst("/",""))).map(x =>
  102. new RazElementScala(x.asInstanceOf[scala.xml.Elem])).toList
  103. def name : String = e label
  104. // TODO optimize
  105. def children : List[RazElement] =
  106. e.child.filter(_.isInstanceOf[scala.xml.Elem]).map(x =>
  107. new RazElementScala(x.asInstanceOf[scala.xml.Elem])).toList
  108. // TODO implement properly - i don't know if text covers CDATA as well
  109. def nodeVal : String = {
  110. val ret:String = e.text
  111. // if (e != null) {
  112. // for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
  113. // if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
  114. // ret = ret == null ? child.getNodeValue() : ret + child.getNodeValue();
  115. // }
  116. // }
  117. // }
  118. return ret;
  119. }
  120. }