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.xml.internal.utils;
23
24 import java.io.Serializable;
25
26 import org.xml.sax.Attributes;
27 import org.xml.sax.helpers.AttributesImpl;
28
29 /**
30 * Mutable version of AttributesImpl.
31 * @xsl.usage advanced
32 */
33 public class MutableAttrListImpl extends AttributesImpl
34 implements Serializable
35 {
36 static final long serialVersionUID = 6289452013442934470L;
37
38 /**
39 * Construct a new, empty AttributesImpl object.
40 */
41
MutableAttrListImpl()42 public MutableAttrListImpl()
43 {
44 super();
45 }
46
47 /**
48 * Copy an existing Attributes object.
49 *
50 * <p>This constructor is especially useful inside a start
51 * element event.</p>
52 *
53 * @param atts The existing Attributes object.
54 */
MutableAttrListImpl(Attributes atts)55 public MutableAttrListImpl(Attributes atts)
56 {
57 super(atts);
58 }
59
60 /**
61 * Add an attribute to the end of the list.
62 *
63 * <p>For the sake of speed, this method does no checking
64 * to see if the attribute is already in the list: that is
65 * the responsibility of the application.</p>
66 *
67 * @param uri The Namespace URI, or the empty string if
68 * none is available or Namespace processing is not
69 * being performed.
70 * @param localName The local name, or the empty string if
71 * Namespace processing is not being performed.
72 * @param qName The qualified (prefixed) name, or the empty string
73 * if qualified names are not available.
74 * @param type The attribute type as a string.
75 * @param value The attribute value.
76 */
addAttribute(String uri, String localName, String qName, String type, String value)77 public void addAttribute(String uri, String localName, String qName,
78 String type, String value)
79 {
80
81 if (null == uri)
82 uri = "";
83
84 // getIndex(qName) seems to be more reliable than getIndex(uri, localName),
85 // in the case of the xmlns attribute anyway.
86 int index = this.getIndex(qName);
87 // int index = this.getIndex(uri, localName);
88
89 // System.out.println("MutableAttrListImpl#addAttribute: "+uri+":"+localName+", "+index+", "+qName+", "+this);
90
91 if (index >= 0)
92 this.setAttribute(index, uri, localName, qName, type, value);
93 else
94 super.addAttribute(uri, localName, qName, type, value);
95 }
96
97 /**
98 * Add the contents of the attribute list to this list.
99 *
100 * @param atts List of attributes to add to this list
101 */
addAttributes(Attributes atts)102 public void addAttributes(Attributes atts)
103 {
104
105 int nAtts = atts.getLength();
106
107 for (int i = 0; i < nAtts; i++)
108 {
109 String uri = atts.getURI(i);
110
111 if (null == uri)
112 uri = "";
113
114 String localName = atts.getLocalName(i);
115 String qname = atts.getQName(i);
116 int index = this.getIndex(uri, localName);
117 // System.out.println("MutableAttrListImpl#addAttributes: "+uri+":"+localName+", "+index+", "+atts.getQName(i)+", "+this);
118 if (index >= 0)
119 this.setAttribute(index, uri, localName, qname, atts.getType(i),
120 atts.getValue(i));
121 else
122 addAttribute(uri, localName, qname, atts.getType(i),
123 atts.getValue(i));
124 }
125 }
126
127 /**
128 * Return true if list contains the given (raw) attribute name.
129 *
130 * @param name Raw name of attribute to look for
131 *
132 * @return true if an attribute is found with this name
133 */
contains(String name)134 public boolean contains(String name)
135 {
136 return getValue(name) != null;
137 }
138 }
139
140 // end of MutableAttrListImpl.java
141