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 2017/06/20 10:44:23 UTC

[3/6] jena git commit: Support --fixup of prefix names.

Support --fixup of prefix names.

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

Branch: refs/heads/master
Commit: 78217659c3a4185a06cc0ef69b3ef6093fcb6c0d
Parents: d377dd1
Author: Andy Seaborne <an...@apache.org>
Authored: Sat Jun 17 15:45:20 2017 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Sat Jun 17 15:45:20 2017 +0100

----------------------------------------------------------------------
 jena-cmds/src/main/java/arq/uparse.java | 70 ++++++++++++++++++++++------
 1 file changed, 57 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/78217659/jena-cmds/src/main/java/arq/uparse.java
----------------------------------------------------------------------
diff --git a/jena-cmds/src/main/java/arq/uparse.java b/jena-cmds/src/main/java/arq/uparse.java
index 83d44cb..22290b5 100644
--- a/jena-cmds/src/main/java/arq/uparse.java
+++ b/jena-cmds/src/main/java/arq/uparse.java
@@ -21,25 +21,30 @@ package arq;
 import java.io.IOException ;
 import java.util.List ;
 
+import arq.cmdline.CmdARQ ;
 import jena.cmd.ArgDecl;
 import jena.cmd.CmdException;
-
 import org.apache.jena.atlas.io.IndentedLineBuffer ;
 import org.apache.jena.atlas.lib.Lib ;
+import org.apache.jena.atlas.logging.LogCtl ;
+import org.apache.jena.query.ARQ ;
+import org.apache.jena.query.QueryException ;
 import org.apache.jena.query.QueryParseException ;
 import org.apache.jena.query.Syntax ;
+import org.apache.jena.sparql.core.QueryCheckException ;
+import org.apache.jena.sparql.lang.ParserBase ;
 import org.apache.jena.sparql.modify.request.UpdateWriter ;
 import org.apache.jena.update.UpdateFactory ;
 import org.apache.jena.update.UpdateRequest ;
 import org.apache.jena.util.FileUtils ;
 
-import arq.cmdline.CmdARQ ;
-
 public class uparse extends CmdARQ
 {
     protected static final ArgDecl fileArg          = new ArgDecl(ArgDecl.HasValue, "file", "update") ;
     protected static final ArgDecl syntaxArg        = new ArgDecl(ArgDecl.HasValue, "syntax", "syn") ;
     protected static final ArgDecl argDeclPrint     = new ArgDecl(ArgDecl.HasValue, "print") ;
+    protected static final ArgDecl argDeclFixup     = new ArgDecl(ArgDecl.NoValue, "fixup") ;
+
     List<String> requestFiles = null ;
     protected Syntax updateSyntax = null ;
     private boolean printUpdate = false ;
@@ -54,6 +59,7 @@ public class uparse extends CmdARQ
         super.add(fileArg, "--file=FILE",  "Update commands to parse") ;
         super.add(syntaxArg, "--syntax=name", "Update syntax") ;
         super.add(argDeclPrint, "--print", "Print in various forms [update, none]") ;
+        super.add(argDeclFixup, "--fixup", "Convert undeclared prefix names to URIs") ;
     }
 
     @Override
@@ -63,6 +69,10 @@ public class uparse extends CmdARQ
         super.processModulesAndArgs() ;
         if ( super.cmdStrictMode )
             updateSyntax = Syntax.syntaxSPARQL_11 ;
+        
+        if ( contains(argDeclFixup) )
+            // Fixup undeclared prefix names.
+            ARQ.set(ARQ.fixupUndefinedPrefixes, true);
 
         // Set syntax
         if ( super.contains(syntaxArg) ) {
@@ -86,7 +96,6 @@ public class uparse extends CmdARQ
         
         if ( !printUpdate && ! printNone )
             printUpdate = true ;
-        
     }
     
     @Override
@@ -156,24 +165,59 @@ public class uparse extends CmdARQ
         if ( printNone )
             return ;
         
-        // And some checking.
+
+        try {
+            LogCtl.disable(ParserBase.ParserLoggerName) ;
+            checkUpdate(req, syntax);
+        } catch (UpdateCheckException ex)
+        {
+            System.err.println() ;
+            System.err.println("**** Check failure: "+ex.getMessage()) ;
+            if ( ex.getCause() != null )
+                ex.getCause().printStackTrace(System.err) ;
+        }
+        finally { LogCtl.setLevel(ParserBase.ParserLoggerName, "INFO") ; }
+    }
+    
+    public static class UpdateCheckException extends QueryException
+    {
+        public UpdateCheckException() { super() ; }
+        public UpdateCheckException(Throwable cause) { super(cause) ; }
+        public UpdateCheckException(String msg) { super(msg) ; }
+        public UpdateCheckException(String msg, Throwable cause) { super(msg, cause) ; }
+    }
+
+    
+    public static void checkUpdate(UpdateRequest req, Syntax syntax)
+    {
         IndentedLineBuffer w = new IndentedLineBuffer() ;
         UpdateWriter.output(req, w) ;
         String updateString2 = w.asString() ;
-        UpdateRequest req2 = null ;
+        
+        UpdateRequest req2;
         try {
+            String baseURI = null ;
+            if ( ! req.explicitlySetBaseURI() )
+                baseURI = req.getBaseURI() ;
             req2 = UpdateFactory.create(updateString2, syntax) ;
-        } catch (QueryParseException ex)
+        } catch (UnsupportedOperationException ex)
         {
-            System.err.println("Can not reparse update after serialization") ;
-            System.err.println(updateString2) ; 
+            // No parser after all.
+            return ;
+        }
+        catch (QueryException ex)
+        {
+            System.err.println(updateString2) ;
+            throw new QueryCheckException("could not parse output update request", ex) ;
         }
-
-        if ( ! req.equalTo(req2) )
-            System.err.println("Reparsed update does not .equalTo original parsed request") ;
-        
         
+//        if ( req.hashCode() != req2.hashCode() )
+//            throw new UpdateCheckException("reparsed query hashCode does not equal parsed input update \nUpdate (hashCode: " + req.hashCode() + ")=\n" + req + "\n\nUpdate2 (hashCode: " + req2.hashCode() + ")=\n" + req2) ;
+//        
+//        if ( ! req.equals(req2) ) 
+//            throw new UpdateCheckException("reparsed output does not equal parsed input") ;
     }
+
     
     static final String divider = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" ;
     //static final String divider = "" ;