/*
 * Copyright (c) 2007 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package org.whattf.checker;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class UsemapChecker extends Checker {

    private final Map<String, Locator> usemapLocationsById = new LinkedHashMap<String, Locator>();
    
    private final Set<String> mapIds = new HashSet<String>();
    
    private Locator locator = null;
    
    public UsemapChecker() {
    }

     /**
     * @see org.whattf.checker.Checker#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if ("http://www.w3.org/1999/xhtml" == uri) {
            if ("map" == localName) {
                String id = atts.getValue("", "id");
                if (id != null && !"".equals(id)) {
                    mapIds.add(id);
                }
                String name = atts.getValue("", "id");
                if (name != null && !"".equals(name)) {
                    mapIds.add(name);
                }
            } else if ("img" == localName || "object" == localName) {
                String usemap = atts.getValue("", "usemap");
                if (usemap != null) {
                    int hashIndex = usemap.indexOf('#');
                    if (hashIndex > -1) {
                        // XXX not complaining about bad values here as 
                        // the schema takes care of that.
                        if (hashIndex < usemap.length() - 1) {
                            String ref = usemap.substring(hashIndex + 1);
                            usemapLocationsById.put(ref, new LocatorImpl(locator));
                        }
                    }
                }
            }
        }
    }

    /**
     * @see org.xml.sax.helpers.XMLFilterImpl#endDocument()
     */
    @Override
    public void endDocument() throws SAXException {
        for (Map.Entry<String, Locator> entry : usemapLocationsById.entrySet()) {
            if (!mapIds.contains(entry.getKey())) {
                err("The hashed ID reference in attribute \u201Cusemap\u201D referred to \u201C" + entry.getKey() + "\u201D, but there is no \u201Cmap\u201D element with that \u201Cid\u201D or \u201Cname\u201D.", entry.getValue());
            }
        }
        usemapLocationsById.clear();
        mapIds.clear();
    }

    /**
     * @see org.xml.sax.helpers.XMLFilterImpl#startDocument()
     */
    @Override
    public void startDocument() throws SAXException {
        usemapLocationsById.clear();
        mapIds.clear();
    }

    /**
     * @see org.xml.sax.helpers.XMLFilterImpl#setDocumentLocator(org.xml.sax.Locator)
     */
    @Override
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    
}

@@ -1162,34 +1162,8 @@
 r;%0A%0A
-import java.util.HashMap;%0A
 impo
@@ -1456,18 +1456,20 @@
 ationsBy
-Id
+Name
  = new L
@@ -1539,18 +1539,20 @@
 ing%3E map
-Id
+Name
 s = new 
@@ -2039,18 +2039,20 @@
  String 
-id
+name
  = atts.
@@ -2069,171 +2069,12 @@
 %22, %22
-id%22);%0A                if (id != null && !%22%22.equals(id)) %7B%0A                    mapIds.add(id);%0A                %7D%0A                String name = atts.getValue(%22%22, %22id
+name
 %22);%0A
@@ -2152,18 +2152,20 @@
      map
-Id
+Name
 s.add(na
@@ -2768,18 +2768,20 @@
 ationsBy
-Id
+Name
 .put(ref
@@ -3113,18 +3113,20 @@
 ationsBy
-Id
+Name
 .entrySe
@@ -3152,18 +3152,20 @@
 if (!map
-Id
+Name
 s.contai
@@ -3219,13 +3219,13 @@
 hash
-ed ID
+-name
  ref
@@ -3360,47 +3360,52 @@
 ith 
-that
+a
  %5Cu201C
-id
+name
 %5Cu201D 
-or %5Cu201Cname%5Cu201D
+attribute with that value
 .%22, 
@@ -3464,34 +3464,36 @@
 semapLocationsBy
-Id
+Name
 .clear();%0A      
@@ -3489,34 +3489,36 @@
 r();%0A        map
-Id
+Name
 s.clear();%0A    %7D
@@ -3690,18 +3690,20 @@
 ationsBy
-Id
+Name
 .clear()
@@ -3719,10 +3719,12 @@
  map
-Id
+Name
 s.cl

Make usemap checking look at names (b=243)