1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xml.internal.utils;
  22 
  23 import org.w3c.dom.Attr;
  24 import org.w3c.dom.NamedNodeMap;
  25 import org.w3c.dom.Node;
  26 
  27 import org.xml.sax.Attributes;
  28 
  29 /**
  30  * Wraps a DOM attribute list in a SAX Attributes.
  31  * @xsl.usage internal
  32  */
  33 public class AttList implements Attributes
  34 {
  35 
  36   /** List of attribute nodes          */
  37   NamedNodeMap m_attrs;
  38 
  39   /** Index of last attribute node          */
  40   int m_lastIndex;
  41 
  42   // JAXP Uses Xerces without setting the namespace processing to ON!
  43   // DOM2Helper m_dh = new DOM2Helper();
  44 
  45   /**
  46    * Constructor AttList
  47    *
  48    *
  49    * @param attrs List of attributes this will contain
  50    * @param dh DOMHelper
  51    */
  52   public AttList(NamedNodeMap attrs)
  53   {
  54     m_attrs = attrs;
  55     m_lastIndex = m_attrs.getLength() - 1;
  56   }
  57 
  58   /**
  59    * Get the number of attribute nodes in the list
  60    *
  61    *
  62    * @return number of attribute nodes
  63    */
  64   public int getLength()
  65   {
  66     return m_attrs.getLength();
  67   }
  68 
  69   /**
  70    * Look up an attribute's Namespace URI by index.
  71    *
  72    * @param index The attribute index (zero-based).
  73    * @return The Namespace URI, or the empty string if none
  74    *         is available, or null if the index is out of
  75    *         range.
  76    */
  77   public String getURI(int index)
  78   {
  79     String ns = DOM2Helper.getNamespaceOfNode(((Attr) m_attrs.item(index)));
  80     if(null == ns)
  81       ns = "";
  82     return ns;
  83   }
  84 
  85   /**
  86    * Look up an attribute's local name by index.
  87    *
  88    * @param index The attribute index (zero-based).
  89    * @return The local name, or the empty string if Namespace
  90    *         processing is not being performed, or null
  91    *         if the index is out of range.
  92    */
  93   public String getLocalName(int index)
  94   {
  95     return DOM2Helper.getLocalNameOfNode(((Attr) m_attrs.item(index)));
  96   }
  97 
  98   /**
  99    * Look up an attribute's qualified name by index.
 100    *
 101    *
 102    * @param i The attribute index (zero-based).
 103    *
 104    * @return The attribute's qualified name
 105    */
 106   public String getQName(int i)
 107   {
 108     return ((Attr) m_attrs.item(i)).getName();
 109   }
 110 
 111   /**
 112    * Get the attribute's node type by index
 113    *
 114    *
 115    * @param i The attribute index (zero-based)
 116    *
 117    * @return the attribute's node type
 118    */
 119   public String getType(int i)
 120   {
 121     return "CDATA";  // for the moment
 122   }
 123 
 124   /**
 125    * Get the attribute's node value by index
 126    *
 127    *
 128    * @param i The attribute index (zero-based)
 129    *
 130    * @return the attribute's node value
 131    */
 132   public String getValue(int i)
 133   {
 134     return ((Attr) m_attrs.item(i)).getValue();
 135   }
 136 
 137   /**
 138    * Get the attribute's node type by name
 139    *
 140    *
 141    * @param name Attribute name
 142    *
 143    * @return the attribute's node type
 144    */
 145   public String getType(String name)
 146   {
 147     return "CDATA";  // for the moment
 148   }
 149 
 150   /**
 151    * Look up an attribute's type by Namespace name.
 152    *
 153    * @param uri The Namespace URI, or the empty String if the
 154    *        name has no Namespace URI.
 155    * @param localName The local name of the attribute.
 156    * @return The attribute type as a string, or null if the
 157    *         attribute is not in the list or if Namespace
 158    *         processing is not being performed.
 159    */
 160   public String getType(String uri, String localName)
 161   {
 162     return "CDATA";  // for the moment
 163   }
 164 
 165   /**
 166    * Look up an attribute's value by name.
 167    *
 168    *
 169    * @param name The attribute node's name
 170    *
 171    * @return The attribute node's value
 172    */
 173   public String getValue(String name)
 174   {
 175     Attr attr = ((Attr) m_attrs.getNamedItem(name));
 176     return (null != attr)
 177           ? attr.getValue() : null;
 178   }
 179 
 180   /**
 181    * Look up an attribute's value by Namespace name.
 182    *
 183    * @param uri The Namespace URI, or the empty String if the
 184    *        name has no Namespace URI.
 185    * @param localName The local name of the attribute.
 186    * @return The attribute value as a string, or null if the
 187    *         attribute is not in the list.
 188    */
 189   public String getValue(String uri, String localName)
 190   {
 191                 Node a=m_attrs.getNamedItemNS(uri,localName);
 192                 return (a==null) ? null : a.getNodeValue();
 193   }
 194 
 195   /**
 196    * Look up the index of an attribute by Namespace name.
 197    *
 198    * @param uri The Namespace URI, or the empty string if
 199    *        the name has no Namespace URI.
 200    * @param localPart The attribute's local name.
 201    * @return The index of the attribute, or -1 if it does not
 202    *         appear in the list.
 203    */
 204   public int getIndex(String uri, String localPart)
 205   {
 206     for(int i=m_attrs.getLength()-1;i>=0;--i)
 207     {
 208       Node a=m_attrs.item(i);
 209       String u=a.getNamespaceURI();
 210       if( (u==null ? uri==null : u.equals(uri))
 211           &&
 212           a.getLocalName().equals(localPart) )
 213         return i;
 214     }
 215     return -1;
 216   }
 217 
 218   /**
 219    * Look up the index of an attribute by raw XML 1.0 name.
 220    *
 221    * @param qName The qualified (prefixed) name.
 222    * @return The index of the attribute, or -1 if it does not
 223    *         appear in the list.
 224    */
 225   public int getIndex(String qName)
 226   {
 227     for(int i=m_attrs.getLength()-1;i>=0;--i)
 228     {
 229       Node a=m_attrs.item(i);
 230       if(a.getNodeName().equals(qName) )
 231         return i;
 232     }
 233     return -1;
 234   }
 235 }