01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.css.engine.sac;
20:
21: import org.apache.batik.css.engine.CSSStylableElement;
22: import org.w3c.dom.Element;
23:
24: /**
25: * This class provides an implementation of the
26: * {@link org.w3c.css.sac.AttributeCondition} interface.
27: *
28: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
29: * @version $Id: CSSClassCondition.java 478160 2006-11-22 13:35:06Z dvholten $
30: */
31: public class CSSClassCondition extends CSSAttributeCondition {
32:
33: /**
34: * Creates a new CSSAttributeCondition object.
35: */
36: public CSSClassCondition(String localName, String namespaceURI,
37: String value) {
38: super (localName, namespaceURI, true, value);
39: }
40:
41: /**
42: * <b>SAC</b>: Implements {@link
43: * org.w3c.css.sac.Condition#getConditionType()}.
44: */
45: public short getConditionType() {
46: return SAC_CLASS_CONDITION;
47: }
48:
49: /**
50: * Tests whether this condition matches the given element.
51: */
52: public boolean match(Element e, String pseudoE) {
53: if (!(e instanceof CSSStylableElement))
54: return false; // Can't match an unstylable element.
55: String attr = ((CSSStylableElement) e).getCSSClass();
56: String val = getValue();
57: int i = attr.indexOf(val);
58: if (i == -1) {
59: return false;
60: }
61: if (i != 0 && !Character.isSpaceChar(attr.charAt(i - 1))) {
62: return false;
63: }
64: int j = i + val.length();
65: return (j == attr.length() || (j < attr.length() && Character
66: .isSpaceChar(attr.charAt(j))));
67: }
68:
69: /**
70: * Returns a text representation of this object.
71: */
72: public String toString() {
73: return '.' + getValue();
74: }
75: }
|