aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.sax; aoqi@0: aoqi@0: import com.sun.xml.internal.fastinfoset.EncodingConstants; aoqi@0: import com.sun.xml.internal.fastinfoset.QualifiedName; aoqi@0: import com.sun.xml.internal.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory; aoqi@0: import java.io.IOException; aoqi@0: import java.util.Map; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithm; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmIndexes; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException; aoqi@0: aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes; aoqi@0: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; aoqi@0: aoqi@0: public class AttributesHolder implements EncodingAlgorithmAttributes { aoqi@0: private static final int DEFAULT_CAPACITY = 8; aoqi@0: aoqi@0: private Map _registeredEncodingAlgorithms; aoqi@0: aoqi@0: private int _attributeCount; aoqi@0: aoqi@0: private QualifiedName[] _names; aoqi@0: private String[] _values; aoqi@0: aoqi@0: private String[] _algorithmURIs; aoqi@0: private int[] _algorithmIds; aoqi@0: private Object[] _algorithmData; aoqi@0: aoqi@0: public AttributesHolder() { aoqi@0: _names = new QualifiedName[DEFAULT_CAPACITY]; aoqi@0: _values = new String[DEFAULT_CAPACITY]; aoqi@0: aoqi@0: _algorithmURIs = new String[DEFAULT_CAPACITY]; aoqi@0: _algorithmIds = new int[DEFAULT_CAPACITY]; aoqi@0: _algorithmData = new Object[DEFAULT_CAPACITY]; aoqi@0: } aoqi@0: aoqi@0: public AttributesHolder(Map registeredEncodingAlgorithms) { aoqi@0: this(); aoqi@0: _registeredEncodingAlgorithms = registeredEncodingAlgorithms; aoqi@0: } aoqi@0: aoqi@0: // org.xml.sax.Attributes aoqi@0: aoqi@0: public final int getLength() { aoqi@0: return _attributeCount; aoqi@0: } aoqi@0: aoqi@0: public final String getLocalName(int index) { aoqi@0: return _names[index].localName; aoqi@0: } aoqi@0: aoqi@0: public final String getQName(int index) { aoqi@0: return _names[index].getQNameString(); aoqi@0: } aoqi@0: aoqi@0: public final String getType(int index) { aoqi@0: return "CDATA"; aoqi@0: } aoqi@0: aoqi@0: public final String getURI(int index) { aoqi@0: return _names[index].namespaceName; aoqi@0: } aoqi@0: aoqi@0: public final String getValue(int index) { aoqi@0: final String value = _values[index]; aoqi@0: if (value != null) { aoqi@0: return value; aoqi@0: } aoqi@0: aoqi@0: if (_algorithmData[index] == null || aoqi@0: (_algorithmIds[index] >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START && aoqi@0: _registeredEncodingAlgorithms == null)) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: return _values[index] = convertEncodingAlgorithmDataToString( aoqi@0: _algorithmIds[index], aoqi@0: _algorithmURIs[index], aoqi@0: _algorithmData[index]).toString(); aoqi@0: } catch (IOException e) { aoqi@0: return null; aoqi@0: } catch (FastInfosetException e) { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final int getIndex(String qName) { aoqi@0: int i = qName.indexOf(':'); aoqi@0: String prefix = ""; aoqi@0: String localName = qName; aoqi@0: if (i >= 0) { aoqi@0: prefix = qName.substring(0, i); aoqi@0: localName = qName.substring(i + 1); aoqi@0: } aoqi@0: aoqi@0: for (i = 0; i < _attributeCount; i++) { aoqi@0: QualifiedName name = _names[i]; aoqi@0: if (localName.equals(name.localName) && aoqi@0: prefix.equals(name.prefix)) { aoqi@0: return i; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: public final String getType(String qName) { aoqi@0: int index = getIndex(qName); aoqi@0: if (index >= 0) { aoqi@0: return "CDATA"; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final String getValue(String qName) { aoqi@0: int index = getIndex(qName); aoqi@0: if (index >= 0) { aoqi@0: return _values[index]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final int getIndex(String uri, String localName) { aoqi@0: for (int i = 0; i < _attributeCount; i++) { aoqi@0: QualifiedName name = _names[i]; aoqi@0: if (localName.equals(name.localName) && aoqi@0: uri.equals(name.namespaceName)) { aoqi@0: return i; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: public final String getType(String uri, String localName) { aoqi@0: int index = getIndex(uri, localName); aoqi@0: if (index >= 0) { aoqi@0: return "CDATA"; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final String getValue(String uri, String localName) { aoqi@0: int index = getIndex(uri, localName); aoqi@0: if (index >= 0) { aoqi@0: return _values[index]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void clear() { aoqi@0: for (int i = 0; i < _attributeCount; i++) { aoqi@0: _values[i] = null; aoqi@0: _algorithmData[i] = null; aoqi@0: } aoqi@0: _attributeCount = 0; aoqi@0: } aoqi@0: aoqi@0: // EncodingAlgorithmAttributes aoqi@0: aoqi@0: public final String getAlgorithmURI(int index) { aoqi@0: return _algorithmURIs[index]; aoqi@0: } aoqi@0: aoqi@0: public final int getAlgorithmIndex(int index) { aoqi@0: return _algorithmIds[index]; aoqi@0: } aoqi@0: aoqi@0: public final Object getAlgorithmData(int index) { aoqi@0: return _algorithmData[index]; aoqi@0: } aoqi@0: aoqi@0: public String getAlpababet(int index) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public boolean getToIndex(int index) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // ----- aoqi@0: aoqi@0: public final void addAttribute(QualifiedName name, String value) { aoqi@0: if (_attributeCount == _names.length) { aoqi@0: resize(); aoqi@0: } aoqi@0: _names[_attributeCount] = name; aoqi@0: _values[_attributeCount++] = value; aoqi@0: } aoqi@0: aoqi@0: public final void addAttributeWithAlgorithmData(QualifiedName name, String URI, int id, Object data) { aoqi@0: if (_attributeCount == _names.length) { aoqi@0: resize(); aoqi@0: } aoqi@0: _names[_attributeCount] = name; aoqi@0: _values[_attributeCount] = null; aoqi@0: aoqi@0: _algorithmURIs[_attributeCount] = URI; aoqi@0: _algorithmIds[_attributeCount] = id; aoqi@0: _algorithmData[_attributeCount++] = data; aoqi@0: } aoqi@0: aoqi@0: public final QualifiedName getQualifiedName(int index) { aoqi@0: return _names[index]; aoqi@0: } aoqi@0: aoqi@0: public final String getPrefix(int index) { aoqi@0: return _names[index].prefix; aoqi@0: } aoqi@0: aoqi@0: private final void resize() { aoqi@0: final int newLength = _attributeCount * 3 / 2 + 1; aoqi@0: aoqi@0: QualifiedName[] names = new QualifiedName[newLength]; aoqi@0: String[] values = new String[newLength]; aoqi@0: aoqi@0: String[] algorithmURIs = new String[newLength]; aoqi@0: int[] algorithmIds = new int[newLength]; aoqi@0: Object[] algorithmData = new Object[newLength]; aoqi@0: aoqi@0: System.arraycopy(_names, 0, names, 0, _attributeCount); aoqi@0: System.arraycopy(_values, 0, values, 0, _attributeCount); aoqi@0: aoqi@0: System.arraycopy(_algorithmURIs, 0, algorithmURIs, 0, _attributeCount); aoqi@0: System.arraycopy(_algorithmIds, 0, algorithmIds, 0, _attributeCount); aoqi@0: System.arraycopy(_algorithmData, 0, algorithmData, 0, _attributeCount); aoqi@0: aoqi@0: _names = names; aoqi@0: _values = values; aoqi@0: aoqi@0: _algorithmURIs = algorithmURIs; aoqi@0: _algorithmIds = algorithmIds; aoqi@0: _algorithmData = algorithmData; aoqi@0: } aoqi@0: aoqi@0: private final StringBuffer convertEncodingAlgorithmDataToString(int identifier, String URI, Object data) throws FastInfosetException, IOException { aoqi@0: EncodingAlgorithm ea = null; aoqi@0: if (identifier < EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) { aoqi@0: ea = BuiltInEncodingAlgorithmFactory.getAlgorithm(identifier); aoqi@0: } else if (identifier == EncodingAlgorithmIndexes.CDATA) { aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.CDATAAlgorithmNotSupported")); aoqi@0: } else if (identifier >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) { aoqi@0: if (URI == null) { aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent") + identifier); aoqi@0: } aoqi@0: aoqi@0: ea = (EncodingAlgorithm)_registeredEncodingAlgorithms.get(URI); aoqi@0: if (ea == null) { aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.algorithmNotRegistered") + URI); aoqi@0: } aoqi@0: } else { aoqi@0: // Reserved built-in algorithms for future use aoqi@0: // TODO should use sax property to decide if event will be aoqi@0: // reported, allows for support through handler if required. aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved")); aoqi@0: } aoqi@0: aoqi@0: final StringBuffer sb = new StringBuffer(); aoqi@0: ea.convertToCharacters(data, sb); aoqi@0: return sb; aoqi@0: } aoqi@0: aoqi@0: }