You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/10/21 16:47:11 UTC

svn commit: r1400661 - in /jena/trunk/jena-core/src: main/java/com/hp/hpl/jena/datatypes/xsd/ test/java/com/hp/hpl/jena/graph/test/

Author: andy
Date: Sun Oct 21 14:47:11 2012
New Revision: 1400661

URL: http://svn.apache.org/viewvc?rev=1400661&view=rev
Log:
JENA-337 : Create common superclass for XSD datatypes that represent binary.

Added:
    jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbinary.java
Modified:
    jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbase64Binary.java
    jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDhexBinary.java
    jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/graph/test/TestTypedLiterals.java

Modified: jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbase64Binary.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbase64Binary.java?rev=1400661&r1=1400660&r2=1400661&view=diff
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbase64Binary.java (original)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbase64Binary.java Sun Oct 21 14:47:11 2012
@@ -18,40 +18,20 @@
 
 package com.hp.hpl.jena.datatypes.xsd;
 
-import java.util.Arrays;
+import org.apache.xerces.impl.dv.util.Base64 ;
 
-import org.apache.xerces.impl.dv.util.Base64;
-import com.hp.hpl.jena.datatypes.DatatypeFormatException;
-import com.hp.hpl.jena.graph.impl.LiteralLabel;
+import com.hp.hpl.jena.datatypes.DatatypeFormatException ;
 
 /**
  * Implement base64binary type. Most of the work is done in the superclass.
  * This only needs to implement the unparsing.
- * 
- * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
- * @version $Revision: 1.1 $ on $Date: 2009-06-29 08:56:03 $
  */
-public class XSDbase64Binary extends XSDDatatype {
-    // See also XSDhexBinary.
-    // XSDhexBinary and XSDbase64Binary could share via XSDBaseBinary.
-    /**
-     * Constructor. 
-     * @param typeName the name of the XSD type to be instantiated, this is 
-     * used to lookup a type definition from the Xerces schema factory.
-     */
+public class XSDbase64Binary extends XSDbinary {
+    
     public XSDbase64Binary(String typeName) {
-        super(typeName, byte[].class);
-    }
-         
-    /**
-     * Test whether the given object is a legal value form
-     * of this datatype. Brute force implementation.
-     */
-    @Override
-    public boolean isValidValue(Object valueForm) {
-        return (valueForm instanceof byte[]);
+        super(typeName);
     }
-    
+
     /**
      * Convert a value of this datatype out
      * to lexical form.
@@ -64,27 +44,4 @@ public class XSDbase64Binary extends XSD
             throw new DatatypeFormatException("base64 asked to encode an unwrapped byte array");
         }
     }
-    
-    /**
-     * Compares two instances of values of the given datatype.
-     * This ignores lang tags and just uses the java.lang.Number 
-     * equality.
-     */
-    @Override
-    public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
-        return value1.getDatatype() == value2.getDatatype()
-            && Arrays.equals((byte[])value1.getValue(), (byte[])value2.getValue());
-//      && value1.getLexicalForm().equals(value2.getLexicalForm());  // bug tracking, not real code
-    }
-   
-    @Override
-    public int getHashCode( LiteralLabel lit )
-    {
-        // Can't use super.getHashCode as that does "value.hashCode"
-        // Java arrays are not equal by value and their hash code of the sameValue array are different. 
-        if ( lit.isWellFormed() )
-            return getHashCode( (byte []) lit.getValue() );
-        else
-            return lit.getLexicalForm().hashCode() ;
-    }
 }

