1 // Created on: 2004-09-27
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
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>
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")
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)
42 for (Standard_Integer i = arr->Lower(); i <= arr->Upper(); i++)
44 const TCollection_ExtendedString& value = arr->Value(i);
45 if (value.Search(c) != -1)
50 return Standard_False;
53 //=======================================================================
54 //function : XmlMDataStd_ExtStringArrayDriver
55 //purpose : Constructor
56 //=======================================================================
58 XmlMDataStd_ExtStringArrayDriver::XmlMDataStd_ExtStringArrayDriver
59 ( const Handle(Message_Messenger)& theMsgDriver )
60 : XmlMDF_ADriver( theMsgDriver, NULL )
63 //=======================================================================
66 //=======================================================================
67 Handle(TDF_Attribute) XmlMDataStd_ExtStringArrayDriver::NewEmpty() const
69 return ( new TDataStd_ExtStringArray() );
72 //=======================================================================
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& ) const
81 Standard_Integer aFirstInd, aLastInd, ind;
82 TCollection_ExtendedString aValue;
83 const XmlObjMgt_Element& anElement = theSource;
85 // Read the FirstIndex; if the attribute is absent initialize to 1
86 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
87 if (aFirstIndex == NULL)
89 else if (!aFirstIndex.GetInteger(aFirstInd)) {
90 TCollection_ExtendedString aMessageString =
91 TCollection_ExtendedString("Cannot retrieve the first index"
92 " for ExtStringArray attribute as \"")
94 myMessageDriver->Send (aMessageString, Message_Fail);
95 return Standard_False;
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;
109 TCollection_ExtendedString separator;
110 XmlObjMgt_DOMString aSeparator = anElement.getAttribute(::Separator());
111 if (aSeparator.Type() != XmlObjMgt_DOMString::LDOM_NULL)
112 separator = aSeparator.GetString();
114 Handle(TDataStd_ExtStringArray) aExtStringArray =
115 Handle(TDataStd_ExtStringArray)::DownCast(theTarget);
116 aExtStringArray->Init(aFirstInd, aLastInd);
120 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
121 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
122 aGUID = TDataStd_ExtStringArray::GetID(); //default case
124 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
126 aExtStringArray->SetID(aGUID);
128 // Read string values.
129 if ( !separator.Length() && anElement.hasChildNodes() )
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++)
137 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
138 aExtStringArray->SetValue(ind, aValueStr);
139 aCurNode = aCurElement->getNextSibling();
140 aCurElement = (LDOM_Element*)&aCurNode;
142 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
143 aExtStringArray->SetValue( aLastInd, aValueStr );
147 TCollection_ExtendedString xstr;
148 XmlObjMgt::GetExtendedString(anElement, xstr);
150 TCollection_AsciiString cstr(xstr, '?');
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++)
158 // Calculate length of the string-value.
159 Standard_Integer iend = isym;
160 while (iend < xstr.Length())
162 if (xstr.Value(iend) == xsep)
168 if (iend <= xstr.Length() &&
169 xstr.Value(iend) != xsep)
174 // Allocate the string-value.
175 TCollection_ExtendedString xvalue(iend - isym, '\0');
178 for (Standard_Integer i = isym; i < iend; ++i)
180 const Standard_ExtCharacter x = xstr.Value(i);
181 xvalue.SetValue(i - isym + 1, x);
184 TCollection_AsciiString cvalue(xvalue, '?');
187 // Set value for the array.
188 aExtStringArray->SetValue(ind, xvalue);
190 // Next string-value.
196 Standard_Boolean aDelta(Standard_False);
198 if(XmlMDataStd::DocumentVersion() > 2) {
199 Standard_Integer aDeltaValue;
200 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
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;
210 aDelta = aDeltaValue != 0;
213 else if(XmlMDataStd::DocumentVersion() == -1)
214 cout << "Current DocVersion field is not initialized. " <<endl;
216 aExtStringArray->SetDelta(aDelta);
218 return Standard_True;
221 //=======================================================================
223 //purpose : transient -> persistent (store)
224 //=======================================================================
225 void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
226 XmlObjMgt_Persistent& theTarget,
227 XmlObjMgt_SRelocationTable& ) const
229 Handle(TDataStd_ExtStringArray) aExtStringArray =
230 Handle(TDataStd_ExtStringArray)::DownCast(theSource);
232 Standard_Integer aL = aExtStringArray->Lower(), anU = aExtStringArray->Upper(), i;
234 XmlObjMgt_Element& anElement = theTarget;
236 if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
237 anElement.setAttribute(::LastIndexString(), anU);
238 anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
241 Standard_Boolean found(Standard_True);
242 // This improvement was defined in the version 8.
243 // So, if the user wants to save the document under the 7th or earlier versions,
244 // don't apply this improvement.
245 Standard_Character c = '-';
246 if (XmlLDrivers::StorageVersion() > 7)
248 // Preferrable symbols for the separator: - _ . : ^ ~
249 // Don't use a space as a separator: XML low-level parser sometimes "eats" it.
250 static Standard_Character aPreferable[] = "-_.:^~";
251 for (i = 0; found && aPreferable[i]; i++)
254 found = Contains(aExtStringArray, TCollection_ExtendedString(c));
256 // If all prefferable symbols exist in the array,
257 // try to use any other simple symbols.
261 while (found && c < '~')
263 found = Standard_False;
265 TCollection_AsciiString cseparator(c); // deb
267 TCollection_ExtendedString separator(c);
268 found = Contains(aExtStringArray, separator);
272 // Skip forbidden symbols for XML.
273 while (c < '~' && (c == '&' || c == '<'))
280 }// check doc version
284 // There is no unique separator. Use child elements for storage of strings of the array.
286 // store a set of elements with string in each of them
287 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
288 for ( i = aL; i <= anU; i++ )
290 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value( i );
291 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
292 XmlObjMgt::SetExtendedString( aCurTarget, aValueStr );
293 anElement.appendChild( aCurTarget );
298 // Set the separator.
299 TCollection_AsciiString csep(c);
300 anElement.setAttribute(::Separator(), csep.ToCString());
302 // Calculate length of the common string.
303 Standard_Integer len(0);
304 for (i = aL; i <= anU; i++)
306 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
307 len += aValueStr.Length();
308 len++; // for separator or ending \0 symbol
311 len++; // for end of line \0 symbol
313 // Merge all strings of the array into one extended string separated by the "separator".
314 Standard_Integer isym(1);
315 TCollection_ExtendedString xstr(len, c);
316 for (i = aL; i <= anU; i++)
318 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
319 for (Standard_Integer k = 1; k <= aValueStr.Length(); k++)
321 xstr.SetValue(isym++, aValueStr.Value(k));
323 xstr.SetValue(isym++, c);
325 if (xstr.SearchFromEnd(c) == isym - 1)
326 isym--; // replace the last separator by '\0'
327 xstr.SetValue(isym, '\0');
329 TCollection_AsciiString cstr(xstr, '?'); // deb
332 // Set UNICODE value.
333 XmlObjMgt::SetExtendedString(theTarget, xstr);
335 if(aExtStringArray->ID() != TDataStd_ExtStringArray::GetID()) {
337 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
338 Standard_PCharacter pGuidStr = aGuidStr;
339 aExtStringArray->ID().ToCString (pGuidStr);
340 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);