src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/sax/AttributesHolder.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 *
aoqi@0 25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
aoqi@0 26 */
aoqi@0 27
aoqi@0 28 package com.sun.xml.internal.fastinfoset.sax;
aoqi@0 29
aoqi@0 30 import com.sun.xml.internal.fastinfoset.EncodingConstants;
aoqi@0 31 import com.sun.xml.internal.fastinfoset.QualifiedName;
aoqi@0 32 import com.sun.xml.internal.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory;
aoqi@0 33 import java.io.IOException;
aoqi@0 34 import java.util.Map;
aoqi@0 35 import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithm;
aoqi@0 36 import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
aoqi@0 37 import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
aoqi@0 38 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
aoqi@0 39
aoqi@0 40 import com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
aoqi@0 41 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
aoqi@0 42
aoqi@0 43 public class AttributesHolder implements EncodingAlgorithmAttributes {
aoqi@0 44 private static final int DEFAULT_CAPACITY = 8;
aoqi@0 45
aoqi@0 46 private Map _registeredEncodingAlgorithms;
aoqi@0 47
aoqi@0 48 private int _attributeCount;
aoqi@0 49
aoqi@0 50 private QualifiedName[] _names;
aoqi@0 51 private String[] _values;
aoqi@0 52
aoqi@0 53 private String[] _algorithmURIs;
aoqi@0 54 private int[] _algorithmIds;
aoqi@0 55 private Object[] _algorithmData;
aoqi@0 56
aoqi@0 57 public AttributesHolder() {
aoqi@0 58 _names = new QualifiedName[DEFAULT_CAPACITY];
aoqi@0 59 _values = new String[DEFAULT_CAPACITY];
aoqi@0 60
aoqi@0 61 _algorithmURIs = new String[DEFAULT_CAPACITY];
aoqi@0 62 _algorithmIds = new int[DEFAULT_CAPACITY];
aoqi@0 63 _algorithmData = new Object[DEFAULT_CAPACITY];
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 public AttributesHolder(Map registeredEncodingAlgorithms) {
aoqi@0 67 this();
aoqi@0 68 _registeredEncodingAlgorithms = registeredEncodingAlgorithms;
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 // org.xml.sax.Attributes
aoqi@0 72
aoqi@0 73 public final int getLength() {
aoqi@0 74 return _attributeCount;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 public final String getLocalName(int index) {
aoqi@0 78 return _names[index].localName;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 public final String getQName(int index) {
aoqi@0 82 return _names[index].getQNameString();
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 public final String getType(int index) {
aoqi@0 86 return "CDATA";
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 public final String getURI(int index) {
aoqi@0 90 return _names[index].namespaceName;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 public final String getValue(int index) {
aoqi@0 94 final String value = _values[index];
aoqi@0 95 if (value != null) {
aoqi@0 96 return value;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 if (_algorithmData[index] == null ||
aoqi@0 100 (_algorithmIds[index] >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START &&
aoqi@0 101 _registeredEncodingAlgorithms == null)) {
aoqi@0 102 return null;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 try {
aoqi@0 106 return _values[index] = convertEncodingAlgorithmDataToString(
aoqi@0 107 _algorithmIds[index],
aoqi@0 108 _algorithmURIs[index],
aoqi@0 109 _algorithmData[index]).toString();
aoqi@0 110 } catch (IOException e) {
aoqi@0 111 return null;
aoqi@0 112 } catch (FastInfosetException e) {
aoqi@0 113 return null;
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 public final int getIndex(String qName) {
aoqi@0 118 int i = qName.indexOf(':');
aoqi@0 119 String prefix = "";
aoqi@0 120 String localName = qName;
aoqi@0 121 if (i >= 0) {
aoqi@0 122 prefix = qName.substring(0, i);
aoqi@0 123 localName = qName.substring(i + 1);
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 for (i = 0; i < _attributeCount; i++) {
aoqi@0 127 QualifiedName name = _names[i];
aoqi@0 128 if (localName.equals(name.localName) &&
aoqi@0 129 prefix.equals(name.prefix)) {
aoqi@0 130 return i;
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133 return -1;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 public final String getType(String qName) {
aoqi@0 137 int index = getIndex(qName);
aoqi@0 138 if (index >= 0) {
aoqi@0 139 return "CDATA";
aoqi@0 140 } else {
aoqi@0 141 return null;
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public final String getValue(String qName) {
aoqi@0 146 int index = getIndex(qName);
aoqi@0 147 if (index >= 0) {
aoqi@0 148 return _values[index];
aoqi@0 149 } else {
aoqi@0 150 return null;
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 public final int getIndex(String uri, String localName) {
aoqi@0 155 for (int i = 0; i < _attributeCount; i++) {
aoqi@0 156 QualifiedName name = _names[i];
aoqi@0 157 if (localName.equals(name.localName) &&
aoqi@0 158 uri.equals(name.namespaceName)) {
aoqi@0 159 return i;
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162 return -1;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public final String getType(String uri, String localName) {
aoqi@0 166 int index = getIndex(uri, localName);
aoqi@0 167 if (index >= 0) {
aoqi@0 168 return "CDATA";
aoqi@0 169 } else {
aoqi@0 170 return null;
aoqi@0 171 }
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 public final String getValue(String uri, String localName) {
aoqi@0 175 int index = getIndex(uri, localName);
aoqi@0 176 if (index >= 0) {
aoqi@0 177 return _values[index];
aoqi@0 178 } else {
aoqi@0 179 return null;
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 public final void clear() {
aoqi@0 184 for (int i = 0; i < _attributeCount; i++) {
aoqi@0 185 _values[i] = null;
aoqi@0 186 _algorithmData[i] = null;
aoqi@0 187 }
aoqi@0 188 _attributeCount = 0;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 // EncodingAlgorithmAttributes
aoqi@0 192
aoqi@0 193 public final String getAlgorithmURI(int index) {
aoqi@0 194 return _algorithmURIs[index];
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 public final int getAlgorithmIndex(int index) {
aoqi@0 198 return _algorithmIds[index];
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 public final Object getAlgorithmData(int index) {
aoqi@0 202 return _algorithmData[index];
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 public String getAlpababet(int index) {
aoqi@0 206 return null;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 public boolean getToIndex(int index) {
aoqi@0 210 return false;
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 // -----
aoqi@0 214
aoqi@0 215 public final void addAttribute(QualifiedName name, String value) {
aoqi@0 216 if (_attributeCount == _names.length) {
aoqi@0 217 resize();
aoqi@0 218 }
aoqi@0 219 _names[_attributeCount] = name;
aoqi@0 220 _values[_attributeCount++] = value;
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 public final void addAttributeWithAlgorithmData(QualifiedName name, String URI, int id, Object data) {
aoqi@0 224 if (_attributeCount == _names.length) {
aoqi@0 225 resize();
aoqi@0 226 }
aoqi@0 227 _names[_attributeCount] = name;
aoqi@0 228 _values[_attributeCount] = null;
aoqi@0 229
aoqi@0 230 _algorithmURIs[_attributeCount] = URI;
aoqi@0 231 _algorithmIds[_attributeCount] = id;
aoqi@0 232 _algorithmData[_attributeCount++] = data;
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 public final QualifiedName getQualifiedName(int index) {
aoqi@0 236 return _names[index];
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 public final String getPrefix(int index) {
aoqi@0 240 return _names[index].prefix;
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 private final void resize() {
aoqi@0 244 final int newLength = _attributeCount * 3 / 2 + 1;
aoqi@0 245
aoqi@0 246 QualifiedName[] names = new QualifiedName[newLength];
aoqi@0 247 String[] values = new String[newLength];
aoqi@0 248
aoqi@0 249 String[] algorithmURIs = new String[newLength];
aoqi@0 250 int[] algorithmIds = new int[newLength];
aoqi@0 251 Object[] algorithmData = new Object[newLength];
aoqi@0 252
aoqi@0 253 System.arraycopy(_names, 0, names, 0, _attributeCount);
aoqi@0 254 System.arraycopy(_values, 0, values, 0, _attributeCount);
aoqi@0 255
aoqi@0 256 System.arraycopy(_algorithmURIs, 0, algorithmURIs, 0, _attributeCount);
aoqi@0 257 System.arraycopy(_algorithmIds, 0, algorithmIds, 0, _attributeCount);
aoqi@0 258 System.arraycopy(_algorithmData, 0, algorithmData, 0, _attributeCount);
aoqi@0 259
aoqi@0 260 _names = names;
aoqi@0 261 _values = values;
aoqi@0 262
aoqi@0 263 _algorithmURIs = algorithmURIs;
aoqi@0 264 _algorithmIds = algorithmIds;
aoqi@0 265 _algorithmData = algorithmData;
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 private final StringBuffer convertEncodingAlgorithmDataToString(int identifier, String URI, Object data) throws FastInfosetException, IOException {
aoqi@0 269 EncodingAlgorithm ea = null;
aoqi@0 270 if (identifier < EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) {
aoqi@0 271 ea = BuiltInEncodingAlgorithmFactory.getAlgorithm(identifier);
aoqi@0 272 } else if (identifier == EncodingAlgorithmIndexes.CDATA) {
aoqi@0 273 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.CDATAAlgorithmNotSupported"));
aoqi@0 274 } else if (identifier >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) {
aoqi@0 275 if (URI == null) {
aoqi@0 276 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent") + identifier);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 ea = (EncodingAlgorithm)_registeredEncodingAlgorithms.get(URI);
aoqi@0 280 if (ea == null) {
aoqi@0 281 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.algorithmNotRegistered") + URI);
aoqi@0 282 }
aoqi@0 283 } else {
aoqi@0 284 // Reserved built-in algorithms for future use
aoqi@0 285 // TODO should use sax property to decide if event will be
aoqi@0 286 // reported, allows for support through handler if required.
aoqi@0 287 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved"));
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 final StringBuffer sb = new StringBuffer();
aoqi@0 291 ea.convertToCharacters(data, sb);
aoqi@0 292 return sb;
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 }

mercurial