Added: jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbinary.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbinary.java?rev=1400661&view=auto
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbinary.java (added)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDbinary.java Sun Oct 21 14:47:11 2012
@@ -0,0 +1,70 @@
+/**
+ * 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 com.hp.hpl.jena.datatypes.xsd;
+
+import java.util.Arrays ;
+
+import com.hp.hpl.jena.graph.impl.LiteralLabel ;
+
+/** Root class for XSD datatypes with binary values, xsd:hexBinary and xsd:base64Binary.
+ * The binary value is stored as a byte[] in the LiteralLabel.
+ */
+
+public abstract class XSDbinary extends XSDDatatype {
+    /**
+     * @param typeName the name of the XSD type to be instantiated, this is 
+     * used to lookup a type definition from the Xerces schema factory.
+     */
+    public XSDbinary(String typeName) {
+        super(typeName, byte[].class);
+    }
+         
+    /**
+     * Test whether the given object is a legal value form
+     * of this datatype. Brute force implementation.
+     */
+    @Override
+    public boolean isValidValue(Object valueForm) {
+        return (valueForm instanceof byte[]);
+    }
+    
+    /**
+     * Compares two instances of values of the given datatype.
+     * This ignores lang tags and just uses the java.lang.Number 
+     * equality.
+     */
+    @Override
+    public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
+        return value1.getDatatype() == value2.getDatatype()
+            && Arrays.equals((byte[])value1.getValue(), (byte[])value2.getValue());
+            //      && value1.getLexicalForm().equals(value2.getLexicalForm());  // bug tracking, not real code
+    }
+   
+    @Override
+    public int getHashCode( LiteralLabel lit )
+    {
+        // Can't use super.getHashCode as that does "value.hashCode"
+        // Java arrays are not equal by value and their hash code of the sameValue array are different. 
+        if ( lit.isWellFormed() )
+            return getHashCode( (byte []) lit.getValue() );
+        else
+            return lit.getLexicalForm().hashCode() ;
+    }
+}
+

Modified: jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDhexBinary.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDhexBinary.java?rev=1400661&r1=1400660&r2=1400661&view=diff
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDhexBinary.java (original)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDhexBinary.java Sun Oct 21 14:47:11 2012
@@ -18,42 +18,20 @@
 
 package com.hp.hpl.jena.datatypes.xsd;
 
-import java.util.Arrays;
+import org.apache.xerces.impl.dv.util.HexBin ;
 
-import org.apache.xerces.impl.dv.util.HexBin;
-import com.hp.hpl.jena.datatypes.DatatypeFormatException;
-import com.hp.hpl.jena.graph.impl.LiteralLabel;
+import com.hp.hpl.jena.datatypes.DatatypeFormatException ;
 
 /**
  * Implement hexbinary type. Most of the work is done in the superclass.
  * This only needs to implement the unparsing.
- * 
- * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
- * @version $Revision: 1.1 $ on $Date: 2009-06-29 08:56:03 $
  */
-public class XSDhexBinary extends XSDDatatype {
-    // See also XSDbase64Binary.
-    // XSDhexBinary and XSDbase64Binary could share via XSDBaseBinary.
-
-    /**
-     * Constructor. 
-     * @param typeName the name of the XSD type to be instantiated, this is 
-     * used to lookup a type    
- definition from the Xerces schema factory.
-     */
+public class XSDhexBinary extends XSDbinary {
+    
     public XSDhexBinary(String typeName) {
-        super(typeName, null);
+        super(typeName);
     }
-         
-    /**
-     * Test whether the given object is a legal value form
-     * of this datatype. Brute force implementation.
-     */
-    @Override
-    public boolean isValidValue(Object valueForm) {
-        return (valueForm instanceof byte[]);
-    }
-    
+
     /**
      * Convert a value of this datatype out
      * to lexical form.
@@ -66,27 +44,4 @@ public class XSDhexBinary extends XSDDat
             throw new DatatypeFormatException("hexBinary asked to encode a non-byte arrary");
         }
     }
-    
-    /**
-     * Compares two instances of values of the given datatype.
-     * This ignores lang tags and just uses the java.lang.Number 
-     * equality.
-     */
-    @Override
-    public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
-        return value1.getDatatype() == value2.getDatatype()
-            && Arrays.equals((byte[])value1.getValue(), (byte[])value2.getValue());
-//      && value1.getLexicalForm().equals(value2.getLexicalForm());  // bug tracking, not real code
-    }
-    
-    @Override
-    public int getHashCode( LiteralLabel lit )
-    {
-        // Can't use super.getHashCode as that does "value.hashCode"
-        // Java arrays are not equal by value and their hash code of the sameValue array are different.
-        if ( lit.isWellFormed() )
-            return getHashCode( (byte []) lit.getValue() );
-        else
-            return lit.getLexicalForm().hashCode() ;
-    }
 }

