All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.qedeq.kernel.xml.dao.Qedeq2Xml Maven / Gradle / Ivy

/* This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2013,  Michael Meyling .
 *
 * "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.dao;

import java.io.IOException;

import org.qedeq.base.io.TextOutput;
import org.qedeq.base.utility.StringUtility;
import org.qedeq.kernel.bo.KernelContext;
import org.qedeq.kernel.bo.module.ControlVisitor;
import org.qedeq.kernel.bo.module.InternalServiceProcess;
import org.qedeq.kernel.bo.module.KernelQedeqBo;
import org.qedeq.kernel.se.base.list.ElementList;
import org.qedeq.kernel.se.base.module.Add;
import org.qedeq.kernel.se.base.module.Author;
import org.qedeq.kernel.se.base.module.AuthorList;
import org.qedeq.kernel.se.base.module.Axiom;
import org.qedeq.kernel.se.base.module.ChangedRule;
import org.qedeq.kernel.se.base.module.Chapter;
import org.qedeq.kernel.se.base.module.Conclusion;
import org.qedeq.kernel.se.base.module.ConditionalProof;
import org.qedeq.kernel.se.base.module.Existential;
import org.qedeq.kernel.se.base.module.FormalProof;
import org.qedeq.kernel.se.base.module.FormalProofLine;
import org.qedeq.kernel.se.base.module.FormalProofLineList;
import org.qedeq.kernel.se.base.module.Formula;
import org.qedeq.kernel.se.base.module.FunctionDefinition;
import org.qedeq.kernel.se.base.module.Header;
import org.qedeq.kernel.se.base.module.Hypothesis;
import org.qedeq.kernel.se.base.module.Import;
import org.qedeq.kernel.se.base.module.ImportList;
import org.qedeq.kernel.se.base.module.InitialFunctionDefinition;
import org.qedeq.kernel.se.base.module.InitialPredicateDefinition;
import org.qedeq.kernel.se.base.module.Latex;
import org.qedeq.kernel.se.base.module.LatexList;
import org.qedeq.kernel.se.base.module.LinkList;
import org.qedeq.kernel.se.base.module.LiteratureItem;
import org.qedeq.kernel.se.base.module.LiteratureItemList;
import org.qedeq.kernel.se.base.module.Location;
import org.qedeq.kernel.se.base.module.LocationList;
import org.qedeq.kernel.se.base.module.ModusPonens;
import org.qedeq.kernel.se.base.module.Node;
import org.qedeq.kernel.se.base.module.PredicateDefinition;
import org.qedeq.kernel.se.base.module.Proof;
import org.qedeq.kernel.se.base.module.Proposition;
import org.qedeq.kernel.se.base.module.Qedeq;
import org.qedeq.kernel.se.base.module.Rename;
import org.qedeq.kernel.se.base.module.Rule;
import org.qedeq.kernel.se.base.module.Section;
import org.qedeq.kernel.se.base.module.Specification;
import org.qedeq.kernel.se.base.module.Subsection;
import org.qedeq.kernel.se.base.module.SubsectionList;
import org.qedeq.kernel.se.base.module.SubstFree;
import org.qedeq.kernel.se.base.module.SubstFunc;
import org.qedeq.kernel.se.base.module.SubstPred;
import org.qedeq.kernel.se.base.module.Term;
import org.qedeq.kernel.se.base.module.Universal;
import org.qedeq.kernel.se.base.module.UsedByList;
import org.qedeq.kernel.se.common.Plugin;
import org.qedeq.kernel.se.common.SourceFileExceptionList;


/**
 * This class prints a QEDEQ module in XML format in an output stream.
 *
 * @author  Michael Meyling
 */
public final class Qedeq2Xml extends ControlVisitor implements Plugin {

    /** Output goes here. */
    private TextOutput printer;

    /**
     * Constructor.
     *
     * @param   plugin  This plugin we work for.
     * @param   bo      QEDEQ BO.
     * @param   printer Print herein.
     */
    public Qedeq2Xml(final Plugin plugin, final KernelQedeqBo bo, final TextOutput printer) {
        super(plugin, bo);
        this.printer = printer;
    }

