001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.util.Enumeration;
023:
024: import javax.swing.text.AttributeSet;
025:
026: class CompositeAttributeSet implements AttributeSet {
027:
028: private final AttributeSet primarySet;
029: private final AttributeSet secondarySet;
030:
031: CompositeAttributeSet(final AttributeSet primarySet,
032: final AttributeSet secondarySet) {
033: this .primarySet = primarySet;
034: this .secondarySet = secondarySet;
035: }
036:
037: public boolean containsAttribute(final Object key,
038: final Object value) {
039: return primarySet.containsAttribute(key, value)
040: || secondarySet.containsAttribute(key, value);
041: }
042:
043: public boolean containsAttributes(final AttributeSet attrSet) {
044: boolean result = true;
045: final Enumeration keys = attrSet.getAttributeNames();
046: while (keys.hasMoreElements() && result) {
047: Object key = keys.nextElement();
048: result = containsAttribute(key, attrSet.getAttribute(key));
049: }
050:
051: return result;
052: }
053:
054: public AttributeSet copyAttributes() {
055: return this ;
056: }
057:
058: public Object getAttribute(final Object key) {
059: Object v = primarySet.getAttribute(key);
060: return v != null ? v : secondarySet.getAttribute(key);
061: }
062:
063: public int getAttributeCount() {
064: return primarySet.getAttributeCount()
065: + secondarySet.getAttributeCount();
066: }
067:
068: public Enumeration getAttributeNames() {
069: return new Enumeration() {
070: private final Enumeration primaryNames = primarySet
071: .getAttributeNames();
072: private final Enumeration secondaryNames = secondarySet
073: .getAttributeNames();
074:
075: public boolean hasMoreElements() {
076: return primaryNames.hasMoreElements()
077: || secondaryNames.hasMoreElements();
078: }
079:
080: public Object nextElement() {
081: return primaryNames.hasMoreElements() ? primaryNames
082: .nextElement() : secondaryNames.nextElement();
083: }
084: };
085: }
086:
087: public AttributeSet getResolveParent() {
088: return (AttributeSet) getAttribute(AttributeSet.ResolveAttribute);
089: }
090:
091: public boolean isDefined(final Object key) {
092: return primarySet.isDefined(key) || secondarySet.isDefined(key);
093: }
094:
095: public boolean isEqual(final AttributeSet attrSet) {
096: if (getAttributeCount() != attrSet.getAttributeCount()) {
097: return false;
098: }
099: return containsAttributes(attrSet);
100: }
101:
102: protected final AttributeSet getPrimarySet() {
103: return primarySet;
104: }
105:
106: protected final AttributeSet getSecondarySet() {
107: return secondarySet;
108: }
109: }
|