at.newmedialab.ldpath.template.engine.LDPathMethod Maven / Gradle / Ivy
/*
* Copyright (c) 2011 Salzburg Research.
*
* Licensed 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 at.newmedialab.ldpath.template.engine;
import at.newmedialab.ldpath.LDPath;
import at.newmedialab.ldpath.api.backend.RDFBackend;
import at.newmedialab.ldpath.exception.LDPathParseException;
import at.newmedialab.ldpath.model.Constants;
import at.newmedialab.ldpath.template.model.freemarker.TemplateNodeModel;
import at.newmedialab.ldpath.template.model.freemarker.TemplateStackModel;
import at.newmedialab.ldpath.template.model.freemarker.TemplateWrapperModel;
import at.newmedialab.ldpath.template.model.transformers.*;
import freemarker.core.Environment;
import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Add file description here!
*
* Author: Sebastian Schaffert
*/
public class LDPathMethod implements TemplateMethodModel {
private LDPath ldpath;
private RDFBackend backend;
public LDPathMethod(RDFBackend backend) {
this.ldpath = new LDPath(backend);
this.backend = backend;
// register custom freemarker transformers for the parser so we get the results immediately in the freemarker model
ldpath.registerTransformer(Constants.NS_XSD + "string", new TemplateScalarTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "decimal", new TemplateLongTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "integer", new TemplateIntegerTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "long", new TemplateLongTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "short", new TemplateIntegerTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "double", new TemplateDoubleTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "float", new TemplateFloatTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "dateTime", new TemplateDateTransformer(TemplateDateModel.DATETIME));
ldpath.registerTransformer(Constants.NS_XSD + "date", new TemplateDateTransformer(TemplateDateModel.DATE));
ldpath.registerTransformer(Constants.NS_XSD + "time", new TemplateDateTransformer(TemplateDateModel.TIME));
ldpath.registerTransformer(Constants.NS_XSD + "boolean", new TemplateBooleanTransformer());
ldpath.registerTransformer(Constants.NS_XSD + "anyURI", new TemplateScalarTransformer());
}
/**
* Executes a method call. All arguments passed to the method call are
* coerced to strings before being passed, if the FreeMarker rules allow
* the coercion. If some of the passed arguments can not be coerced to a
* string, an exception will be raised in the engine and the method will
* not be called. If your method would like to act on actual data model
* objects instead of on their string representations, implement the
* {@link freemarker.template.TemplateMethodModelEx} instead.
*
* @param arguments a List of String objects
* containing the values of the arguments passed to the method.
* @return the return value of the method, or null. If the returned value
* does not implement {@link freemarker.template.TemplateModel}, it will be automatically
* wrapped using the {@link freemarker.core.Environment#getObjectWrapper() environment
* object wrapper}.
*/
@Override
public Object exec(List arguments) throws TemplateModelException {
Environment env = Environment.getCurrentEnvironment();
TemplateStackModel contextStack = (TemplateStackModel)env.getVariable("context");
if(contextStack == null || contextStack.empty()) {
throw new TemplateModelException("error; no context node available");
}
TemplateNodeModel context = (TemplateNodeModel)contextStack.peek();
String path;
if(arguments.size() != 1) {
throw new TemplateModelException("the directive has been called without a path parameter");
} else {
path = (String)arguments.get(0);
if(!path.contains("::")) {
path = path + " :: xsd:string";
}
}
TemplateWrapperModel