1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.util;
  23 
  24 
  25 import java.util.Collections;
  26 import java.util.Enumeration;
  27 import java.util.List;
  28 import java.util.TreeSet;
  29 import java.util.Vector;
  30 
  31 import javax.xml.XMLConstants;
  32 
  33 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  34 
  35 /**
  36  * <p>A read-only XNI wrapper around a JAXP NamespaceContext.</p>
  37  *
  38  * @author Michael Glavassevich, IBM
  39  *
  40  * @version $Id: JAXPNamespaceContextWrapper.java,v 1.2 2010-10-26 23:01:13 joehw Exp $
  41  */
  42 public final class JAXPNamespaceContextWrapper implements NamespaceContext {
  43 
  44     private javax.xml.namespace.NamespaceContext fNamespaceContext;
  45     private SymbolTable fSymbolTable;
  46     private List fPrefixes;
  47     private final Vector fAllPrefixes = new Vector();
  48 
  49     private int[] fContext = new int[8];
  50     private int fCurrentContext;
  51 
  52     public JAXPNamespaceContextWrapper(SymbolTable symbolTable) {
  53         setSymbolTable(symbolTable);
  54     }
  55 
  56     public void setNamespaceContext(javax.xml.namespace.NamespaceContext context) {
  57         fNamespaceContext = context;
  58     }
  59 
  60     public javax.xml.namespace.NamespaceContext getNamespaceContext() {
  61         return fNamespaceContext;
  62     }
  63 
  64     public void setSymbolTable(SymbolTable symbolTable) {
  65         fSymbolTable = symbolTable;
  66     }
  67 
  68     public SymbolTable getSymbolTable() {
  69         return fSymbolTable;
  70     }
  71 
  72     public void setDeclaredPrefixes(List prefixes) {
  73         fPrefixes = prefixes;
  74     }
  75 
  76     public List getDeclaredPrefixes() {
  77         return fPrefixes;
  78     }
  79 
  80     /*
  81      * NamespaceContext methods
  82      */
  83 
  84     public String getURI(String prefix) {
  85         if (fNamespaceContext != null) {
  86             String uri = fNamespaceContext.getNamespaceURI(prefix);
  87             if (uri != null && !XMLConstants.NULL_NS_URI.equals(uri)) {
  88                 return (fSymbolTable != null) ? fSymbolTable.addSymbol(uri) : uri.intern();
  89             }
  90         }
  91         return null;
  92     }
  93 
  94     public String getPrefix(String uri) {
  95         if (fNamespaceContext != null) {
  96             if (uri == null) {
  97                 uri = XMLConstants.NULL_NS_URI;
  98             }
  99             String prefix = fNamespaceContext.getPrefix(uri);
 100             if (prefix == null) {
 101                 prefix = XMLConstants.DEFAULT_NS_PREFIX;
 102             }
 103             return (fSymbolTable != null) ? fSymbolTable.addSymbol(prefix) : prefix.intern();
 104         }
 105         return null;
 106     }
 107 
 108     public Enumeration getAllPrefixes() {
 109         // There may be duplicate prefixes in the list so we
 110         // first transfer them to a set to ensure uniqueness.
 111         return Collections.enumeration(new TreeSet(fAllPrefixes));
 112     }
 113 
 114     public void pushContext() {
 115         // extend the array, if necessary
 116         if (fCurrentContext + 1 == fContext.length) {
 117             int[] contextarray = new int[fContext.length * 2];
 118             System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
 119             fContext = contextarray;
 120         }
 121         // push context
 122         fContext[++fCurrentContext] = fAllPrefixes.size();
 123         if (fPrefixes != null) {
 124             fAllPrefixes.addAll(fPrefixes);
 125         }
 126     }
 127 
 128     public void popContext() {
 129         fAllPrefixes.setSize(fContext[fCurrentContext--]);
 130     }
 131 
 132     public boolean declarePrefix(String prefix, String uri) {
 133         return true;
 134     }
 135 
 136     public int getDeclaredPrefixCount() {
 137         return (fPrefixes != null) ? fPrefixes.size() : 0;
 138     }
 139 
 140     public String getDeclaredPrefixAt(int index) {
 141         return (String) fPrefixes.get(index);
 142     }
 143 
 144     public void reset() {
 145         fCurrentContext = 0;
 146         fContext[fCurrentContext] = 0;
 147         fAllPrefixes.clear();
 148     }
 149 
 150 } // JAXPNamespaceContextWrapper