01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.XPathContext;
04: import net.sf.saxon.om.Item;
05: import net.sf.saxon.om.NodeInfo;
06: import net.sf.saxon.style.StandardNames;
07: import net.sf.saxon.trans.XPathException;
08: import net.sf.saxon.type.Type;
09: import net.sf.saxon.value.BooleanValue;
10:
11: /**
12: * This class supports the nilled() function
13: */
14:
15: public class Nilled extends SystemFunction {
16:
17: /**
18: * Evaluate the function
19: */
20:
21: public Item evaluateItem(XPathContext c) throws XPathException {
22: NodeInfo node = (NodeInfo) argument[0].evaluateItem(c);
23: return getNilledProperty(node);
24: }
25:
26: /**
27: * Determine whether a node has the nilled property
28: * @param node the node in question (if null, the function returns null)
29: * @return the value of the nilled accessor. Returns null for any node other than an
30: * element node. For an element node, returns true if the element has been validated and
31: * has an xsi:nil attribute whose value is true.
32: */
33:
34: public static BooleanValue getNilledProperty(NodeInfo node) {
35: if (node == null || node.getNodeKind() != Type.ELEMENT) {
36: return null;
37: }
38: if (node.getTypeAnnotation() == -1) {
39: return BooleanValue.FALSE;
40: }
41: String val = node.getAttributeValue(StandardNames.XSI_NIL);
42: if (val == null) {
43: return BooleanValue.FALSE;
44: }
45: if (val.trim().equals("1") || val.trim().equals("true")) {
46: return BooleanValue.TRUE;
47: }
48: return BooleanValue.FALSE;
49: }
50:
51: /**
52: * Determine whether a node is nilled. Returns true if the value
53: * of the nilled property is true; false if the value is false or absent
54: */
55:
56: public static boolean isNilled(NodeInfo node) {
57: BooleanValue b = getNilledProperty(node);
58: if (b == null) {
59: return false;
60: } else {
61: return b.getBooleanValue();
62: }
63: }
64: }
65:
66: //
67: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
68: // you may not use this file except in compliance with the License. You may obtain a copy of the
69: // License at http://www.mozilla.org/MPL/
70: //
71: // Software distributed under the License is distributed on an "AS IS" basis,
72: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
73: // See the License for the specific language governing rights and limitations under the License.
74: //
75: // The Original Code is: all this file.
76: //
77: // The Initial Developer of the Original Code is Michael H. Kay.
78: //
79: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
80: //
81: // Contributor(s): none.
82: //
|