Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENA-1845: Complete the collection of "fn:*-from-*" functions #693

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,6 @@ public static NodeValue getSeconds(NodeValue nv) {
return dtGetSeconds(nv) ;
}

private static int F_UNDEF = DatatypeConstants.FIELD_UNDEFINED;
public static NodeValue dtDateTime(NodeValue nv1, NodeValue nv2) {
if ( ! nv1.isDate() )
throw new ExprEvalException("fn:dateTime: arg1: Not an xsd:date: "+nv1) ;
Expand All @@ -1390,7 +1389,7 @@ public static NodeValue dtDateTime(NodeValue nv1, NodeValue nv2) {
return NodeValue.makeDateTime(lex);
}

// Datetime accessors
// Accessors: datetime, date, Gregorian.
public static NodeValue dtGetYear(NodeValue nv) {
if ( nv.isDateTime() || nv.isDate() || nv.isGYear() || nv.isGYearMonth() ) {
DateTimeStruct dts = parseAnyDT(nv) ;
Expand All @@ -1412,7 +1411,7 @@ public static NodeValue dtGetDay(NodeValue nv) {
DateTimeStruct dts = parseAnyDT(nv) ;
return NodeValue.makeNode(dts.day, XSDDatatype.XSDinteger) ;
}
throw new ExprEvalException("Not a month datatype") ;
throw new ExprEvalException("Not a day datatype") ;
}

private static DateTimeStruct parseAnyDT(NodeValue nv) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@
import org.apache.jena.sparql.expr.ExprList ;
import org.apache.jena.sparql.expr.NodeValue ;

/** Implementation root for custom function evaluation. */
/** Implementation root for custom function evaluation. */
public abstract class FunctionBase implements Function {

@Override
public final void build(String uri, ExprList args) {
// Rename for legacy reasons.
checkBuild(uri, args) ;
}

@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
if ( args == null )
// The contract on the function interface is that this should not happen.
throw new ARQInternalErrorException("FunctionBase: Null args list") ;

List<NodeValue> evalArgs = evalArgs(binding, args, env);

return exec(evalArgs, env) ;
}

Expand All @@ -55,15 +56,15 @@ public static List<NodeValue> evalArgs(Binding binding, ExprList args, FunctionE
return evalArgs;
}

/** Evaluation with access to the environment.
* Special and careful use only!
/** Evaluation with access to the environment.
* Special and careful use only!
* Arity will have been checked if using a fixed-count FunctionBase subclass.
*/
*/
protected NodeValue exec(List<NodeValue> args, FunctionEnv env) {
return exec(args);
}
/** Function call to a list of evaluated argument values */

/** Function call to a list of evaluated argument values */
public abstract NodeValue exec(List<NodeValue> args) ;

public abstract void checkBuild(String uri, ExprList args) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,20 @@ public static void loadStdDefs(FunctionRegistry registry) {

add(registry, xfn+"encode-for-uri", FN_StrEncodeForURI.class) ;

add(registry, xfn+"years-from-date", FN_YearsFromDate.class) ;
add(registry, xfn+"months-from-date", FN_MonthsFromDate.class) ;
add(registry, xfn+"days-from-date", FN_DaysFromDate.class) ;
add(registry, xfn+"timezone-from-date", FN_TimezoneFromDate.class) ;

add(registry, xfn+"hours-from-time", FN_HoursFromTime.class) ;
add(registry, xfn+"minutes-from-time", FN_MinutesFromTime.class) ;
add(registry, xfn+"seconds-from-time", FN_SecondsFromTime.class) ;
add(registry, xfn+"timezone-from-time", FN_TimezoneFromTime.class) ;

add(registry, xfn+"dateTime", FN_DateTime.class) ;
add(registry, xfn+"year-from-dateTime", FN_YearFromDateTime.class) ;
add(registry, xfn+"month-from-dateTime", FN_MonthFromDateTime.class) ;
add(registry, xfn+"day-from-dateTime", FN_DayFromDateTime.class) ;
add(registry, xfn+"years-from-dateTime", FN_YearsFromDateTime.class) ;
add(registry, xfn+"months-from-dateTime", FN_MonthsFromDateTime.class) ;
add(registry, xfn+"days-from-dateTime", FN_DaysFromDateTime.class) ;
add(registry, xfn+"hours-from-dateTime", FN_HoursFromDateTime.class) ;
add(registry, xfn+"minutes-from-dateTime", FN_MinutesFromDateTime.class) ;
add(registry, xfn+"seconds-from-dateTime", FN_SecondsFromDateTime.class) ;
Expand All @@ -191,9 +201,9 @@ public static void loadStdDefs(FunctionRegistry registry) {
// 9.6.1 fn:adjust-dateTime-to-timezone
add(registry, xfn+"adjust-dateTime-to-timezone", FN_AdjustDatetimeToTimezone.class) ;
// 9.6.2 fn:adjust-date-to-timezone
add(registry, xfn+"adjust-date-to-timezone", FN_AdjustDatetimeToTimezone.class) ;
add(registry, xfn+"adjust-date-to-timezone", FN_AdjustDateToTimezone.class) ;
// 9.6.3 fn:adjust-time-to-timezone
add(registry, xfn+"adjust-time-to-timezone", FN_AdjustDatetimeToTimezone.class) ;
add(registry, xfn+"adjust-time-to-timezone", FN_AdjustTimeToTimezone.class) ;
// 9.8.1 fn:format-dateTime
// 9.8.2 fn:format-date
// 9.8.3 fn:format-time
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.jena.sparql.function.library;

import org.apache.jena.atlas.lib.Lib;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.QueryBuildException;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.ExprList;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
import org.apache.jena.sparql.function.FunctionBase;

import java.util.List;

public class FN_AdjustDateToTimezone extends FunctionBase {
public FN_AdjustDateToTimezone(){super();}

@Override
public void checkBuild(String uri, ExprList args)
{
if ( args.size() != 1 && args.size() != 2 )
throw new QueryBuildException("Function '"+ Lib.className(this)+"' takes one or two arguments") ;
}
@Override
public NodeValue exec(List<NodeValue> args)
{
if ( args.size() != 1 && args.size() != 2 )
throw new ExprEvalException("fn:adjustDateToTimezone: Wrong number of arguments: "+args.size()+" : [wanted 1 or 2]") ;

NodeValue v1 = args.get(0) ;
if ( ARQ.isStrictMode() && !v1.isDate() )
throw new ExprEvalException("Not an xsd:date : " + v1);

if ( args.size() == 2 )
{
NodeValue v2 = args.get(1) ;
return XSDFuncOp.adjustDatetimeToTimezone(v1, v2) ;
}

return XSDFuncOp.adjustDatetimeToTimezone(v1, null) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.jena.sparql.function.library;

import org.apache.jena.atlas.lib.Lib;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.QueryBuildException;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.ExprList;
Expand All @@ -40,10 +41,12 @@ public void checkBuild(String uri, ExprList args)
public NodeValue exec(List<NodeValue> args)
{
if ( args.size() != 1 && args.size() != 2 )
throw new ExprEvalException("FN_StrNormalizeUnicode: Wrong number of arguments: "+args.size()+" : [wanted 1 or 2]") ;
throw new ExprEvalException("fn:adjustDateTimeToTimezone: Wrong number of arguments: "+args.size()+" : [wanted 1 or 2]") ;

NodeValue v1 = args.get(0) ;

if ( ARQ.isStrictMode() && !v1.isDateTime() )
throw new ExprEvalException("Not an xsd:dateTime : " + v1);

if ( args.size() == 2 )
{
NodeValue v2 = args.get(1) ;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.jena.sparql.function.library;

import org.apache.jena.atlas.lib.Lib;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.QueryBuildException;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.ExprList;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
import org.apache.jena.sparql.function.FunctionBase;

import java.util.List;

public class FN_AdjustTimeToTimezone extends FunctionBase {
public FN_AdjustTimeToTimezone(){super();}

@Override
public void checkBuild(String uri, ExprList args)
{
if ( args.size() != 1 && args.size() != 2 )
throw new QueryBuildException("Function '"+ Lib.className(this)+"' takes one or two arguments") ;
}
@Override
public NodeValue exec(List<NodeValue> args)
{
if ( args.size() != 1 && args.size() != 2 )
throw new ExprEvalException("fn:adjustTimeToTimezone: Wrong number of arguments: "+args.size()+" : [wanted 1 or 2]") ;

NodeValue v1 = args.get(0) ;
if ( ARQ.isStrictMode() && !v1.isTime() )
throw new ExprEvalException("Not an xsd:time : " + v1);

if ( args.size() == 2 )
{
NodeValue v2 = args.get(1) ;
return XSDFuncOp.adjustDatetimeToTimezone(v1, v2) ;
}

return XSDFuncOp.adjustDatetimeToTimezone(v1, null) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

package org.apache.jena.sparql.function.library;

import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp ;
import org.apache.jena.sparql.function.FunctionBase1 ;
import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
import org.apache.jena.sparql.function.FunctionBase1;

public class FN_MonthFromDateTime extends FunctionBase1
{
public class FN_DaysFromDate extends FunctionBase1 {
@Override
public NodeValue exec(NodeValue v)
{
return XSDFuncOp.dtGetMonth(v) ;
public NodeValue exec(NodeValue v) {
if ( ARQ.isStrictMode() && !v.isDate() )
throw new ExprEvalException("Not an xsd:date : " + v);
return XSDFuncOp.dtGetDay(v);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@

package org.apache.jena.sparql.function.library;

import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp ;
import org.apache.jena.sparql.function.FunctionBase1 ;

public class FN_DayFromDateTime extends FunctionBase1
public class FN_DaysFromDateTime extends FunctionBase1
{
@Override
public NodeValue exec(NodeValue v)
{
if ( ARQ.isStrictMode() && ! v.isDateTime() )
throw new ExprEvalException("Not an xsd:dateTime : "+v);
return XSDFuncOp.dtGetDay(v) ;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

package org.apache.jena.sparql.function.library;

import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp ;
import org.apache.jena.sparql.function.FunctionBase1 ;
import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
import org.apache.jena.sparql.function.FunctionBase1;

public class FN_HoursFromDateTime extends FunctionBase1
{
public class FN_HoursFromDateTime extends FunctionBase1 {
@Override
public NodeValue exec(NodeValue v)
{
return XSDFuncOp.dtGetHours(v) ;
public NodeValue exec(NodeValue v) {
if ( ARQ.isStrictMode() && !v.isDateTime() )
throw new ExprEvalException("Not an xsd:dateTime : " + v);
return XSDFuncOp.dtGetHours(v);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.jena.sparql.function.library;

import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue ;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp ;
import org.apache.jena.sparql.function.FunctionBase1 ;
Expand All @@ -27,6 +29,8 @@ public class FN_HoursFromDuration extends FunctionBase1
@Override
public NodeValue exec(NodeValue v)
{
if ( ARQ.isStrictMode() && !v.isDuration() )
throw new ExprEvalException("Not an xsd:duration : " + v);
return XSDFuncOp.durGetHours(v) ;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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.jena.sparql.function.library;

import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.nodevalue.XSDFuncOp;
import org.apache.jena.sparql.function.FunctionBase1;

public class FN_HoursFromTime extends FunctionBase1 {
@Override
public NodeValue exec(NodeValue v) {
if ( ARQ.isStrictMode() && !v.isDateTime() )
throw new ExprEvalException("Not an xsd:dateTime : " + v);
return XSDFuncOp.dtGetHours(v);
}
}