1 /* MultiAttributeSet.java -- Multiplexes between a set of AttributeSets
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text.html;
41 import java.util.Enumeration;
42 import java.util.NoSuchElementException;
44 import javax.swing.text.AttributeSet;
45 import javax.swing.text.SimpleAttributeSet;
48 * An AttributeSet impl that multiplexes between a set of other AttributeSets.
50 * @author Roman Kennke (kennke@aicas.com)
52 class MultiAttributeSet
53 implements AttributeSet
57 * The Enumeration for the multiplexed names.
59 private class MultiNameEnumeration
60 implements Enumeration
63 * The index of the current AttributeSet.
68 * The names Enumeration of the current AttributeSet.
70 private Enumeration current;
73 * Creates a new instance.
75 MultiNameEnumeration()
78 current = multi[0].getAttributeNames();
81 public boolean hasMoreElements()
83 return current.hasMoreElements() || index < multi.length - 1;
86 public Object nextElement()
88 if (! current.hasMoreElements())
90 if (index < multi.length - 1)
93 current = multi[index].getAttributeNames();
96 throw new NoSuchElementException();
98 return current.nextElement();
104 * The AttributeSets to multiplex.
106 AttributeSet[] multi;
109 * Provided for subclasses that need to initialize via {@link #init}.
113 // Nothing to do here.
117 * Creates a new instance.
119 * @param m the AttributeSets to multiplex
121 MultiAttributeSet(AttributeSet[] m)
127 * Provided for subclasses to initialize the attribute set.
129 * @param m the attributes to multiplex
131 void init(AttributeSet[] m)
136 public boolean containsAttribute(Object name, Object value)
139 for (int i = 0; i < multi.length && ret == false; i++)
141 if (multi[i].containsAttribute(name, value))
147 public boolean containsAttributes(AttributeSet attributes)
150 Enumeration e = attributes.getAttributeNames();
151 while (ret && e.hasMoreElements())
153 Object key = e.nextElement();
154 ret = attributes.getAttribute(key).equals(getAttribute(key));
159 public AttributeSet copyAttributes()
161 SimpleAttributeSet copy = new SimpleAttributeSet();
162 for (int i = 0; i < multi.length; i++)
164 copy.addAttributes(multi[i]);
169 public Object getAttribute(Object key)
172 for (int i = 0; i < multi.length && ret == null; i++)
174 ret = multi[i].getAttribute(key);
179 public int getAttributeCount()
182 for (int i = 0; i < multi.length; i++)
184 n += multi[i].getAttributeCount();
189 public Enumeration getAttributeNames()
191 return new MultiNameEnumeration();
194 public AttributeSet getResolveParent()
199 public boolean isDefined(Object attrName)
202 for (int i = 0; i < multi.length && ! ret; i++)
203 ret = multi[i].isDefined(attrName);
207 public boolean isEqual(AttributeSet attr)
209 return getAttributeCount() == attr.getAttributeCount()
210 && containsAttributes(attr);