KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > fun > KeyFun


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl.fun;
30
31 import com.caucho.xpath.Expr;
32 import com.caucho.xpath.ExprEnvironment;
33 import com.caucho.xpath.XPathException;
34 import com.caucho.xpath.XPathFun;
35 import com.caucho.xpath.pattern.AbstractPattern;
36
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44 /**
45  * The key(...) function.
46  */

47 public class KeyFun extends XPathFun {
48   private HashMap JavaDoc<String JavaDoc,Key> _keys;
49
50   public KeyFun()
51   {
52     _keys = new HashMap JavaDoc<String JavaDoc,Key>();
53   }
54
55   /**
56    * Add a new key.
57    *
58    * @param name name of the key
59    * @param match the key's match pattern
60    * @param use the key's use expression
61    */

62   public void add(String JavaDoc name, AbstractPattern match, Expr use)
63   {
64     _keys.put(name, new Key(match, use));
65   }
66
67   public HashMap JavaDoc<String JavaDoc,Key> getKeys()
68   {
69     return _keys;
70   }
71
72   /**
73    * Evaluate the function.
74    *
75    * @param pattern The context pattern.
76    * @param args The evaluated arguments
77    */

78   public Object JavaDoc eval(Node JavaDoc node, ExprEnvironment env,
79              AbstractPattern pattern, ArrayList JavaDoc args)
80     throws XPathException
81   {
82     if (args.size() < 2)
83       return null;
84
85     String JavaDoc name = Expr.toString(args.get(0));
86     Key key = _keys.get(name);
87     Object JavaDoc value = args.get(1);
88
89     if (key == null)
90       return null;
91
92     if (value == null)
93       return null;
94
95     ArrayList JavaDoc nodes = new ArrayList JavaDoc();
96     if (value instanceof NodeList JavaDoc) {
97       NodeList JavaDoc list = (NodeList JavaDoc) value;
98       for (int i = 0; i < list.getLength(); i++)
99     key(node, env, key._match, key._use, Expr.toString(list.item(i)), nodes);
100     }
101     else if (value instanceof ArrayList JavaDoc) {
102       ArrayList JavaDoc list = (ArrayList JavaDoc) value;
103       for (int i = 0; i < list.size(); i++)
104     key(node, env, key._match, key._use, Expr.toString(list.get(i)), nodes);
105     }
106     else if (value instanceof Iterator JavaDoc) {
107       Iterator JavaDoc iter = (Iterator JavaDoc) value;
108       while (iter.hasNext())
109     key(node, env, key._match, key._use, Expr.toString(iter.next()), nodes);
110     }
111     else
112       key(node, env, key._match, key._use, Expr.toString(value), nodes);
113
114     return nodes;
115   }
116
117   private void key(Node JavaDoc node, ExprEnvironment env,
118            AbstractPattern match, Expr use, String JavaDoc value,
119                    ArrayList JavaDoc nodes)
120     throws XPathException
121   {
122     Iterator JavaDoc iter = match.select(node, env);
123     while (iter.hasNext()) {
124       Node JavaDoc subnode = (Node JavaDoc) iter.next();
125       String JavaDoc nodeValue = use.evalString(subnode, env);
126
127       if (value.equals(nodeValue))
128     nodes.add(subnode);
129     }
130   }
131
132   public static class Key {
133     AbstractPattern _match;
134     Expr _use;
135
136     Key(AbstractPattern match, Expr use)
137     {
138       _match = match;
139       _use = use;
140     }
141
142     public AbstractPattern getMatch()
143     {
144       return _match;
145     }
146
147     public Expr getUse()
148     {
149       return _use;
150     }
151   }
152 }
153
Popular Tags