// ============================================================
zfbool ZFSerializableDataParseXml(ZF_OUT ZFSerializableData &ret, ZF_IN const ZFInputCallback &input)
{
    if(!input.callbackIsValid())
    {
        return zffalse;
    }
    ZFXmlItem xmlElement = ZFXmlParseFirstElement(input);
    if(xmlElement.xmlType() == ZFXmlType::e_XmlNull)
    {
        return zffalse;
    }
    if(!ZFXmlToSerializableData(ret, xmlElement))
    {
        return zffalse;
    }

    {
        ZFString *v = input.callbackTagGet<ZFString *>(ZFCallbackTagKeyword_resPath);
        if(v != zfnull)
        {
            ret.serializableDataTagSetMarkCached(ZFSerializableDataTagKeyword_resPath, v);
        }
        else
        {
            v = input.callbackTagGet<ZFString *>(ZFCallbackTagKeyword_filePath);
            if(v != zfnull)
            {
                ret.serializableDataTagSetMarkCached(ZFSerializableDataTagKeyword_filePath, v);
            }
        }
    }
    return zftrue;
}
Exemplo n.º 2
0
static zfbool _ZFP_ZFXmlOutputElementAttributeNeedNewLine(ZF_IN const ZFXmlItem &element,
                                                          ZF_IN const ZFXmlOutputFlags &flags,
                                                          ZF_IN const ZFXmlVisitData &data)
{
    if(element.xmlIsNull())
    {
        return zffalse;
    }
    ZFXmlItem xmlAttribute = element.xmlAttributeFirst();
    if(flags.xmlElementAttributeCountBeforeAddNewLine == zfindexMax)
    {
        return zffalse;
    }
    if(flags.xmlElementAttributeCountBeforeAddNewLine == 0)
    {
        return !xmlAttribute.xmlIsNull();
    }
    zfindex xmlAttributeCount = 0;
    while(!xmlAttribute.xmlIsNull())
    {
        ++xmlAttributeCount;
        if(xmlAttributeCount > flags.xmlElementAttributeCountBeforeAddNewLine)
        {
            return zftrue;
        }
        xmlAttribute = xmlAttribute.xmlAttributeNext();
    }
    return zffalse;
}
void ZFXmlPrint(ZF_IN const ZFSerializableData &serializableData,
                ZF_IN_OPT const ZFOutputCallback &outputCallback /* = ZFOutputCallbackDefault */,
                ZF_IN_OPT const ZFXmlOutputFlags &flags /* = ZFXmlOutputFlagsDefault */)
{
    ZFXmlItem xmlElement = ZFXmlFromSerializableData(serializableData);
    if(xmlElement.xmlType() != ZFXmlType::e_XmlNull)
    {
        xmlElement->xmlAttributeSortRecursively();
        xmlElement->xmlVisit(ZFXmlVisitCallbackForOutput(outputCallback, flags));
        outputCallback.execute(zfText("\n"));
    }
}
ZF_NAMESPACE_GLOBAL_BEGIN

