/* 
 * $Id: GLSDBanBunModuleChecker.cpp,v 1.6 2006/06/23 07:26:08 ozawa Exp $
 *
 * Copyright 2005- ONGS Inc. All rights reserved.
 * 
 * author: Masanori OZAWA (ozawa@ongs.co.jp)
 * version: $Revision: 1.6 $
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY ONGS INC ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ONGS INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the ONGS Inc.
 * 
 */

#include "xdtp.h"
#include "modules/GLSDBanBunModuleChecker.h"

#include "XMLTool.h"

using namespace XDTP;

GLSDBanBunModuleChecker::GLSDBanBunModuleChecker()
{
}

GLSDBanBunModuleChecker::~GLSDBanBunModuleChecker()
{
}

/**
 * 版文の正当性を確認します。
 * 
 * @param aDocument ロードした直後のXMLファイル
 * @return 変換処理を中断する場合は false を戻します。
 */
bool GLSDBanBunModuleChecker::checkFormat(xmlDocPtr aDocument)
{
    bool result = true;
    bool bp = false;
    int count;
    int count2;

    XMLTool tool;
    xmlNodePtr node;
    xmlNodePtr child;
    xmlNodePtr root;
    xmlXPathObjectPtr obj;
    xmlXPathObjectPtr children;
    xmlNodeSetPtr nodes;
    xmlNodeSetPtr childset;

    root = xmlDocGetRootElement(aDocument);

    obj = tool.getNodeList(root, "//docgroup/docinfo");
    if (obj) {
        nodes = obj->nodesetval;
        
        for (count = 0; result && count < nodes->nodeNr; count++) {
            node = nodes->nodeTab[count];
            if (!tool.isElement(node)) continue;
            children = tool.getNodeList(node, "./*");

            if (!children) {
                fprintf(stderr, "%s: GLSDBanBunModuleChecker: docinfo need some children." LINE_SEP, APP_NAME);
                result = false;
            }
            else {
                childset = children->nodesetval;
                
                for (count2 = 0; result && count2 < childset->nodeNr; count2++) {
                    child = childset->nodeTab[count2];

                    if (xmlStrcmp(child->name, (const xmlChar*)"number") == 0 ||
                        xmlStrcmp(child->name, (const xmlChar*)"revisionnumber") == 0 ||
                        xmlStrcmp(child->name, (const xmlChar*)"newernumber") == 0 ||
                        xmlStrcmp(child->name, (const xmlChar*)"obsoletenumber") == 0 ||
                        xmlStrcmp(child->name, (const xmlChar*)"category") == 0)
                    {
                        bp = false;
                        node = child->children;

                        for (node = child->children; result && node; node = node->next) {
                            if (tool.isElement(node)) {
                                if (xmlStrcmp(node->name, (const xmlChar*)"p") == 0) {
                                    if (bp) result = false;
                                    bp = true;
                                }
                                else {
                                    result = false;
                                }
                            }
                            else if (tool.isText(node)) {
                                if (0 < tool.getText(node).length()) result = false;
                            }
                            else if (!tool.isComment(node)) {
                                result = false;
                            }
                        }

                        if (!bp) result = false; // need a 'p' element
                    }
                }

                xmlXPathFreeObject(children);
            }
        }

        xmlXPathFreeObject(obj);
    }

    if (!result)
    	fprintf(stderr, "%s: GLSDBanBunModuleChecker: check failed." LINE_SEP, APP_NAME);

    return result;
}


syntax highlighted by Code2HTML, v. 0.9.1