You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2014/11/25 17:26:49 UTC

[27/48] jena git commit: JENA-818 : Handle language strings, and ^^rdf:langString without lang.

JENA-818 : Handle language strings, and ^^rdf:langString without lang.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/4352b0f4
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/4352b0f4
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/4352b0f4

Branch: refs/heads/hadoop-rdf
Commit: 4352b0f481a33c19d9488b87847ce2dd58ad3286
Parents: 225eea9
Author: Andy Seaborne <an...@apache.org>
Authored: Fri Nov 21 18:37:00 2014 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri Nov 21 18:37:00 2014 +0000

----------------------------------------------------------------------
 .../apache/jena/riot/out/NodeFormatterBase.java | 48 +++++++++++++------
 .../jena/graph/impl/LiteralLabelFactory.java    | 37 +++++++++++----
 .../hpl/jena/graph/impl/LiteralLabelImpl.java   |  8 ++--
 .../com/hp/hpl/jena/rdf/model/impl/Util.java    | 40 +++++++++-------
 .../hpl/jena/rdfxml/xmloutput/impl/Basic.java   | 20 ++++----
 .../jena/rdfxml/xmloutput/impl/Unparser.java    | 50 ++++++++------------
 6 files changed, 119 insertions(+), 84 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterBase.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterBase.java b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterBase.java
index 6ea8dd7..1b4b5f9 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterBase.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterBase.java
@@ -21,6 +21,7 @@ package org.apache.jena.riot.out;
 import org.apache.jena.atlas.io.AWriter ;
 
 import com.hp.hpl.jena.JenaRuntime ;
+import com.hp.hpl.jena.datatypes.RDFDatatype ;
 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
 import com.hp.hpl.jena.graph.Node ;
 import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
@@ -53,27 +54,46 @@ public abstract class NodeFormatterBase implements NodeFormatter
     @Override
     public void formatBNode(AWriter w, Node n)       { formatBNode(w, n.getBlankNodeLabel()) ; }
 
