Home | History | Annotate | Download | only in utils
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: PrefixResolverDefault.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xml.utils;
     22 
     23 import org.w3c.dom.NamedNodeMap;
     24 import org.w3c.dom.Node;
     25 
     26 /**
     27  * This class implements a generic PrefixResolver that
     28  * can be used to perform prefix-to-namespace lookup
     29  * for the XPath object.
     30  * @xsl.usage general
     31  */
     32 public class PrefixResolverDefault implements PrefixResolver
     33 {
     34 
     35   /**
     36    * The context to resolve the prefix from, if the context
     37    * is not given.
     38    */
     39   Node m_context;
     40 
     41   /**
     42    * Construct a PrefixResolverDefault object.
     43    * @param xpathExpressionContext The context from
     44    * which XPath expression prefixes will be resolved.
     45    * Warning: This will not work correctly if xpathExpressionContext
     46    * is an attribute node.
     47    */
     48   public PrefixResolverDefault(Node xpathExpressionContext)
     49   {
     50     m_context = xpathExpressionContext;
     51   }
     52 
     53   /**
     54    * Given a namespace, get the corrisponding prefix.  This assumes that
     55    * the PrevixResolver hold's it's own namespace context, or is a namespace
     56    * context itself.
     57    * @param prefix Prefix to resolve.
     58    * @return Namespace that prefix resolves to, or null if prefix
     59    * is not bound.
     60    */
     61   public String getNamespaceForPrefix(String prefix)
     62   {
     63     return getNamespaceForPrefix(prefix, m_context);
     64   }
     65 
     66   /**
     67    * Given a namespace, get the corrisponding prefix.
     68    * Warning: This will not work correctly if namespaceContext
     69    * is an attribute node.
     70    * @param prefix Prefix to resolve.
     71    * @param namespaceContext Node from which to start searching for a
     72    * xmlns attribute that binds a prefix to a namespace.
     73    * @return Namespace that prefix resolves to, or null if prefix
     74    * is not bound.
     75    */
     76   public String getNamespaceForPrefix(String prefix,
     77                                       org.w3c.dom.Node namespaceContext)
     78   {
     79 
     80     Node parent = namespaceContext;
     81     String namespace = null;
     82 
     83     if (prefix.equals("xml"))
     84     {
     85       namespace = Constants.S_XMLNAMESPACEURI;
     86     }
     87     else
     88     {
     89       int type;
     90 
     91       while ((null != parent) && (null == namespace)
     92              && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
     93                  || (type == Node.ENTITY_REFERENCE_NODE)))
     94       {
     95         if (type == Node.ELEMENT_NODE)
     96         {
     97                 if (parent.getNodeName().indexOf(prefix+":") == 0)
     98                         return parent.getNamespaceURI();
     99           NamedNodeMap nnm = parent.getAttributes();
    100 
    101           for (int i = 0; i < nnm.getLength(); i++)
    102           {
    103             Node attr = nnm.item(i);
    104             String aname = attr.getNodeName();
    105             boolean isPrefix = aname.startsWith("xmlns:");
    106 
    107             if (isPrefix || aname.equals("xmlns"))
    108             {
    109               int index = aname.indexOf(':');
    110               String p = isPrefix ? aname.substring(index + 1) : "";
    111 
    112               if (p.equals(prefix))
    113               {
    114                 namespace = attr.getNodeValue();
    115 
    116                 break;
    117               }
    118             }
    119           }
    120         }
    121 
    122         parent = parent.getParentNode();
    123       }
    124     }
    125 
    126     return namespace;
    127   }
    128 
    129   /**
    130    * Return the base identifier.
    131    *
    132    * @return null
    133    */
    134   public String getBaseIdentifier()
    135   {
    136     return null;
    137   }
    138 	/**
    139 	 * @see PrefixResolver#handlesNullPrefixes()
    140 	 */
    141 	public boolean handlesNullPrefixes() {
    142 		return false;
    143 	}
    144 
    145 }
    146