001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: MutableAttrListImpl.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    import java.io.Serializable;
024    
025    import org.xml.sax.Attributes;
026    import org.xml.sax.helpers.AttributesImpl;
027    
028    /**
029     * Mutable version of AttributesImpl.
030     * @xsl.usage advanced
031     */
032    public class MutableAttrListImpl extends AttributesImpl
033            implements Serializable
034    {
035        static final long serialVersionUID = 6289452013442934470L;
036    
037    /**
038     * Construct a new, empty AttributesImpl object.
039     */
040    
041    public MutableAttrListImpl()
042      {
043        super();
044      }
045    
046      /**
047       * Copy an existing Attributes object.
048       *
049       * <p>This constructor is especially useful inside a start
050       * element event.</p>
051       *
052       * @param atts The existing Attributes object.
053       */
054      public MutableAttrListImpl(Attributes atts)
055      {
056        super(atts);
057      }
058    
059      /**
060       * Add an attribute to the end of the list.
061       *
062       * <p>For the sake of speed, this method does no checking
063       * to see if the attribute is already in the list: that is
064       * the responsibility of the application.</p>
065       *
066       * @param uri The Namespace URI, or the empty string if
067       *        none is available or Namespace processing is not
068       *        being performed.
069       * @param localName The local name, or the empty string if
070       *        Namespace processing is not being performed.
071       * @param qName The qualified (prefixed) name, or the empty string
072       *        if qualified names are not available.
073       * @param type The attribute type as a string.
074       * @param value The attribute value.
075       */
076      public void addAttribute(String uri, String localName, String qName,
077                               String type, String value)
078      {
079    
080        if (null == uri)
081          uri = "";
082    
083        // getIndex(qName) seems to be more reliable than getIndex(uri, localName), 
084        // in the case of the xmlns attribute anyway.
085        int index = this.getIndex(qName);
086        // int index = this.getIndex(uri, localName);
087       
088        // System.out.println("MutableAttrListImpl#addAttribute: "+uri+":"+localName+", "+index+", "+qName+", "+this);
089    
090        if (index >= 0)
091          this.setAttribute(index, uri, localName, qName, type, value);
092        else
093          super.addAttribute(uri, localName, qName, type, value);
094      }
095    
096      /**
097       * Add the contents of the attribute list to this list.
098       *
099       * @param atts List of attributes to add to this list
100       */
101      public void addAttributes(Attributes atts)
102      {
103    
104        int nAtts = atts.getLength();
105    
106        for (int i = 0; i < nAtts; i++)
107        {
108          String uri = atts.getURI(i);
109    
110          if (null == uri)
111            uri = "";
112    
113          String localName = atts.getLocalName(i);
114          String qname = atts.getQName(i);
115          int index = this.getIndex(uri, localName);
116          // System.out.println("MutableAttrListImpl#addAttributes: "+uri+":"+localName+", "+index+", "+atts.getQName(i)+", "+this);
117          if (index >= 0)
118            this.setAttribute(index, uri, localName, qname, atts.getType(i),
119                              atts.getValue(i));
120          else
121            addAttribute(uri, localName, qname, atts.getType(i),
122                         atts.getValue(i));
123        }
124      }
125    
126      /**
127       * Return true if list contains the given (raw) attribute name.
128       *
129       * @param name Raw name of attribute to look for 
130       *
131       * @return true if an attribute is found with this name
132       */
133      public boolean contains(String name)
134      {
135        return getValue(name) != null;
136      }
137    }
138    
139    // end of MutableAttrListImpl.java