src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/PrefixArray.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  *
    25  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.util;
    30 import com.sun.xml.internal.fastinfoset.EncodingConstants;
    31 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    32 import java.util.Iterator;
    33 import java.util.NoSuchElementException;
    34 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
    36 public class PrefixArray extends ValueArray {
    37     public static final int PREFIX_MAP_SIZE = 64;
    39     private int _initialCapacity;
    41     public String[] _array;
    43     private PrefixArray _readOnlyArray;
    45     private static class PrefixEntry {
    46         private PrefixEntry next;
    47         private int prefixId;
    48     }
    50     private PrefixEntry[] _prefixMap = new PrefixEntry[PREFIX_MAP_SIZE];
    52     private PrefixEntry _prefixPool;
    54     private static class NamespaceEntry {
    55         private NamespaceEntry next;
    56         private int declarationId;
    57         private int namespaceIndex;
    59         private String prefix;
    60         private String namespaceName;
    61         private int prefixEntryIndex;
    62     }
    64     private NamespaceEntry _namespacePool;
    66     private NamespaceEntry[] _inScopeNamespaces;
    68     public int[] _currentInScope;
    70     public int _declarationId;
    72     public PrefixArray(int initialCapacity, int maximumCapacity) {
    73         _initialCapacity = initialCapacity;
    74         _maximumCapacity = maximumCapacity;
    76         _array = new String[initialCapacity];
    77         // Sizes of _inScopeNamespaces and _currentInScope need to be two
    78         // greater than _array because 0 represents the empty string and
    79         // 1 represents the xml prefix
    80         _inScopeNamespaces = new NamespaceEntry[initialCapacity + 2];
    81         _currentInScope = new int[initialCapacity + 2];
    83         increaseNamespacePool(initialCapacity);
    84         increasePrefixPool(initialCapacity);
    86         initializeEntries();
    87     }
    89     public PrefixArray() {
    90         this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY);
    91     }
    93     private final void initializeEntries() {
    94         _inScopeNamespaces[0] = _namespacePool;
    95         _namespacePool = _namespacePool.next;
    96         _inScopeNamespaces[0].next = null;
    97         _inScopeNamespaces[0].prefix = "";
    98         _inScopeNamespaces[0].namespaceName = "";
    99         _inScopeNamespaces[0].namespaceIndex = _currentInScope[0] = 0;
   101         int index = KeyIntMap.indexFor(KeyIntMap.hashHash(_inScopeNamespaces[0].prefix.hashCode()), _prefixMap.length);
   102         _prefixMap[index] = _prefixPool;
   103         _prefixPool = _prefixPool.next;
   104         _prefixMap[index].next = null;
   105         _prefixMap[index].prefixId = 0;
   108         _inScopeNamespaces[1] = _namespacePool;
   109         _namespacePool = _namespacePool.next;
   110         _inScopeNamespaces[1].next = null;
   111         _inScopeNamespaces[1].prefix = EncodingConstants.XML_NAMESPACE_PREFIX;
   112         _inScopeNamespaces[1].namespaceName = EncodingConstants.XML_NAMESPACE_NAME;
   113         _inScopeNamespaces[1].namespaceIndex = _currentInScope[1] = 1;
   115         index = KeyIntMap.indexFor(KeyIntMap.hashHash(_inScopeNamespaces[1].prefix.hashCode()), _prefixMap.length);
   116         if (_prefixMap[index] == null) {
   117             _prefixMap[index] = _prefixPool;
   118             _prefixPool = _prefixPool.next;
   119             _prefixMap[index].next = null;
   120         } else {
   121             final PrefixEntry e = _prefixMap[index];
   122             _prefixMap[index] = _prefixPool;
   123             _prefixPool = _prefixPool.next;
   124             _prefixMap[index].next = e;
   125         }
   126         _prefixMap[index].prefixId = 1;
   127     }
   129     private final void increaseNamespacePool(int capacity) {
   130         if (_namespacePool == null) {
   131             _namespacePool = new NamespaceEntry();
   132         }
   134         for (int i = 0; i < capacity; i++) {
   135             NamespaceEntry ne = new NamespaceEntry();
   136             ne.next = _namespacePool;
   137             _namespacePool = ne;
   138         }
   139     }
   141     private final void increasePrefixPool(int capacity) {
   142         if (_prefixPool == null) {
   143             _prefixPool = new PrefixEntry();
   144         }
   146         for (int i = 0; i < capacity; i++) {
   147             PrefixEntry pe = new PrefixEntry();
   148             pe.next = _prefixPool;
   149             _prefixPool = pe;
   150         }
   151     }
   153     public int countNamespacePool() {
   154         int i = 0;
   155         NamespaceEntry e = _namespacePool;
   156         while (e != null) {
   157             i++;
   158             e = e.next;
   159         }
   160         return i;
   161     }
   163     public int countPrefixPool() {
   164         int i = 0;
   165         PrefixEntry e = _prefixPool;
   166         while (e != null) {
   167             i++;
   168             e = e.next;
   169         }
   170         return i;
   171     }
   173     public final void clear() {
   174         for (int i = _readOnlyArraySize; i < _size; i++) {
   175             _array[i] = null;
   176         }
   177         _size = _readOnlyArraySize;
   178     }
   180     public final void clearCompletely() {
   181         _prefixPool = null;
   182         _namespacePool = null;
   184         for (int i = 0; i < _size + 2; i++) {
   185             _currentInScope[i] = 0;
   186             _inScopeNamespaces[i] = null;
   187         }
   189         for (int i = 0; i < _prefixMap.length; i++) {
   190             _prefixMap[i] = null;
   191         }
   193         increaseNamespacePool(_initialCapacity);
   194         increasePrefixPool(_initialCapacity);
   196         initializeEntries();
   198         _declarationId = 0;
   200         clear();
   201     }
   203     /**
   204      * Returns cloned version of internal String[].
   205      * @return cloned version of internal String[].
   206      */
   207     public final String[] getArray() {
   208         if (_array == null) return null;
   210         final String[] clonedArray = new String[_array.length];
   211         System.arraycopy(_array, 0, clonedArray, 0, _array.length);
   212         return clonedArray;
   213     }
   215     public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) {
   216         if (!(readOnlyArray instanceof PrefixArray)) {
   217             throw new IllegalArgumentException(CommonResourceBundle.getInstance().
   218                     getString("message.illegalClass", new Object[]{readOnlyArray}));
   219         }
   221         setReadOnlyArray((PrefixArray)readOnlyArray, clear);
   222     }
   224     public final void setReadOnlyArray(PrefixArray readOnlyArray, boolean clear) {
   225         if (readOnlyArray != null) {
   226             _readOnlyArray = readOnlyArray;
   227             _readOnlyArraySize = readOnlyArray.getSize();
   229             clearCompletely();
   231             // Resize according to size of read only arrays
   232             _inScopeNamespaces = new NamespaceEntry[_readOnlyArraySize + _inScopeNamespaces.length];
   233             _currentInScope = new int[_readOnlyArraySize + _currentInScope.length];
   234             // Intialize first two entries
   235             initializeEntries();
   237             if (clear) {
   238                 clear();
   239             }
   241             _array = getCompleteArray();
   242             _size = _readOnlyArraySize;
   243         }
   244     }
   246     public final String[] getCompleteArray() {
   247         if (_readOnlyArray == null) {
   248             // Return cloned version of internal _array
   249             return getArray();
   250 //            return _array;
   251         } else {
   252             final String[] ra = _readOnlyArray.getCompleteArray();
   253             final String[] a = new String[_readOnlyArraySize + _array.length];
   254             System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
   255             return a;
   256         }
   257     }
   259     public final String get(int i) {
   260         return _array[i];
   261    }
   263     public final int add(String s) {
   264         if (_size == _array.length) {
   265             resize();
   266         }
   268        _array[_size++] = s;
   269        return _size;
   270     }
   272     protected final void resize() {
   273         if (_size == _maximumCapacity) {
   274             throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity"));
   275         }
   277         int newSize = _size * 3 / 2 + 1;
   278         if (newSize > _maximumCapacity) {
   279             newSize = _maximumCapacity;
   280         }
   282         final String[] newArray = new String[newSize];
   283         System.arraycopy(_array, 0, newArray, 0, _size);
   284         _array = newArray;
   286         newSize += 2;
   287         final NamespaceEntry[] newInScopeNamespaces = new NamespaceEntry[newSize];
   288         System.arraycopy(_inScopeNamespaces, 0, newInScopeNamespaces, 0, _inScopeNamespaces.length);
   289         _inScopeNamespaces = newInScopeNamespaces;
   291         final int[] newCurrentInScope = new int[newSize];
   292         System.arraycopy(_currentInScope, 0, newCurrentInScope, 0, _currentInScope.length);
   293         _currentInScope = newCurrentInScope;
   294     }
   296     public final void clearDeclarationIds() {
   297         for (int i = 0; i < _size; i++) {
   298             final NamespaceEntry e = _inScopeNamespaces[i];
   299             if (e != null) {
   300                 e.declarationId = 0;
   301             }
   302         }
   304         _declarationId = 1;
   305     }
   307     public final void pushScope(int prefixIndex, int namespaceIndex) throws FastInfosetException {
   308         if (_namespacePool == null) {
   309             increaseNamespacePool(16);
   310         }
   312         final NamespaceEntry e = _namespacePool;
   313         _namespacePool = e.next;
   315         final NamespaceEntry current = _inScopeNamespaces[++prefixIndex];
   316         if (current == null) {
   317             e.declarationId = _declarationId;
   318             e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex;
   319             e.next = null;
   321             _inScopeNamespaces[prefixIndex] = e;
   322         } else if (current.declarationId < _declarationId) {
   323             e.declarationId = _declarationId;
   324             e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex;
   325             e.next = current;
   327             current.declarationId = 0;
   328             _inScopeNamespaces[prefixIndex] = e;
   329         } else {
   330             throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.duplicateNamespaceAttribute"));
   331         }
   332     }
   334     public final void pushScopeWithPrefixEntry(String prefix, String namespaceName,
   335             int prefixIndex, int namespaceIndex) throws FastInfosetException {
   336         if (_namespacePool == null) {
   337             increaseNamespacePool(16);
   338         }
   339         if (_prefixPool == null) {
   340             increasePrefixPool(16);
   341         }
   343         final NamespaceEntry e = _namespacePool;
   344         _namespacePool = e.next;
   346         final NamespaceEntry current = _inScopeNamespaces[++prefixIndex];
   347         if (current == null) {
   348             e.declarationId = _declarationId;
   349             e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex;
   350             e.next = null;
   352             _inScopeNamespaces[prefixIndex] = e;
   353         } else if (current.declarationId < _declarationId) {
   354             e.declarationId = _declarationId;
   355             e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex;
   356             e.next = current;
   358             current.declarationId = 0;
   359             _inScopeNamespaces[prefixIndex] = e;
   360         } else {
   361             throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.duplicateNamespaceAttribute"));
   362         }
   364         final PrefixEntry p = _prefixPool;
   365         _prefixPool = _prefixPool.next;
   366         p.prefixId = prefixIndex;
   368         e.prefix = prefix;
   369         e.namespaceName = namespaceName;
   370         e.prefixEntryIndex = KeyIntMap.indexFor(KeyIntMap.hashHash(prefix.hashCode()), _prefixMap.length);
   372         final PrefixEntry pCurrent = _prefixMap[e.prefixEntryIndex];
   373         p.next = pCurrent;
   374         _prefixMap[e.prefixEntryIndex] = p;
   375     }
   377     public final void popScope(int prefixIndex) {
   378         final NamespaceEntry e = _inScopeNamespaces[++prefixIndex];
   379         _inScopeNamespaces[prefixIndex] = e.next;
   380         _currentInScope[prefixIndex] = (e.next != null) ? e.next.namespaceIndex : 0;
   382         e.next = _namespacePool;
   383         _namespacePool = e;
   384     }
   386     public final void popScopeWithPrefixEntry(int prefixIndex) {
   387         final NamespaceEntry e = _inScopeNamespaces[++prefixIndex];
   389         _inScopeNamespaces[prefixIndex] = e.next;
   390         _currentInScope[prefixIndex] = (e.next != null) ? e.next.namespaceIndex : 0;
   392         e.prefix = e.namespaceName = null;
   393         e.next = _namespacePool;
   394         _namespacePool = e;
   396         PrefixEntry current = _prefixMap[e.prefixEntryIndex];
   397         if (current.prefixId == prefixIndex) {
   398             _prefixMap[e.prefixEntryIndex] = current.next;
   399             current.next = _prefixPool;
   400             _prefixPool = current;
   401         } else {
   402             PrefixEntry prev = current;
   403             current = current.next;
   404             while (current != null) {
   405                 if (current.prefixId == prefixIndex) {
   406                     prev.next = current.next;
   407                     current.next = _prefixPool;
   408                     _prefixPool = current;
   409                     break;
   410                 }
   411                 prev = current;
   412                 current = current.next;
   413             }
   414         }
   415     }
   417     public final String getNamespaceFromPrefix(String prefix) {
   418         final int index = KeyIntMap.indexFor(KeyIntMap.hashHash(prefix.hashCode()), _prefixMap.length);
   419         PrefixEntry pe = _prefixMap[index];
   420         while (pe != null) {
   421             final NamespaceEntry ne = _inScopeNamespaces[pe.prefixId];
   422             if (prefix == ne.prefix || prefix.equals(ne.prefix)) {
   423                 return ne.namespaceName;
   424             }
   425             pe = pe.next;
   426         }
   428         return null;
   429     }
   431     public final String getPrefixFromNamespace(String namespaceName) {
   432         int position = 0;
   433         while (++position < _size + 2) {
   434             final NamespaceEntry ne = _inScopeNamespaces[position];
   435             if (ne != null && namespaceName.equals(ne.namespaceName)) {
   436                 return ne.prefix;
   437             }
   438         }
   440         return null;
   441     }
   443     public final Iterator getPrefixes() {
   444         return new Iterator() {
   445             int _position = 1;
   446             NamespaceEntry _ne = _inScopeNamespaces[_position];
   448             public boolean hasNext() {
   449                 return _ne != null;
   450             }
   452             public Object next() {
   453                 if (_position == _size + 2) {
   454                     throw new NoSuchElementException();
   455                 }
   457                 final String prefix = _ne.prefix;
   458                 moveToNext();
   459                 return prefix;
   460             }
   462             public void remove() {
   463                 throw new UnsupportedOperationException();
   464             }
   466             private final void moveToNext() {
   467                 while (++_position < _size + 2) {
   468                     _ne = _inScopeNamespaces[_position];
   469                     if (_ne != null) {
   470                         return;
   471                     }
   472                 }
   473                 _ne = null;
   474             }
   476         };
   477     }
   479     public final Iterator getPrefixesFromNamespace(final String namespaceName) {
   480         return new Iterator() {
   481             String _namespaceName = namespaceName;
   482             int _position = 0;
   483             NamespaceEntry _ne;
   485             {
   486                 moveToNext();
   487             }
   489             public boolean hasNext() {
   490                 return _ne != null;
   491             }
   493             public Object next() {
   494                 if (_position == _size + 2) {
   495                     throw new NoSuchElementException();
   496                 }
   498                 final String prefix = _ne.prefix;
   499                 moveToNext();
   500                 return prefix;
   501             }
   503             public void remove() {
   504                 throw new UnsupportedOperationException();
   505             }
   507             private final void moveToNext() {
   508                 while (++_position < _size + 2) {
   509                     _ne = _inScopeNamespaces[_position];
   510                     if (_ne != null && _namespaceName.equals(_ne.namespaceName)) {
   511                         return;
   512                     }
   513                 }
   514                 _ne = null;
   515             }
   516         };
   517     }
   518 }

mercurial