1 /*
2 
3    Copyright 2002, 2015  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 
19 /* This class copied from org.apache.batik.css.engine.sac */
20 
21 package org.eclipse.e4.ui.css.core.impl.sac;
22 
23 import java.util.Set;
24 import org.w3c.dom.Element;
25 
26 /**
27  * This class provides an implementation of the
28  * {@link org.w3c.css.sac.AttributeCondition} interface.
29  */
30 public class CSSAttributeConditionImpl extends AbstractAttributeCondition {
31 	/**
32 	 * The attribute's local name.
33 	 */
34 	protected String localName;
35 
36 	/**
37 	 * The attribute's namespace URI.
38 	 */
39 	protected String namespaceURI;
40 
41 	/**
42 	 * Whether this condition applies to specified attributes.
43 	 */
44 	protected boolean specified;
45 
46 	/**
47 	 * Creates a new CSSAttributeCondition object.
48 	 */
CSSAttributeConditionImpl(String localName, String namespaceURI, boolean specified, String value)49 	public CSSAttributeConditionImpl(String localName, String namespaceURI,
50 			boolean specified, String value) {
51 		super(value);
52 		this.localName = localName;
53 		this.namespaceURI = namespaceURI;
54 		this.specified = specified;
55 	}
56 
57 	/**
58 	 * Indicates whether some other object is "equal to" this one.
59 	 *
60 	 * @param obj
61 	 *            the reference object with which to compare.
62 	 */
63 	@Override
equals(Object obj)64 	public boolean equals(Object obj) {
65 		if (!super.equals(obj)) {
66 			return false;
67 		}
68 		CSSAttributeConditionImpl c = (CSSAttributeConditionImpl) obj;
69 		return (c.namespaceURI.equals(namespaceURI)
70 				&& c.localName.equals(localName) && c.specified == specified);
71 	}
72 
73 	/**
74 	 * equal objects should have equal hashCodes.
75 	 *
76 	 * @return hashCode of this CSSAttributeCondition
77 	 */
78 	@Override
hashCode()79 	public int hashCode() {
80 		return namespaceURI.hashCode() ^ localName.hashCode()
81 				^ Boolean.hashCode(specified);
82 	}
83 
84 	/**
85 	 * <b>SAC</b>: Implements {@link
86 	 * org.w3c.css.sac.Condition#getConditionType()}.
87 	 */
88 	@Override
getConditionType()89 	public short getConditionType() {
90 		return SAC_ATTRIBUTE_CONDITION;
91 	}
92 
93 	/**
94 	 * <b>SAC</b>: Implements {@link
95 	 * org.w3c.css.sac.AttributeCondition#getNamespaceURI()}.
96 	 */
97 	@Override
getNamespaceURI()98 	public String getNamespaceURI() {
99 		return namespaceURI;
100 	}
101 
102 	/**
103 	 * <b>SAC</b>: Implements {@link
104 	 * org.w3c.css.sac.AttributeCondition#getLocalName()}.
105 	 */
106 	@Override
getLocalName()107 	public String getLocalName() {
108 		return localName;
109 	}
110 
111 	/**
112 	 * <b>SAC</b>: Implements {@link
113 	 * org.w3c.css.sac.AttributeCondition#getSpecified()}.
114 	 */
115 	@Override
getSpecified()116 	public boolean getSpecified() {
117 		return specified;
118 	}
119 
120 	/**
121 	 * Tests whether this condition matches the given element.
122 	 */
123 	@Override
match(Element e, String pseudoE)124 	public boolean match(Element e, String pseudoE) {
125 		if (!e.hasAttribute(getLocalName())) {
126 			return false;
127 		}
128 		String val = getValue();
129 		if (val == null) {
130 			return true;
131 		}
132 		return e.getAttribute(getLocalName()).equals(val);
133 	}
134 
135 	/**
136 	 * Fills the given set with the attribute names found in this selector.
137 	 */
138 	@Override
fillAttributeSet(Set<String> attrSet)139 	public void fillAttributeSet(Set<String> attrSet) {
140 		attrSet.add(localName);
141 	}
142 
143 	/**
144 	 * Returns a text representation of this object.
145 	 */
146 	@Override
toString()147 	public String toString() {
148 		if (value == null) {
149 			return '[' + localName + ']';
150 		}
151 		return '[' + localName + "=\"" + value + "\"]";
152 	}
153 }
154