0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / XmlMDataStd / XmlMDataStd_ExtStringListDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Message_Messenger.hxx>
18 #include <LDOM_MemManager.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_ExtStringList.hxx>
21 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <XmlMDataStd_ExtStringListDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Document.hxx>
26 #include <XmlObjMgt_Persistent.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ExtStringListDriver,XmlMDF_ADriver)
29 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
30 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
31 IMPLEMENT_DOMSTRING (ExtString,        "string")
32 IMPLEMENT_DOMSTRING (AttributeIDString, "extstrlistattguid")
33
34 //=======================================================================
35 //function : XmlMDataStd_ExtStringListDriver
36 //purpose  : Constructor
37 //=======================================================================
38 XmlMDataStd_ExtStringListDriver::XmlMDataStd_ExtStringListDriver(const Handle(Message_Messenger)& theMsgDriver)
39      : XmlMDF_ADriver (theMsgDriver, NULL)
40 {
41
42 }
43
44 //=======================================================================
45 //function : NewEmpty
46 //purpose  : 
47 //=======================================================================
48 Handle(TDF_Attribute) XmlMDataStd_ExtStringListDriver::NewEmpty() const
49 {
50   return new TDataStd_ExtStringList();
51 }
52
53 //=======================================================================
54 //function : Paste
55 //purpose  : persistent -> transient (retrieve)
56 //=======================================================================
57 Standard_Boolean XmlMDataStd_ExtStringListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
58                                                         const Handle(TDF_Attribute)& theTarget,
59                                                         XmlObjMgt_RRelocationTable&  ) const
60 {
61   Standard_Integer aFirstInd, aLastInd;
62   const XmlObjMgt_Element& anElement = theSource;
63
64   // Read the FirstIndex; if the attribute is absent initialize to 1
65   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
66   if (aFirstIndex == NULL)
67     aFirstInd = 1;
68   else if (!aFirstIndex.GetInteger(aFirstInd)) 
69   {
70     TCollection_ExtendedString aMessageString =
71       TCollection_ExtendedString("Cannot retrieve the first index"
72                                  " for ExtStringList attribute as \"")
73         + aFirstIndex + "\"";
74     myMessageDriver->Send (aMessageString, Message_Fail);
75     return Standard_False;
76   }
77
78   // Read the LastIndex; the attribute should present
79   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
80   {
81     TCollection_ExtendedString aMessageString =
82       TCollection_ExtendedString("Cannot retrieve the last index"
83                                  " for ExtStringList attribute as \"")
84         + aFirstIndex + "\"";
85     myMessageDriver->Send (aMessageString, Message_Fail);
86     return Standard_False;
87   }
88
89   const Handle(TDataStd_ExtStringList) anExtStringList = Handle(TDataStd_ExtStringList)::DownCast(theTarget);
90   // attribute id
91   Standard_GUID aGUID;
92   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
93   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
94     aGUID = TDataStd_ExtStringList::GetID(); //default case
95   else
96     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
97
98   anExtStringList->SetID(aGUID);
99
100   if(aLastInd > 0) {
101     if (!anElement.hasChildNodes())
102     {
103       TCollection_ExtendedString aMessageString = 
104         TCollection_ExtendedString("Cannot retrieve a list of extended strings");
105       myMessageDriver->Send (aMessageString, Message_Warning);
106     }
107
108     LDOM_Node aCurNode = anElement.getFirstChild();
109     LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
110     TCollection_ExtendedString aValueStr;
111     while (*aCurElement != anElement.getLastChild())
112     {
113       XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
114       anExtStringList->Append(aValueStr);
115       aCurNode = aCurElement->getNextSibling();
116       aCurElement = (LDOM_Element*)&aCurNode;
117     }
118
119     XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
120     anExtStringList->Append(aValueStr);
121   }
122
123   return Standard_True;
124 }
125
126 //=======================================================================
127 //function : Paste
128 //purpose  : transient -> persistent (store)
129 //=======================================================================
130 void XmlMDataStd_ExtStringListDriver::Paste(const Handle(TDF_Attribute)& theSource,
131                                             XmlObjMgt_Persistent&        theTarget,
132                                             XmlObjMgt_SRelocationTable&  ) const
133 {
134   const Handle(TDataStd_ExtStringList) anExtStringList = Handle(TDataStd_ExtStringList)::DownCast(theSource);
135
136   Standard_Integer anU = anExtStringList->Extent();
137   XmlObjMgt_Element& anElement = theTarget;
138   anElement.setAttribute(::LastIndexString(), anU);
139   
140   XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
141   
142   TDataStd_ListIteratorOfListOfExtendedString itr(anExtStringList->List());
143   for (; itr.More(); itr.Next())
144   {
145     const TCollection_ExtendedString& aValueStr = itr.Value();
146     XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
147     XmlObjMgt::SetExtendedString( aCurTarget, aValueStr );
148     anElement.appendChild( aCurTarget );
149   }
150
151   if(anExtStringList->ID() != TDataStd_ExtStringList::GetID()) {
152     //convert GUID
153     Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
154     Standard_PCharacter pGuidStr = aGuidStr;
155     anExtStringList->ID().ToCString (pGuidStr);
156     theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
157   }
158 }