001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: KeyDeclaration.java 468643 2006-10-28 06:56:03Z minchau $
020 */
021 package org.apache.xalan.templates;
022
023 import org.apache.xml.utils.QName;
024 import org.apache.xpath.XPath;
025
026 /**
027 * Holds the attribute declarations for the xsl:keys element.
028 * A stylesheet declares a set of keys for each document using
029 * the xsl:key element. When this set of keys contains a member
030 * with node x, name y and value z, we say that node x has a key
031 * with name y and value z.
032 * @see <a href="http://www.w3.org/TR/xslt#key">key in XSLT Specification</a>
033 * @xsl.usage internal
034 */
035 public class KeyDeclaration extends ElemTemplateElement
036 {
037 static final long serialVersionUID = 7724030248631137918L;
038
039 /**
040 * Constructs a new element representing the xsl:key. The parameters
041 * are needed to prioritize this key element as part of the recomposing
042 * process. For this element, they are not automatically created
043 * because the element is never added on to the stylesheet parent.
044 */
045 public KeyDeclaration(Stylesheet parentNode, int docOrderNumber)
046 {
047 m_parentNode = parentNode;
048 setUid(docOrderNumber);
049 }
050
051 /**
052 * The "name" property.
053 * @serial
054 */
055 private QName m_name;
056
057 /**
058 * Set the "name" attribute.
059 * The name attribute specifies the name of the key. The value
060 * of the name attribute is a QName, which is expanded as
061 * described in [2.4 Qualified Names].
062 *
063 * @param name Value to set for the "name" attribute.
064 */
065 public void setName(QName name)
066 {
067 m_name = name;
068 }
069
070 /**
071 * Get the "name" attribute.
072 * The name attribute specifies the name of the key. The value
073 * of the name attribute is a QName, which is expanded as
074 * described in [2.4 Qualified Names].
075 *
076 * @return Value of the "name" attribute.
077 */
078 public QName getName()
079 {
080 return m_name;
081 }
082
083 /**
084 * Return the node name.
085 *
086 * @return the element's name
087 */
088 public String getNodeName()
089 {
090 return Constants.ELEMNAME_KEY_STRING;
091 }
092
093
094 /**
095 * The "match" attribute.
096 * @serial
097 */
098 private XPath m_matchPattern = null;
099
100 /**
101 * Set the "match" attribute.
102 * The match attribute is a Pattern; an xsl:key element gives
103 * information about the keys of any node that matches the
104 * pattern specified in the match attribute.
105 * @see <a href="http://www.w3.org/TR/xslt#patterns">patterns in XSLT Specification</a>
106 *
107 * @param v Value to set for the "match" attribute.
108 */
109 public void setMatch(XPath v)
110 {
111 m_matchPattern = v;
112 }
113
114 /**
115 * Get the "match" attribute.
116 * The match attribute is a Pattern; an xsl:key element gives
117 * information about the keys of any node that matches the
118 * pattern specified in the match attribute.
119 * @see <a href="http://www.w3.org/TR/xslt#patterns">patterns in XSLT Specification</a>
120 *
121 * @return Value of the "match" attribute.
122 */
123 public XPath getMatch()
124 {
125 return m_matchPattern;
126 }
127
128 /**
129 * The "use" attribute.
130 * @serial
131 */
132 private XPath m_use;
133
134 /**
135 * Set the "use" attribute.
136 * The use attribute is an expression specifying the values
137 * of the key; the expression is evaluated once for each node
138 * that matches the pattern.
139 *
140 * @param v Value to set for the "use" attribute.
141 */
142 public void setUse(XPath v)
143 {
144 m_use = v;
145 }
146
147 /**
148 * Get the "use" attribute.
149 * The use attribute is an expression specifying the values
150 * of the key; the expression is evaluated once for each node
151 * that matches the pattern.
152 *
153 * @return Value of the "use" attribute.
154 */
155 public XPath getUse()
156 {
157 return m_use;
158 }
159
160 /**
161 * Get an int constant identifying the type of element.
162 * @see org.apache.xalan.templates.Constants
163 *
164 * @return The token ID for this element
165 */
166 public int getXSLToken()
167 {
168 return Constants.ELEMNAME_KEY;
169 }
170
171 /**
172 * This function is called after everything else has been
173 * recomposed, and allows the template to set remaining
174 * values that may be based on some other property that
175 * depends on recomposition.
176 */
177 public void compose(StylesheetRoot sroot)
178 throws javax.xml.transform.TransformerException
179 {
180 super.compose(sroot);
181 java.util.Vector vnames = sroot.getComposeState().getVariableNames();
182 if(null != m_matchPattern)
183 m_matchPattern.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
184 if(null != m_use)
185 m_use.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
186 }
187
188 /**
189 * This function is called during recomposition to
190 * control how this element is composed.
191 * @param root The root stylesheet for this transformation.
192 */
193 public void recompose(StylesheetRoot root)
194 {
195 root.recomposeKeys(this);
196 }
197
198 }