Ejemplo n.º 1
0
void MessageGenerator::genRoxieMessage(const char* templatemsg, StringBuffer& message)
{
    Owned<IPropertyTree> tmplat;
    StringBuffer root;

    if (templatemsg)
    {
        tmplat.setown(createPTreeFromXMLString(templatemsg));
        if(!tmplat.get())
            throw MakeStringException(-1, "can't generate property tree from input, please make sure it's valid xml.");
        root = tmplat->queryName();
        tmplat.setown(tmplat->getPropTree(VStringBuffer("//Results/Result")));
        if (!tmplat.get())
            throw MakeStringException(-1, "can't find Results/Result in input XML");
    }
    else 
        root = "Unknown"; // TODO: find out the root?

    message.appendf("<!-- <%s> --> %s", root.str(), LT);
    message.appendf(" <!-- <Results> --> %s", LT);
    message.appendf("  <Result>%s", LT);

    Owned<IPropertyTreeIterator> it = m_roxieSchemaRoot->getElements(VStringBuffer("XmlSchema"));
    for (it->first(); it->isValid(); it->next())
    {
        IPropertyTree* ds = &it->query();

        const char* name = ds->queryProp("@name");
        if (!name)
        {
            ERRLOG("XmlSchema without name");
            continue;
        }
        
        IPropertyTree* p = ds->queryBranch(VStringBuffer("%s:schema", xsdNs()));
        m_schemaTree.setown(LINK(p));
        IXmlSchema* xs = createXmlSchemaFromPTree(m_schemaTree);
        IXmlType* type = xs->queryElementType("Dataset");
        if (!type)
        {
            ERRLOG("Can not find type '%s'", name);
            continue;
        }
        // get the Row type
        type = type->queryFieldType(0);
        if (!type)
        {
            ERRLOG("The root element for %s is not an array", name);
            continue;
        }

        IPropertyTree* dsTmplat = tmplat.get() ? tmplat->queryPropTree(VStringBuffer("Dataset[@name='%s']",name)) : NULL;
        if (dsTmplat && dsTmplat->numChildren()>0)
        {
            message.appendf("    <Dataset name=\"%s\">%s", name, LT);
    
            Owned<IPropertyTreeIterator> row = dsTmplat->getElements("Row");
            for (row->first(); row->isValid(); row->next())
            {
                message.appendf("     <Row>%s", LT);
    
                StringStack parent;
                doType(parent,5,"Row",type, &row->query(), message);

                message.appendf("     </Row>%s", LT);
            }

            message.appendf("   </Dataset>%s", LT);
        }
        else if (m_genAllDatasets)
        {
            message.appendf("    <Dataset name=\"%s\">%s", name, LT);
            for (int i=0; i < m_items; i++)
            {
                message.appendf("     <Row>%s", LT);
                StringStack parent;
                doType(parent,6,"Row",type,message);
                message.appendf("     </Row>%s", LT);
            }
            if (m_ecl2esp)
                message.appendf("     <Row/>%s", LT);

            message.appendf("   </Dataset>%s", LT);
        }
    }

    message.appendf("  </Result>%s", LT);
    message.appendf(" <!-- </Results> --> %s", LT);
    message.appendf("<!-- </%s> --> %s", root.str(), LT);
}