Created
July 23, 2010 13:17
-
-
Save vijedi/487428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really cool, thanks. Could you please provide a .xsl snippet to show how to declare and use this function ?