1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: KeyManager.java 468645 2006-10-28 06:57:24Z minchau $
20 */
21package org.apache.xalan.transformer;
22
23import java.util.Vector;
24
25import org.apache.xalan.templates.ElemTemplateElement;
26import org.apache.xml.utils.PrefixResolver;
27import org.apache.xml.utils.QName;
28import org.apache.xml.utils.XMLString;
29import org.apache.xpath.XPathContext;
30import org.apache.xpath.objects.XNodeSet;
31
32/**
33 * This class manages the key tables.
34 */
35public class KeyManager
36{
37
38  /**
39   * Table of tables of element keys.
40   * @see org.apache.xalan.transformer.KeyTable
41   */
42  private transient Vector m_key_tables = null;
43
44  /**
45   * Given a valid element key, return the corresponding node list.
46   *
47   * @param xctxt The XPath runtime state
48   * @param doc The document node
49   * @param name The key element name
50   * @param ref The key value we're looking for
51   * @param nscontext The prefix resolver for the execution context
52   *
53   * @return A nodelist of nodes mathing the given key
54   *
55   * @throws javax.xml.transform.TransformerException
56   */
57  public XNodeSet getNodeSetDTMByKey(
58          XPathContext xctxt, int doc, QName name, XMLString ref, PrefixResolver nscontext)
59            throws javax.xml.transform.TransformerException
60  {
61
62    XNodeSet nl = null;
63    ElemTemplateElement template = (ElemTemplateElement) nscontext;  // yuck -sb
64
65    if ((null != template)
66            && null != template.getStylesheetRoot().getKeysComposed())
67    {
68      boolean foundDoc = false;
69
70      if (null == m_key_tables)
71      {
72        m_key_tables = new Vector(4);
73      }
74      else
75      {
76        int nKeyTables = m_key_tables.size();
77
78        for (int i = 0; i < nKeyTables; i++)
79        {
80          KeyTable kt = (KeyTable) m_key_tables.elementAt(i);
81
82          if (kt.getKeyTableName().equals(name) && doc == kt.getDocKey())
83          {
84            nl = kt.getNodeSetDTMByKey(name, ref);
85
86            if (nl != null)
87            {
88              foundDoc = true;
89
90              break;
91            }
92          }
93        }
94      }
95
96      if ((null == nl) &&!foundDoc /* && m_needToBuildKeysTable */)
97      {
98        KeyTable kt =
99          new KeyTable(doc, nscontext, name,
100                       template.getStylesheetRoot().getKeysComposed(),
101                       xctxt);
102
103        m_key_tables.addElement(kt);
104
105        if (doc == kt.getDocKey())
106        {
107          foundDoc = true;
108          nl = kt.getNodeSetDTMByKey(name, ref);
109        }
110      }
111    }
112
113    return nl;
114  }
115}
116