std::string Expression2PresentationMMLUnits::getMathML(const UnitDefinition & ud) const
{
  std::string tmp;

  /*tmp += "<mrow>";
  unsigned int i, imax = ud.getNumUnits();
  for (i=0; i<imax; ++i)
  {
    if (i) tmp += "<mo>&CenterDot;</mo>";
    tmp += getMathML(ud.getUnit(i), false);
  }
  tmp += "</mrow>";*/

  tmp += "<mrow>";
  bool isFirst = true;
  unsigned int i, imax = ud.getNumUnits();

  for (i = 0; i < imax; ++i)
    {
      if (ud.getUnit(i)->getExponent() >= 0)
        {
          if (!isFirst) tmp += "<mo>&CenterDot;</mo>";

          tmp += getMathML(ud.getUnit(i), true);
          isFirst = false;
        }
    }

  if (isFirst) tmp += "<mn>1</mn>"; //there are no units with pos. exponent

  std::string tmp2;
  isFirst = true;

  for (i = 0; i < imax; ++i)
    {
      if (ud.getUnit(i)->getExponent() < 0)
        {
          if (!isFirst) tmp2 += "<mo>&CenterDot;</mo>";

          tmp2 += getMathML(ud.getUnit(i), true);
          isFirst = false;
        }
    }

  if (!isFirst) tmp += "<mo>/</mo>" + tmp2; //only create fraction when there is a denominator

  tmp += "</mrow>";

  return tmp;
}
std::string Expression2PresentationMMLUnits::getMathML(const CUnitInformation & ui) const
{
  std::string tmp;
  tmp += "<mrow>";

  if (ui.isConflict())
    tmp += "<mi mathcolor = \"red\">conflict!</mi>";

  if (ui.getSymbolicExpExp() > 0)
    tmp += "<mfenced>";

  switch (ui.getInfo())
    {
      case CUnitInformation::UNKNOWN:
        tmp += "<mi mathcolor = \"orange\">unknown</mi>";
        break;

      case CUnitInformation::DEFAULT:
        tmp += "<mstyle mathcolor=\"#000060\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
        break;

      case CUnitInformation::GLOBAL:
        tmp += "<mstyle mathcolor=\"#0000a0\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
        break;

      case CUnitInformation::PROVIDED:
        tmp += "<mstyle mathcolor=\"#2020ff\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
        break;

      case CUnitInformation::DERIVED:
        tmp += "<mstyle mathcolor=\"green\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
        break;
    }

  if (ui.getSymbolicExpExp() > 0)
    {
      std::ostringstream ttt; ttt << ui.getSymbolicExpExp();
      tmp += "</mfenced><mo>^</mo><mi mathcolor = \"#c06000\">" + ui.getSymbolicExponent() + "</mi><mo>^</mo><mn>" + ttt.str() + "</mn>";
    }

  tmp += "</mrow>";
  return tmp;
}
void Expression2PresentationMMLUnits::writeMathMLNumber(std::ostream & out, const ASTNode* node, unsigned int l) const
{
  CUnitInformation* unitInformation = mpUnitInterface->getMappedUnitFromNumberNode(node);
  CUnitInformation* unitInformation2 = NULL;

  if (mpUnitInterface2)
    unitInformation2 = mpUnitInterface2->getMappedUnitFromNumberNode(node);

  if (!unitInformation && !unitInformation2)
    {
      Expression2PresentationMML::writeMathMLNumber(out, node, l + 1);
      return;
    }

  std::string color;

  if (mpUnitInterface->getListOfConflictingNodes().count(node)
      || mpUnitInterface2->getListOfConflictingNodes().count(node))
    color = "#f0b0b0";
  else
    color = "#d0e0d0";

  std::ostringstream oss;
  Expression2PresentationMML::writeMathMLNumber(oss, node, l + 2);

  if (!unitInformation && unitInformation2)
    {
      writeMathMLBox(out, oss.str(), "<mi>unknown</mi>", getMathML(*unitInformation2), color, l);
    }

  if (unitInformation && !unitInformation2)
    {
      writeMathMLBox(out, oss.str(),  getMathML(*unitInformation), color, l);
    }

  if (unitInformation && unitInformation2)
    {
      writeMathMLBox(out, oss.str(),  getMathML(*unitInformation), getMathML(*unitInformation2), color, l);
    }
}
void Expression2PresentationMMLUnits::writeMathMLName(std::ostream & out, const ASTNode* node, unsigned int l) const
{
  std::string reactionID = mpReaction ? mpReaction->getId() : "";
  CUnitInformation* unitInformation = mpUnitInterface->getMappedUnitFromIdentifier(node->getName(),
                                      CUnitInterfaceSBML::CEnvironmentInformation(reactionID));

  if (!unitInformation)
    {
      //use the base class implementation
      Expression2PresentationMML::writeMathMLName(out, node, l + 1);
      return;
    }

  std::string color;

  if (mpUnitInterface->getListOfConflictingNodes().count(node)
      || mpUnitInterface2->getListOfConflictingNodes().count(node))
    color = "#f0b0b0";
  else
    color = "#d0d0e0";

  CUnitInformation * unitInformation2 = NULL;

  if (mpUnitInterface2)
    unitInformation2 = mpUnitInterface2->getMappedUnitFromIdentifier(node->getName(),
                       CUnitInterfaceSBML::CEnvironmentInformation(reactionID));

  std::ostringstream oss;
  Expression2PresentationMML::writeMathMLName(oss, node, l + 2);

  if (unitInformation2 != NULL)
    {
      if (CUnitInformation::isEqual(*unitInformation, *unitInformation2))
        writeMathMLBox(out, oss.str(), getMathML(*unitInformation), "", color, l);
      else
        writeMathMLBox(out, oss.str(), getMathML(*unitInformation), getMathML(*unitInformation2), color, l);
    }
  else
    writeMathMLBox(out, oss.str(), getMathML(*unitInformation), color, l);
}
UT_Error OXML_Element_Math::serialize(IE_Exp_OpenXML* exporter)
{
    UT_Error err = UT_OK;
	
    err = exporter->startMath();
    if(err != UT_OK)
        return err;
    
    std::string sMathML;
    sMathML.assign(getMathML());
    std::string sOMML;

    if(convertMathMLtoOMML(sMathML, sOMML))
    {
        err = exporter->writeMath(sOMML.c_str());
        if(err != UT_OK)
            return err;
    }
    
    return exporter->finishMath();
}