/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */


package edu.stanford.smi.protegex.owl.swrl.bridge.builtins.swrlxml;

import edu.stanford.smi.protegex.owl.swrl.bridge.BuiltInArgument;
import edu.stanford.smi.protegex.owl.swrl.bridge.OWLIndividual;
import edu.stanford.smi.protegex.owl.swrl.bridge.exceptions.BuiltInException;
import edu.stanford.smi.protegex.owl.swrl.bridge.builtins.AbstractSWRLBuiltInLibrary;
import edu.stanford.smi.protegex.owl.swrl.bridge.builtins.SWRLBuiltInUtil;

import org.jdom.*;

import java.util.*;

/**
 */
public class SWRLBuiltInLibraryImpl extends AbstractSWRLBuiltInLibrary 
{
  private static String SWRLXMLLibraryName = "SWRLXMLBuiltIns";

  public SWRLBuiltInLibraryImpl() 
  { 
    super(SWRLXMLLibraryName); 
    reset();
  }

  private XMLProcessor xmlProcessor;
  private XMLMapper xmlMapper;

  private Map<String, OWLIndividual> mappedSources;

  public void reset() 
  {
    xmlProcessor = new XMLProcessor();
    xmlMapper = new XMLMapper();
    mappedSources = new HashMap<String, OWLIndividual>();
  } // reset

  public boolean makeXMLDocument(List<BuiltInArgument> arguments) throws BuiltInException 
  { 
    boolean result;

    SWRLBuiltInUtil.checkNumberOfArgumentsEqualTo(2, arguments.size());
    SWRLBuiltInUtil.checkForUnboundNonFirstArguments(arguments);

    if (SWRLBuiltInUtil.isUnboundArgument(0, arguments)) {
      String inputXMLStreamName = SWRLBuiltInUtil.getArgumentAsAString(1, arguments);
      OWLIndividual xmlDocumentMapping = null;
      Document doc;

      if (!mappedSources.containsKey(inputXMLStreamName)) {

        try {
          doc = xmlProcessor.processXMLStream(inputXMLStreamName);
          xmlDocumentMapping = xmlMapper.document2XMLDocumentMapping(doc, getInvokingBridge());
          mappedSources.put(inputXMLStreamName, xmlDocumentMapping);
        } catch (XMLProcessorException e) {
          throw new BuiltInException("error processing XML stream '" + inputXMLStreamName + "': " + e.getMessage());
        } catch (XMLMapperException e) {
          throw new BuiltInException("error mapping XML stream '" + inputXMLStreamName + "': " + e.getMessage());
        } // try
      } else xmlDocumentMapping = mappedSources.get(inputXMLStreamName);

      arguments.set(0, xmlDocumentMapping); // Bind the result to the first parameter

      result = true;
    } else {
      result = false;
    } // if
    return result;

  } // makeXMLDocument

} // SWRLBuiltInLibraryImpl
