Author: reto
Date: Wed Jun 22 20:34:20 2011
New Revision: 1138621

URL: http://svn.apache.org/viewvc?rev=1138621&view=rev
Log:
CLEREZZA-577: Added implicit conversions to Preamble, only touched EasyGraph to 
extract NameSpace-class to its own file

Added:
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
Modified:
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala

Modified: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala?rev=1138621&r1=1138620&r2=1138621&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
 Wed Jun 22 20:34:20 2011
@@ -306,19 +306,3 @@ class EasyGraphNode(val ref: NonLiteral,
 
 }
 
-/**
- * A IRI-namespace prefix
- */
-//Or should this be called EasyNameSpace ?
-class NameSpace(prefix: String) {
-
-       /**
-        * returns a UriRef applying this namespace prefix to the given symbol
-        */
-       def +(s: Symbol) = new UriRef(prefix + s.name)
-
-       /**
-        * returns a UriRef applying this prefix to the given string
-        */
-       def +(s: String) = new UriRef(prefix + s)
-}

Added: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala?rev=1138621&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
 (added)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
 Wed Jun 22 20:34:20 2011
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.rdf.scala.utils
+
+import org.apache.clerezza.rdf.core.UriRef
+
+/**
+ * A IRI-namespace prefix
+ */
+class NameSpace(prefix: String) {
+
+       /**
+        * returns a UriRef applying this namespace prefix to the given symbol
+        */
+       def +(s: Symbol) = new UriRef(prefix + s.name)
+
+       /**
+        * returns a UriRef applying this prefix to the given string
+        */
+       def +(s: String) = new UriRef(prefix + s)
+}

Modified: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala?rev=1138621&r1=1138620&r2=1138621&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
 Wed Jun 22 20:34:20 2011
@@ -18,24 +18,49 @@
  */
 package org.apache.clerezza.rdf.scala.utils
 
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl
+import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl
+import org.apache.clerezza.rdf.ontologies.XSD
 import org.apache.clerezza.rdf.utils.GraphNode
+import java.math.BigInteger
+import java.net.URI
+import java.net.URL
+import java.util.Date
 import org.apache.clerezza.rdf.core._
 
 /**
 * This object provides the implicit conversions. Typically this is used by
 * adding
-* <code>import org.apache.clerezza.rdf.scala.utils.Preamble._</code> near the 
top of the
+* {{{
+* import org.apache.clerezza.rdf.scala.utils.Preamble._
+* }}} near the top of the
 * file using SCB Utilities for Scala
 */
+object Preamble extends TcIndependentConversions {
+
+}
+
+/**
+* This class provides the implicit conversions of its companion Object and
+* additional conversions that require an evaluation graph, i.e. the conversion
+* from a resource to a RichGraphNode.
+*
+* Typically this is used by
+* adding
+* {{{
+* val preamble = new org.apache.clerezza.rdf.scala.utils.Preamble(myMGraph)
+* import preamble._
+* }}}
+* before the
+* code section using the conversions
+*/
 class Preamble(baseTc: TripleCollection) extends TcIndependentConversions {
        implicit def toRichGraphNode(resource: Resource) = {
                new RichGraphNode(new GraphNode(resource, baseTc))
        }
 }
-object Preamble extends TcIndependentConversions {
 
-}
-trait TcIndependentConversions {
+protected trait TcIndependentConversions {
        implicit def toRichGraphNode(node: GraphNode) = {
                new RichGraphNode(node)
        }
@@ -47,8 +72,59 @@ trait TcIndependentConversions {
                        TcIndependentConversions.emptyLiteral
                }
        }
+
+       private val litFactory = LiteralFactory.getInstance
+
+       implicit def string2litBuilder(str: String) = new 
TcIndependentConversions.LiteralBuilder(str)
+
+       implicit def string2lit(str: String) = 
litFactory.createTypedLiteral(str)
+
+       implicit def lit2String(lit: Literal) = lit.getLexicalForm
+
+       implicit def litBuilder2lit(litBuilder: 
TcIndependentConversions.LiteralBuilder) = 
litFactory.createTypedLiteral(litBuilder.lexicalForm)
+
+       implicit def date2lit(date: Date) = litFactory.createTypedLiteral(date)
+
+       implicit def int2lit(int: Int) = litFactory.createTypedLiteral(int)
+
+       implicit def bigint2lit(bint: BigInt) = 
litFactory.createTypedLiteral(bint.underlying())
+
+       implicit def bigint2lit(bigInt: BigInteger) = 
litFactory.createTypedLiteral(bigInt)
+
+       implicit def bool2lit(boolean: Boolean) = 
litFactory.createTypedLiteral(boolean)
+
+       implicit def scalaBool2lit(boolean: scala.Boolean) = 
litFactory.createTypedLiteral(boolean)
+
+       implicit def long2lit(long: Long) = litFactory.createTypedLiteral(long)
+
+       implicit def double2lit(double: Double) = 
litFactory.createTypedLiteral(double)
+
+       implicit def uriRef2Prefix(uriRef: UriRef) = new 
NameSpace(uriRef.getUnicodeString)
+
+       implicit def URItoUriRef(uri: URI) = new UriRef(uri.toString)
+
+       implicit def URLtoUriRef(url: URL) = new UriRef(url.toExternalForm)
+       
 }
-object TcIndependentConversions {
+protected object TcIndependentConversions {
        val emptyGraph = new impl.SimpleGraph(new impl.SimpleMGraph)
        val emptyLiteral = new RichGraphNode(new GraphNode(new 
impl.PlainLiteralImpl(""), emptyGraph))
-}
+
+       /**
+        * An Easy Literal, contains functions for mapping literals to other 
literals, ie from String literals to
+        * typed literals.
+        */
+       class LiteralBuilder(val lexicalForm: String) {
+
+               /**
+                * @return a plain literal with language specified by lang
+                */
+               def lang(lang: Lang) = new PlainLiteralImpl(lexicalForm, lang)
+               def lang(lang: Symbol) = new PlainLiteralImpl(lexicalForm, new 
Language(lang.name))
+
+               def ^^(typ: UriRef) = new TypedLiteralImpl(lexicalForm, typ)
+
+               def uri = new UriRef(lexicalForm)
+
+       }
+}
\ No newline at end of file

Modified: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala?rev=1138621&r1=1138620&r2=1138621&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
 Wed Jun 22 20:34:20 2011
@@ -27,7 +27,7 @@ class TypeConversionTest {
 
        private val literalFactory = LiteralFactory.getInstance()
 
-       import EasyGraph._
+       import Preamble._
 
        @Test
        def useStringAsObject {


Reply via email to