PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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