0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[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,
b34d86cb 79 XmlObjMgt_RRelocationTable& theRelocTable) 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
cbc4faa9 118 // attribute id
119 Standard_GUID aGUID;
120 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
121 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
122 aGUID = TDataStd_ExtStringArray::GetID(); //default case
123 else
124 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
125
126 aExtStringArray->SetID(aGUID);
127
e9947e12 128 // Read string values.
129 if ( !separator.Length() && anElement.hasChildNodes() )
7fd59977 130 {
e9947e12 131 // Read values written by <string>VALUE<\string> notion - as children of the attribute.
132 LDOM_Node aCurNode = anElement.getFirstChild();
133 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
134 TCollection_ExtendedString aValueStr;
135 for (ind = aFirstInd; ind <= aLastInd && *aCurElement != anElement.getLastChild(); ind++)
136 {
137 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
138 aExtStringArray->SetValue(ind, aValueStr);
139 aCurNode = aCurElement->getNextSibling();
140 aCurElement = (LDOM_Element*)&aCurNode;
141 }
7fd59977 142 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
e9947e12 143 aExtStringArray->SetValue( aLastInd, aValueStr );
7fd59977 144 }
e9947e12 145 else
146 {
147 TCollection_ExtendedString xstr;
148 XmlObjMgt::GetExtendedString(anElement, xstr);
149#ifdef _DEBUG
150 TCollection_AsciiString cstr(xstr, '?');
151#endif
152
153 // Split strings by the separator.
154 Standard_Integer isym(1); // index of symbol in xstr
155 Standard_ExtCharacter xsep = separator.Value(1);
156 for (ind = aFirstInd; ind <= aLastInd; ind++)
157 {
158 // Calculate length of the string-value.
159 Standard_Integer iend = isym;
160 while (iend < xstr.Length())
161 {
162 if (xstr.Value(iend) == xsep)
163 {
164 break;
165 }
166 iend++;
167 }
168 if (iend <= xstr.Length() &&
169 xstr.Value(iend) != xsep)
170 {
171 iend++;
172 }
173
174 // Allocate the string-value.
175 TCollection_ExtendedString xvalue(iend - isym, '\0');
176
177 // Set string-value.
178 for (Standard_Integer i = isym; i < iend; ++i)
179 {
180 const Standard_ExtCharacter x = xstr.Value(i);
181 xvalue.SetValue(i - isym + 1, x);
182 }
183#ifdef _DEBUG
184 TCollection_AsciiString cvalue(xvalue, '?');
185#endif
7fd59977 186
e9947e12 187 // Set value for the array.
188 aExtStringArray->SetValue(ind, xvalue);
7fd59977 189
e9947e12 190 // Next string-value.
191 isym = iend + 1;
192 }
193 }
194
195 // Read delta-flag.
7fd59977 196 Standard_Boolean aDelta(Standard_False);
197
b34d86cb 198 if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() > 2) {
7fd59977 199 Standard_Integer aDeltaValue;
200 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
201 {
5a1271c8 202 TCollection_ExtendedString aMessageString =
203 TCollection_ExtendedString("Cannot retrieve the isDelta value"
204 " for IntegerArray attribute as \"")
205 + aDeltaValue + "\"";
83ae3591 206 myMessageDriver->Send (aMessageString, Message_Fail);
5a1271c8 207 return Standard_False;
7fd59977 208 }
209 else
dde68833 210 aDelta = aDeltaValue != 0;
7fd59977 211 }
b34d86cb 212
7fd59977 213 aExtStringArray->SetDelta(aDelta);
214
215 return Standard_True;
216}
217
218//=======================================================================
219//function : Paste
220//purpose : transient -> persistent (store)
221//=======================================================================
222void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
223 XmlObjMgt_Persistent& theTarget,
b34d86cb 224 XmlObjMgt_SRelocationTable& theRelocTable) const
7fd59977 225{
226 Handle(TDataStd_ExtStringArray) aExtStringArray =
227 Handle(TDataStd_ExtStringArray)::DownCast(theSource);
228
e9947e12 229 Standard_Integer aL = aExtStringArray->Lower(), anU = aExtStringArray->Upper(), i;
7fd59977 230
231 XmlObjMgt_Element& anElement = theTarget;
232
233 if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
234 anElement.setAttribute(::LastIndexString(), anU);
dde68833 235 anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
7fd59977 236
e9947e12 237 // Find a separator.
238 Standard_Boolean found(Standard_True);
93445088 239 // This improvement was defined in the version 8.
240 // So, if the user wants to save the document under the 7th or earlier versions,
241 // don't apply this improvement.
e9947e12 242 Standard_Character c = '-';
b34d86cb 243 if (theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() > 7)
e9947e12 244 {
93445088 245 // Preferrable symbols for the separator: - _ . : ^ ~
246 // Don't use a space as a separator: XML low-level parser sometimes "eats" it.
247 static Standard_Character aPreferable[] = "-_.:^~";
248 for (i = 0; found && aPreferable[i]; i++)
249 {
250 c = aPreferable[i];
251 found = Contains(aExtStringArray, TCollection_ExtendedString(c));
252 }
253 // If all prefferable symbols exist in the array,
254 // try to use any other simple symbols.
255 if (found)
e9947e12 256 {
93445088 257 c = '!';
258 while (found && c < '~')
259 {
260 found = Standard_False;
e9947e12 261#ifdef _DEBUG
93445088 262 TCollection_AsciiString cseparator(c); // deb
e9947e12 263#endif
93445088 264 TCollection_ExtendedString separator(c);
265 found = Contains(aExtStringArray, separator);
266 if (found)
e9947e12 267 {
268 c++;
93445088 269 // Skip forbidden symbols for XML.
270 while (c < '~' && (c == '&' || c == '<'))
271 {
272 c++;
273 }
e9947e12 274 }
275 }
276 }
93445088 277 }// check doc version
7fd59977 278
e9947e12 279 if (found)
7fd59977 280 {
e9947e12 281 // There is no unique separator. Use child elements for storage of strings of the array.
282
283 // store a set of elements with string in each of them
284 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
285 for ( i = aL; i <= anU; i++ )
286 {
287 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value( i );
288 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
289 XmlObjMgt::SetExtendedString( aCurTarget, aValueStr );
290 anElement.appendChild( aCurTarget );
291 }
292 }
293 else
294 {
295 // Set the separator.
296 TCollection_AsciiString csep(c);
297 anElement.setAttribute(::Separator(), csep.ToCString());
298
299 // Calculate length of the common string.
300 Standard_Integer len(0);
301 for (i = aL; i <= anU; i++)
302 {
303 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
304 len += aValueStr.Length();
305 len++; // for separator or ending \0 symbol
306 }
307 if (!len)
308 len++; // for end of line \0 symbol
309
310 // Merge all strings of the array into one extended string separated by the "separator".
311 Standard_Integer isym(1);
312 TCollection_ExtendedString xstr(len, c);
313 for (i = aL; i <= anU; i++)
314 {
315 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
316 for (Standard_Integer k = 1; k <= aValueStr.Length(); k++)
317 {
318 xstr.SetValue(isym++, aValueStr.Value(k));
319 }
320 xstr.SetValue(isym++, c);
321 }
322 if (xstr.SearchFromEnd(c) == isym - 1)
323 isym--; // replace the last separator by '\0'
324 xstr.SetValue(isym, '\0');
325#ifdef _DEBUG
326 TCollection_AsciiString cstr(xstr, '?'); // deb
327#endif
328
329 // Set UNICODE value.
330 XmlObjMgt::SetExtendedString(theTarget, xstr);
7fd59977 331 }
5a1271c8 332 if(aExtStringArray->ID() != TDataStd_ExtStringArray::GetID()) {
333 //convert GUID
334 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
335 Standard_PCharacter pGuidStr = aGuidStr;
336 aExtStringArray->ID().ToCString (pGuidStr);
337 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
338 }
7fd59977 339}