0029443: It is not possible to store an ExtStringArray Ocaf attribute to any previous...
[occt.git] / src / XmlMDataStd / XmlMDataStd_ExtStringArrayDriver.cxx
CommitLineData
b311480e 1// Created on: 2004-09-27
2// Created by: Pavel TELKOV
973c2be1 3// Copyright (c) 2004-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>
7fd59977 18#include <LDOM_MemManager.hxx>
42cf5bc1 19#include <Standard_Type.hxx>
20#include <TDataStd_ExtStringArray.hxx>
21#include <TDF_Attribute.hxx>
22#include <XmlMDataStd.hxx>
23#include <XmlMDataStd_ExtStringArrayDriver.hxx>
7fd59977 24#include <XmlObjMgt.hxx>
25#include <XmlObjMgt_Document.hxx>
42cf5bc1 26#include <XmlObjMgt_Persistent.hxx>
93445088 27#include <XmlLDrivers.hxx>
7fd59977 28
92efcf78 29IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ExtStringArrayDriver,XmlMDF_ADriver)
7fd59977 30IMPLEMENT_DOMSTRING (FirstIndexString, "first")
31IMPLEMENT_DOMSTRING (LastIndexString, "last")
32IMPLEMENT_DOMSTRING (ExtString, "string")
33IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
e9947e12 34IMPLEMENT_DOMSTRING (Separator, "separator")
5a1271c8 35IMPLEMENT_DOMSTRING (AttributeIDString, "extstrarrattguid")
e9947e12 36
37// Searches for a symbol within an array of strings.
38// Returns TRUE if the symbol is found.
39static Standard_Boolean Contains(const Handle(TDataStd_ExtStringArray)& arr,
40 const TCollection_ExtendedString& c)
41{
42 for (Standard_Integer i = arr->Lower(); i <= arr->Upper(); i++)
43 {
44 const TCollection_ExtendedString& value = arr->Value(i);
45 if (value.Search(c) != -1)
46 {
47 return Standard_True;
48 }
49 }
50 return Standard_False;
51}
52
7fd59977 53//=======================================================================
54//function : XmlMDataStd_ExtStringArrayDriver
55//purpose : Constructor
56//=======================================================================
57
58XmlMDataStd_ExtStringArrayDriver::XmlMDataStd_ExtStringArrayDriver
83ae3591 59 ( const Handle(Message_Messenger)& theMsgDriver )
7fd59977 60: XmlMDF_ADriver( theMsgDriver, NULL )
61{}
62
63//=======================================================================
64//function : NewEmpty
65//purpose :
66//=======================================================================
67Handle(TDF_Attribute) XmlMDataStd_ExtStringArrayDriver::NewEmpty() const
68{
69 return ( new TDataStd_ExtStringArray() );
70}
71
72//=======================================================================
73//function : Paste
74//purpose : persistent -> transient (retrieve)
75//=======================================================================
76Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
5a1271c8 77 (const XmlObjMgt_Persistent& theSource,
78 const Handle(TDF_Attribute)& theTarget,
79 XmlObjMgt_RRelocationTable& ) const
7fd59977 80{
81 Standard_Integer aFirstInd, aLastInd, ind;
82 TCollection_ExtendedString aValue;
83 const XmlObjMgt_Element& anElement = theSource;
84
85 // Read the FirstIndex; if the attribute is absent initialize to 1
86 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
87 if (aFirstIndex == NULL)
88 aFirstInd = 1;
89 else if (!aFirstIndex.GetInteger(aFirstInd)) {
90 TCollection_ExtendedString aMessageString =
91 TCollection_ExtendedString("Cannot retrieve the first index"
92 " for ExtStringArray attribute as \"")
93 + aFirstIndex + "\"";
83ae3591 94 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 95 return Standard_False;
96 }
97
98 // Read LastIndex; the attribute should be present
99 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
100 TCollection_ExtendedString aMessageString =
101 TCollection_ExtendedString("Cannot retrieve the last index"
102 " for ExtStringArray attribute as \"")
103 + aFirstIndex + "\"";
83ae3591 104 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 105 return Standard_False;
106 }
107
e9947e12 108 // Read separator.
109 TCollection_ExtendedString separator;
110 XmlObjMgt_DOMString aSeparator = anElement.getAttribute(::Separator());
111 if (aSeparator.Type() != XmlObjMgt_DOMString::LDOM_NULL)
112 separator = aSeparator.GetString();
113
7fd59977 114 Handle(TDataStd_ExtStringArray) aExtStringArray =
115 Handle(TDataStd_ExtStringArray)::DownCast(theTarget);
116 aExtStringArray->Init(aFirstInd, aLastInd);
117
e9947e12 118 // Read string values.
119 if ( !separator.Length() && anElement.hasChildNodes() )
7fd59977 120 {
e9947e12 121 // Read values written by <string>VALUE<\string> notion - as children of the attribute.
122 LDOM_Node aCurNode = anElement.getFirstChild();
123 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
124 TCollection_ExtendedString aValueStr;
125 for (ind = aFirstInd; ind <= aLastInd && *aCurElement != anElement.getLastChild(); ind++)
126 {
127 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
128 aExtStringArray->SetValue(ind, aValueStr);
129 aCurNode = aCurElement->getNextSibling();
130 aCurElement = (LDOM_Element*)&aCurNode;
131 }
7fd59977 132 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
e9947e12 133 aExtStringArray->SetValue( aLastInd, aValueStr );
7fd59977 134 }
e9947e12 135 else
136 {
137 TCollection_ExtendedString xstr;
138 XmlObjMgt::GetExtendedString(anElement, xstr);
139#ifdef _DEBUG
140 TCollection_AsciiString cstr(xstr, '?');
141#endif
142
143 // Split strings by the separator.
144 Standard_Integer isym(1); // index of symbol in xstr
145 Standard_ExtCharacter xsep = separator.Value(1);
146 for (ind = aFirstInd; ind <= aLastInd; ind++)
147 {
148 // Calculate length of the string-value.
149 Standard_Integer iend = isym;
150 while (iend < xstr.Length())
151 {
152 if (xstr.Value(iend) == xsep)
153 {
154 break;
155 }
156 iend++;
157 }
158 if (iend <= xstr.Length() &&
159 xstr.Value(iend) != xsep)
160 {
161 iend++;
162 }
163
164 // Allocate the string-value.
165 TCollection_ExtendedString xvalue(iend - isym, '\0');
166
167 // Set string-value.
168 for (Standard_Integer i = isym; i < iend; ++i)
169 {
170 const Standard_ExtCharacter x = xstr.Value(i);
171 xvalue.SetValue(i - isym + 1, x);
172 }
173#ifdef _DEBUG
174 TCollection_AsciiString cvalue(xvalue, '?');
175#endif
7fd59977 176
e9947e12 177 // Set value for the array.
178 aExtStringArray->SetValue(ind, xvalue);
7fd59977 179
e9947e12 180 // Next string-value.
181 isym = iend + 1;
182 }
183 }
184
185 // Read delta-flag.
7fd59977 186 Standard_Boolean aDelta(Standard_False);
187
188 if(XmlMDataStd::DocumentVersion() > 2) {
189 Standard_Integer aDeltaValue;
190 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
191 {
5a1271c8 192 TCollection_ExtendedString aMessageString =
193 TCollection_ExtendedString("Cannot retrieve the isDelta value"
194 " for IntegerArray attribute as \"")
195 + aDeltaValue + "\"";
83ae3591 196 myMessageDriver->Send (aMessageString, Message_Fail);
5a1271c8 197 return Standard_False;
7fd59977 198 }
199 else
dde68833 200 aDelta = aDeltaValue != 0;
7fd59977 201 }
0797d9d3 202#ifdef OCCT_DEBUG
7fd59977 203 else if(XmlMDataStd::DocumentVersion() == -1)
204 cout << "Current DocVersion field is not initialized. " <<endl;
205#endif
206 aExtStringArray->SetDelta(aDelta);
207
5a1271c8 208 // attribute id
209 Standard_GUID aGUID;
210 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
211 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
212 aGUID = TDataStd_ExtStringArray::GetID(); //default case
213 else
214 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
215
216 aExtStringArray->SetID(aGUID);
217
7fd59977 218 return Standard_True;
219}
220
221//=======================================================================
222//function : Paste
223//purpose : transient -> persistent (store)
224//=======================================================================
225void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
226 XmlObjMgt_Persistent& theTarget,
227 XmlObjMgt_SRelocationTable& ) const
228{
229 Handle(TDataStd_ExtStringArray) aExtStringArray =
230 Handle(TDataStd_ExtStringArray)::DownCast(theSource);
231
e9947e12 232 Standard_Integer aL = aExtStringArray->Lower(), anU = aExtStringArray->Upper(), i;
7fd59977 233
234 XmlObjMgt_Element& anElement = theTarget;
235
236 if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
237 anElement.setAttribute(::LastIndexString(), anU);
dde68833 238 anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
7fd59977 239
e9947e12 240 // Find a separator.
241 Standard_Boolean found(Standard_True);
93445088 242 // This improvement was defined in the version 8.
243 // So, if the user wants to save the document under the 7th or earlier versions,
244 // don't apply this improvement.
e9947e12 245 Standard_Character c = '-';
93445088 246 if (XmlLDrivers::StorageVersion() > 7)
e9947e12 247 {
93445088 248 // Preferrable symbols for the separator: - _ . : ^ ~
249 // Don't use a space as a separator: XML low-level parser sometimes "eats" it.
250 static Standard_Character aPreferable[] = "-_.:^~";
251 for (i = 0; found && aPreferable[i]; i++)
252 {
253 c = aPreferable[i];
254 found = Contains(aExtStringArray, TCollection_ExtendedString(c));
255 }
256 // If all prefferable symbols exist in the array,
257 // try to use any other simple symbols.
258 if (found)
e9947e12 259 {
93445088 260 c = '!';
261 while (found && c < '~')
262 {
263 found = Standard_False;
e9947e12 264#ifdef _DEBUG
93445088 265 TCollection_AsciiString cseparator(c); // deb
e9947e12 266#endif
93445088 267 TCollection_ExtendedString separator(c);
268 found = Contains(aExtStringArray, separator);
269 if (found)
e9947e12 270 {
271 c++;
93445088 272 // Skip forbidden symbols for XML.
273 while (c < '~' && (c == '&' || c == '<'))
274 {
275 c++;
276 }
e9947e12 277 }
278 }
279 }
93445088 280 }// check doc version
7fd59977 281
e9947e12 282 if (found)
7fd59977 283 {
e9947e12 284 // There is no unique separator. Use child elements for storage of strings of the array.
285
286 // store a set of elements with string in each of them
287 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
288 for ( i = aL; i <= anU; i++ )
289 {
290 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value( i );
291 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
292 XmlObjMgt::SetExtendedString( aCurTarget, aValueStr );
293 anElement.appendChild( aCurTarget );
294 }
295 }
296 else
297 {
298 // Set the separator.
299 TCollection_AsciiString csep(c);
300 anElement.setAttribute(::Separator(), csep.ToCString());
301
302 // Calculate length of the common string.
303 Standard_Integer len(0);
304 for (i = aL; i <= anU; i++)
305 {
306 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
307 len += aValueStr.Length();
308 len++; // for separator or ending \0 symbol
309 }
310 if (!len)
311 len++; // for end of line \0 symbol
312
313 // Merge all strings of the array into one extended string separated by the "separator".
314 Standard_Integer isym(1);
315 TCollection_ExtendedString xstr(len, c);
316 for (i = aL; i <= anU; i++)
317 {
318 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
319 for (Standard_Integer k = 1; k <= aValueStr.Length(); k++)
320 {
321 xstr.SetValue(isym++, aValueStr.Value(k));
322 }
323 xstr.SetValue(isym++, c);
324 }
325 if (xstr.SearchFromEnd(c) == isym - 1)
326 isym--; // replace the last separator by '\0'
327 xstr.SetValue(isym, '\0');
328#ifdef _DEBUG
329 TCollection_AsciiString cstr(xstr, '?'); // deb
330#endif
331
332 // Set UNICODE value.
333 XmlObjMgt::SetExtendedString(theTarget, xstr);
7fd59977 334 }
5a1271c8 335 if(aExtStringArray->ID() != TDataStd_ExtStringArray::GetID()) {
336 //convert GUID
337 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
338 Standard_PCharacter pGuidStr = aGuidStr;
339 aExtStringArray->ID().ToCString (pGuidStr);
340 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
341 }
7fd59977 342}