    /**
     * Prints a XML representation of given QEDEQ module into a given output stream.
     *
     * @param   process             Service process we work for.
     * @param   plugin              Plugin we work for.
     * @param   bo                  BO QEDEQ module object.
     * @param   printer             Print herein.
     * @throws  SourceFileExceptionList Major problem occurred.
     * @throws  IOException         Writing failed.
     */
    public static void print(final InternalServiceProcess process,
            final Plugin plugin, final KernelQedeqBo bo, final TextOutput printer) throws
            SourceFileExceptionList, IOException {
        final Qedeq2Xml converter = new Qedeq2Xml(plugin, bo, printer);
        try {
            converter.traverse(process);
        } finally {
            printer.flush();
        }
        if (printer.checkError()) {
            throw printer.getError();
        }
    }

    public final void visitEnter(final Qedeq qedeq) {
        printer.println("");
        printer.println("");
        printer.pushLevel();
    }

    public final void visitLeave(final Qedeq qedeq) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Header header) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Header header) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Specification specification) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Specification specification) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final LatexList latexList) {
        final String last = getCurrentContext().getLocationWithinModule();
        if (last.endsWith(".getTitle()")) {
            printer.println("");
        } else if (last.endsWith(".getSummary()")) {
            printer.println("<ABSTRACT>");
        } else if (last.endsWith(".getIntroduction()")) {
            printer.println("<INTRODUCTION>");
        } else if (last.endsWith(".getName()")) {
            printer.println("<NAME>");
        } else if (last.endsWith(".getPrecedingText()")) {
            printer.println("<PRECEDING>");
        } else if (last.endsWith(".getSucceedingText()")) {
            printer.println("<SUCCEEDING>");
        } else if (last.endsWith(".getLatex()")) {
            printer.println("<TEXT>");
        } else if (last.endsWith(".getDescription()")) {
            if (last.indexOf(".getChangedRuleList().get(") < 0) {
                printer.println("<DESCRIPTION>");
            }
        }
        printer.pushLevel();
    }

    public void visitLeave(final LatexList latexList) {
        printer.popLevel();
        final String last = getCurrentContext().getLocationWithinModule();
        if (last.endsWith(".getTitle()")) {
            printer.println("");
        } else if (last.endsWith(".getSummary()")) {
            printer.println("");
        } else if (last.endsWith(".getIntroduction()")) {
            printer.println("");
        } else if (last.endsWith(".getName()")) {
            printer.println("");
        } else if (last.endsWith(".getPrecedingText()")) {
            printer.println("");
        } else if (last.endsWith(".getSucceedingText()")) {
            printer.println("");
        } else if (last.endsWith(".getLatex()")) {
            printer.println("");
        } else if (last.endsWith(".getDescription()")) {
            if (last.indexOf(".getChangedRuleList().get(") < 0) {
                printer.println("");
            }
        }
    }

    public void visitEnter(final Latex latex) {
        printer.print("");
        if (latex.getLatex() != null) {
            printer.pushLevel();
            printer.println("
            final String data = StringUtility.replace(latex.getLatex(),
                "]]>", "]]]]>");
            printer.println(StringUtility.useSystemLineSeparator(data).trim());
            printer.pushLevel(tabs);
        }
    }

    public void visitLeave(final Latex latex) {
        if (latex.getLatex() != null) {
            printer.println("]]>");
            printer.popLevel();
        }
        printer.println("");
    }

    public void visitEnter(final LocationList locationList) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final LocationList locationList) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Location location) {
        printer.print("");
    }

    public void visitEnter(final AuthorList authorList) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final AuthorList authorList) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Author author) {
        printer.print("");
        printer.pushLevel();
        if (author.getName() != null) {
            printer.println("");
        }
        printer.pushLevel();
    }

    public void visitLeave(final Author author) {
        printer.popLevel();
        if (author.getName() != null) {
            printer.println("");
        }
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final ImportList importList) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final ImportList importList) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Import imp) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Import imp) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final UsedByList usedByList) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final UsedByList usedByList) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Chapter chapter) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Chapter chapter) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Section section) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Section section) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final SubsectionList subsectionList) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final SubsectionList subsectionList) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Subsection subsection) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Subsection subsection) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Node node) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Node node) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Axiom axiom) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Axiom axiom) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Proposition proposition) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final Proposition proposition) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Proof proof) {
        printer.print("");
    }

    public void visitLeave(final Proof proof) {
        printer.println("");
    }

    public void visitEnter(final FormalProof proof) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final FormalProof proof) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final FormalProofLineList proof) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final FormalProofLineList proof) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final FormalProofLine line) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final FormalProofLine line) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final ModusPonens reason) {
        printer.print("");
    }

    public void visitEnter(final Add reason) {
        printer.print("");
    }

    public void visitEnter(final Rename reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Rename reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final SubstFree reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final SubstFree reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final SubstFunc reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final SubstFunc reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final SubstPred reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final SubstPred reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Existential reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Existential reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Universal reason) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Universal reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final ConditionalProof reason) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final ConditionalProof reason) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Hypothesis hypothesis) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Hypothesis hypothesis) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Conclusion conclusion) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Conclusion conclusion) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final InitialPredicateDefinition definition) {
        printer.print("");
        printer.pushLevel();
        if (definition.getLatexPattern() != null) {
            printer.println("" + StringUtility.escapeXml(definition.getLatexPattern())
                + "");
        }
    }

    public void visitLeave(final InitialPredicateDefinition definition) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final PredicateDefinition definition) {
        printer.print("");
        printer.pushLevel();
        if (definition.getLatexPattern() != null) {
            printer.println("" + StringUtility.escapeXml(definition.getLatexPattern())
                + "");
        }
    }

    public void visitLeave(final PredicateDefinition definition) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final InitialFunctionDefinition definition) {
        printer.print("");
        printer.pushLevel();
        if (definition.getLatexPattern() != null) {
            printer.println("" + definition.getLatexPattern()
                + "");
        }
    }

    public void visitLeave(final InitialFunctionDefinition definition) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final FunctionDefinition definition) {
        printer.print("");
        printer.pushLevel();
        if (definition.getLatexPattern() != null) {
            printer.println("" + definition.getLatexPattern()
                + "");
        }
    }

    public void visitLeave(final FunctionDefinition definition) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Rule rule) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final Rule rule) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final LinkList linkList) {
        for (int i = 0; i < linkList.size(); i++) {
            printer.print("");
        };
    }

    public void visitEnter(final ChangedRule rule) {
        printer.print("");
    }

    public void visitLeave(final ChangedRule rule) {
        printer.println("");
    }

    public void visitEnter(final Formula formula) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final Formula formula) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final Term term) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final Term term) {
        printer.popLevel();
        printer.println("");
    }

    // TODO mime 20070217: what do we do if an atom is not first element of a list?
    // we wouldn't get it here!!! But can we think of an output syntax anyway????
    public void visitEnter(final ElementList list) {
        final String operator = list.getOperator();
        printer.print("<" + operator);
        final boolean firstIsAtom = list.size() > 0 && list.getElement(0).isAtom();
        if (firstIsAtom) {
            final String atom = list.getElement(0).getAtom().getString();
            if (atom != null) {
                if ("VAR".equals(operator) || "PREDVAR".equals(operator)
                        || "FUNVAR".equals(operator)) {
                    printer.print(" id=\"" + StringUtility.escapeXml(atom) + "\"");
                } else if ("PREDCON".equals(operator) || "FUNCON".equals(operator)) {
                    printer.print(" ref=\"" + StringUtility.escapeXml(atom) + "\"");
                } else {
                    printer.print(" unknown=\"" + StringUtility.escapeXml(atom) + "\"");
                }
            }
        }
        if (list.size() == 0 || list.size() == 1 && list.getElement(0).isAtom()) {
            printer.print("/");
        }
        printer.println(">");
        printer.pushLevel();
    }

    public void visitLeave(final ElementList list) {
        printer.popLevel();
        if (list.size() == 0 || list.size() == 1 && list.getElement(0).isAtom()) {
            return;
        }
        printer.println("");
    }

    public void visitEnter(final LiteratureItemList list) {
        printer.println("");
        printer.pushLevel();
    }

    public void visitLeave(final LiteratureItemList list) {
        printer.popLevel();
        printer.println("");
    }

    public void visitEnter(final LiteratureItem item) {
        printer.print("");
        printer.pushLevel();
    }

    public void visitLeave(final LiteratureItem item) {
        printer.popLevel();
        printer.println("");
    }

    public String getServiceId() {
        return this.getClass().getName();
    }

    public String getServiceAction() {
        return "generate XML";
    }

    public String getServiceDescription() {
        return "Transformes QEDEQ module into XML data";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy