Skip to content

Instantly share code, notes, and snippets.

@vijedi
Created July 23, 2010 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijedi/487428 to your computer and use it in GitHub Desktop.
Save vijedi/487428 to your computer and use it in GitHub Desktop.
package com.va.harvest.normalize.saxon;
import com.va.harvest.web.contrib.CommonUtil;
import net.sf.saxon.expr.StaticProperty;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.functions.ExtensionFunctionCall;
import net.sf.saxon.functions.ExtensionFunctionDefinition;
import net.sf.saxon.om.*;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.StringValue;
/**
* Author: Tejus Parikh
* Date: Mar 29, 2010 2:25:01 PM
*/
public class AbsolutizeUrl extends ExtensionFunctionDefinition {
private static final StructuredQName qName =
new StructuredQName("",
"http://vijedi.net/",
"absolutizeUrl");
private final static SequenceType[] argumentTypes = new SequenceType[] {
SequenceType.SINGLE_STRING,
SequenceType.OPTIONAL_STRING
};
@Override
public StructuredQName getFunctionQName() {
return qName;
}
@Override
public int getMinimumNumberOfArguments() {
return 1;
}
@Override
public int getMaximumNumberOfArguments() {
return 2;
}
@Override
public SequenceType[] getArgumentTypes() {
return argumentTypes;
}
@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.makeSequenceType(
suppliedArgumentTypes[0].getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_ONE);
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new AbsolutizeUrlCall();
}
private static class AbsolutizeUrlCall extends ExtensionFunctionCall {
@Override
public SequenceIterator call(SequenceIterator[] arguments, XPathContext xPathContext) throws XPathException {
StringValue pageUrlSV = (StringValue) arguments[0].next();
if(null == pageUrlSV) {
return EmptyIterator.getInstance();
}
StringValue hrefUrlSV = null;
if(arguments.length > 1) {
hrefUrlSV = (StringValue) arguments[1].next();
if(null == hrefUrlSV) {
return EmptyIterator.getInstance();
}
}
String pageUrl = pageUrlSV.getStringValue();
String hrefUrl = hrefUrlSV.getStringValue();
// Url transformation magic goes here
Item item = new StringValue(fullUrl);
return SingletonIterator.makeIterator(item);
}
}
}
@davidwasamrf
Copy link

This is really cool, thanks. Could you please provide a .xsl snippet to show how to declare and use this function ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment