1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <XmlMDataStd_ByteArrayDriver.ixx>
22 #include <TDataStd_ByteArray.hxx>
23 #include <TColStd_HArray1OfByte.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlMDataStd.hxx>
27 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
28 IMPLEMENT_DOMSTRING (LastIndexString, "last")
29 IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
30 //=======================================================================
31 //function : XmlMDataStd_ByteArrayDriver
32 //purpose : Constructor
33 //=======================================================================
34 XmlMDataStd_ByteArrayDriver::XmlMDataStd_ByteArrayDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
35 : XmlMDF_ADriver (theMsgDriver, NULL)
40 //=======================================================================
43 //=======================================================================
44 Handle(TDF_Attribute) XmlMDataStd_ByteArrayDriver::NewEmpty() const
46 return new TDataStd_ByteArray();
49 //=======================================================================
51 //purpose : persistent -> transient (retrieve)
52 //=======================================================================
53 Standard_Boolean XmlMDataStd_ByteArrayDriver::Paste(const XmlObjMgt_Persistent& theSource,
54 const Handle(TDF_Attribute)& theTarget,
55 XmlObjMgt_RRelocationTable& ) const
57 Standard_Integer aFirstInd, aLastInd, aValue;
58 const XmlObjMgt_Element& anElement = theSource;
60 // Read the FirstIndex; if the attribute is absent initialize to 1
61 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
62 if (aFirstIndex == NULL)
64 else if (!aFirstIndex.GetInteger(aFirstInd))
66 TCollection_ExtendedString aMessageString =
67 TCollection_ExtendedString("Cannot retrieve the first index"
68 " for ByteArray attribute as \"")
70 WriteMessage (aMessageString);
71 return Standard_False;
74 // Read the LastIndex; the attribute should be present
75 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
77 TCollection_ExtendedString aMessageString =
78 TCollection_ExtendedString("Cannot retrieve the last index"
79 " for ByteArray attribute as \"")
81 WriteMessage (aMessageString);
82 return Standard_False;
85 if (aFirstInd > aLastInd)
87 TCollection_ExtendedString aMessageString =
88 TCollection_ExtendedString("The last index is greater than the first index"
89 " for ByteArray attribute \"");
90 WriteMessage (aMessageString);
91 return Standard_False;
95 Handle(TDataStd_ByteArray) aByteArray = Handle(TDataStd_ByteArray)::DownCast(theTarget);
96 Handle(TColStd_HArray1OfByte) array = new TColStd_HArray1OfByte(aFirstInd, aLastInd);
98 Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
99 Standard_Integer i = array->Lower(), upper = array->Upper();
100 for (; i <= upper; i++)
102 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
104 TCollection_ExtendedString aMessageString =
105 TCollection_ExtendedString("Cannot retrieve integer member"
106 " for ByteArray attribute as \"")
108 WriteMessage (aMessageString);
109 return Standard_False;
111 array->SetValue(i, (Standard_Byte) aValue);
113 aByteArray->ChangeArray(array);
116 //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
118 Standard_Boolean aDelta(Standard_False);
120 if(XmlMDataStd::DocumentVersion() > 2) {
121 Standard_Integer aDeltaValue;
122 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
124 TCollection_ExtendedString aMessageString =
125 TCollection_ExtendedString("Cannot retrieve the isDelta value"
126 " for ByteArray attribute as \"")
127 + aDeltaValue + "\"";
128 WriteMessage (aMessageString);
129 return Standard_False;
132 aDelta = (Standard_Boolean)aDeltaValue;
135 else if(XmlMDataStd::DocumentVersion() == -1)
136 cout << "Current DocVersion field is not initialized. " <<endl;
138 aByteArray->SetDelta(aDelta);
140 return Standard_True;
143 //=======================================================================
145 //purpose : transient -> persistent (store)
146 //=======================================================================
147 void XmlMDataStd_ByteArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
148 XmlObjMgt_Persistent& theTarget,
149 XmlObjMgt_SRelocationTable& ) const
151 Handle(TDataStd_ByteArray) aByteArray = Handle(TDataStd_ByteArray)::DownCast(theSource);
153 Standard_Integer aL = aByteArray->Lower();
154 Standard_Integer anU = aByteArray->Upper();
155 TCollection_AsciiString aValueStr;
157 theTarget.Element().setAttribute(::FirstIndexString(), aL);
158 theTarget.Element().setAttribute(::LastIndexString(), anU);
159 theTarget.Element().setAttribute(::IsDeltaOn(),aByteArray->GetDelta());
161 const Handle(TColStd_HArray1OfByte)& array = aByteArray->InternalArray();
162 Standard_Integer lower = array->Lower(), i = lower, upper = array->Upper();
163 for (; i <= upper; i++)
165 aValueStr += TCollection_AsciiString((Standard_Integer) array->Value(i));
168 XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);