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