KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLAttribute


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.Context;
4 import com.icl.saxon.Controller;
5 import com.icl.saxon.om.Name;
6 import com.icl.saxon.om.NodeInfo;
7 import com.icl.saxon.om.NamePool;
8 import com.icl.saxon.om.NamespaceException;
9 import com.icl.saxon.tree.NodeImpl;
10 import com.icl.saxon.expr.*;
11 import com.icl.saxon.output.Outputter;
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.TransformerConfigurationException JavaDoc;
14
15 /**
16 * xsl:attribute element in stylesheet.<BR>
17 */

18
19 public final class XSLAttribute extends XSLStringConstructor {
20
21     private Expression attributeName;
22     private Expression namespace=null;
23     private boolean disable = false;
24
25     public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
26
27         StandardNames sn = getStandardNames();
28         AttributeCollection atts = getAttributeList();
29         
30         String JavaDoc nameAtt = null;
31         String JavaDoc namespaceAtt = null;
32         String JavaDoc disableAtt = null;
33         
34         for (int a=0; a<atts.getLength(); a++) {
35             int nc = atts.getNameCode(a);
36             int f = nc & 0xfffff;
37             if (f==sn.NAME) {
38                 nameAtt = atts.getValue(a);
39             } else if (f==sn.NAMESPACE) {
40                 namespaceAtt = atts.getValue(a);
41             } else if (f==sn.SAXON_DISABLE_OUTPUT_ESCAPING) {
42                 disableAtt = atts.getValue(a);
43             } else {
44                 checkUnknownAttribute(nc);
45             }
46         }
47
48         if (nameAtt==null) {
49             reportAbsence("name");
50             return;
51         } else {
52             attributeName = makeAttributeValueTemplate(nameAtt);
53             if (attributeName instanceof StringValue) {
54                 if (!Name.isQName(((StringValue)attributeName).asString())) {
55                     compileError("Attribute name is not a valid QName");
56                 }
57             }
58         }
59         
60         if (namespaceAtt!=null) {
61             namespace = makeAttributeValueTemplate(namespaceAtt);
62         }
63
64         disable = (disableAtt != null && disableAtt.equals("yes"));
65
66     }
67
68     public void validate() throws TransformerConfigurationException JavaDoc {
69         if (!(getParentNode() instanceof XSLAttributeSet)) {
70             checkWithinTemplate();
71         }
72         optimize();
73     }
74
75     public void process(Context context) throws TransformerException JavaDoc
76     {
77         String JavaDoc expandedName = attributeName.evaluateAsString(context);
78         Controller controller = context.getController();
79         NamePool pool = controller.getNamePool();
80         
81         if (!Name.isQName(expandedName)) {
82             controller.reportRecoverableError(
83                 "Invalid attribute name: " + expandedName, this);
84             return;
85         }
86
87         if (expandedName.equals("xmlns")) {
88             if (namespace==null) {
89                 controller.reportRecoverableError(
90                     "Invalid attribute name: " + expandedName, this);
91                 return;
92             }
93         }
94         if (expandedName.length()>6 && expandedName.substring(0,6).equals("xmlns:")) {
95             if (namespace==null) {
96                 controller.reportRecoverableError(
97                     "Invalid attribute name: " + expandedName, this);
98                 return;
99             } else {
100                 // ignore the prefix "xmlns"
101
expandedName = expandedName.substring(6);
102             }
103         }
104
105         String JavaDoc prefix = Name.getPrefix(expandedName);
106         short uriCode;
107         
108         if (namespace==null) {
109             
110             // NB, we can't just call makeNameCode() because that would use the wrong
111
// name pool
112

113             if (prefix.equals("")) {
114                 uriCode = 0;
115             } else {
116                 try {
117                     uriCode = getURICodeForPrefix(prefix); // fails if not declared
118
} catch (NamespaceException err) {
119                     //TODO: should be a recoverable error?
120
throw styleError(err.getMessage());
121                 }
122             }
123                         
124         } else {
125
126             // generate a name using the supplied namespace URI
127

128             String JavaDoc uri = namespace.evaluateAsString(context);
129             if (uri.equals("")) {
130                 // there is a special rule for this case in the specification;
131
// we force the attribute to go in the null namespace
132
prefix = "";
133                 
134             } else {
135                 if (prefix.equals("")) {
136                     prefix = getPrefixForURI(uri);
137                     // prefix will be null if the namespace is undeclared, and "" if the
138
// namespace is the default namespace. In both cases, invent a prefix.
139
if (prefix==null || prefix=="") {
140                         prefix="ns0"; // arbitrary generated prefix; will be changed later if
141
// not unique
142
}
143                 }
144             }
145                              
146             uriCode = pool.allocateCodeForURI(uri); // allocate code in run-time name pool
147

148
149         }
150             
151         String JavaDoc localName = Name.getLocalName(expandedName);
152         int nameCode = pool.allocate(prefix, uriCode, localName);
153
154         Outputter out = controller.getOutputter();
155
156         // we may need to change the namespace prefix if the one we chose is
157
// already in use with a different namespace URI
158
if (out.thereIsAnOpenStartTag()) {
159             if (((nameCode>>20)&0xff) != 0) { // prefix!=""
160
nameCode = out.checkAttributePrefix(nameCode);
161             }
162             out.writeAttribute(nameCode, expandChildren(context), disable);
163         } else {
164             context.getController().reportRecoverableError(
165                 "Cannot write an attribute node when no element start tag is open",
166                 this);
167         }
168     }
169
170 }
171
172 //
173
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
174
// you may not use this file except in compliance with the License. You may obtain a copy of the
175
// License at http://www.mozilla.org/MPL/
176
//
177
// Software distributed under the License is distributed on an "AS IS" basis,
178
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
179
// See the License for the specific language governing rights and limitations under the License.
180
//
181
// The Original Code is: all this file.
182
//
183
// The Initial Developer of the Original Code is
184
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
185
//
186
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
187
//
188
// Contributor(s): none.
189
//
190
Popular Tags