+//    /** A Node is a simple string if:
+//     * <li>(RDF 1.0) No datatype and no language tag.
+//     * <li>(RDF 1.1) xsd:string
+//     */
+//    private static boolean isSimpleString(Node n) {
+//        RDFDatatype dt = n.getLiteralDatatype() ;
+//        if ( dt == null )
+//            return ! isLangString(n) ;
+//        if ( JenaRuntime.isRDF11 )
+//            return dt.equals(XSDDatatype.XSDstring) ; 
+//        return false ;
+//    }         
+//    
+//    /** A Node is a language string if it has a language tag. (RDF 1.0 and RDF 1.1) */
+//    private static boolean isLangString(Node n) {
+//        String lang = n.getLiteralLanguage() ;
+//        if ( lang == null )
+//            return false ;
+//        return ! lang.equals("") ;
+//    }
+ 
     @Override
     public void formatLiteral(AWriter w, Node n)
     {
-        String dt = n.getLiteralDatatypeURI() ;
+        RDFDatatype dt = n.getLiteralDatatype() ;
         String lang = n.getLiteralLanguage() ;
         String lex = n.getLiteralLexicalForm() ;
         
-        // In RDF 1.1, print xsd:string and language strings without datatype explicitly.
-        // dt should not be null for RDF 1.1 but let's play carefully. 
-        boolean shortString = JenaRuntime.isRDF11 ? (dt == null || dt.equals(XSDDatatype.XSDstring.getURI())) 
-                                                  : (dt == null) ;
-        
-        if ( shortString )
-        {
-            if ( lang == null || lang.equals("") )
-                formatLitString(w, lex) ;
-            else
-                formatLitLang(w, lex,lang) ;
+        if ( lang != null && ! lang.equals("") ) {
+            formatLitLang(w, lex, lang) ;
+        } else if ( dt == null ) {
+            // RDF 1.0, simple literal.
+            formatLitString(w, lex) ;
+        } else if ( JenaRuntime.isRDF11 && dt.equals(XSDDatatype.XSDstring) ) {
+            // RDF 1.1, xsd:string - outptu as short string.
+            formatLitString(w, lex) ;
+        } else {
+            // Datatype, no language tag, not short string.
+            formatLitDT(w, lex, dt.getURI()) ;
         }
-        else
-            formatLitDT(w, lex, dt) ;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelFactory.java
----------------------------------------------------------------------
diff --git a/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelFactory.java b/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelFactory.java
index 69c9ae1..97dfbf0 100644
--- a/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelFactory.java
+++ b/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelFactory.java
@@ -18,23 +18,39 @@
 
 package com.hp.hpl.jena.graph.impl;
 
-import com.hp.hpl.jena.datatypes.DatatypeFormatException;
-import com.hp.hpl.jena.datatypes.RDFDatatype;
+import com.hp.hpl.jena.JenaRuntime ;
+import com.hp.hpl.jena.datatypes.DatatypeFormatException ;
+import com.hp.hpl.jena.datatypes.RDFDatatype ;
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
+import com.hp.hpl.jena.graph.NodeFactory ;
+import com.hp.hpl.jena.vocabulary.RDF ;
 
 public class LiteralLabelFactory
 {
+    private static final RDFDatatype dtSLangString = NodeFactory.getType(RDF.Nodes.langString.getURI()) ;
+    
+    private static RDFDatatype fixDatatype(RDFDatatype dtype, String lang) {
+        if ( dtype != null ) 
+            return dtype ;
+        if ( JenaRuntime.isRDF11 )
+            dtype = (lang == null || lang.equals("")) ? XSDDatatype.XSDstring : dtSLangString  ;
+        return dtype ;
+    }
+    
     public static LiteralLabel createLiteralLabel( String lex, String lang, RDFDatatype dtype ) 
     throws DatatypeFormatException
-    { return new LiteralLabelImpl( lex, lang, dtype ); }
+    { 
+        dtype = fixDatatype(dtype, lang) ;
+        return new LiteralLabelImpl( lex, lang, dtype ); }
 
     /**
      * Build a plain literal label from its lexical form. 
      * @param lex the lexical form of the literal
      * @param lang the optional language tag, only relevant for plain literals
      */
-    public static LiteralLabel create(String lex, String lang) 
-    {
-        return new LiteralLabelImpl(lex, lang, null);
+    public static LiteralLabel create(String lex, String lang) {
+        RDFDatatype dt =  fixDatatype(null, lang) ;
+        return new LiteralLabelImpl(lex, lang, dt);
     }
 
     /**
@@ -46,6 +62,7 @@ public class LiteralLabelFactory
      * @param dtype the type of the literal, null for old style "plain" literals
      */
     public static LiteralLabel create(Object value, String lang, RDFDatatype dtype) throws DatatypeFormatException {
+        dtype = fixDatatype(dtype, lang) ;
         return new LiteralLabelImpl(value, lang, dtype) ; 
     }
 
@@ -56,6 +73,8 @@ public class LiteralLabelFactory
      * @param value the literal value to encapsulate
      */
     public static LiteralLabel create(Object value) {
+        if ( value instanceof String )
+            create((String)value, null) ;
         return new LiteralLabelImpl(value) ;
     }
 
@@ -63,8 +82,10 @@ public class LiteralLabelFactory
      * Creates either a plain literal or an XMLLiteral.
      *       @param xml If true then s is exclusive canonical XML of type rdf:XMLLiteral, and no checking will be invoked.
      */
-    public static LiteralLabel create(String s, String lg, boolean xml) {
-        return new LiteralLabelImpl(s, lg, xml) ;
+    public static LiteralLabel create(String s, String lang, boolean xml) {
+        if ( xml )
+            return new LiteralLabelImpl(s, lang, xml) ;
+        return create(s, lang) ;
     }
 
     

http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelImpl.java
----------------------------------------------------------------------
diff --git a/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelImpl.java b/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelImpl.java
index c4c46b0..b31288c 100644
--- a/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelImpl.java
+++ b/jena-core/src/main/java/com/hp/hpl/jena/graph/impl/LiteralLabelImpl.java
@@ -182,14 +182,14 @@ final /*public*/ class LiteralLabelImpl implements LiteralLabel {
 	 *       @param xml If true then s is exclusive canonical XML of type rdf:XMLLiteral, and no checking will be invoked.
 	
 	 */
-	LiteralLabelImpl(String s, String lg, boolean xml) {
-	    setLiteralLabel_3(s, lg, xml) ;
+	LiteralLabelImpl(String s, String lang, boolean xml) {
+	    setLiteralLabel_3(s, lang, xml) ;
 	}
 
-	private void setLiteralLabel_3(String s, String lg, boolean xml) {
+	private void setLiteralLabel_3(String s, String lang, boolean xml) {
 	    // Constructor extraction: Preparation for moving into Node_Literal.
         this.lexicalForm = s;
-        this.lang = (lg == null ? "" : lg);
+        this.lang = (lang == null ? "" : lang);
         if (xml) {
             // XML Literal
             this.dtype = XMLLiteralType.theXMLLiteralType;

http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-core/src/main/java/com/hp/hpl/jena/rdf/model/impl/Util.java
----------------------------------------------------------------------
diff --git a/jena-core/src/main/java/com/hp/hpl/jena/rdf/model/impl/Util.java b/jena-core/src/main/java/com/hp/hpl/jena/rdf/model/impl/Util.java
index ece5e9a..623174a 100644
--- a/jena-core/src/main/java/com/hp/hpl/jena/rdf/model/impl/Util.java
+++ b/jena-core/src/main/java/com/hp/hpl/jena/rdf/model/impl/Util.java
@@ -17,11 +17,15 @@
  */
 
 package com.hp.hpl.jena.rdf.model.impl;
-import java.util.regex.*;
+import java.util.regex.Matcher ;
+import java.util.regex.Pattern ;
 
-import org.apache.xerces.util.XMLChar;
+import org.apache.xerces.util.XMLChar ;
 
-import com.hp.hpl.jena.shared.*;
+import com.hp.hpl.jena.JenaRuntime ;
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
+import com.hp.hpl.jena.rdf.model.Literal ;
+import com.hp.hpl.jena.shared.CannotEncodeCharacterException ;
 
 /** Some utility functions.
  */
@@ -197,21 +201,21 @@ public class Util extends Object {
         return result + s.substring(lastPos, s.length());
     }
 
-    /** Call System.getProperty and suppresses SecurityException, (simply returns null).
-     *@return The property value, or null if none or there is a SecurityException.
-     */
-    public static String XgetProperty(String p) {
-        return XgetProperty( p, null );
-    }
-    /** Call System.getProperty and suppresses SecurityException, (simply returns null).
-     *@return The property value, or null if none or there is a SecurityException.
-     */
-    public static String XgetProperty(String p, String def) {
-        try {
-            return System.getProperty(p, def);
-        } catch (SecurityException e) {
-            return def;
+    /** Return true if the literal should be written as a string, without datatype or lang. (RDF 1.0 and RDF 1.1) */ 
+    public static boolean isSimpleString(Literal lit) {
+        if ( lit.getDatatypeURI() == null ) {
+            return ! isLangString(lit) ;
         }
+        if ( JenaRuntime.isRDF11 && lit.getDatatypeURI().equals(XSDDatatype.XSDstring.getURI()) )
+            return true;
+        return false ;
+    }
+    
+    /** Return true if the literals shodul be written with a language string. (RDF 1.0 and RDF 1.1) */
+    public static boolean isLangString(Literal lit) {
+        String lang = lit.getLanguage() ;
+        if ( lang == null )
+            return false ;
+        return ! lang.equals("") ;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Basic.java
----------------------------------------------------------------------
diff --git a/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Basic.java b/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Basic.java
index 8b3869c..73d03fe 100644
--- a/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Basic.java
+++ b/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Basic.java
@@ -163,20 +163,22 @@ public class Basic extends BaseXMLWriter
 	protected void writeLiteral( Literal l, PrintWriter writer ) {
 		String lang = l.getLanguage();
         String form = l.getLexicalForm();
-		if (!lang.equals("")) {
+		if (Util.isLangString(l)) {
 			writer.print(" xml:lang=" + attributeQuoted( lang ));
-		}
-		if (l.isWellFormedXML() && !blockLiterals) {
+		} else if (l.isWellFormedXML() && !blockLiterals) {
+		    // RDF XML Literals inline.
 			writer.print(" " + rdfAt("parseType") + "=" + attributeQuoted( "Literal" )+">");
 			writer.print( form );
+			return ;
 		} else {
-		    // No lang.
-			String dt = l.getDatatypeURI();
-			if ( ! Unparser.isSimpleString(l) ) 
-			    writer.print( " " + rdfAt( "datatype" ) + "=" + substitutedAttribute( dt ) );
-            writer.print(">");
-            writer.print( Util.substituteEntitiesInElementContent( form ) );
+	        // Datatype (if not xsd:string and RDF 1.1) 
+	        String dt = l.getDatatypeURI();
+	        if ( ! Util.isSimpleString(l) ) 
+	            writer.print( " " + rdfAt( "datatype" ) + "=" + substitutedAttribute( dt ) );
 		}
+		// Content.
+		writer.print(">");
+		writer.print( Util.substituteEntitiesInElementContent( form ) );
 	}
 
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/4352b0f4/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Unparser.java
----------------------------------------------------------------------
diff --git a/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Unparser.java b/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Unparser.java
index feab029..c523c70 100644
--- a/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Unparser.java
+++ b/jena-core/src/main/java/com/hp/hpl/jena/rdfxml/xmloutput/impl/Unparser.java
@@ -115,24 +115,22 @@ package com.hp.hpl.jena.rdfxml.xmloutput.impl;
  * 
  * [6.34] literal ::= (any well-formed XML)
  */
-import java.io.PrintWriter;
-import java.util.*;
-
-import org.apache.xerces.util.XMLChar;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.jena.iri.IRI;
-
-import com.hp.hpl.jena.JenaRuntime ;
-import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
-import com.hp.hpl.jena.rdf.model.*;
-import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
-import com.hp.hpl.jena.rdf.model.impl.Util;
-import com.hp.hpl.jena.shared.BrokenException;
-import com.hp.hpl.jena.shared.JenaException;
-import com.hp.hpl.jena.shared.PropertyNotFoundException;
-import com.hp.hpl.jena.util.iterator.*;
-import com.hp.hpl.jena.vocabulary.RDF;
+import java.io.PrintWriter ;
+import java.util.* ;
+
+import org.apache.jena.iri.IRI ;
+import org.apache.xerces.util.XMLChar ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.rdf.model.* ;
+import com.hp.hpl.jena.rdf.model.impl.PropertyImpl ;
+import com.hp.hpl.jena.rdf.model.impl.Util ;
+import com.hp.hpl.jena.shared.BrokenException ;
+import com.hp.hpl.jena.shared.JenaException ;
+import com.hp.hpl.jena.shared.PropertyNotFoundException ;
+import com.hp.hpl.jena.util.iterator.* ;
+import com.hp.hpl.jena.vocabulary.RDF ;
 
 /**
  * An Unparser will output a model in the abbreviated syntax. *
@@ -458,24 +456,14 @@ class Unparser {
         return true;
     }
     
-    /** Return true if to be written as a string, without datatype or lang */ 
-    /*package*/ static boolean isSimpleString(Literal lit) {
-        if ( lit.getDatatypeURI() == null ) 
-            return true;
-        if ( JenaRuntime.isRDF11 && lit.getDatatypeURI().equals(XSDDatatype.XSDstring.getURI()) )
-            return true;
-        if ( JenaRuntime.isRDF11 && lit.getDatatypeURI().equals(RDF.langString.getId()) )
-            return true;
-        
-        return false ;
-    }
-
     private boolean wPropertyEltDatatype(WType wt, Property prop, Statement s,
             RDFNode r) {
         if (! (r instanceof Literal) )
             return false ;
         Literal lit = ((Literal) r) ;
-        if ( isSimpleString(lit) ) 
+        if ( Util.isSimpleString(lit) ) 
+            return false;
+        if ( Util.isLangString(lit) )
             return false;
         
         // print out with "datatype="