Modified: jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/graph/test/TestTypedLiterals.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/graph/test/TestTypedLiterals.java?rev=1400661&r1=1400660&r2=1400661&view=diff
==============================================================================
--- jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/graph/test/TestTypedLiterals.java (original)
+++ jena/trunk/jena-core/src/test/java/com/hp/hpl/jena/graph/test/TestTypedLiterals.java Sun Oct 21 14:47:11 2012
@@ -33,10 +33,7 @@ import com.hp.hpl.jena.datatypes.BaseDat
 import com.hp.hpl.jena.datatypes.DatatypeFormatException ;
 import com.hp.hpl.jena.datatypes.RDFDatatype ;
 import com.hp.hpl.jena.datatypes.TypeMapper ;
-import com.hp.hpl.jena.datatypes.xsd.IllegalDateTimeFieldException ;
-import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
-import com.hp.hpl.jena.datatypes.xsd.XSDDateTime ;
-import com.hp.hpl.jena.datatypes.xsd.XSDDuration ;
+import com.hp.hpl.jena.datatypes.xsd.* ;
 import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType ;
 import com.hp.hpl.jena.enhanced.EnhNode ;
 import com.hp.hpl.jena.graph.Factory ;
@@ -54,9 +51,6 @@ import com.hp.hpl.jena.vocabulary.XSD ;
 /**
  * Unit test for the typed literal machinery - including RDFDatatype,
  * TypeMapper and LiteralLabel.
- * 
- * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
- * @version $Revision: 1.3 $ on $Date: 2010-05-09 10:22:07 $
  */
 public class TestTypedLiterals extends TestCase {
               
@@ -912,84 +906,65 @@ public class TestTypedLiterals extends T
        assertTrue( ! XSDDatatype.XSDfloat.isValidValue(new Double("2.3")));
     }
     
+    private static byte[] data = new byte[]{12, 42, 99};
     /**
      * Test binary types base64 and hexbinary
      */
-    public void testBinary() {
+    public void testBinary1() {
         // Check byte[] maps onto a binary type
         byte[] data = new byte[]{12, 42, 99};
         Literal l = m.createTypedLiteral(data);
         LiteralLabel ll = l.asNode().getLiteral();
-        assertEquals("binary test 1", ll.getDatatype(), XSDDatatype.XSDbase64Binary);
-        assertEquals("binary test 2", "DCpj", ll.getLexicalForm());
         
+        assertTrue("binary test 1", ll.getDatatype() instanceof XSDbinary);
+        
+        if ( ll.getDatatype() instanceof XSDhexBinary)
+            assertEquals("binary test 2a", "0C2A63", ll.getLexicalForm());
+        else if ( ll.getDatatype() instanceof XSDbase64Binary)
+            assertEquals("binary test 2b", "DCpj", ll.getLexicalForm());
+        else
+            fail("Unrecognized binary datatype") ;
+    }
+    
+    public void testBinary2() {
         // Check round tripping from value
-        LiteralLabel l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDbase64Binary).asNode().getLiteral();
+        LiteralLabel l2 = m.createTypedLiteral("DCpj", XSDDatatype.XSDbase64Binary).asNode().getLiteral();
         Object data2 = l2.getValue();
         assertTrue("binary test 3", data2 instanceof byte[]);
         byte[] data2b = (byte[])data2;
-        assertEquals("binary test 4", data2b[0], 12);
-        assertEquals("binary test 5", data2b[1], 42);
-        assertEquals("binary test 6", data2b[2], 99);
-        assertEquals(l2, ll);
-        
-        l2 = m.createTypedLiteral("DCpj", XSDDatatype.XSDbase64Binary).asNode().getLiteral();
-        data2 = l2.getValue();
-        assertTrue("binary test 3", data2 instanceof byte[]);
-        data2b = ((byte[])data2);
-        assertEquals("binary test 4", data2b[0], 12);
-        assertEquals("binary test 5", data2b[1], 42);
-        assertEquals("binary test 6", data2b[2], 99);
-        
+        assertEquals("binary test 4", data2b[0], data[0]);
+        assertEquals("binary test 5", data2b[1], data[1]);
+        assertEquals("binary test 6", data2b[2], data[2]);
+    }
+    
+    public void testBinary3() {
         // Check hexBinary
-        l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
-        ll = l.asNode().getLiteral();
+        Literal l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
+        LiteralLabel ll = l.asNode().getLiteral();
         assertEquals("binary test 1b", ll.getDatatype(), XSDDatatype.XSDhexBinary);
         assertEquals("binary test 2b", HexBin.encode(data), ll.getLexicalForm());
         
         // Check round tripping from value
-        l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDhexBinary).asNode().getLiteral();
-        data2 = l2.getValue();
+        LiteralLabel l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDhexBinary).asNode().getLiteral();
+        Object data2 = l2.getValue();
         assertTrue("binary test 3b", data2 instanceof byte[]);
