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