xml/xmldomandxpath/src/xmlenginedom/xmlengcharacterdata.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Methods for all kind of text nodes
       
    15 //
       
    16 
       
    17 #include <xml/dom/xmlengcharacterdata.h>
       
    18 #include <stdapis/libxml2/libxml2_tree.h>
       
    19 #include "xmlengdomdefs.h"
       
    20 #include <stdapis/libxml2/libxml2_globals.h>
       
    21 #include <xml/utils/xmlengxestrings.h>		//pjj18 new
       
    22 
       
    23 #define CHARACTER_DATA (static_cast<xmlNodePtr>(iInternal))
       
    24 
       
    25 // -------------------------------------------------------------------------------
       
    26 // @return Character contents of the node
       
    27 //
       
    28 // This method applies to TXmlEngCDATASection, TXmlEngComment and TXmlEngTextNode nodes.
       
    29 // -------------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C TPtrC8 TXmlEngCharacterData::Contents() const
       
    32     {
       
    33     return ((TXmlEngConstString)CAST_XMLCHAR_TO_DOMSTRING(LIBXML_NODE ? LIBXML_NODE->content : NULL)).PtrC8();
       
    34     }
       
    35     
       
    36 // -------------------------------------------------------------------------------
       
    37 // Sets contents of basic character nodes: TXmlEngTextNode, TXmlEngComment, TXmlEngCDATASection
       
    38 //
       
    39 // @param aNewContents  The actual value to store
       
    40 //
       
    41 // The input is taken as non-escaped: for example, 
       
    42 // aNewContents = "123 > 34 && P" will be serialized as "123 &gt; 34 &amp;&amp; P"
       
    43 //
       
    44 // Escaped contents may be set only for TXmlEngElement and TXmlEngAttr nodes.
       
    45 // @see TXmlEngAttr::SetRawValueL(), TXmlEngElement::SetRawTextL()
       
    46 // -------------------------------------------------------------------------------
       
    47 //
       
    48 EXPORT_C void TXmlEngCharacterData::SetContentsL(
       
    49     const TDesC8& aNewContents )
       
    50     {
       
    51     char* newContent = (char*)new(ELeave) TUint8[aNewContents.Length() + 1];
       
    52     *(Mem::Copy((TAny*)newContent, aNewContents.Ptr(), aNewContents.Length())) = 0;
       
    53     if (LIBXML_NODE->content
       
    54             &&
       
    55          !(LIBXML_NODE->doc       &&
       
    56            LIBXML_NODE->doc->dict &&
       
    57            xmlDictOwns(LIBXML_NODE->doc->dict, LIBXML_NODE->content)))
       
    58         {
       
    59         xmlFree(LIBXML_NODE->content);
       
    60         }
       
    61     
       
    62     LIBXML_NODE->content = (unsigned char *)newContent;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // Extends the contents of the node by appending aString
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C void TXmlEngCharacterData::AppendContentsL(
       
    70     const TDesC8& aString)
       
    71     {
       
    72     if (!LIBXML_NODE) 
       
    73     	{
       
    74     	User::Leave(KXmlEngErrNullNode);
       
    75     	}
       
    76     
       
    77     TPtrC8 prevContent = Contents();
       
    78     TInt len = prevContent.Length() + aString.Length() + 1;
       
    79     char* newContent = (char*)new(ELeave) TUint8[len];
       
    80     
       
    81     TAny* last = Mem::Copy((TAny*)newContent, prevContent.Ptr(), prevContent.Length());
       
    82     *(Mem::Copy(last, aString.Ptr(), aString.Length())) = 0;
       
    83     
       
    84     if (LIBXML_NODE->content
       
    85             &&
       
    86          !(LIBXML_NODE->doc       &&
       
    87            LIBXML_NODE->doc->dict &&
       
    88            xmlDictOwns(LIBXML_NODE->doc->dict, LIBXML_NODE->content)))
       
    89         {
       
    90         xmlFree(LIBXML_NODE->content);
       
    91         }
       
    92     
       
    93     LIBXML_NODE->content = (unsigned char *)newContent;
       
    94     }
       
    95 
       
    96 // -------------------------------------------------------------------------------
       
    97 // @return Number of characters in the contents
       
    98 // -------------------------------------------------------------------------------
       
    99 //
       
   100 EXPORT_C TUint TXmlEngCharacterData::Length() const
       
   101     {
       
   102     return CHARACTER_DATA && CHARACTER_DATA->content ?
       
   103         xmlUTF8Strlen(CHARACTER_DATA->content) : 0;
       
   104     }