-        data2b = ((byte[])data2);
-        assertEquals("binary test 4b", data2b[0], 12);
-        assertEquals("binary test 5b", data2b[1], 42);
-        assertEquals("binary test 6b", data2b[2], 99);
+        byte[] data2b = ((byte[])data2);
+        assertEquals("binary test 4b", data2b[0], data[0]);
+        assertEquals("binary test 5b", data2b[1], data[1]);
+        assertEquals("binary test 6b", data2b[2], data[2]);
         assertEquals(l2, ll);
-
+    }
+        
+    public void testBinary4() {   
         Literal la = m.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
         Literal lb = m.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
         assertTrue("equality test", la.sameValueAs(lb));
         
         data = new byte[] {15, (byte)0xB7};
-        l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
+        Literal l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
         assertEquals("hexBinary encoding", "0FB7", l.getLexicalForm());
     }
     
-    /**
-     * Attempt to isolate a JDK-dependent bug that only appears under 1.4.1_*.
-     * This failed to provoke the bug.
-     */
-    public void XXtestBinaryBug() throws IOException {
-        Model orig = ModelFactory.createDefaultModel();
-        Resource r = orig.createResource("http://jena.hpl.hp.com/test#r");
-        Property p = orig.createProperty("http://jena.hpl.hp.com/test#p");
-        Literal l  = orig.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
-        orig.add(r, p, l);
-        for (int i = 0; i < 150; i++) {
-            l  = orig.createTypedLiteral(new byte[]{(byte)i, (byte)i, (byte)i});
-            orig.add(orig.createResource("urn:x-hp:" + i), p, l);
-        }
-        ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
-        orig.write(out, "RDF/XML-ABBREV");
-        out.close();
-        InputStream ins = new ByteArrayInputStream(out.toByteArray());
-        Model m2 = ModelFactory.createDefaultModel();
-        m2.read(ins, null);
-        ins.close();
-        assertTrue(orig.isIsomorphicWith(m2));
-    }
-    
     /** Test that XSD anyURI is not sameValueAs XSD string (Xerces returns a string as the value for both) */ 
     public void testXSDanyURI() {
         Node node1 = Node.createLiteral("http://example/", null, XSDDatatype.XSDanyURI) ;
@@ -1071,8 +1046,6 @@ public class TestTypedLiterals extends T
                 "11/04/2012 03:29",
             };
 
-
-
             String format = "MM/dd/yyy HH:mm";
             for (String tstr : sampletimelist){
                 Date dt=getDateFromPattern(tstr, format, timezoneid);