0030169: Application Framework - Document format version management improvement
[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 <XmlMDataStd.hxx>
23 #include <XmlMDataStd_ExtStringArrayDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Document.hxx>
26 #include <XmlObjMgt_Persistent.hxx>
27 #include <XmlLDrivers.hxx>
28
29 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ExtStringArrayDriver,XmlMDF_ADriver)
30 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
31 IMPLEMENT_DOMSTRING (LastIndexString, "last")
32 IMPLEMENT_DOMSTRING (ExtString,       "string")
33 IMPLEMENT_DOMSTRING (IsDeltaOn,       "delta")
34 IMPLEMENT_DOMSTRING (Separator,       "separator")
35 IMPLEMENT_DOMSTRING (AttributeIDString, "extstrarrattguid")
36
37 // Searches for a symbol within an array of strings.
38 // Returns TRUE if the symbol is found.
39 static 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
53 //=======================================================================
54 //function : XmlMDataStd_ExtStringArrayDriver
55 //purpose  : Constructor
56 //=======================================================================
57
58 XmlMDataStd_ExtStringArrayDriver::XmlMDataStd_ExtStringArrayDriver
59                         ( const Handle(Message_Messenger)& theMsgDriver )
60 : XmlMDF_ADriver( theMsgDriver, NULL )
61 {}
62
63 //=======================================================================
64 //function : NewEmpty
65 //purpose  : 
66 //=======================================================================
67 Handle(TDF_Attribute) XmlMDataStd_ExtStringArrayDriver::NewEmpty() const
68 {
69   return ( new TDataStd_ExtStringArray() );
70 }
71
72 //=======================================================================
73 //function : Paste
74 //purpose  : persistent -> transient (retrieve)
75 //=======================================================================
76 Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
77                                         (const XmlObjMgt_Persistent&  theSource,
78                                          const Handle(TDF_Attribute)& theTarget,
79                                          XmlObjMgt_RRelocationTable& theRelocTable) const
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 + "\"";
94     myMessageDriver->Send (aMessageString, Message_Fail);
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 + "\"";
104     myMessageDriver->Send (aMessageString, Message_Fail);
105     return Standard_False;
106   }
107
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
114   Handle(TDataStd_ExtStringArray) aExtStringArray =
115     Handle(TDataStd_ExtStringArray)::DownCast(theTarget);
116   aExtStringArray->Init(aFirstInd, aLastInd);
117   
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
128   // Read string values.
129   if ( !separator.Length() && anElement.hasChildNodes() )
130   {
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     }
142     XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
143     aExtStringArray->SetValue( aLastInd, aValueStr );
144   }
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
186
187       // Set value for the array.
188       aExtStringArray->SetValue(ind, xvalue);
189
190       // Next string-value.
191       isym = iend + 1;
192     }
193   }
194
195   // Read delta-flag.
196   Standard_Boolean aDelta(Standard_False);
197   
198   if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() > 2) {
199     Standard_Integer aDeltaValue;
200     if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
201       {
202         TCollection_ExtendedString aMessageString =
203           TCollection_ExtendedString("Cannot retrieve the isDelta value"
204                                      " for IntegerArray attribute as \"")
205                                      + aDeltaValue + "\"";
206         myMessageDriver->Send (aMessageString, Message_Fail);
207         return Standard_False;
208       } 
209     else
210       aDelta = aDeltaValue != 0;
211   }
212
213   aExtStringArray->SetDelta(aDelta);
214
215   return Standard_True;
216 }
217
218 //=======================================================================
219 //function : Paste
220 //purpose  : transient -> persistent (store)
221 //=======================================================================
222 void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
223                                          XmlObjMgt_Persistent&        theTarget,
224                                          XmlObjMgt_SRelocationTable&  theRelocTable) const
225 {
226   Handle(TDataStd_ExtStringArray) aExtStringArray =
227     Handle(TDataStd_ExtStringArray)::DownCast(theSource);
228
229   Standard_Integer aL = aExtStringArray->Lower(), anU = aExtStringArray->Upper(), i;
230
231   XmlObjMgt_Element& anElement = theTarget;
232
233   if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
234   anElement.setAttribute(::LastIndexString(), anU);
235   anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
236
237   // Find a separator.
238   Standard_Boolean found(Standard_True);
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.
242   Standard_Character c = '-';
243   if (theRelocTable.GetHeaderData()->StorageVersion().IntegerValue()  > 7)
244   {
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)
256     {
257       c = '!';
258       while (found && c < '~')
259       {
260         found = Standard_False;
261 #ifdef _DEBUG
262         TCollection_AsciiString cseparator(c); // deb
263 #endif
264         TCollection_ExtendedString separator(c);
265         found = Contains(aExtStringArray, separator);
266         if (found)
267         {
268           c++;
269           // Skip forbidden symbols for XML.
270           while (c < '~' && (c == '&' || c == '<'))
271           {
272             c++;
273           }
274         }
275       }
276     }
277   }// check doc version
278   
279   if (found)
280   {
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);
331   }
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   }
339 }