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