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 | |
7fd59977 |
16 | |
83ae3591 |
17 | #include <Message_Messenger.hxx> |
42cf5bc1 |
18 | #include <LDOM_MemManager.hxx> |
19 | #include <Standard_Type.hxx> |
7fd59977 |
20 | #include <TDataStd_ReferenceArray.hxx> |
42cf5bc1 |
21 | #include <TDF_Attribute.hxx> |
22 | #include <TDF_Label.hxx> |
23 | #include <TDF_Tool.hxx> |
24 | #include <XmlMDataStd_ReferenceArrayDriver.hxx> |
25 | #include <XmlObjMgt.hxx> |
26 | #include <XmlObjMgt_Document.hxx> |
27 | #include <XmlObjMgt_Persistent.hxx> |
7fd59977 |
28 | |
92efcf78 |
29 | IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ReferenceArrayDriver,XmlMDF_ADriver) |
7fd59977 |
30 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
31 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
32 | IMPLEMENT_DOMSTRING (ExtString, "string") |
5a1271c8 |
33 | IMPLEMENT_DOMSTRING (AttributeIDString, "refarrattguid") |
7fd59977 |
34 | //======================================================================= |
35 | //function : XmlMDataStd_ReferenceArrayDriver |
36 | //purpose : Constructor |
37 | //======================================================================= |
83ae3591 |
38 | XmlMDataStd_ReferenceArrayDriver::XmlMDataStd_ReferenceArrayDriver(const Handle(Message_Messenger)& theMsgDriver) |
7fd59977 |
39 | : XmlMDF_ADriver (theMsgDriver, NULL) |
40 | { |
41 | |
42 | } |
43 | |
44 | //======================================================================= |
45 | //function : NewEmpty |
46 | //purpose : |
47 | //======================================================================= |
48 | Handle(TDF_Attribute) XmlMDataStd_ReferenceArrayDriver::NewEmpty() const |
49 | { |
50 | return new TDataStd_ReferenceArray(); |
51 | } |
52 | |
53 | //======================================================================= |
54 | //function : Paste |
55 | //purpose : persistent -> transient (retrieve) |
56 | //======================================================================= |
57 | Standard_Boolean XmlMDataStd_ReferenceArrayDriver::Paste(const XmlObjMgt_Persistent& theSource, |
5a1271c8 |
58 | const Handle(TDF_Attribute)& theTarget, |
59 | XmlObjMgt_RRelocationTable& ) const |
7fd59977 |
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 ReferenceArray attribute as \"") |
73 | + aFirstIndex + "\""; |
83ae3591 |
74 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
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 ReferenceArray attribute as \"") |
84 | + aFirstIndex + "\""; |
83ae3591 |
85 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
86 | return Standard_False; |
87 | } |
88 | |
89 | Handle(TDataStd_ReferenceArray) aReferenceArray = Handle(TDataStd_ReferenceArray)::DownCast(theTarget); |
90 | aReferenceArray->Init(aFirstInd, aLastInd); |
91 | |
cbc4faa9 |
92 | // attribute id |
93 | Standard_GUID aGUID; |
94 | XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString()); |
95 | if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL) |
96 | aGUID = TDataStd_ReferenceArray::GetID(); //default case |
97 | else |
98 | aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case |
99 | |
100 | aReferenceArray->SetID(aGUID); |
101 | |
7fd59977 |
102 | if (!anElement.hasChildNodes()) |
103 | { |
104 | TCollection_ExtendedString aMessageString = |
105 | TCollection_ExtendedString("Cannot retrieve a Array of reference"); |
83ae3591 |
106 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
107 | return Standard_False; |
108 | } |
109 | |
110 | LDOM_Node aCurNode = anElement.getFirstChild(); |
111 | LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode; |
112 | XmlObjMgt_DOMString aValueStr; |
113 | Standard_Integer i = aFirstInd; |
114 | while (*aCurElement != anElement.getLastChild()) |
115 | { |
116 | aValueStr = XmlObjMgt::GetStringValue( *aCurElement ); |
117 | if (aValueStr == NULL) |
118 | { |
83ae3591 |
119 | myMessageDriver->Send ("Cannot retrieve reference string from element", Message_Fail); |
7fd59977 |
120 | return Standard_False; |
121 | } |
122 | TCollection_AsciiString anEntry; |
123 | if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False) |
124 | { |
125 | TCollection_ExtendedString aMessage = |
5a1271c8 |
126 | TCollection_ExtendedString ("Cannot retrieve reference from \"") |
127 | + aValueStr + '\"'; |
83ae3591 |
128 | myMessageDriver->Send (aMessage, Message_Fail); |
7fd59977 |
129 | return Standard_False; |
130 | } |
131 | // Find label by entry |
132 | TDF_Label tLab; // Null label. |
133 | if (anEntry.Length() > 0) |
134 | { |
135 | TDF_Tool::Label(aReferenceArray->Label().Data(), anEntry, tLab, Standard_True); |
136 | } |
137 | aReferenceArray->SetValue(i++, tLab); |
138 | aCurNode = aCurElement->getNextSibling(); |
139 | aCurElement = (LDOM_Element*)&aCurNode; |
140 | } |
141 | |
142 | // Last reference |
143 | aValueStr = XmlObjMgt::GetStringValue( *aCurElement ); |
144 | if (aValueStr == NULL) |
145 | { |
83ae3591 |
146 | myMessageDriver->Send ("Cannot retrieve reference string from element", Message_Fail); |
7fd59977 |
147 | return Standard_False; |
148 | } |
149 | TCollection_AsciiString anEntry; |
150 | if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False) |
151 | { |
152 | TCollection_ExtendedString aMessage = |
153 | TCollection_ExtendedString ("Cannot retrieve reference from \"") |
5a1271c8 |
154 | + aValueStr + '\"'; |
83ae3591 |
155 | myMessageDriver->Send (aMessage, Message_Fail); |
7fd59977 |
156 | return Standard_False; |
157 | } |
158 | // Find label by entry |
159 | TDF_Label tLab; // Null label. |
160 | if (anEntry.Length() > 0) |
161 | { |
162 | TDF_Tool::Label(aReferenceArray->Label().Data(), anEntry, tLab, Standard_True); |
163 | } |
164 | aReferenceArray->SetValue(i, tLab); |
165 | |
166 | return Standard_True; |
167 | } |
168 | |
169 | //======================================================================= |
170 | //function : Paste |
171 | //purpose : transient -> persistent (store) |
172 | //======================================================================= |
173 | void XmlMDataStd_ReferenceArrayDriver::Paste(const Handle(TDF_Attribute)& theSource, |
5a1271c8 |
174 | XmlObjMgt_Persistent& theTarget, |
175 | XmlObjMgt_SRelocationTable& ) const |
7fd59977 |
176 | { |
177 | Handle(TDataStd_ReferenceArray) aReferenceArray = Handle(TDataStd_ReferenceArray)::DownCast(theSource); |
178 | TDF_Label L = aReferenceArray->Label(); |
179 | if (L.IsNull()) |
180 | { |
83ae3591 |
181 | myMessageDriver->Send ("Label of a ReferenceArray is Null.", Message_Fail); |
7fd59977 |
182 | return; |
183 | } |
184 | |
185 | Standard_Integer aL = aReferenceArray->Lower(); |
186 | Standard_Integer anU = aReferenceArray->Upper(); |
187 | XmlObjMgt_Element& anElement = theTarget; |
188 | anElement.setAttribute(::FirstIndexString(), aL); |
189 | anElement.setAttribute(::LastIndexString(), anU); |
190 | |
e8862cf4 |
191 | XmlObjMgt_Document aDoc (anElement.getOwnerDocument()); |
7fd59977 |
192 | |
193 | for (Standard_Integer i = aL; i <= anU; i++) |
194 | { |
195 | if (L.IsDescendant(aReferenceArray->Value(i).Root())) |
196 | { |
197 | // Internal reference |
198 | TCollection_AsciiString anEntry; |
199 | TDF_Tool::Entry(aReferenceArray->Value(i), anEntry); |
200 | |
201 | XmlObjMgt_DOMString aDOMString; |
202 | XmlObjMgt::SetTagEntryString (aDOMString, anEntry); |
203 | XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() ); |
204 | XmlObjMgt::SetStringValue (aCurTarget, aDOMString, Standard_True); |
205 | anElement.appendChild( aCurTarget ); |
206 | } |
207 | } |
5a1271c8 |
208 | if(aReferenceArray->ID() != TDataStd_ReferenceArray::GetID()) { |
209 | //convert GUID |
210 | Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC]; |
211 | Standard_PCharacter pGuidStr = aGuidStr; |
212 | aReferenceArray->ID().ToCString (pGuidStr); |
213 | theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr); |
214 | } |
7fd59977 |
215 | } |