Commits

squire_linden  committed 2a3df8b

Moving WIP to desktop

  • Participants
  • Parent commits 1593a19

Comments (0)

Files changed (5)

File scala/src/main/scala/com/lindenlab/llsd/LLSD.scala

   extends Exception("Cannot convert "+sourceType + "to" + targetType)
 
 trait LLSD {
-  def toXml : Elem = <llsd>{ this.xmlValue }</llsd>
   def convertError(targetType: String) : Nothing = { throw new ConversionException(getClass.getName(),targetType) }
-  def xmlValue : Elem
   def booleanValue: Boolean = convertError("Boolean")
   def intValue : Int = convertError("Int")
   def realValue : Double = convertError("Double")
   def stringValue : String = convertError("String")
   def UUIDValue : UUID = convertError("UUID")
   def dateValue : Date = convertError("Date")
-  def binaryValue : Array[Short] = convertError("Binary") // Short as we don't have unsigned Bytes
+  def binaryValue : Array[Byte] = convertError("Binary") // Short as we don't have unsigned Bytes
 }
 
-case class UndefLLSD() extends LLSD {
-  override def xmlValue = <undef/>
-}
+case class UndefLLSD() extends LLSD
 
 case class BooleanLLSD(value: Boolean) extends LLSD {
-  override def xmlValue = <boolean>{if (value) "true" else "false"}</boolean>
   override def booleanValue = value
   override def intValue = if (value) 1 else 0
   override def realValue = intValue
   override def stringValue = if (value) "true" else "false"
-  override def binaryValue = Array(intValue toShort)
-
+  override def binaryValue = Array(intValue toByte)
 }
 case class IntLLSD(value: Int) extends LLSD {
-  override def xmlValue = <int>{value toString}</int>
   override def booleanValue = value != 0
   override def intValue = value
   override def realValue = value
   override def stringValue = value toString
-
 }
 
 case class RealLLSD(value: Double) extends LLSD {
   // TODO - this needs to be replaced with spec compliant formatting
-  override def xmlValue = <real>{value toString}</real>
   override def booleanValue = value != 0.0
   override def intValue = value toInt
   override def realValue = value
 
 case class StringLLSD(value: String) extends LLSD {
   // TODO - needs protection for non-XML compliant characters, UTF-8 encoding etc.
-  override def xmlValue = <string>{Text(value)}</string>
   override def booleanValue: Boolean = value != ""
   override def intValue : Int = value toInt
   override def realValue : Double = value toDouble
   override def stringValue : String = value
   override def UUIDValue : UUID = UUID(value)
 }
-
-case class UUIDLLSD(value:UUID) extends LLSD {
-  override def xmlValue = <uuid>{value.toString}</uuid>
-}
+case class UUIDLLSD(value:UUID) extends LLSD
+case class DateLLSD(value:Date) extends LLSD
+case class BinaryLLSD(value: Array[Byte]) extends LLSD
+case class MapLLSD(value: Map[String, LLSD]) extends LLSD
+case class ArrayLLSD(value: List[LLSD]) extends LLSD
 
 object LLSD {
   implicit def boolean2BooleanLLSD(value: Boolean) : BooleanLLSD = BooleanLLSD(value)
   implicit def int2IntLLSD(value: Int) : IntLLSD = IntLLSD(value)
   implicit def double2RealLLSD(value: Double) : RealLLSD = RealLLSD(value)
   implicit def string2StringLLSD(value: String) : StringLLSD = StringLLSD(value)
+  implicit def UUID2UUIDLLSD(value: UUID) : UUIDLLSD = UUIDLLSD(value)
+  implicit def date2DateLLSD(value: Date) : DateLLSD = DateLLSD(value)
 
   def apply() : LLSD = UndefLLSD()
   def apply(value: Boolean) : LLSD = BooleanLLSD(value)
   def apply(value: Int) : LLSD = IntLLSD(value)
   def apply(value: Double) : LLSD = RealLLSD(value)
   def apply(value: String) : LLSD = StringLLSD(value)
-
-  def parseXMLValue(xmlValue: Elem) : LLSD = {
-    xmlValue match {
-      case <undef/> => new UndefLLSD()
-      case <boolean/> => new BooleanLLSD(false)
-      case <boolean>{booleanNode: Text}</boolean> => booleanNode.data match {
-        case "1" => true
-        case "0" => false
-        case "true" => true
-        case "false" => false
-      }
-      case <int>{intNode: Text}</int> => new IntLLSD(intNode.data toInt)
-      case <real>{realNode: Text}</real> => new RealLLSD(realNode.data toDouble)
-
-    }
-
-  }
-  def apply(xml: Elem) : LLSD = {
-    xml match {
-      case <llsd>{ xmlValue: Elem }</llsd> => parseXMLValue(xmlValue)
-      case _ => throw new IllegalArgumentException("Unrecognized LLSD xml enacoding " + xml.toString)
-    }
-  }
+  def apply(value: UUID) : LLSD = UUIDLLSD(value)
+  def apply(value: Date) : LLSD = DateLLSD(value)
 }

File scala/src/main/scala/com/lindenlab/llsd/XmlSerializer.scala

+package com.lindenlab.llsd
+import xml.{Node, Text, Elem}
+import com.lindenlab.UUID
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: nick
+ * Date: 11/22/11
+ * Time: 10:19 AM
+ * Serializes LLSD objects to and from XML
+ */
+
+object XmlSerializer {
+
+  private def xmlValueOf(llsd : LLSD): Elem = {
+    llsd match {
+      case UndefLLSD() => <undef/>
+      case BooleanLLSD(value) => <boolean>{if (value) "true" else "false"}</boolean>
+      case IntLLSD(value) => <int>{value toString}</int>
+      case RealLLSD(value) => <real>{value toString}</real>
+      case StringLLSD(value) => <real>{Text(value)}</real>
+      case UUIDLLSD(value) => <uuid>{value toString}</uuid>
+      case DateLLSD(value) => <date>{value toString}</date>
+      case ArrayLLSD(list) => <array>{for (item <- list) yield xmlValueOf(item)}</array>
+      case MapLLSD(mapValue) => <map>{for ((k,v) <- mapValue) yield List(<key>{k}</key>, xmlValueOf(v))}</map>
+    }
+  }
+
+  def toXml(llsd: LLSD ) : Elem = <llsd>{xmlValueOf(llsd)}</llsd>
+
+  private def valueFromXml(value: Elem) : LLSD = {
+    value match {
+      case <undef/> => LLSD()
+      case <boolean/> => new BooleanLLSD(false)
+      case <boolean>{booleanNode: Text}</boolean> => booleanNode.data match {
+        case "1" => true
+        case "0" => false
+        case "true" => true
+        case "false" => false
+      }
+      case <int>{intNode: Text}</int> => new IntLLSD(intNode.data toInt)
+      case <real>{realNode: Text}</real> => new RealLLSD(realNode.data toDouble)
+      case <string>{stringNode: Text}</string> => stringNode.data
+      case <uuid>{stringNode: Text}</uuid> => UUID(stringNode.data)
+      case <map>{<key>{key:Text}</key>,value:Elem,rest}</map> =>
+    }
+  }
+
+  def fromXml(document: Elem) : LLSD = {
+    document match {
+      case <llsd>{contents @ _*}</llsd> => valueFromXml(contents)
+      case _ => throw new IllegalArgumentException("Unrecognized LLSD xml element" + document.toString)
+    }
+  }
+}

File scala/src/test/scala/com/lindenlab/llsd/LLSD_test.scala

+package com.lindenlab.llsd
+
+import org.scalatest.FunSuite
+import scala.math.Pi
+import scala.math.E
+
+class LLSD_test extends FunSuite {
+ 
+  test("LLSD undef") {
+    val llsd : LLSD = LLSD()
+    assert (llsd.isInstanceOf[UndefLLSD])
+
+    // All other conversions are undefined
+    intercept[ConversionException] { llsd.booleanValue }
+    intercept[ConversionException] { llsd.intValue }
+    intercept[ConversionException] { llsd.realValue }
+    intercept[ConversionException] { llsd.stringValue }
+    intercept[ConversionException] { llsd.UUIDValue }
+    intercept[ConversionException] { llsd.dateValue }
+    intercept[ConversionException] { llsd.binaryValue }
+  }
+
+  test("LLSD boolean") {
+    val llsdTrue : LLSD = true
+    val llsdFalse = LLSD(false)
+    assert (llsdTrue.isInstanceOf[BooleanLLSD])
+    assert (llsdFalse.isInstanceOf[BooleanLLSD])
+    assert (llsdTrue.intValue == 1 )
+    assert (llsdFalse.intValue == 0 )
+    assert (llsdTrue.realValue == 1.0 )
+    assert (llsdFalse.realValue == 0.0 )
+    assert (llsdTrue.stringValue == "true" )
+    assert (llsdFalse.stringValue == "false" )
+  }
+
+  test("LLSD integer") {
+    val llsdThree : LLSD = 3
+    val llsdMinus3145 = LLSD(-3145)
+    assert (llsdThree.isInstanceOf[IntLLSD])
+    assert (llsdMinus3145.isInstanceOf[IntLLSD])
+    assert (llsdThree.intValue == 3 )
+    assert (llsdMinus3145.intValue == -3145 )
+    assert (llsdThree.realValue == 3.0 )
+    assert (llsdMinus3145.realValue == -3145.0 )
+    assert (llsdThree.stringValue == "3" )
+    assert (llsdMinus3145.stringValue == "-3145" )
+  }
+
+  test("LLSD Real") {
+    val llsdPi : LLSD = Pi
+    val llsdMinusE = LLSD(- E)
+    assert (llsdPi.isInstanceOf[RealLLSD])
+    assert (llsdMinusE.isInstanceOf[RealLLSD])
+    assert (llsdPi.intValue == 3 )
+    assert (llsdMinusE.intValue == -2 )
+    assert (llsdPi.realValue == 3.141592653589793 )
+    assert (llsdMinusE.realValue == -2.718281828459045 )
+    assert (llsdPi.stringValue == "3.141592653589793" )
+    assert (llsdMinusE.stringValue == "-2.718281828459045" )
+  }
+
+  test("LLSD String") {
+    val llsdFox : LLSD = "The quick brown fox jumps over the lazy dog"
+    val llsdKana = LLSD("ニク ツイマん")
+    assert (llsdFox.isInstanceOf[StringLLSD])
+    assert (llsdKana.isInstanceOf[StringLLSD])
+//    assert (llsdPi.xmlValue == <real>3.141592653589793</real> )
+//    assert (llsdMinusE.xmlValue == <real>-2.718281828459045</real> )
+//    assert (llsdPi.intValue == 3 )
+//    assert (llsdMinusE.intValue == -2 )
+//    assert (llsdPi.realValue == 3.141592653589793 )
+//    assert (llsdMinusE.realValue == -2.718281828459045 )
+//    assert (llsdPi.stringValue == "3.141592653589793" )
+//    assert (llsdMinusE.stringValue == "-2.718281828459045" )
+  }
+
+}

File scala/src/test/scala/com/lindenlab/llsd/XmlSerializer_test.scala

+package com.lindenlab.llsd
+import org.scalatest.FunSuite
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: nick
+ * Date: 11/22/11
+ * Time: 2:01 PM
+ * To change this template use File | Settings | File Templates.
+ */
+
+class XmlSerializer_test extends FunSuite {
+
+  test("XML - undef") {
+    val undef = LLSD()
+    assert(XmlSerializer.toXml(undef) == <llsd><undef/></llsd>)
+
+    val incoming1 = XmlSerializer.fromXml(<llsd><undef/></llsd>)
+    assert(incoming1.isInstanceOf[UndefLLSD])
+    val incoming2 = XmlSerializer.fromXml(<llsd><undef></undef></llsd>)
+    assert(incoming2.isInstanceOf[UndefLLSD])
+  }
+
+  test("XML - boolean") {
+    val bTrue = LLSD(true)
+    val bFalse = LLSD(false)
+    assert(XmlSerializer.toXml(bTrue) == <llsd><boolean>true</boolean></llsd>)
+    assert(XmlSerializer.toXml(bFalse) == <llsd><boolean>false</boolean></llsd>)
+
+    val true1 = XmlSerializer.fromXml(<llsd><boolean>true</boolean></llsd>)
+    val true2 = XmlSerializer.fromXml(<llsd><boolean>1</boolean></llsd>)
+    val false1 = XmlSerializer.fromXml(<llsd><boolean>false</boolean></llsd>)
+    val false2 = XmlSerializer.fromXml(<llsd><boolean>0</boolean></llsd>)
+    val false3= XmlSerializer.fromXml(<llsd><boolean></boolean></llsd>)
+    val false4= XmlSerializer.fromXml(<llsd><boolean/></llsd>)
+    assert(true1.isInstanceOf[BooleanLLSD])
+    assert(true2.isInstanceOf[BooleanLLSD])
+    assert(false1.isInstanceOf[BooleanLLSD])
+    assert(false2.isInstanceOf[BooleanLLSD])
+    assert(false3.isInstanceOf[BooleanLLSD])
+    assert(false4.isInstanceOf[BooleanLLSD])
+    assert(true1.booleanValue)
+    assert(true2.booleanValue)
+    assert(!false1.booleanValue)
+    assert(!false2.booleanValue)
+    assert(!false3.booleanValue)
+  }
+
+  test("XML - int") {
+    val iZero = XmlSerializer.fromXml(<llsd><int>0</int></llsd>)
+    assert(iZero.isInstanceOf[IntLLSD])
+    assert(iZero.intValue == 0)
+    val iPositive = XmlSerializer.fromXml(<llsd><int>31415926</int></llsd>)
+    assert(iPositive.isInstanceOf[IntLLSD])
+    assert(iPositive.intValue == 31415926)
+    val iNegative = XmlSerializer.fromXml(<llsd><int>-98349234</int></llsd>)
+    assert(iNegative.isInstanceOf[IntLLSD])
+    assert(iNegative.intValue == -98349234)
+    assert(XmlSerializer.toXml(iZero) == <llsd><int>0</int></llsd>)
+    assert(XmlSerializer.toXml(iPositive) == <llsd><int>31415926</int></llsd>)
+    assert(XmlSerializer.toXml(iNegative) == <llsd><int>-98349234</int></llsd>)
+  }
+
+
+  test("XML - map") {
+    val mTest = XmlSerializer.fromXml(<llsd><map>
+      <key>kUndef</key><undef/>
+      <key>kBoolean</key><boolean>true</boolean>
+      <key>kInt</key>
+      <int>1234567</int>
+      </map>
+      </llsd>)
+    assert(mTest.isInstanceOf[MapLLSD])
+  }
+}

File scala/src/test/scala/llsd_test.scala

-package com.lindenlab.llsd
-
-import org.scalatest.FunSuite
-import scala.math.Pi
-import scala.math.E
-
-class LLSDSuite extends FunSuite {
- 
-  test("LLSD undef") {
-    val llsd : LLSD = LLSD()
-    assert (llsd.isInstanceOf[UndefLLSD])
-    assert (llsd.xmlValue == <undef/> )
-
-    // All other conversions are undefined
-    intercept[ConversionException] { llsd.booleanValue }
-    intercept[ConversionException] { llsd.intValue }
-    intercept[ConversionException] { llsd.realValue }
-    intercept[ConversionException] { llsd.stringValue }
-    intercept[ConversionException] { llsd.UUIDValue }
-    intercept[ConversionException] { llsd.dateValue }
-    intercept[ConversionException] { llsd.binaryValue }
-  }
-
-  test("LLSD boolean") {
-    val llsdTrue : LLSD = true
-    val llsdFalse = LLSD(false)
-    assert (llsdTrue.isInstanceOf[BooleanLLSD])
-    assert (llsdFalse.isInstanceOf[BooleanLLSD])
-    assert (llsdTrue.xmlValue == <boolean>true</boolean> )
-    assert (llsdFalse.xmlValue == <boolean>false</boolean> )
-    assert (llsdTrue.intValue == 1 )
-    assert (llsdFalse.intValue == 0 )
-    assert (llsdTrue.realValue == 1.0 )
-    assert (llsdFalse.realValue == 0.0 )
-    assert (llsdTrue.stringValue == "true" )
-    assert (llsdFalse.stringValue == "false" )
-  }
-
-  test("LLSD integer") {
-    val llsdThree : LLSD = 3
-    val llsdMinus3145 = LLSD(-3145)
-    assert (llsdThree.isInstanceOf[IntLLSD])
-    assert (llsdMinus3145.isInstanceOf[IntLLSD])
-    assert (llsdThree.xmlValue == <int>3</int> )
-    assert (llsdMinus3145.xmlValue == <int>-3145</int> )
-    assert (llsdThree.intValue == 3 )
-    assert (llsdMinus3145.intValue == -3145 )
-    assert (llsdThree.realValue == 3.0 )
-    assert (llsdMinus3145.realValue == -3145.0 )
-    assert (llsdThree.stringValue == "3" )
-    assert (llsdMinus3145.stringValue == "-3145" )
-  }
-
-  test("LLSD Real") {
-    val llsdPi : LLSD = Pi
-    val llsdMinusE = LLSD(- E)
-    assert (llsdPi.isInstanceOf[RealLLSD])
-    assert (llsdMinusE.isInstanceOf[RealLLSD])
-    assert (llsdPi.xmlValue == <real>3.141592653589793</real> )
-    assert (llsdMinusE.xmlValue == <real>-2.718281828459045</real> )
-    assert (llsdPi.intValue == 3 )
-    assert (llsdMinusE.intValue == -2 )
-    assert (llsdPi.realValue == 3.141592653589793 )
-    assert (llsdMinusE.realValue == -2.718281828459045 )
-    assert (llsdPi.stringValue == "3.141592653589793" )
-    assert (llsdMinusE.stringValue == "-2.718281828459045" )
-  }
-
-  test("LLSD String") {
-    val llsdFox : LLSD = "The quick brown fox jumps over the lazy dog"
-    val llsdKana = LLSD("ニク ツイマん")
-    assert (llsdFox.isInstanceOf[StringLLSD])
-    assert (llsdKana.isInstanceOf[StringLLSD])
-//    assert (llsdPi.xmlValue == <real>3.141592653589793</real> )
-//    assert (llsdMinusE.xmlValue == <real>-2.718281828459045</real> )
-//    assert (llsdPi.intValue == 3 )
-//    assert (llsdMinusE.intValue == -2 )
-//    assert (llsdPi.realValue == 3.141592653589793 )
-//    assert (llsdMinusE.realValue == -2.718281828459045 )
-//    assert (llsdPi.stringValue == "3.141592653589793" )
-//    assert (llsdMinusE.stringValue == "-2.718281828459045" )
-  }
-
-}