KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > TagAttributes


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * A set of TagAttributes, usually representing all attributes on a Tag.
25  *
26  * @see com.sun.facelets.tag.Tag
27  * @see com.sun.facelets.tag.TagAttribute
28  * @author Jacob Hookom
29  * @version $Id: TagAttributes.java,v 1.2 2005/08/24 04:38:48 jhook Exp $
30  */

31 public final class TagAttributes {
32     private final static TagAttribute[] EMPTY = new TagAttribute[0];
33
34     private final TagAttribute[] attrs;
35
36     private final String JavaDoc[] ns;
37
38     private final List JavaDoc nsattrs;
39
40     /**
41      *
42      */

43     public TagAttributes(TagAttribute[] attrs) {
44         this.attrs = attrs;
45
46         // grab namespaces
47
int i = 0;
48         Set JavaDoc set = new HashSet JavaDoc();
49         for (i = 0; i < this.attrs.length; i++) {
50             set.add(this.attrs[i].getNamespace());
51         }
52         this.ns = (String JavaDoc[]) set.toArray(new String JavaDoc[set.size()]);
53         Arrays.sort(ns);
54
55         // assign attrs
56
this.nsattrs = new ArrayList JavaDoc();
57         for (i = 0; i < ns.length; i++) {
58             nsattrs.add(i, new ArrayList JavaDoc());
59         }
60         int nsIdx = 0;
61         for (i = 0; i < this.attrs.length; i++) {
62             nsIdx = Arrays.binarySearch(ns, this.attrs[i].getNamespace());
63             ((List JavaDoc) nsattrs.get(nsIdx)).add(this.attrs[i]);
64         }
65         for (i = 0; i < ns.length; i++) {
66             List JavaDoc r = (List JavaDoc) nsattrs.get(i);
67             nsattrs.set(i, r.toArray(new TagAttribute[r.size()]));
68         }
69     }
70
71     /**
72      * Return an array of all TagAttributes in this set
73      *
74      * @return a non-null array of TagAttributes
75      */

76     public TagAttribute[] getAll() {
77         return this.attrs;
78     }
79
80     /**
81      * Using no namespace, find the TagAttribute
82      *
83      * @see #get(String, String)
84      * @param localName
85      * tag attribute name
86      * @return the TagAttribute found, otherwise null
87      */

88     public TagAttribute get(String JavaDoc localName) {
89         return get("", localName);
90     }
91
92     /**
93      * Find a TagAttribute that matches the passed namespace and local name.
94      *
95      * @param ns
96      * namespace of the desired attribute
97      * @param localName
98      * local name of the attribute
99      * @return a TagAttribute found, otherwise null
100      */

101     public TagAttribute get(String JavaDoc ns, String JavaDoc localName) {
102         if (ns != null && localName != null) {
103             int idx = Arrays.binarySearch(this.ns, ns);
104             if (idx >= 0) {
105                 TagAttribute[] uia = (TagAttribute[]) this.nsattrs.get(idx);
106                 for (int i = 0; i < uia.length; i++) {
107                     if (localName.equals(uia[i].getLocalName())) {
108                         return uia[i];
109                     }
110                 }
111             }
112         }
113         return null;
114     }
115
116     /**
117      * Get all TagAttributes for the passed namespace
118      *
119      * @param namespace
120      * namespace to search
121      * @return a non-null array of TagAttributes
122      */

123     public TagAttribute[] getAll(String JavaDoc namespace) {
124         int idx = 0;
125         if (namespace == null) {
126             idx = Arrays.binarySearch(this.ns, "");
127         } else {
128             idx = Arrays.binarySearch(this.ns, namespace);
129         }
130         if (idx >= 0) {
131             return (TagAttribute[]) this.nsattrs.get(idx);
132         }
133         return EMPTY;
134     }
135
136     /**
137      * A list of Namespaces found in this set
138      *
139      * @return a list of Namespaces found in this set
140      */

141     public String JavaDoc[] getNamespaces() {
142         return this.ns;
143     }
144
145     /*
146      * (non-Javadoc)
147      *
148      * @see java.lang.Object#toString()
149      */

150     public String JavaDoc toString() {
151         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
152         for (int i = 0; i < this.attrs.length; i++) {
153             sb.append(this.attrs[i]);
154             sb.append(' ');
155         }
156         if (sb.length() > 1) {
157             sb.setLength(sb.length() - 1);
158         }
159         return sb.toString();
160     }
161 }
162
Popular Tags