1   package org.idpf.epubcheck.util.saxon95;
2   
3   import net.sf.saxon.expr.XPathContext;
4   import net.sf.saxon.lib.ExtensionFunctionCall;
5   import net.sf.saxon.lib.ExtensionFunctionDefinition;
6   import net.sf.saxon.om.NodeInfo;
7   import net.sf.saxon.om.Sequence;
8   import net.sf.saxon.om.StructuredQName;
9   import net.sf.saxon.trans.XPathException;
10  import net.sf.saxon.value.Int64Value;
11  import net.sf.saxon.value.SequenceType;
12  
13  import org.osgi.service.component.annotations.Component;
14  
15  @Component(
16  	name = "saxon:line-number",
17  	service = { ExtensionFunctionDefinition.class }
18  )
19  public class LineNumberFunction extends ExtensionFunctionDefinition {
20  
21  	private static final long serialVersionUID = -4202710868367933385L;
22  	
23  	public static StructuredQName QNAME = new StructuredQName("saxon", "http://saxon.sf.net/", "line-number");
24  
25  	@Override
26  	public StructuredQName getFunctionQName() {
27  		return QNAME;
28  	}
29  
30  	@Override
31  	public int getMaximumNumberOfArguments() {
32  		return 1;
33  	}
34  
35  	@Override
36  	public int getMinimumNumberOfArguments() {
37  		return 0;
38  	}
39  
40  	@Override
41  	public SequenceType[] getArgumentTypes() {
42  		return new SequenceType[] { SequenceType.SINGLE_NODE };
43  	}
44  
45  	@Override
46  	public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
47  		return SequenceType.SINGLE_INTEGER;
48  	}
49  
50  	@Override
51  	public boolean dependsOnFocus() {
52  		return true;
53  	}
54  
55  	@Override
56  	public boolean trustResultType() {
57  		return true;
58  	}
59  
60  	@Override
61  	public ExtensionFunctionCall makeCallExpression() {
62  		return new ExtensionFunctionCall() {
63  
64  			private static final long serialVersionUID = -4202710868367933385L;
65  
66  			public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
67  				if (context.getContextItem() instanceof NodeInfo) {
68  					return new Int64Value(
69  							((NodeInfo) context.getContextItem())
70  									.getLineNumber());
71  				}
72  				throw new XPathException(
73  						"Unexpected XPath context for saxon:line-number");
74  			}
75  		};
76  	}
77  }