1 38 39 40 package com.sun.xml.fastinfoset.sax; 41 42 import com.sun.xml.fastinfoset.EncodingConstants; 43 import com.sun.xml.fastinfoset.QualifiedName; 44 import com.sun.xml.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory; 45 import java.io.IOException ; 46 import java.util.Map ; 47 import org.jvnet.fastinfoset.EncodingAlgorithm; 48 import org.jvnet.fastinfoset.EncodingAlgorithmException; 49 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes; 50 import org.jvnet.fastinfoset.FastInfosetException; 51 52 import org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes; 53 import com.sun.xml.fastinfoset.CommonResourceBundle; 54 55 public class AttributesHolder implements EncodingAlgorithmAttributes { 56 private static final int DEFAULT_CAPACITY = 8; 57 58 private Map _registeredEncodingAlgorithms; 59 60 private int _attributeCount; 61 62 private QualifiedName[] _names; 63 private String [] _values; 64 65 private String [] _algorithmURIs; 66 private int[] _algorithmIds; 67 private Object [] _algorithmData; 68 69 public AttributesHolder() { 70 _names = new QualifiedName[DEFAULT_CAPACITY]; 71 _values = new String [DEFAULT_CAPACITY]; 72 73 _algorithmURIs = new String [DEFAULT_CAPACITY]; 74 _algorithmIds = new int[DEFAULT_CAPACITY]; 75 _algorithmData = new Object [DEFAULT_CAPACITY]; 76 } 77 78 public AttributesHolder(Map registeredEncodingAlgorithms) { 79 this(); 80 _registeredEncodingAlgorithms = registeredEncodingAlgorithms; 81 } 82 83 85 public final int getLength() { 86 return _attributeCount; 87 } 88 89 public final String getLocalName(int index) { 90 return _names[index].localName; 91 } 92 93 public final String getQName(int index) { 94 return _names[index].getQNameString(); 95 } 96 97 public final String getType(int index) { 98 return "CDATA"; 99 } 100 101 public final String getURI(int index) { 102 return _names[index].namespaceName; 103 } 104 105 public final String getValue(int index) { 106 final String value = _values[index]; 107 if (value != null) { 108 return value; 109 } 110 111 if (_algorithmData[index] == null || _registeredEncodingAlgorithms == null) { 112 return null; 113 } 114 115 try { 116 return _values[index] = convertEncodingAlgorithmDataToString( 117 _algorithmIds[index], 118 _algorithmURIs[index], 119 _algorithmData[index]).toString(); 120 } catch (IOException e) { 121 return null; 122 } catch (FastInfosetException e) { 123 return null; 124 } 125 } 126 127 public final int getIndex(String qName) { 128 int i = qName.indexOf(':'); 129 String prefix = ""; 130 String localName = qName; 131 if (i >= 0) { 132 prefix = qName.substring(0, i); 133 localName = qName.substring(i + 1); 134 } 135 136 for (i = 0; i < _attributeCount; i++) { 137 QualifiedName name = _names[i]; 138 if (localName.equals(name.localName) && 139 prefix.equals(name.prefix)) { 140 return i; 141 } 142 } 143 return -1; 144 } 145 146 public final String getType(String qName) { 147 int index = getIndex(qName); 148 if (index >= 0) { 149 return "CDATA"; 150 } else { 151 return null; 152 } 153 } 154 155 public final String getValue(String qName) { 156 int index = getIndex(qName); 157 if (index >= 0) { 158 return _values[index]; 159 } else { 160 return null; 161 } 162 } 163 164 public final int getIndex(String uri, String localName) { 165 for (int i = 0; i < _attributeCount; i++) { 166 QualifiedName name = _names[i]; 167 if (localName.equals(name.localName) && 168 uri.equals(name.namespaceName)) { 169 return i; 170 } 171 } 172 return -1; 173 } 174 175 public final String getType(String uri, String localName) { 176 int index = getIndex(uri, localName); 177 if (index >= 0) { 178 return "CDATA"; 179 } else { 180 return null; 181 } 182 } 183 184 public final String getValue(String uri, String localName) { 185 int index = getIndex(uri, localName); 186 if (index >= 0) { 187 return _values[index]; 188 } else { 189 return null; 190 } 191 } 192 193 public final void clear() { 194 for (int i = 0; i < _attributeCount; i++) { 195 _values[i] = null; 196 _algorithmData[i] = null; 197 } 198 _attributeCount = 0; 199 } 200 201 203 public final String getAlgorithmURI(int index) { 204 return _algorithmURIs[index]; 205 } 206 207 public final int getAlgorithmIndex(int index) { 208 return _algorithmIds[index]; 209 } 210 211 public final Object getAlgorithmData(int index) { 212 return _algorithmData[index]; 213 } 214 215 216 218 public final void addAttribute(QualifiedName name, String value) { 219 if (_attributeCount == _names.length) { 220 resize(); 221 } 222 _names[_attributeCount] = name; 223 _values[_attributeCount++] = value; 224 } 225 226 public final void addAttributeWithAlgorithmData(QualifiedName name, String URI, int id, Object data) { 227 if (_attributeCount == _names.length) { 228 resize(); 229 } 230 _names[_attributeCount] = name; 231 _values[_attributeCount] = null; 232 233 _algorithmURIs[_attributeCount] = URI; 234 _algorithmIds[_attributeCount] = id; 235 _algorithmData[_attributeCount++] = data; 236 } 237 238 public final QualifiedName getQualifiedName(int index) { 239 return _names[index]; 240 } 241 242 public final String getPrefix(int index) { 243 return _names[index].prefix; 244 } 245 246 private final void resize() { 247 final int newLength = _attributeCount * 3 / 2 + 1; 248 249 QualifiedName[] names = new QualifiedName[newLength]; 250 String [] values = new String [newLength]; 251 252 String [] algorithmURIs = new String [newLength]; 253 int[] algorithmIds = new int[newLength]; 254 Object [] algorithmData = new Object [newLength]; 255 256 System.arraycopy(_names, 0, names, 0, _attributeCount); 257 System.arraycopy(_values, 0, values, 0, _attributeCount); 258 259 System.arraycopy(_algorithmURIs, 0, algorithmURIs, 0, _attributeCount); 260 System.arraycopy(_algorithmIds, 0, algorithmIds, 0, _attributeCount); 261 System.arraycopy(_algorithmData, 0, algorithmData, 0, _attributeCount); 262 263 _names = names; 264 _values = values; 265 266 _algorithmURIs = algorithmURIs; 267 _algorithmIds = algorithmIds; 268 _algorithmData = algorithmData; 269 } 270 271 private final StringBuffer convertEncodingAlgorithmDataToString(int identifier, String URI, Object data) throws FastInfosetException, IOException { 272 EncodingAlgorithm ea = null; 273 if (identifier < EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) { 274 ea = BuiltInEncodingAlgorithmFactory.table[identifier]; 275 } else if (identifier == EncodingAlgorithmIndexes.CDATA) { 276 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.CDATAAlgorithmNotSupported")); 277 } else if (identifier >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) { 278 if (URI == null) { 279 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent") + identifier); 280 } 281 282 ea = (EncodingAlgorithm)_registeredEncodingAlgorithms.get(URI); 283 if (ea == null) { 284 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.algorithmNotRegistered") + URI); 285 } 286 } else { 287 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved")); 291 } 292 293 final StringBuffer sb = new StringBuffer (); 294 ea.convertToCharacters(data, sb); 295 return sb; 296 } 297 } 298 | Popular Tags |