webservices/wsutils/src/senxmlproperties.cpp
changeset 0 62f9d29f7211
child 11 6abf3d6055cd
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:      
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include <s32strm.h> // RWriteStream
       
    26 
       
    27 #include <SenXmlReader.h>
       
    28 #include <SenXmlUtils.h>
       
    29 #include <SenBaseAttribute.h>
       
    30 #include "SenXmlProperties.h"
       
    31 #include "senpropertiesfragment.h"
       
    32 #include "senxmldebug.h"
       
    33 
       
    34 namespace
       
    35     {
       
    36     const TInt KFlatBufSize     = 64;  // CBufFlat
       
    37     }
       
    38 
       
    39 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewL()
       
    40     {
       
    41     CSenXmlProperties* pNew = NewLC();
       
    42     CleanupStack::Pop(); // pNew
       
    43     return pNew;
       
    44     }
       
    45 
       
    46 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewLC()
       
    47     {
       
    48     CSenXmlProperties* pNew = new (ELeave) CSenXmlProperties;
       
    49     CleanupStack::PushL(pNew);
       
    50 
       
    51     // omit parsing by passing zero-length descriptor
       
    52     pNew->BaseConstructL(KSenXmlPropertiesLocalname,
       
    53                          KNullDesC8,
       
    54                          NULL);
       
    55     return pNew;
       
    56     }
       
    57 
       
    58 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewL(const TDesC8& aXmlUtf8,
       
    59                                            CSenXmlReader& aParser)
       
    60     {
       
    61     CSenXmlProperties* pNew = NewLC(aXmlUtf8, aParser);
       
    62     CleanupStack::Pop(); // pNew
       
    63     return pNew;
       
    64     }
       
    65 
       
    66 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewLC(const TDesC8& aXmlUtf8,
       
    67                                             CSenXmlReader& aParser)
       
    68     {
       
    69     CSenXmlProperties* pNew = new (ELeave) CSenXmlProperties;
       
    70     CleanupStack::PushL(pNew);
       
    71 
       
    72     // parse the XML document  descriptor into DOM fragment
       
    73     pNew->BaseConstructL(KSenXmlPropertiesLocalname,
       
    74                          aXmlUtf8,
       
    75                          &aParser);
       
    76     return pNew;
       
    77     }
       
    78 
       
    79 
       
    80 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewL(const CSenElement& aElement)
       
    81     {
       
    82     CSenXmlProperties* pNew = NewLC(aElement);
       
    83     CleanupStack::Pop(); // pNew
       
    84     return pNew;
       
    85     }
       
    86 
       
    87 EXPORT_C CSenXmlProperties* CSenXmlProperties::NewLC(const CSenElement& aElement)
       
    88     {
       
    89     CSenXmlProperties* pNew = new (ELeave) CSenXmlProperties;
       
    90     CleanupStack::PushL(pNew);
       
    91 
       
    92     // parse the XML document  descriptor into DOM fragment
       
    93     pNew->BaseConstructL(aElement);
       
    94     return pNew;
       
    95     }
       
    96     
       
    97 void CSenXmlProperties::SetReader(CSenXmlReader& aReader)
       
    98     {
       
    99     // de-allocate the owned reader, if one exists:
       
   100     if( ipReader )
       
   101         {
       
   102         delete ipReader; 
       
   103         ipReader = NULL;
       
   104         }        
       
   105     // Note: ipFragment does not own the reader 
       
   106     // behind the given reference (aReader) 
       
   107     ipFragment->SetReader(aReader); 
       
   108     }    
       
   109 
       
   110 MSenProperties::TSenPropertiesClassType CSenXmlProperties::PropertiesClassType() 
       
   111     { 
       
   112     return ESenXmlProperties; 
       
   113     }
       
   114 
       
   115 void CSenXmlProperties::WriteToL(RWriteStream& aWriteStream)
       
   116     {
       
   117     ipFragment->WriteAsXMLToL(aWriteStream);
       
   118     }
       
   119 
       
   120 void CSenXmlProperties::ReadFromL(const TDesC8& aBuffer)
       
   121     {
       
   122     // Sanity check: ignore zero-length strings
       
   123     if ( aBuffer.Length() == 0)
       
   124         {
       
   125         return;
       
   126         }
       
   127     // Check if fragment already has a reference to *some*
       
   128     // XML reader
       
   129     if ( !ipFragment->Reader() )
       
   130         {
       
   131         // Check if there is already an owned reader instance
       
   132         if( !ipReader )
       
   133             {
       
   134             ipReader = CSenXmlReader::NewL(KXmlParserMimeType); // use libxml2 sax parser
       
   135             }
       
   136         // Sets reference of that XML reader for this fragment:
       
   137         ipFragment->ParseWithL(*ipReader);
       
   138         }
       
   139     ipFragment->ParseL(aBuffer);   
       
   140     }
       
   141 
       
   142 HBufC8* CSenXmlProperties::AsUtf8L()
       
   143     {
       
   144     return ipFragment->AsXmlL();
       
   145     }
       
   146 
       
   147 HBufC8* CSenXmlProperties::AsUtf8LC()
       
   148     {
       
   149     HBufC8* pAsXml = ipFragment->AsXmlL();
       
   150     CleanupStack::PushL(pAsXml);
       
   151     return pAsXml;
       
   152     }
       
   153     
       
   154 TBool CSenXmlProperties::IsSafeToCast(TSenPropertiesClassType aClass)
       
   155     {
       
   156     if ( aClass == MSenProperties::ESenXmlProperties )
       
   157         {
       
   158         return ETrue;
       
   159         }
       
   160     else
       
   161         {
       
   162         return EFalse; // has only abstact, layered TP super classes 
       
   163         }
       
   164     }      
       
   165 
       
   166 TInt CSenXmlProperties::SetPropertyL(const TDesC8& aName,
       
   167                                      const TDesC8& aValue)
       
   168     {
       
   169 	CSenElement* pChildElement = ipFragment->AsElement().Element(aName);
       
   170 	if ( !pChildElement )
       
   171 		{
       
   172 		// Add new element
       
   173 		pChildElement =  &ipFragment->AsElement().AddElementL(aName);
       
   174 		}
       
   175 	pChildElement->SetContentL(aValue);
       
   176     return KErrNone;
       
   177     }
       
   178 
       
   179 TInt CSenXmlProperties::PropertyL(const TDesC8& aName, TPtrC8& aValue)
       
   180     {
       
   181 	CSenElement* pElement = ipFragment->AsElement().Element(aName);
       
   182 	if ( pElement )
       
   183 		{
       
   184 		aValue.Set(pElement->Content());
       
   185         return KErrNone;
       
   186 		}
       
   187     else
       
   188         {
       
   189         return KErrNotFound;
       
   190         }
       
   191     }
       
   192     
       
   193 TInt CSenXmlProperties::SetPropertyL(const TDesC8& aName,
       
   194                                      const TDesC8& aValue,
       
   195                                      const TDesC8& aType)
       
   196     {
       
   197 	CSenElement* pElement = ipFragment->AsElement().Element(aName);
       
   198 	if ( !pElement )
       
   199 		{
       
   200 		// Add new element
       
   201 		pElement =  &ipFragment->AsElement().AddElementL(aName);
       
   202 		}
       
   203 	pElement->SetContentL(aValue);
       
   204 	SenXmlUtils::AddAttributeL(*pElement, KSenTypeAttributeName, aType);
       
   205     return KErrNone;
       
   206     }
       
   207 
       
   208 TInt CSenXmlProperties::PropertyL(const TDesC8& aName,
       
   209                                   TPtrC8& aValue,
       
   210                                   TPtrC8& aType)
       
   211     {
       
   212 	CSenElement* pElement = ipFragment->AsElement().Element(aName);
       
   213 	if ( pElement )
       
   214 		{
       
   215 		aValue.Set(pElement->Content());
       
   216 		aType.Set(*pElement->AttrValue(KSenTypeAttributeName));
       
   217         return KErrNone;
       
   218 		}
       
   219     else
       
   220         {
       
   221         return KErrNotFound;
       
   222         }
       
   223     }    
       
   224     
       
   225 TInt CSenXmlProperties::SetIntPropertyL(const TDesC8& aName, const TInt aValue)
       
   226     {
       
   227     TBuf8<KFlatBufSize> buffer;
       
   228     buffer.Num(aValue);
       
   229     return SetPropertyL(aName, buffer);
       
   230     }
       
   231 
       
   232 TInt CSenXmlProperties::IntPropertyL(const TDesC8& aName, TInt& aValue)
       
   233     {
       
   234     TPtrC8 value;
       
   235     TInt retVal(KErrNone);
       
   236     retVal = PropertyL(aName, value);
       
   237     if ( retVal == KErrNone )
       
   238         {
       
   239         TLex8 lex(value);
       
   240         retVal = lex.Val(aValue);
       
   241         }
       
   242     return retVal;
       
   243     }
       
   244     
       
   245 TInt CSenXmlProperties::SetBoolPropertyL(const TDesC8& aName, const TBool aValue)
       
   246     {
       
   247     if ( aValue )
       
   248         {
       
   249         return SetPropertyL(aName, KSenPropertyTrue);
       
   250         }
       
   251     else
       
   252         {
       
   253         return SetPropertyL(aName, KSenPropertyFalse);
       
   254         }
       
   255     }
       
   256 
       
   257 TInt CSenXmlProperties::BoolPropertyL(const TDesC8& aName, TBool& aValue)
       
   258     {
       
   259     TInt retVal(KErrNone);
       
   260     TPtrC8 value;
       
   261     retVal = PropertyL(aName, value);
       
   262     if ( retVal == KErrNone )
       
   263         {
       
   264         if (value == KSenPropertyTrue)
       
   265             {
       
   266             aValue = ETrue;
       
   267             }
       
   268         else if (value == KSenPropertyFalse)
       
   269             {
       
   270             aValue = EFalse;
       
   271             }
       
   272         else
       
   273             {
       
   274             retVal = KErrGeneral;
       
   275             }
       
   276         }
       
   277     return retVal;
       
   278     }    
       
   279 
       
   280 TInt CSenXmlProperties::SetOmittedL(const TDesC8& aName, TBool aValue)
       
   281     {
       
   282     TBool newElementCreated = EFalse;
       
   283  	CSenElement* pElement = ipFragment->AsElement().Element(aName);
       
   284 	if ( !pElement )
       
   285 		{
       
   286 		// Add new element
       
   287 		pElement =  &ipFragment->AsElement().AddElementL(aName);
       
   288 		newElementCreated = ETrue;
       
   289 		}
       
   290     else
       
   291         {
       
   292         const TDesC8* pVal = pElement->AttrValue(KSenOmittedAttributeName);
       
   293         if ( pVal )
       
   294             {
       
   295             CSenBaseAttribute* pAttribute = SenXmlUtils::RemoveAttributeL(*pElement, KSenOmittedAttributeName);
       
   296             delete pAttribute;
       
   297             }
       
   298         }
       
   299       
       
   300     if ( pElement )  
       
   301         {
       
   302         if ( aValue )
       
   303             {
       
   304             if ( newElementCreated )
       
   305                 {
       
   306                 SenXmlUtils::AddAttributeL(*pElement, KSenOmittedAttributeName, KSenOmittedTrueNoValue);
       
   307                 }
       
   308             else
       
   309                 {
       
   310                 SenXmlUtils::AddAttributeL(*pElement, KSenOmittedAttributeName, KSenPropertyTrue);
       
   311                 }
       
   312             }
       
   313         else
       
   314             {
       
   315             SenXmlUtils::AddAttributeL(*pElement, KSenOmittedAttributeName, KSenPropertyFalse);
       
   316             }
       
   317         }
       
   318     else
       
   319         {
       
   320         return KErrNotFound;
       
   321         }
       
   322         
       
   323     return KErrNone;   
       
   324     }
       
   325     
       
   326 TInt CSenXmlProperties::RemovePropertyL(const TDesC8& aName)
       
   327     {
       
   328  	CSenElement* pElement = ipFragment->AsElement().RemoveElement(aName);
       
   329 	if ( pElement )
       
   330 		{
       
   331 		delete pElement;
       
   332 		return KErrNone;
       
   333 		}
       
   334     else
       
   335         {
       
   336         return KErrNotFound;
       
   337         }
       
   338     }
       
   339 
       
   340 CSenXmlProperties::~CSenXmlProperties()
       
   341     {
       
   342     delete ipReader;
       
   343     delete ipFragment;
       
   344     }
       
   345 
       
   346 void CSenXmlProperties::BaseConstructL(const TDesC8& aLocalname, 
       
   347                                        const TDesC8& aXml,
       
   348                                        CSenXmlReader* aParser)
       
   349     {
       
   350     ipFragment = CSenPropertiesFragment::NewL(aLocalname);
       
   351     if ( aParser && aXml != KNullDesC8 )
       
   352         {
       
   353         // Do parsing
       
   354         ipFragment->ParseWithL(*aParser);
       
   355         ipFragment->ParseL(aXml);
       
   356         }
       
   357    }
       
   358 
       
   359 void CSenXmlProperties::BaseConstructL(const TDesC8& aNamespace, 
       
   360                                        const TDesC8& aLocalname, 
       
   361                                        const TDesC8& aXml,
       
   362                                        CSenXmlReader* aParser)
       
   363     {
       
   364     ipFragment = CSenPropertiesFragment::NewL(aNamespace, aLocalname);
       
   365     if ( aParser && aXml != KNullDesC8 )
       
   366         {
       
   367         // Do parsing
       
   368         ipFragment->ParseWithL(*aParser);
       
   369         ipFragment->ParseL(aXml);
       
   370         }
       
   371     }
       
   372 
       
   373 void CSenXmlProperties::BaseConstructL(const TDesC8& aNamespace, 
       
   374                                        const TDesC8& aLocalname, 
       
   375                                        const TDesC8& aQualifiedName, 
       
   376                                        const TDesC8& aXml,
       
   377                                        CSenXmlReader* aParser)
       
   378     {
       
   379     ipFragment = CSenPropertiesFragment::NewL(aNamespace, 
       
   380                                         aLocalname, 
       
   381                                         aQualifiedName);
       
   382     if ( aParser && aXml != KNullDesC8 )
       
   383         {
       
   384         // Do parsing
       
   385         ipFragment->ParseWithL(*aParser);
       
   386         ipFragment->ParseL(aXml);
       
   387         }
       
   388     }
       
   389 
       
   390 void CSenXmlProperties::BaseConstructL(const CSenElement& aElement)
       
   391     {
       
   392     ipFragment = CSenPropertiesFragment::NewL(aElement.LocalName());
       
   393     CSenElement& element = ipFragment->AsElement();
       
   394     element.CopyFromL((CSenElement&)aElement);
       
   395     }
       
   396 
       
   397 CSenXmlProperties::CSenXmlProperties()
       
   398 :   ipFragment(NULL),
       
   399     ipReader(NULL)
       
   400     {
       
   401     }
       
   402     
       
   403 MSenProperties* CSenXmlProperties::CloneL() const
       
   404     {
       
   405     CSenElement& element = ipFragment->AsElement();
       
   406     
       
   407     CSenXmlProperties* pProperties = this->NewLC(element);
       
   408     CleanupStack::Pop(pProperties);
       
   409     return pProperties;
       
   410     }
       
   411 
       
   412 MSenProperties* CSenXmlProperties::Clone(TInt& aOkOrError) const
       
   413     {
       
   414     MSenProperties* pClone = NULL;
       
   415     TRAP( aOkOrError, pClone = CloneL(); )
       
   416     return pClone;
       
   417     }
       
   418     
       
   419 
       
   420 // END OF FILE
       
   421 
       
   422