1 14 15 package com.sun.facelets.tag; 16 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Set ; 22 23 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 [] ns; 37 38 private final List nsattrs; 39 40 43 public TagAttributes(TagAttribute[] attrs) { 44 this.attrs = attrs; 45 46 int i = 0; 48 Set set = new HashSet (); 49 for (i = 0; i < this.attrs.length; i++) { 50 set.add(this.attrs[i].getNamespace()); 51 } 52 this.ns = (String []) set.toArray(new String [set.size()]); 53 Arrays.sort(ns); 54 55 this.nsattrs = new ArrayList (); 57 for (i = 0; i < ns.length; i++) { 58 nsattrs.add(i, new ArrayList ()); 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 ) nsattrs.get(nsIdx)).add(this.attrs[i]); 64 } 65 for (i = 0; i < ns.length; i++) { 66 List r = (List ) nsattrs.get(i); 67 nsattrs.set(i, r.toArray(new TagAttribute[r.size()])); 68 } 69 } 70 71 76 public TagAttribute[] getAll() { 77 return this.attrs; 78 } 79 80 88 public TagAttribute get(String localName) { 89 return get("", localName); 90 } 91 92 101 public TagAttribute get(String ns, String 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 123 public TagAttribute[] getAll(String 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 141 public String [] getNamespaces() { 142 return this.ns; 143 } 144 145 150 public String toString() { 151 StringBuffer sb = new StringBuffer (); 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 |