/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#include <foostring/foostring.h>

#include <bp/ng/corpus.h>
#include <bp/ng/document.h>
#include <bp/ng/xml-ipi.h>
#include <bp/ng/xml-nkjp.h>
#include <bp/ng/xml-parser.h>
#include <bp/common.h>

int xml_parse_morph(xml_reader *reader, struct document *document, struct corpus *corpus)
{
   int rc;
   assert(reader != NULL);
   while ((rc = xml_read(reader)) == 1) {
      if (!xml_has_start_tag(reader))
         continue;
      if (xml_has_ipi_root(reader)) {
         rc = xml_parse_morph_ipi(reader, document, corpus);
         break;
      }
      else if (xml_has_nkjp_root(reader)) {
         rc = xml_parse_morph_nkjp(reader, document, corpus);
         break;
      }
      else {
         bp_warning("unknown XML format");
         return -1;
      }
   }
   return -(rc != 0);
}

int document_parse_morph(struct document *document, struct corpus *corpus)
{
   xml_reader *reader;
   char* path;
   int rc;
   assert(document != NULL);
   path = string_aformat("%s/%s", document->dir_name, document->morph_name);
   reader = xml_reader_new(path);
   if (reader == NULL) {
      bp_warning("unable to parse %s", path);
      free(path);
      return -1;
   }
   free(path);
   rc = xml_parse_morph(reader, document, corpus);
   xml_reader_free(reader);
   return rc;
}

