b311480e |
1 | // Created on: 2007-05-29 |
2 | // Created by: Vlad Romashko |
973c2be1 |
3 | // Copyright (c) 2007-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
42cf5bc1 |
16 | |
17 | #include <CDM_MessageDriver.hxx> |
18 | #include <NCollection_LocalArray.hxx> |
19 | #include <Standard_Type.hxx> |
7fd59977 |
20 | #include <TDataStd_BooleanList.hxx> |
21 | #include <TDataStd_ListIteratorOfListOfByte.hxx> |
42cf5bc1 |
22 | #include <TDF_Attribute.hxx> |
23 | #include <XmlMDataStd_BooleanListDriver.hxx> |
7fd59977 |
24 | #include <XmlObjMgt.hxx> |
42cf5bc1 |
25 | #include <XmlObjMgt_Persistent.hxx> |
7fd59977 |
26 | |
92efcf78 |
27 | IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_BooleanListDriver,XmlMDF_ADriver) |
7fd59977 |
28 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
29 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
30 | |
31 | //======================================================================= |
32 | //function : XmlMDataStd_BooleanListDriver |
33 | //purpose : Constructor |
34 | //======================================================================= |
35 | XmlMDataStd_BooleanListDriver::XmlMDataStd_BooleanListDriver(const Handle(CDM_MessageDriver)& theMsgDriver) |
36 | : XmlMDF_ADriver (theMsgDriver, NULL) |
37 | { |
38 | |
39 | } |
40 | |
41 | //======================================================================= |
42 | //function : NewEmpty |
43 | //purpose : |
44 | //======================================================================= |
45 | Handle(TDF_Attribute) XmlMDataStd_BooleanListDriver::NewEmpty() const |
46 | { |
47 | return new TDataStd_BooleanList(); |
48 | } |
49 | |
50 | //======================================================================= |
51 | //function : Paste |
52 | //purpose : persistent -> transient (retrieve) |
53 | //======================================================================= |
54 | Standard_Boolean XmlMDataStd_BooleanListDriver::Paste(const XmlObjMgt_Persistent& theSource, |
55 | const Handle(TDF_Attribute)& theTarget, |
56 | XmlObjMgt_RRelocationTable& ) const |
57 | { |
58 | Standard_Integer aFirstInd, aLastInd, aValue, ind; |
59 | const XmlObjMgt_Element& anElement = theSource; |
60 | |
61 | // Read the FirstIndex; if the attribute is absent initialize to 1 |
62 | XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString()); |
63 | if (aFirstIndex == NULL) |
64 | aFirstInd = 1; |
65 | else if (!aFirstIndex.GetInteger(aFirstInd)) |
66 | { |
67 | TCollection_ExtendedString aMessageString = |
68 | TCollection_ExtendedString("Cannot retrieve the first index" |
69 | " for BooleanList attribute as \"") |
70 | + aFirstIndex + "\""; |
71 | WriteMessage (aMessageString); |
72 | return Standard_False; |
73 | } |
74 | |
75 | // Read the LastIndex; the attribute should be present |
76 | if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) |
77 | { |
78 | TCollection_ExtendedString aMessageString = |
79 | TCollection_ExtendedString("Cannot retrieve the last index" |
80 | " for BooleanList attribute as \"") |
81 | + aFirstIndex + "\""; |
82 | WriteMessage (aMessageString); |
83 | return Standard_False; |
84 | } |
85 | |
d585e74e |
86 | const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theTarget); |
87 | if(aLastInd == 0) aFirstInd = 0; |
88 | if (aFirstInd == aLastInd && aLastInd > 0) |
7fd59977 |
89 | { |
90 | Standard_Integer anInteger; |
91 | if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger)) |
92 | { |
93 | TCollection_ExtendedString aMessageString = |
94 | TCollection_ExtendedString("Cannot retrieve integer member" |
95 | " for BooleanList attribute as \""); |
96 | WriteMessage (aMessageString); |
97 | return Standard_False; |
98 | } |
99 | aBooleanList->Append(anInteger ? Standard_True : Standard_False); |
100 | } |
d585e74e |
101 | else if(aLastInd >= 1) |
7fd59977 |
102 | { |
103 | Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString()); |
104 | for (ind = aFirstInd; ind <= aLastInd; ind++) |
105 | { |
106 | if (!XmlObjMgt::GetInteger(aValueStr, aValue)) |
107 | { |
108 | TCollection_ExtendedString aMessageString = |
109 | TCollection_ExtendedString("Cannot retrieve integer member" |
110 | " for BooleanList attribute as \"") |
111 | + aValueStr + "\""; |
112 | WriteMessage (aMessageString); |
113 | return Standard_False; |
114 | } |
115 | aBooleanList->Append(aValue ? Standard_True : Standard_False); |
116 | } |
117 | } |
118 | |
119 | return Standard_True; |
120 | } |
121 | |
122 | //======================================================================= |
123 | //function : Paste |
124 | //purpose : transient -> persistent (store) |
125 | //======================================================================= |
126 | void XmlMDataStd_BooleanListDriver::Paste(const Handle(TDF_Attribute)& theSource, |
127 | XmlObjMgt_Persistent& theTarget, |
128 | XmlObjMgt_SRelocationTable& ) const |
129 | { |
d585e74e |
130 | const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theSource); |
7fd59977 |
131 | |
132 | Standard_Integer anU = aBooleanList->Extent(); |
7fd59977 |
133 | theTarget.Element().setAttribute(::LastIndexString(), anU); |
d585e74e |
134 | // Allocation of 1 char for each boolean value + a space. |
135 | NCollection_LocalArray<Standard_Character> str(2 * anU + 1); |
136 | if(anU == 0) str[0] = 0; |
137 | else if (anU >= 1) |
7fd59977 |
138 | { |
d585e74e |
139 | Standard_Integer iChar(0); |
7fd59977 |
140 | TDataStd_ListIteratorOfListOfByte itr(aBooleanList->List()); |
141 | for (; itr.More(); itr.Next()) |
142 | { |
f7b4312f |
143 | const Standard_Byte& byte = itr.Value(); |
144 | iChar += Sprintf(&(str[iChar]), "%d ", byte); |
7fd59977 |
145 | } |
146 | } |
d585e74e |
147 | XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True); |
7fd59977 |
148 | } |