/**
 * Copyright (C) 2010 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.analysis;

import org.dom4j.Document;
import org.dom4j.Element;
import org.junit.Test;
import org.orbeon.oxf.test.ResourceManagerTestBase;
import org.orbeon.oxf.util.IndentedLogger;
import org.orbeon.oxf.util.XPathCache;
import org.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.XFormsStaticStateImpl;
import org.orbeon.oxf.xforms.XFormsUtils;
import org.orbeon.oxf.xforms.processor.XFormsServer;
import org.orbeon.oxf.xforms.xbl.XBLBindings;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.oxf.xml.dom4j.Dom4jUtils;
import org.orbeon.saxon.dom4j.DocumentWrapper;
import org.orbeon.saxon.dom4j.NodeWrapper;

import java.util.HashMap;
import java.util.List;

import static junit.framework.Assert.*;

public class XFormsAnnotatorContentHandlerTest extends ResourceManagerTestBase {

    public static final HashMap<String, String> BASIC_NAMESPACE_MAPPINGS = new HashMap<String, String>();
    static {
        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XFORMS_PREFIX, XFormsConstants.XFORMS_NAMESPACE_URI);
        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XXFORMS_PREFIX, XFormsConstants.XXFORMS_NAMESPACE_URI);
        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XML_EVENTS_PREFIX, XFormsConstants.XML_EVENTS_NAMESPACE_URI);
        BASIC_NAMESPACE_MAPPINGS.put(XMLConstants.XHTML_PREFIX, XMLConstants.XHTML_NAMESPACE_URI);
    }

    @Test
    public void formNamespaceElements() {

        final Metadata metadata = new Metadata();
        final XFormsAnnotatorContentHandler ch = new XFormsAnnotatorContentHandler(metadata);
        XMLUtils.urlToSAX("oxf:/org/orbeon/oxf/xforms/processor/test-form.xml", ch, XMLUtils.ParserConfiguration.PLAIN, false);

        // Test that ns information is provided for those elements
        assertNotNull(metadata.getNamespaceMapping("output-in-title").mapping);
        assertNotNull(metadata.getNamespaceMapping("html").mapping);
        assertNotNull(metadata.getNamespaceMapping("main-instance").mapping);
        assertNotNull(metadata.getNamespaceMapping("dateTime-component").mapping);
        assertNotNull(metadata.getNamespaceMapping("dateTime1-control").mapping);
        assertNotNull(metadata.getNamespaceMapping("value1-control").mapping);
        assertNotNull(metadata.getNamespaceMapping("output-in-label").mapping);
        assertNotNull(metadata.getNamespaceMapping("img-in-label").mapping);
        assertNotNull(metadata.getNamespaceMapping("span").mapping);

        // Test that ns information is NOT provided for those elements (because processed as part of shadow tree processing)
        assertNull(metadata.getNamespaceMapping("instance-in-xbl"));
        assertNull(metadata.getNamespaceMapping("div-in-xbl"));

        // Test that ns information is NOT provided for those elements (because in instances or schemas)
        assertNull(metadata.getNamespaceMapping("instance-root"));
        assertNull(metadata.getNamespaceMapping("instance-value"));
        assertNull(metadata.getNamespaceMapping("xbl-instance-root"));
        assertNull(metadata.getNamespaceMapping("xbl-instance-value"));
        assertNull(metadata.getNamespaceMapping("schema-element"));
    }

    // Test that xxf:attribute elements with @id and @for were created for
    @Test
    public void xxformsAttribute() {

        final Document document = Dom4jUtils.readFromURL("oxf:/org/orbeon/oxf/xforms/processor/test-form.xml", XMLUtils.ParserConfiguration.PLAIN);
        final Metadata metadata = new Metadata();
        final Document annotatedDocument = new XBLBindings(new IndentedLogger(XFormsServer.logger, ""), null, metadata)
                .annotateShadowTree(document, "", false);
        final DocumentWrapper documentWrapper = new DocumentWrapper(annotatedDocument, null, XPathCache.getGlobalConfiguration());

        // Check there is an xxf:attribute for "html" with correct name
        List<Object> result = XPathCache.evaluate(documentWrapper, "//xxf:attribute[@for = 'html']", XFormsStaticStateImpl.BASIC_NAMESPACE_MAPPING(), null, null, null, null, null, null);

        assertNotNull(result);
        assertEquals(1, result.size());
        Element resultElement = (Element) ((NodeWrapper) result.get(0)).getUnderlyingNode();
        assertTrue(XFormsUtils.getElementId(resultElement).trim().length() > 0);
        assertEquals("lang", resultElement.attributeValue(XFormsConstants.NAME_QNAME));

        // Check there is an xxf:attribute for "span" with correct name
        result = XPathCache.evaluate(documentWrapper, "//xxf:attribute[@for = 'span']", XFormsStaticStateImpl.BASIC_NAMESPACE_MAPPING(), null, null, null, null, null, null);

        assertNotNull(result);
        assertEquals(1, result.size());
        resultElement = (Element) ((NodeWrapper) result.get(0)).getUnderlyingNode();
        assertTrue(XFormsUtils.getElementId(resultElement).trim().length() > 0);
        assertEquals("style", resultElement.attributeValue(XFormsConstants.NAME_QNAME));
    }
}

@@ -1134,48 +1134,8 @@
 gs;%0A
-import org.orbeon.oxf.xml.XMLConstants;%0A
 impo
@@ -1162,24 +1162,24 @@
 l.XMLUtils;%0A
+
 import org.o
@@ -1305,34 +1305,8 @@
 r;%0A%0A
-import java.util.HashMap;%0A
 impo
@@ -1366,16 +1366,16 @@
 ert.*;%0A%0A
+
 public c
@@ -1452,564 +1452,8 @@
  %7B%0A%0A
-    public static final HashMap%3CString, String%3E BASIC_NAMESPACE_MAPPINGS = new HashMap%3CString, String%3E();%0A    static %7B%0A        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XFORMS_PREFIX, XFormsConstants.XFORMS_NAMESPACE_URI);%0A        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XXFORMS_PREFIX, XFormsConstants.XXFORMS_NAMESPACE_URI);%0A        BASIC_NAMESPACE_MAPPINGS.put(XFormsConstants.XML_EVENTS_PREFIX, XFormsConstants.XML_EVENTS_NAMESPACE_URI);%0A        BASIC_NAMESPACE_MAPPINGS.put(XMLConstants.XHTML_PREFIX, XMLConstants.XHTML_NAMESPACE_URI);%0A    %7D%0A%0A
     

Remove unused code