Source code: org/apache/batik/css/engine/sac/CSSConditionalSelector.java
1 /*
2
3 Copyright 2002 The Apache Software Foundation
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
18 package org.apache.batik.css.engine.sac;
19
20 import java.util.Set;
21
22 import org.w3c.css.sac.Condition;
23 import org.w3c.css.sac.ConditionalSelector;
24 import org.w3c.css.sac.SimpleSelector;
25 import org.w3c.dom.Element;
26
27 /**
28 * This class provides an implementation of the
29 * {@link org.w3c.css.sac.ConditionalSelector} interface.
30 *
31 * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
32 * @version $Id: CSSConditionalSelector.java,v 1.4 2004/08/18 07:12:51 vhardy Exp $
33 */
34 public class CSSConditionalSelector
35 implements ConditionalSelector,
36 ExtendedSelector {
37
38 /**
39 * The simple selector.
40 */
41 protected SimpleSelector simpleSelector;
42
43 /**
44 * The condition.
45 */
46 protected Condition condition;
47
48 /**
49 * Creates a new ConditionalSelector object.
50 */
51 public CSSConditionalSelector(SimpleSelector s, Condition c) {
52 simpleSelector = s;
53 condition = c;
54 }
55
56 /**
57 * Indicates whether some other object is "equal to" this one.
58 * @param obj the reference object with which to compare.
59 */
60 public boolean equals(Object obj) {
61 if (obj == null || !(obj.getClass() != getClass())) {
62 return false;
63 }
64 CSSConditionalSelector s = (CSSConditionalSelector)obj;
65 return s.simpleSelector.equals(simpleSelector) &&
66 s.condition.equals(condition);
67 }
68
69 /**
70 * <b>SAC</b>: Implements {@link
71 * org.w3c.css.sac.Selector#getSelectorType()}.
72 */
73 public short getSelectorType() {
74 return SAC_CONDITIONAL_SELECTOR;
75 }
76
77 /**
78 * Tests whether this selector matches the given element.
79 */
80 public boolean match(Element e, String pseudoE) {
81 return ((ExtendedSelector)getSimpleSelector()).match(e, pseudoE) &&
82 ((ExtendedCondition)getCondition()).match(e, pseudoE);
83 }
84
85 /**
86 * Fills the given set with the attribute names found in this selector.
87 */
88 public void fillAttributeSet(Set attrSet) {
89 ((ExtendedSelector)getSimpleSelector()).fillAttributeSet(attrSet);
90 ((ExtendedCondition)getCondition()).fillAttributeSet(attrSet);
91 }
92
93 /**
94 * Returns the specificity of this selector.
95 */
96 public int getSpecificity() {
97 return ((ExtendedSelector)getSimpleSelector()).getSpecificity() +
98 ((ExtendedCondition)getCondition()).getSpecificity();
99 }
100
101 /**
102 * <b>SAC</b>: Implements {@link
103 * org.w3c.css.sac.ConditionalSelector#getSimpleSelector()}.
104 */
105 public SimpleSelector getSimpleSelector() {
106 return simpleSelector;
107 }
108
109 /**
110 * <b>SAC</b>: Implements {@link
111 * org.w3c.css.sac.ConditionalSelector#getCondition()}.
112 */
113 public Condition getCondition() {
114 return condition;
115 }
116
117 /**
118 * Returns a representation of the selector.
119 */
120 public String toString() {
121 return "" + simpleSelector + condition;
122 }
123 }