/* $Id: Context2XPath.java,v 1.15 2005/12/17 10:31:51 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 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 General Public License for more details.
 */

package org.qedeq.kernel.xml.mapper;

import java.util.StringTokenizer;

import org.qedeq.kernel.bo.control.Context;
import org.qedeq.kernel.log.Trace;
import org.qedeq.kernel.utility.ReplaceUtility;
import org.qedeq.kernel.xml.tracker.SimpleXPath;

/**
 * Parses complete qedeq modules.
 *
 * @version $Revision: 1.15 $
 * @author Michael Meyling
 */
public final class Context2XPath {

    /**
     * Constructor.
     */
    private Context2XPath() {
        // nothing to do
    }

    /**
     * Get file path out of context information.
     *
     * @param   context Context information.
     * @return  File path and name.
     */
    public static final String getFileName(final Context context) {
        return context.getModuleLocation();
    }

    /**
     * Get XPath out of context information.
     *
     * @param   context Context information.
     * @return  XPath.
     */
    public static final String getXPath(final Context context) {
        final String method = "getXPath(String)";
        String xpath = context.getLocationWithinModule();
        Trace.traceParam(Context2XPath.class, method, "context", xpath);
        xpath = ReplaceUtility.replace(xpath, ".get(", "[");
        xpath = ReplaceUtility.replace(xpath, "()", "");
        xpath = ReplaceUtility.replace(xpath, ")", "]");
        xpath = ReplaceUtility.replace(xpath, ".get", "/");
        xpath = ReplaceUtility.replace(xpath, "get", "/Qedeq/");

        xpath = incrementNumbers(xpath);

        // TODO mime 20050807: what if no Latex, Author, or other, exist? For regular files this is
        // ok, but if there is no element in the list?
        // TODO mime 20050708: isn't a replacement only one element by one better?
        xpath = ReplaceUtility.replace(xpath, "Title", "Title/Latex");
        // TODO mime 20050708: definition in XML file only formula
        xpath = ReplaceUtility.replace(xpath, "Definition/FormulaOrTerm", "Definition/Formula");
        xpath = ReplaceUtility.replace(xpath, "AuthorList", "Authors/Author");
        xpath = ReplaceUtility.replace(xpath, "ImportList", "Imports/Import");
        xpath = ReplaceUtility.replace(xpath, "UsedByList", "UsedBy/Specification");
        xpath = ReplaceUtility.replace(xpath, "ChapterList", "Chapter");
        xpath = ReplaceUtility.replace(xpath, "SectionList", "Section");
        xpath = ReplaceUtility.replace(xpath, "SubsectionList", "Subsections/*");
        xpath = ReplaceUtility.replace(xpath, "VariableList", "VARLIST/VAR");
        xpath = ReplaceUtility.replace(xpath, "List", "s");
        xpath = ReplaceUtility.replace(xpath, "/NodeType", "");
        xpath = ReplaceUtility.replace(xpath, "Summary", "Abstract/Latex");
        xpath = ReplaceUtility.replace(xpath, "Introduction", "Introduction/Latex");
        xpath = ReplaceUtility.replace(xpath, "PrecedingText", "PRECEDING/Latex");
        xpath = ReplaceUtility.replace(xpath, "SucceedingText", "SUCCEEDING/Latex");
        xpath = ReplaceUtility.replace(xpath, "Element", "*");
        xpath = ReplaceUtility.replace(xpath, "Proposition", "Theorem");
        xpath = ReplaceUtility.replace(xpath, "/Proofs", "");

        // TODO mime 20050807: integrate non formal proofs
        xpath = ReplaceUtility.replace(xpath, "/NonFormalProof", "/Proof/Latex");

        xpath = xpath.toUpperCase();

        SimpleXPath sxp = new SimpleXPath(xpath);

        final String beforeLast = sxp.getBeforeLastElement();
        final String last = sxp.getLastElement();
        if ("EMAIL".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("email");
        } else if ("LABEL".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("label");
        } else if ("ID".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("id");
        } else if ("SPECIFICATION".equals(beforeLast) && "NAME".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("name");
        } else if ("SPECIFICATION".equals(beforeLast) && "RULEVERSION".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("ruleVersion");
        } else if ("CHAPTER".equals(beforeLast) && "NONUMBER".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("noNumber");
        } else if ("SECTION".equals(beforeLast) && "NONUMBER".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("noNumber");
        } else if ("*".equals(beforeLast) && "LATEX".equals(last)) {
            sxp.deleteLastElement();
            sxp.addElement("TEXT");
            sxp.addElement("LATEX");
        } else if ("DEFINITION".equals(beforeLast) && "TYPE".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("kind");
        } else if ("DEFINITION".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("arguments");
        } else if ("DEFINITION".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("arguments");
        } else if ("*".equals(beforeLast) && "LEVEL".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("level");
        } else if ("*".equals(beforeLast) && "NONUMBER".equals(last)) {
            sxp.deleteLastElement();
            sxp.setAttribute("noNumber");
        } else if ("*".equals(beforeLast) && "NAME".equals(last)) {
            final int len = sxp.getElementOccurrence(sxp.size() - 1);
            sxp.deleteLastElement();
            sxp.addElement("NAME");
            sxp.addElement("LATEX", len);
        }

        xpath = sxp.toString();
        Trace.traceParam(Context2XPath.class, method, "xpath", xpath);
        return xpath;
    }

    /**
     * Increment all element occurrence numbers in "[]" by one.
     *
     * @param   xpath   Like "a[0]b[1]".
     * @return  Like "a[1]b[2]".
     */
    private static String incrementNumbers(final String xpath) {
        final StringTokenizer tokenizer = new StringTokenizer(xpath, "/", true);
        String newXpath = "";
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            if (token.indexOf('[') >= 0) {
                final StringTokenizer getnu = new StringTokenizer(token, "[]");
                newXpath += getnu.nextToken() + "[";
                newXpath += ((new Integer(getnu.nextToken())).intValue() + 1) + "]";
            } else {
                newXpath += token;
            }
        }
        return newXpath;
    }

}
