You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/02/13 15:23:22 UTC

svn commit: r909829 - in /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src: main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java

Author: reto
Date: Sat Feb 13 14:23:22 2010
New Revision: 909829

URL: http://svn.apache.org/viewvc?rev=909829&view=rev
Log:
CLEREZZA-123: converting back to java objects from many xsd types

Added:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java
Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java?rev=909829&r1=909828&r2=909829&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java Sat Feb 13 14:23:22 2010
@@ -20,9 +20,12 @@
 
 import java.text.DateFormat;
 import java.text.ParseException;
+import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import org.apache.clerezza.rdf.core.InvalidLiteralTypeException;
 import org.apache.clerezza.rdf.core.LiteralFactory;
 import org.apache.clerezza.rdf.core.NoConvertorException;
@@ -40,8 +43,25 @@
 
 public class SimpleLiteralFactory extends LiteralFactory {
 
-	final static Class<? extends byte[]> byteArrayType;
+	final private static UriRef xsdInteger =
+			new UriRef("http://www.w3.org/2001/XMLSchema#integer");
+	final private static UriRef xsdInt =
+			new UriRef("http://www.w3.org/2001/XMLSchema#int");
+	final private static UriRef xsdShort =
+			new UriRef("http://www.w3.org/2001/XMLSchema#short");
+	final private static UriRef xsdByte =
+			new UriRef("http://www.w3.org/2001/XMLSchema#byte");
+	
 
+	final private static Set<UriRef> decimalTypes = new HashSet<UriRef>();
+	static {
+		Collections.addAll(decimalTypes, xsdInteger, xsdInt, xsdByte, xsdShort);
+	}
+
+	final private static UriRef xsdDouble =
+			new UriRef("http://www.w3.org/2001/XMLSchema#double");
+
+	final static Class<? extends byte[]> byteArrayType;
 	static {
 		byte[] byteArray = new byte[0];
 		byteArrayType = byteArray.getClass();
@@ -138,17 +158,15 @@
 
 	private static class IntegerConverter implements TypeConverter<Integer> {
 
-		private static final UriRef intUri =
-			new UriRef("http://www.w3.org/2001/XMLSchema#int");
 
 		@Override
 		public TypedLiteral createTypedLiteral(Integer value) {
-			return new TypedLiteralImpl(value.toString(), intUri);
+			return new TypedLiteralImpl(value.toString(), xsdInt);
 		}
 
 		@Override
 		public Integer createObject(TypedLiteral literal) {
-			if (!literal.getDataType().equals(intUri)) {
+			if (!decimalTypes.contains(literal.getDataType())) {
 				throw new InvalidLiteralTypeException(Integer.class, literal.getDataType());
 			}
 			return new Integer(literal.getLexicalForm());
@@ -157,17 +175,16 @@
 
 	private static class LongConverter implements TypeConverter<Long> {
 
-		private static final UriRef longUri =
-			new UriRef("http://www.w3.org/2001/XMLSchema#integer");
+		
 
 		@Override
 		public TypedLiteral createTypedLiteral(Long value) {
-			return new TypedLiteralImpl(value.toString(), longUri);
+			return new TypedLiteralImpl(value.toString(), xsdInteger);
 		}
 
 		@Override
 		public Long createObject(TypedLiteral literal) {
-			if (!literal.getDataType().equals(longUri)) {
+			if (!decimalTypes.contains(literal.getDataType())) {
 				throw new InvalidLiteralTypeException(Long.class, literal.getDataType());
 			}
 			return new Long(literal.getLexicalForm());
@@ -176,17 +193,16 @@
 
 	private static class DoubleConverter implements TypeConverter<Double> {
 
-		private static final UriRef doubleUri =
-			new UriRef("http://www.w3.org/2001/XMLSchema#double");
+
 
 		@Override
 		public TypedLiteral createTypedLiteral(Double value) {
-			return new TypedLiteralImpl(value.toString(), doubleUri);
+			return new TypedLiteralImpl(value.toString(), xsdDouble);
 		}
 
 		@Override
 		public Double createObject(TypedLiteral literal) {
-			if (!literal.getDataType().equals(doubleUri)) {
+			if (!literal.getDataType().equals(xsdDouble)) {
 				throw new InvalidLiteralTypeException(Double.class, literal.getDataType());
 			}
 			return new Double(literal.getLexicalForm());

Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java?rev=909829&view=auto
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java (added)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactoryTest.java Sat Feb 13 14:23:22 2010
@@ -0,0 +1,59 @@
+/*
+ * 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.core.impl;
+
+import junit.framework.Assert;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class SimpleLiteralFactoryTest {
+
+	final private static UriRef xsdInteger = 
+			new UriRef("http://www.w3.org/2001/XMLSchema#integer");
+	final private static UriRef xsdInt =
+			new UriRef("http://www.w3.org/2001/XMLSchema#int");
+	SimpleLiteralFactory simpleLiteralFactory = new SimpleLiteralFactory();
+
+	@Test
+	public void longToXsdIntegerAndBackToMany() {
+		long value = 14l;
+		TypedLiteral tl = simpleLiteralFactory.createTypedLiteral(value);
+		Assert.assertEquals(xsdInteger, tl.getDataType());
+		long longValue = simpleLiteralFactory.createObject(Long.class, tl);
+		Assert.assertEquals(value, longValue);
+		int intValue = simpleLiteralFactory.createObject(Integer.class, tl);
+		Assert.assertEquals(value, intValue);
+	}
+
+	@Test
+	public void intToXsdIntAndBackToMany() {
+		int value = 14;
+		TypedLiteral tl = simpleLiteralFactory.createTypedLiteral(value);
+		Assert.assertEquals(xsdInt, tl.getDataType());
+		long longValue = simpleLiteralFactory.createObject(Long.class, tl);
+		Assert.assertEquals(value, longValue);
+		int intValue = simpleLiteralFactory.createObject(Integer.class, tl);
+		Assert.assertEquals(value, intValue);
+	}
+}