|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xerces.internal.util; |
|
|
|
import com.sun.org.apache.xerces.internal.xni.NamespaceContext; |
|
import java.util.ArrayList; |
|
import java.util.Enumeration; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.NoSuchElementException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class NamespaceSupport implements NamespaceContext { |
|
|
|
// |
|
// Data |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected String[] fNamespace = new String[16 * 2]; |
|
|
|
|
|
protected int fNamespaceSize; |
|
|
|
// NOTE: The constructor depends on the initial context size |
|
// being at least 1. -Ac |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected int[] fContext = new int[8]; |
|
|
|
|
|
protected int fCurrentContext; |
|
|
|
protected String[] fPrefixes = new String[16]; |
|
|
|
// |
|
// Constructors |
|
// |
|
|
|
|
|
public NamespaceSupport() { |
|
} // <init>() |
|
|
|
|
|
|
|
|
|
*/ |
|
public NamespaceSupport(NamespaceContext context) { |
|
pushContext(); |
|
|
|
Enumeration<String> prefixes = context.getAllPrefixes(); |
|
while (prefixes.hasMoreElements()){ |
|
String prefix = prefixes.nextElement(); |
|
String uri = context.getURI(prefix); |
|
declarePrefix(prefix, uri); |
|
} |
|
} // <init>(NamespaceContext) |
|
|
|
|
|
// |
|
// Public methods |
|
// |
|
|
|
|
|
|
|
*/ |
|
public void reset() { |
|
|
|
|
|
fNamespaceSize = 0; |
|
fCurrentContext = 0; |
|
|
|
|
|
|
|
fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML; |
|
fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI; |
|
|
|
fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS; |
|
fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI; |
|
|
|
fContext[fCurrentContext] = fNamespaceSize; |
|
//++fCurrentContext; |
|
|
|
} // reset(SymbolTable) |
|
|
|
|
|
|
|
|
|
*/ |
|
public void pushContext() { |
|
|
|
|
|
if (fCurrentContext + 1 == fContext.length) { |
|
int[] contextarray = new int[fContext.length * 2]; |
|
System.arraycopy(fContext, 0, contextarray, 0, fContext.length); |
|
fContext = contextarray; |
|
} |
|
|
|
|
|
fContext[++fCurrentContext] = fNamespaceSize; |
|
//System.out.println("calling push context, current context = " + fCurrentContext); |
|
} // pushContext() |
|
|
|
|
|
|
|
|
|
*/ |
|
public void popContext() { |
|
fNamespaceSize = fContext[fCurrentContext--]; |
|
//System.out.println("Calling popContext, fCurrentContext = " + fCurrentContext); |
|
} // popContext() |
|
|
|
|
|
|
|
*/ |
|
public boolean declarePrefix(String prefix, String uri) { |
|
|
|
if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) { |
|
return false; |
|
} |
|
|
|
|
|
for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) { |
|
if (fNamespace[i - 2] == prefix) { |
|
// REVISIT: [Q] Should the new binding override the |
|
// previously declared binding or should it |
|
// it be ignored? -Ac |
|
// NOTE: The SAX2 "NamespaceSupport" helper allows |
|
// re-bindings with the new binding overwriting |
|
|
|
fNamespace[i - 1] = uri; |
|
return true; |
|
} |
|
} |
|
|
|
|
|
if (fNamespaceSize == fNamespace.length) { |
|
String[] namespacearray = new String[fNamespaceSize * 2]; |
|
System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize); |
|
fNamespace = namespacearray; |
|
} |
|
|
|
|
|
fNamespace[fNamespaceSize++] = prefix; |
|
fNamespace[fNamespaceSize++] = uri; |
|
|
|
return true; |
|
|
|
} // declarePrefix(String,String):boolean |
|
|
|
|
|
|
|
*/ |
|
public String getURI(String prefix) { |
|
|
|
|
|
for (int i = fNamespaceSize; i > 0; i -= 2) { |
|
if (fNamespace[i - 2] == prefix) { |
|
return fNamespace[i - 1]; |
|
} |
|
} |
|
|
|
|
|
return null; |
|
|
|
} // getURI(String):String |
|
|
|
|
|
|
|
|
|
*/ |
|
public String getPrefix(String uri) { |
|
|
|
|
|
for (int i = fNamespaceSize; i > 0; i -= 2) { |
|
if (fNamespace[i - 1] == uri) { |
|
if (getURI(fNamespace[i - 2]) == uri) |
|
return fNamespace[i - 2]; |
|
} |
|
} |
|
|
|
|
|
return null; |
|
|
|
} // getPrefix(String):String |
|
|
|
|
|
|
|
*/ |
|
public int getDeclaredPrefixCount() { |
|
return (fNamespaceSize - fContext[fCurrentContext]) / 2; |
|
} // getDeclaredPrefixCount():int |
|
|
|
|
|
|
|
*/ |
|
public String getDeclaredPrefixAt(int index) { |
|
return fNamespace[fContext[fCurrentContext] + index * 2]; |
|
} // getDeclaredPrefixAt(int):String |
|
|
|
public Iterator<String> getPrefixes(){ |
|
int count = 0; |
|
if (fPrefixes.length < (fNamespace.length/2)) { |
|
|
|
String[] prefixes = new String[fNamespaceSize]; |
|
fPrefixes = prefixes; |
|
} |
|
String prefix = null; |
|
boolean unique = true; |
|
for (int i = 2; i < (fNamespaceSize-2); i += 2) { |
|
prefix = fNamespace[i + 2]; |
|
for (int k=0;k<count;k++){ |
|
if (fPrefixes[k]==prefix){ |
|
unique = false; |
|
break; |
|
} |
|
} |
|
if (unique){ |
|
fPrefixes[count++] = prefix; |
|
} |
|
unique = true; |
|
} |
|
return new IteratorPrefixes(fPrefixes, count); |
|
}//getPrefixes |
|
|
|
|
|
*/ |
|
public Enumeration<String> getAllPrefixes() { |
|
int count = 0; |
|
if (fPrefixes.length < (fNamespace.length/2)) { |
|
|
|
String[] prefixes = new String[fNamespaceSize]; |
|
fPrefixes = prefixes; |
|
} |
|
String prefix = null; |
|
boolean unique = true; |
|
for (int i = 2; i < (fNamespaceSize-2); i += 2) { |
|
prefix = fNamespace[i + 2]; |
|
for (int k=0;k<count;k++){ |
|
if (fPrefixes[k]==prefix){ |
|
unique = false; |
|
break; |
|
} |
|
} |
|
if (unique){ |
|
fPrefixes[count++] = prefix; |
|
} |
|
unique = true; |
|
} |
|
return new Prefixes(fPrefixes, count); |
|
} |
|
|
|
public List<String> getPrefixes(String uri){ |
|
int count = 0; |
|
String prefix = null; |
|
boolean unique = true; |
|
List<String> prefixList = new ArrayList<>(); |
|
for (int i = fNamespaceSize; i >0 ; i -= 2) { |
|
if(fNamespace[i-1] == uri){ |
|
if(!prefixList.contains(fNamespace[i-2])) |
|
prefixList.add(fNamespace[i-2]); |
|
} |
|
} |
|
return prefixList; |
|
} |
|
|
|
/* |
|
* non-NamespaceContext methods |
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean containsPrefix(String prefix) { |
|
|
|
|
|
for (int i = fNamespaceSize; i > 0; i -= 2) { |
|
if (fNamespace[i - 2] == prefix) { |
|
return true; |
|
} |
|
} |
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean containsPrefixInCurrentContext(String prefix) { |
|
|
|
|
|
for (int i = fContext[fCurrentContext]; i < fNamespaceSize; i += 2) { |
|
if (fNamespace[i] == prefix) { |
|
return true; |
|
} |
|
} |
|
|
|
|
|
return false; |
|
} |
|
|
|
protected final class IteratorPrefixes implements Iterator<String> { |
|
private String[] prefixes; |
|
private int counter = 0; |
|
private int size = 0; |
|
|
|
|
|
|
|
*/ |
|
public IteratorPrefixes(String [] prefixes, int size) { |
|
this.prefixes = prefixes; |
|
this.size = size; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean hasNext() { |
|
return (counter < size); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String next() { |
|
if (counter< size){ |
|
return fPrefixes[counter++]; |
|
} |
|
throw new NoSuchElementException("Illegal access to Namespace prefixes enumeration."); |
|
} |
|
|
|
public String toString(){ |
|
StringBuilder buf = new StringBuilder(); |
|
for (int i=0;i<size;i++){ |
|
buf.append(prefixes[i]); |
|
buf.append(" "); |
|
} |
|
|
|
return buf.toString(); |
|
} |
|
|
|
public void remove(){ |
|
throw new UnsupportedOperationException(); |
|
} |
|
} |
|
|
|
|
|
protected final class Prefixes implements Enumeration<String> { |
|
private String[] prefixes; |
|
private int counter = 0; |
|
private int size = 0; |
|
|
|
|
|
|
|
*/ |
|
public Prefixes(String [] prefixes, int size) { |
|
this.prefixes = prefixes; |
|
this.size = size; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean hasMoreElements() { |
|
return (counter< size); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String nextElement() { |
|
if (counter< size){ |
|
return fPrefixes[counter++]; |
|
} |
|
throw new NoSuchElementException("Illegal access to Namespace prefixes enumeration."); |
|
} |
|
|
|
public String toString(){ |
|
StringBuilder buf = new StringBuilder(); |
|
for (int i=0;i<size;i++){ |
|
buf.append(prefixes[i]); |
|
buf.append(" "); |
|
} |
|
|
|
return buf.toString(); |
|
} |
|
|
|
|
|
} |
|
|
|
} // class NamespaceSupport |