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