ZFSERIALIZABLEDATA_REFERENCE_TYPE_DEFINE(ZFSerializableDataRefType_xml, serializableData, data, outErrorHintToAppend)
{
    ZFXmlItem xmlElement = ZFXmlParseFirstElement(ZFInputCallbackForFileDescriptor(data));
    if(xmlElement.xmlType() == ZFXmlType::e_XmlNull)
    {
        ZFSerializableUtil::errorOccurred(outErrorHintToAppend,
            zfText("failed to load xml element from \"%s\""), data);
        return zffalse;
    }
    return ZFXmlToSerializableData(serializableData, xmlElement);
}
Exemplo n.º 5
0
static zfbool _ZFP_ZFXmlOutputElementChildNeedNewLine(ZF_IN const ZFXmlItem &xmlChild,
                                                      ZF_IN const ZFXmlOutputFlags &flags,
                                                      ZF_IN const ZFXmlVisitData &data)
{
    if(flags.xmlElementTrimTagIfNoChildren && xmlChild.xmlIsNull())
    {
        return zffalse;
    }
    if(!flags.xmlElementEndTagAtSameLineIfNoChildElement)
    {
        return zftrue;
    }

    if(xmlChild.xmlType() != ZFXmlType::e_XmlText)
    {
        return zftrue;
    }
    for(ZFXmlItem t = xmlChild.xmlSiblingNext(); !t.xmlIsNull(); t = t.xmlSiblingNext())
    {
        if(t.xmlType() != ZFXmlType::e_XmlText)
        {
            return zftrue;
        }
    }
    for(ZFXmlItem t = xmlChild.xmlSiblingPrev(); !t.xmlIsNull(); t = t.xmlSiblingPrev())
    {
        if(t.xmlType() != ZFXmlType::e_XmlText)
        {
            return zftrue;
        }
    }

    return zffalse;
}
ZFOBJECT_CREATOR_DEFINE(ZFObjectCreatorType_xml, data)
{
    ZFXmlItem xmlElement = ZFXmlParseFirstElement(ZFInputCallbackForFileDescriptor(data));
    if(xmlElement.xmlIsNull())
    {
        return zfautoObjectNull;
    }
    ZFSerializableData serializableData;
    if(ZFXmlParseToSerializableData(serializableData, xmlElement))
    {
        return ZFObjectFromSerializableData(serializableData);
    }
    return zfautoObjectNull;
}
ZFXmlItem ZFXmlFromSerializableData(ZF_IN const ZFSerializableData &serializableData,
                                    ZF_OUT_OPT zfstring *outErrorHintToAppend /* = zfnull */,
                                    ZF_OUT_OPT const ZFSerializableData **outErrorPos /* = zfnull */)
{
    if(serializableData.itemClass() == zfnull)
    {
        ZFSerializableUtil::errorOccurred(outErrorHintToAppend, outErrorPos, serializableData, zfText("missing serializable class"));
        return ZFXmlItem();
    }

    ZFXmlItem ret(ZFXmlType::e_XmlElement);
    ret.xmlNameSet(serializableData.itemClass());

    if(serializableData.referenceRefType() != zfnull)
    {
        ret.xmlAttributeAdd(ZFSerializableKeyword_refType, serializableData.referenceRefType());
    }
    if(serializableData.referenceRefData() != zfnull)
    {
        ret.xmlAttributeAdd(ZFSerializableKeyword_refData, serializableData.referenceRefData());
    }

    for(zfiterator it = serializableData.attributeIterator();
        serializableData.attributeIteratorIsValid(it);
        serializableData.attributeIteratorNext(it))
    {
        ret.xmlAttributeAdd(serializableData.attributeIteratorGetKey(it),
            serializableData.attributeIteratorGet(it));
    }

    for(zfindex i = 0; i < serializableData.elementCount(); ++i)
    {
        ZFXmlItem child = ZFXmlFromSerializableData(serializableData.elementAtIndex(i), outErrorHintToAppend, outErrorPos);
        if(child.xmlType() == ZFXmlType::e_XmlNull)
        {
            return ZFXmlItem();
        }
        ret->xmlChildAdd(child);
    }

    return ret;
}
static zfbool _ZFP_ZFXmlParseToSerializableData(ZF_OUT ZFSerializableData &serializableData,
                                                ZF_IN const ZFXmlItem &xmlElement,
                                                ZF_OUT_OPT zfstring *outErrorHintToAppend = zfnull,
                                                ZF_OUT_OPT ZFXmlItem *outErrorPos = zfnull)
{
    if(xmlElement.xmlIsNull())
    {
        ZFSerializableUtil::errorOccurred(outErrorHintToAppend, zfText("null xml element"));
        if(outErrorPos != zfnull)
        {
            *outErrorPos = xmlElement;
        }
        return zffalse;
    }
    if(xmlElement.xmlType() != ZFXmlType::e_XmlElement)
    {
        ZFSerializableUtil::errorOccurred(outErrorHintToAppend, zfText("param not type of xml element"));
        if(outErrorPos != zfnull)
        {
            *outErrorPos = xmlElement;
        }
        return zffalse;
    }

    if(xmlElement.xmlName() == zfnull)
    {
        ZFSerializableUtil::errorOccurred(outErrorHintToAppend, zfText("missing xml node name"));
        if(outErrorPos != zfnull)
        {
            *outErrorPos = xmlElement;
        }
        return zffalse;
    }
    serializableData.itemClassSet(xmlElement.xmlName());

    ZFXmlItem attribute = xmlElement.xmlAttributeFirst();
    while(!attribute.xmlIsNull())
    {
        if(attribute.xmlName() == zfnull)
        {
            ZFSerializableUtil::errorOccurred(outErrorHintToAppend, zfText("missing xml attribute name"));
            if(outErrorPos != zfnull)
            {
                *outErrorPos = attribute;
            }
            return zffalse;
        }
        if(zfscmpTheSame(attribute.xmlName(), ZFSerializableKeyword_refType))
        {
            serializableData.referenceRefTypeSet(attribute.xmlValue());
        }
        else if(zfscmpTheSame(attribute.xmlName(), ZFSerializableKeyword_refData))
        {
            serializableData.referenceRefDataSet(attribute.xmlValue());
        }
        else
        {
            serializableData.attributeSet(attribute.xmlName(), attribute.xmlValue());
        }

        attribute = attribute.xmlAttributeNext();
    }

    ZFXmlItem element = xmlElement.xmlChildElementFirst();
    while(!element.xmlIsNull())
    {
        ZFSerializableData childData;
        if(!_ZFP_ZFXmlParseToSerializableData(childData, element, outErrorHintToAppend, outErrorPos))
        {
            return zffalse;
        }
        serializableData.elementAdd(childData);
        element = element.xmlSiblingElementNext();
    }

    return zftrue;
}