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>
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")
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)
41 for (Standard_Integer i = arr->Lower(); i <= arr->Upper(); i++)
43 const TCollection_ExtendedString& value = arr->Value(i);
44 if (value.Search(c) != -1)
49 return Standard_False;
52 //=======================================================================
53 //function : XmlMDataStd_ExtStringArrayDriver
54 //purpose : Constructor
55 //=======================================================================
57 XmlMDataStd_ExtStringArrayDriver::XmlMDataStd_ExtStringArrayDriver
58 ( const Handle(Message_Messenger)& theMsgDriver )
59 : XmlMDF_ADriver( theMsgDriver, NULL )
62 //=======================================================================
65 //=======================================================================
66 Handle(TDF_Attribute) XmlMDataStd_ExtStringArrayDriver::NewEmpty() const
68 return ( new TDataStd_ExtStringArray() );
71 //=======================================================================
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
80 Standard_Integer aFirstInd, aLastInd, ind;
81 TCollection_ExtendedString aValue;
82 const XmlObjMgt_Element& anElement = theSource;
84 // Read the FirstIndex; if the attribute is absent initialize to 1
85 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
86 if (aFirstIndex == NULL)
88 else if (!aFirstIndex.GetInteger(aFirstInd)) {
89 TCollection_ExtendedString aMessageString =
90 TCollection_ExtendedString("Cannot retrieve the first index"
91 " for ExtStringArray attribute as \"")
93 myMessageDriver->Send (aMessageString, Message_Fail);
94 return Standard_False;
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;
108 TCollection_ExtendedString separator;
109 XmlObjMgt_DOMString aSeparator = anElement.getAttribute(::Separator());
110 if (aSeparator.Type() != XmlObjMgt_DOMString::LDOM_NULL)
111 separator = aSeparator.GetString();
113 Handle(TDataStd_ExtStringArray) aExtStringArray =
114 Handle(TDataStd_ExtStringArray)::DownCast(theTarget);
115 aExtStringArray->Init(aFirstInd, aLastInd);
117 // Read string values.
118 if ( !separator.Length() && anElement.hasChildNodes() )
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++)
126 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
127 aExtStringArray->SetValue(ind, aValueStr);
128 aCurNode = aCurElement->getNextSibling();
129 aCurElement = (LDOM_Element*)&aCurNode;
131 XmlObjMgt::GetExtendedString( *aCurElement, aValueStr );
132 aExtStringArray->SetValue( aLastInd, aValueStr );
136 TCollection_ExtendedString xstr;
137 XmlObjMgt::GetExtendedString(anElement, xstr);
139 TCollection_AsciiString cstr(xstr, '?');
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++)
147 // Calculate length of the string-value.
148 Standard_Integer iend = isym;
149 while (iend < xstr.Length())
151 if (xstr.Value(iend) == xsep)
157 if (iend <= xstr.Length() &&
158 xstr.Value(iend) != xsep)
163 // Allocate the string-value.
164 TCollection_ExtendedString xvalue(iend - isym, '\0');
167 for (Standard_Integer i = isym; i < iend; ++i)
169 const Standard_ExtCharacter x = xstr.Value(i);
170 xvalue.SetValue(i - isym + 1, x);
173 TCollection_AsciiString cvalue(xvalue, '?');
176 // Set value for the array.
177 aExtStringArray->SetValue(ind, xvalue);
179 // Next string-value.
185 Standard_Boolean aDelta(Standard_False);
187 if(XmlMDataStd::DocumentVersion() > 2) {
188 Standard_Integer aDeltaValue;
189 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
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;
199 aDelta = aDeltaValue != 0;
202 else if(XmlMDataStd::DocumentVersion() == -1)
203 cout << "Current DocVersion field is not initialized. " <<endl;
205 aExtStringArray->SetDelta(aDelta);
209 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
210 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
211 aGUID = TDataStd_ExtStringArray::GetID(); //default case
213 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
215 aExtStringArray->SetID(aGUID);
217 return Standard_True;
220 //=======================================================================
222 //purpose : transient -> persistent (store)
223 //=======================================================================
224 void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
225 XmlObjMgt_Persistent& theTarget,
226 XmlObjMgt_SRelocationTable& ) const
228 Handle(TDataStd_ExtStringArray) aExtStringArray =
229 Handle(TDataStd_ExtStringArray)::DownCast(theSource);
231 Standard_Integer aL = aExtStringArray->Lower(), anU = aExtStringArray->Upper(), i;
233 XmlObjMgt_Element& anElement = theTarget;
235 if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
236 anElement.setAttribute(::LastIndexString(), anU);
237 anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
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++)
248 found = Contains(aExtStringArray, TCollection_ExtendedString(c));
250 // If all prefferable symbols exist in the array,
251 // try to use any other simple symbols.
255 while (found && c < '~')
257 found = Standard_False;
259 TCollection_AsciiString cseparator(c); // deb
261 TCollection_ExtendedString separator(c);
262 found = Contains(aExtStringArray, separator);
266 // Skip forbidden symbols for XML.
267 while (c < '~' && (c == '&' || c == '<'))
277 // There is no unique separator. Use child elements for storage of strings of the array.
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++ )
283 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value( i );
284 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
285 XmlObjMgt::SetExtendedString( aCurTarget, aValueStr );
286 anElement.appendChild( aCurTarget );
291 // Set the separator.
292 TCollection_AsciiString csep(c);
293 anElement.setAttribute(::Separator(), csep.ToCString());
295 // Calculate length of the common string.
296 Standard_Integer len(0);
297 for (i = aL; i <= anU; i++)
299 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
300 len += aValueStr.Length();
301 len++; // for separator or ending \0 symbol
304 len++; // for end of line \0 symbol
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++)
311 const TCollection_ExtendedString& aValueStr = aExtStringArray->Value(i);
312 for (Standard_Integer k = 1; k <= aValueStr.Length(); k++)
314 xstr.SetValue(isym++, aValueStr.Value(k));
316 xstr.SetValue(isym++, c);
318 if (xstr.SearchFromEnd(c) == isym - 1)
319 isym--; // replace the last separator by '\0'
320 xstr.SetValue(isym, '\0');
322 TCollection_AsciiString cstr(xstr, '?'); // deb
325 // Set UNICODE value.
326 XmlObjMgt::SetExtendedString(theTarget, xstr);
328 if(aExtStringArray->ID() != TDataStd_ExtStringArray::GetID()) {
330 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
331 Standard_PCharacter pGuidStr = aGuidStr;
332 aExtStringArray->ID().ToCString (pGuidStr);
333 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);