KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > RoleLocator


1 package net.sf.saxon.expr;
2
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.type.ItemType;
5
6 import javax.xml.transform.SourceLocator JavaDoc;
7 import java.io.Serializable JavaDoc;
8
9 /**
10  * A RoleLocator identifies the role in which an expression is used, for example as
11  * the third argument of the concat() function. This information is stored in an
12  * ItemChecker or CardinalityChecker so that good diagnostics can be
13  * achieved when run-time type errors are detected.
14  */

15 public class RoleLocator implements Serializable JavaDoc {
16
17     private int kind;
18     private Object JavaDoc container;
19     private int operand;
20     private NamePool namePool;
21     private String JavaDoc errorCode = "XPTY0004"; // default error code for type errors
22
private SourceLocator JavaDoc sourceLocator;
23
24     public static final int FUNCTION = 0;
25     public static final int BINARY_EXPR = 1;
26     public static final int TYPE_OP = 2;
27     public static final int VARIABLE = 3;
28     public static final int INSTRUCTION = 4;
29     public static final int FUNCTION_RESULT = 5;
30     public static final int ORDER_BY = 6;
31     public static final int TEMPLATE_RESULT = 7;
32
33     /**
34      * Create information about the role of a subexpression within its parent expression
35      * @param kind the kind of parent expression, e.g. a function call or a variable reference
36      * @param container the name of the object in the parent expression, e.g. a function name or
37      * instruction name. May be expressed either as a String or as an Integer nameCode in the name pool.
38      * @param operand Ordinal position of this subexpression, e.g. the position of an argument in
39      * @param namePool The name pool. Must be supplied if the second argument is an Integer namecode.
40      * Otherwise, may be null.
41      */

42
43     public RoleLocator(int kind, Object JavaDoc container, int operand, NamePool namePool) {
44         this.kind = kind;
45         this.container = container;
46         this.operand = operand;
47         this.namePool = namePool;
48     }
49
50     /**
51      * Set the error code to be produced if a type error is detected
52      * @param code The error code
53      */

54
55     public void setErrorCode(String JavaDoc code) {
56         if (code != null) {
57             this.errorCode = code;
58         }
59     }
60
61     /**
62      * Get the error code to be produced if a type error is detected
63      * @return code The error code
64      */

65
66     public String JavaDoc getErrorCode() {
67         return errorCode;
68     }
69
70     /**
71      * Set the source location
72      */

73
74     public void setSourceLocator(SourceLocator JavaDoc locator) {
75         // this is currently used only when type-checking literals,
76
// which don't have any location information of their own
77
if (locator instanceof ExpressionLocation) {
78             this.sourceLocator = locator;
79         } else {
80             this.sourceLocator = new ExpressionLocation(locator);
81         }
82         // the supplied value isn't saved because the locator may be an expression that
83
// contains links back to the containing stylesheet, which causes the stylesheet
84
// to remain in memory at run-time (and prevents stylesheet compilation)
85
}
86
87     /**
88      * Get the source location (if known - return null if not known)
89      */

90
91     public SourceLocator JavaDoc getSourceLocator() {
92         return sourceLocator;
93     }
94
95     /**
96      * Construct and return the error message indicating a type error
97      * @return the constructed error message
98      */

99     public String JavaDoc getMessage() {
100         String JavaDoc name;
101         if (container instanceof String JavaDoc) {
102             name = (String JavaDoc)container;
103         } else {
104             if (namePool == null) {
105                 name = "*unknown*";
106             } else {
107                 name = namePool.getDisplayName(((Integer JavaDoc)container).intValue());
108             }
109         }
110
111         switch (kind) {
112             case FUNCTION:
113                 return ordinal(operand+1) + " argument of " + name + "()";
114             case BINARY_EXPR:
115                 return ordinal(operand+1) + " operand of '" + name + '\'';
116             case TYPE_OP:
117                 return "value in '" + name + "' expression";
118             case VARIABLE:
119                 return "value of variable $" + name;
120             case INSTRUCTION:
121                 int slash = name.indexOf('/');
122                 String JavaDoc attributeName = "";
123                 if (slash >= 0) {
124                     attributeName = name.substring(slash+1);
125                     name = name.substring(0, slash);
126                 }
127                 return '@' + attributeName + " attribute of " + name;
128             case FUNCTION_RESULT:
129                 return "result of function " + name + "()";
130             case TEMPLATE_RESULT:
131                 return "result of template " + name;
132             case ORDER_BY:
133                 return ordinal(operand+1) + " sort key";
134             default:
135                 return "";
136         }
137     }
138
139     /**
140      * Construct a full error message
141      */

142
143     public String JavaDoc composeErrorMessage(ItemType requiredItemType, ItemType suppliedItemType, NamePool pool) {
144         return "Required type of " + getMessage() +
145                      " is " + requiredItemType.toString(pool) +
146                      "; supplied value has type " +
147                      suppliedItemType.toString(pool);
148     }
149
150     /**
151      * Get the ordinal representation of a number (used to identify which argument of a function
152      * is in error)
153      * @param n the cardinal number
154      * @return the ordinal representation
155      */

156     private static String JavaDoc ordinal(int n) {
157         switch(n) {
158             case 1:
159                 return "first";
160             case 2:
161                 return "second";
162             case 3:
163                 return "third";
164             default:
165                 // we can live with 21th, 22th... How many functions have >20 arguments?
166
return n + "th";
167         }
168     }
169 }
170
171 //
172
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
173
// you may not use this file except in compliance with the License. You may obtain a copy of the
174
// License at http://www.mozilla.org/MPL/
175
//
176
// Software distributed under the License is distributed on an "AS IS" basis,
177
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
178
// See the License for the specific language governing rights and limitations under the License.
179
//
180
// The Original Code is: all this file.
181
//
182
// The Initial Developer of the Original Code is Michael H. Kay.
183
//
184
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
185
//
186
// Contributor(s): none.
187
//
Popular Tags