Package org.deri.grefine.rdf.expr.functions.strings

Source Code of org.deri.grefine.rdf.expr.functions.strings.Urlify

package org.deri.grefine.rdf.expr.functions.strings;

import java.util.Properties;

import org.json.JSONException;
import org.json.JSONWriter;

import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;

public class Urlify implements Function {

    public Object call(Properties bindings, Object[] args) {
        if(args.length==1){
            String s = args[0].toString();
            if(s.isEmpty()){
              return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " Cannot urlify empty string");
            }
            s = s.toLowerCase().replaceAll("\\s+", "-").replaceAll("[^-a-zA-Z0-9]", "").replaceAll("\\-\\-+", "-");
            return s;
        }
        return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string");
    }

    public void write(JSONWriter writer, Properties options)
            throws JSONException {
        writer.object();
        writer.key("description"); writer.value("replaces spaces with underscore");
        writer.key("params"); writer.value("string s");
        writer.key("returns"); writer.value("string");
        writer.endObject();
       
    }

}
TOP

Related Classes of org.deri.grefine.rdf.expr.functions.strings.Urlify

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.