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