You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by sa...@apache.org on 2014/05/16 02:30:26 UTC

svn commit: r1595083 - /jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java

Author: sallen
Date: Fri May 16 00:30:26 2014
New Revision: 1595083

URL: http://svn.apache.org/r1595083
Log:
JENA-697 Property Function to Split a String into Multiple Bindings

Added:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java

Added: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java?rev=1595083&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java (added)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/pfunction/library/strSplit.java Fri May 16 00:30:26 2014
@@ -0,0 +1,66 @@
+/*
+ * 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.sparql.pfunction.library ;
+
+import org.apache.jena.atlas.lib.StrUtils ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.NodeFactory ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.ExecutionContext ;
+import com.hp.hpl.jena.sparql.engine.QueryIterator ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.engine.iterator.QueryIterConcat ;
+import com.hp.hpl.jena.sparql.expr.ExprEvalException ;
+import com.hp.hpl.jena.sparql.pfunction.PFuncSimpleAndList ;
+import com.hp.hpl.jena.sparql.pfunction.PropFuncArg ;
+import com.hp.hpl.jena.sparql.util.IterLib ;
+
+/**
+ * Property function that requires the subject to be unbound, and the object to
+ * contain a list of two items, the first of which is a string to be split, and
+ * the second is a regular expression denoting the split point. The subject
+ * variable is bound for each result of the split, and each result has the
+ * whitespace trimmed from it.
+ */
+public class strSplit extends PFuncSimpleAndList
+{
+    @Override
+    public QueryIterator execEvaluated(Binding binding, Node subject, Node predicate, PropFuncArg object, ExecutionContext execCxt)
+    {
+        if (!Var.isVar(subject))
+            throw new ExprEvalException("Subject is not a variable (" + subject + ")") ;
+
+        if (object.getArgListSize() != 2)
+            throw new ExprEvalException("Object list must contain exactly two arguments, the string to split and a regular expression") ;
+
+        String s = object.getArg(0).getLiteralLexicalForm() ;
+        String regex = object.getArg(1).getLiteralLexicalForm() ;
+
+        QueryIterConcat cIter = new QueryIterConcat(execCxt) ;
+        // StrUtils will also trim whitespace
+        for (String token : StrUtils.split(s, regex))
+        {
+            cIter.add(IterLib.oneResult(binding, Var.alloc(subject), NodeFactory.createLiteral(token), execCxt)) ;
+        }
+
+        return cIter ;
+    }
+
+}