/*

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package org.apache.batik.css.engine.sac;

import org.apache.batik.css.engine.CSSStylableElement;
import org.w3c.dom.Element;

/**
 * This class provides an implementation of the
 * {@link org.w3c.css.sac.AttributeCondition} interface.
 *
 * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
 * @version $Id$
 */
public class CSSClassCondition extends CSSAttributeCondition {

    /**
     * Creates a new CSSAttributeCondition object.
     */
    public CSSClassCondition(String localName,
                             String namespaceURI,
                             String value) {
        super(localName, namespaceURI, true, value);
    }

    /**
     * <b>SAC</b>: Implements {@link
     * org.w3c.css.sac.Condition#getConditionType()}.
     */
    public short getConditionType() {
        return SAC_CLASS_CONDITION;
    }

    /**
     * Tests whether this condition matches the given element.
     */
    public boolean match(Element e, String pseudoE) {
        if (!(e instanceof CSSStylableElement))
            return false;  // Can't match an unstylable element.
        String attr = ((CSSStylableElement)e).getCSSClass();
        String val = getValue();
        int i = attr.indexOf(val);
        if (i == -1) {
            return false;
        }
        if (i != 0 && !Character.isSpaceChar(attr.charAt(i - 1))) {
            return false;
        }
        int j = i + val.length();
        return (j == attr.length() ||
                (j < attr.length() && Character.isSpaceChar(attr.charAt(j))));
    }

    /**
     * Returns a text representation of this object.
     */
    public String toString() {
        return '.' + getValue();
    }
}

@@ -1999,17 +1999,23 @@
 int 
-i
+attrLen
  = attr.
 inde
@@ -2014,19 +2014,15 @@
 ttr.
-indexOf(val
+length(
 );%0A 
@@ -2033,66 +2033,107 @@
    i
-f (i == -1) %7B%0A            return false;%0A        %7D%0A
+nt valLen = val.length();%0A%0A        int i = attr.indexOf(val);%0A        while (i != -1) %7B%0A   
         
 if (
@@ -2132,23 +2132,23 @@
     
+ 
 if (i 
-!
+=
 = 0 
-&& !
+%7C%7C 
 Char
@@ -2200,29 +2200,48 @@
         
-return false;
+    if (i + valLen == attrLen %7C%7C
 %0A       
@@ -2245,18 +2245,16 @@
     
-%7D%0A
         
 int 
@@ -2253,71 +2253,116 @@
     
-int j = i + val.length();%0A        return (j == attr.length() %7C%7C
+        Character.isSpaceChar(attr.charAt(i + valLen))) %7B%0A                    return true;%0A                %7D
 %0A   
@@ -2372,75 +2372,89 @@
     
+  %7D%0A
       
-(j %3C attr.length() && Character.isSpaceChar(attr.charAt(j))))
+      i = attr.indexOf(val, i + valLen);%0A        %7D%0A        return false
 ;%0A  

Fix bug in CSS class name matching.