| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Callable; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.NodeInfo; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.type.Type; |
| 17 |
import net.sf.saxon.value.AnyURIValue; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public abstract class UnparsedEntity extends SystemFunction implements Callable { |
| 27 |
|
| 28 |
public static int URI = 0; |
| 29 |
public static int PUBLIC_ID = 1; |
| 30 |
|
| 31 |
public abstract int getOp(); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 43 |
int operation = getOp(); |
| 44 |
String arg0 = arguments[0].head().getStringValue(); |
| 45 |
NodeInfo doc = null; |
| 46 |
if (getArity() == 1) { |
| 47 |
Item it = context.getContextItem(); |
| 48 |
if (it instanceof NodeInfo) { |
| 49 |
doc = ((NodeInfo)it).getRoot(); |
| 50 |
} |
| 51 |
if (doc == null || doc.getNodeKind() != Type.DOCUMENT) { |
| 52 |
String code = operation == URI ? "XTDE1370" : "XTDE1380"; |
| 53 |
throw new XPathException("In function " + getFunctionName().getDisplayName() + |
| 54 |
", the context item must be a node in a tree whose root is a document node", code, context); |
| 55 |
} |
| 56 |
} else { |
| 57 |
doc = (NodeInfo) arguments[1].head(); |
| 58 |
if (doc != null) { |
| 59 |
doc = doc.getRoot(); |
| 60 |
} |
| 61 |
if (doc == null || doc.getNodeKind() != Type.DOCUMENT) { |
| 62 |
String code = operation == URI ? "XTDE1370" : "XTDE1380"; |
| 63 |
throw new XPathException("In function " + getFunctionName().getDisplayName() + |
| 64 |
", the second argument must be a document node", code, context); |
| 65 |
} |
| 66 |
} |
| 67 |
String[] ids = doc.getTreeInfo().getUnparsedEntity(arg0); |
| 68 |
String result = ids == null ? "" : ids[operation]; |
| 69 |
return operation == URI ? new AnyURIValue(result) : new StringValue(result); |
| 70 |
} |
| 71 |
|
| 72 |
public static class UnparsedEntityUri extends UnparsedEntity { |
| 73 |
public int getOp() { |
| 74 |
return URI; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public static class UnparsedEntityPublicId extends UnparsedEntity { |
| 79 |
public int getOp() { |
| 80 |
return PUBLIC_ID; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|