Ejemplo n.º 1
0
// ============================================================================
// CWidgetEntry::InternalizeXmlL()
// read from persistent storage(file)
//
// @since 5.0
// ============================================================================
//
void CWidgetEntry::InternalizeXmlL( RFs& aFileSession,
                                    xmlDocPtr aDoc,
                                    xmlNode* n,
                                    CWidgetRegistryXml* aXmlProcessor )
{
    // <prop>name<val>value<type>typename</type></val></prop>
    //
    // prop subtree traversal assumes strict adherence to the
    // prototype structure, otherwise code leaves with corrupt code
    for ( ;
            n;
        )
    {
        // permit some non element stuff (comment, whitespace...) before <prop>
        while ( n && ( n->type != XML_ELEMENT_NODE ) )
        {
            n = n->next;
        }
        if ( NULL == n )
        {
            for (TInt i = iPropertyValues.Count(); i < EWidgetPropertyIdCount; i++)
            {
                CWidgetPropertyValue* val = CWidgetPropertyValue::NewL();
                CleanupStack::PushL(val);
                iPropertyValues.AppendL( val );
                CleanupStack::Pop(); // val
            }
            return;
        }
        TPtrC8 propTag( n->name );
        if ( 0 != propTag.Compare( KXmlPropTag() ) )
        {
            // TODO unrecognized subtree?
            return;
        }
        // TODO validate n->children != NULL and type XML_TEXT_NODE
        HBufC* name;
        aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &name );

        // get value array index (TWidgetPropertyId) for name
        TPtr namePtr( name->Des() );
        TInt propId =
            aXmlProcessor->GetPropertyId( namePtr );
        delete name;
        name = NULL;
        if ( EWidgetPropertyIdInvalid == propId )
        {
            User::Leave( KErrNoMemory );
        }

        for (TInt i = iPropertyValues.Count(); i <= propId; i++)
        {
            CWidgetPropertyValue* val = CWidgetPropertyValue::NewL();
            CleanupStack::PushL(val);
            iPropertyValues.AppendL( val );
            CleanupStack::Pop(); // val
        }

        n = n->children->next; // down to val
        if ( NULL == n )
        {
            User::Leave( KErrCorrupt );
        }
        TPtrC8 valTag( n->name );
        if ( 0 != valTag.Compare( KXmlValTag() ) )
        {
            User::Leave( KErrCorrupt );
        }
        if (propId >= EWidgetPropertyIdCount) // unsupported property
        {
            HBufC* value = NULL;
            if (n->children)
            {
                aXmlProcessor->GetTextContentAsStringL( aFileSession, aDoc, n->children, &value );
            }
            else
            {
                value = KNullDesC().AllocL();
            }
            (*this)[propId].iValue.s = value;
            (*this)[propId].iType = EWidgetPropTypeBlob;
            n = (n->parent)->next; // up two and next sibling
            continue;
        }
        // TODO validate n->children != NULL and type XML_TEXT_NODE
        HBufC* value;
        aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &value );
        CleanupStack::PushL( value );

        n = n->children->next; // down to type
        if ( NULL == n )
        {
            User::Leave( KErrCorrupt );
        }
        TPtrC8 typeTag( n->name );
        if ( 0 != typeTag.Compare( KXmlTypeTag() ) )
        {
            User::Leave( KErrCorrupt );
        }
        // TODO validate n->children != NULL and type XML_TEXT_NODE
        HBufC* type;
        aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &type );
        CleanupStack::PushL( type );

        // now have: name, value, type
        // convert type string to TWidgetPropertyType
        //
        // assume void/unknown is not put in XML format so anything
        // not recognized should be handled like other unrecognized
        // subtree
        TWidgetPropertyType typeEnum = EWidgetPropTypeUnknown;
        if ( 0 == type->Des().Compare( KXmlDataTypeBool() ) )
        {
            typeEnum = EWidgetPropTypeBool;
        }
        else if ( 0 == type->Des().Compare( KXmlDataTypeInt() ) )
        {
            typeEnum = EWidgetPropTypeInt;
        }
        else if ( 0 == type->Des().Compare( KXmlDataTypeString() ) )
        {
            typeEnum = EWidgetPropTypeString;
        }
        else if ( 0 == type->Des().Compare( KXmlDataTypeUid() ) )
        {
            typeEnum = EWidgetPropTypeUid;
        }
        CleanupStack::PopAndDestroy( type );

        // TODO handle unknown type due to future extensions: add prop
        // subtree to list of unrecognized subtrees

        // set prop according to type
        switch ( typeEnum )
        {
        case EWidgetPropTypeBool:
            if ( 0 == value->Des().Compare( _L("0") ) )
            {
                (*this)[propId].iValue.i = 0;
            }
            else
            {
                (*this)[propId].iValue.i = 1;
            }
            break;
        case EWidgetPropTypeInt:
        {
            TLex toInt( value->Des() );
            TInt k;
            if ( KErrNone != toInt.Val( k ) )
            {
                User::Leave( KErrCorrupt );
            }
            (*this)[propId].iValue.i = k;
        }
        break;
        case EWidgetPropTypeString:
            (*this)[propId].iValue.s = value;
            break;
        case EWidgetPropTypeUid:
        {
            TLex toUid( value->Des() );
            TInt u;
            if ( KErrNone != toUid.Val( u ) )
            {
                User::Leave( KErrCorrupt );
            }
            (*this)[propId].iValue.uid = TUid::Uid( u );
        }
        break;
        };

        (*this)[propId].iType = typeEnum;

        CleanupStack::Pop( value );
        if ( EWidgetPropTypeString != typeEnum )
        {
            delete value;
        }

        n = ((n->parent)->parent)->next; // up two and next sibling
    }
}
Ejemplo n.º 2
0
/**
	Convert entity to xml dom document
   	\param preferAttrib - if true (default) generate attributes, else elements
	... <attr name="atrname" val="atrVal"/> ... vs <atrname>atrVal<atrname/>
	\param skipEmpty	- if true (default) skip empty values
	\param upperCase	- if true - uppercase all tags (default is false)
	\return Dom document reference
*/
QDomDocument& RecordBase::GetDom
(
	bool preferAttrib,
	bool skipEmpty,
	bool upperCase
)
{
	QString idTag(kTagID);
	QString attrTag(kTagAttr);
	QString nameTag(kTagName);
	QString valTag(kTagValue);
	QString entName(EntityName());

	if (upperCase) 
	{
		entName = entName.toUpper();
		idTag   = idTag.toUpper();
		attrTag = attrTag.toUpper();
		nameTag = nameTag.toUpper();
		valTag  = valTag.toUpper();
	}

	if (_xmlDoc)
		delete _xmlDoc;

	QDomElement root;

	_xmlDoc = new QDomDocument;
	
	root = _xmlDoc->createElement(entName);
    _xmlDoc->appendChild(root);
	// append id attrib to root

	root.setAttribute(idTag, Key());
	// enumerate fields
	for (quint32 i(1); i < Count(); i++) 
	{
		QString name = Field(i);
		QString v = Value(i).toString().trimmed();
		
		if (v.length() > 0 || (v.length() == 0 && !skipEmpty)) 
		{
			if (upperCase) 
				name = name.toUpper();

			if (preferAttrib) 
			{
				QDomElement elem = _xmlDoc->createElement(attrTag);
				elem.setAttribute(nameTag, name);
				elem.setAttribute(valTag , v);
				root.appendChild (elem);
			}
			else // use elements
			{
				
				QDomElement elem = _xmlDoc->createElement(name);
				QDomText txt  = _xmlDoc->createTextNode(v);
				elem.appendChild(txt);
				root.appendChild(elem);
			}
		}
	}

	return *_xmlDoc;
}