0023024: Update headers of OCCT files
[occt.git] / src / XmlMDataStd / XmlMDataStd_ByteArrayDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20
21 #include <XmlMDataStd_ByteArrayDriver.ixx>
22 #include <TDataStd_ByteArray.hxx>
23 #include <TColStd_HArray1OfByte.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlMDataStd.hxx>
26
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)
36 {
37
38 }
39
40 //=======================================================================
41 //function : NewEmpty
42 //purpose  : 
43 //=======================================================================
44 Handle(TDF_Attribute) XmlMDataStd_ByteArrayDriver::NewEmpty() const
45 {
46   return new TDataStd_ByteArray();
47 }
48
49 //=======================================================================
50 //function : Paste
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
56 {
57   Standard_Integer aFirstInd, aLastInd, aValue;
58   const XmlObjMgt_Element& anElement = theSource;
59
60   // Read the FirstIndex; if the attribute is absent initialize to 1
61   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
62   if (aFirstIndex == NULL)
63     aFirstInd = 1;
64   else if (!aFirstIndex.GetInteger(aFirstInd)) 
65   {
66     TCollection_ExtendedString aMessageString =
67       TCollection_ExtendedString("Cannot retrieve the first index"
68                                  " for ByteArray attribute as \"")
69         + aFirstIndex + "\"";
70     WriteMessage (aMessageString);
71     return Standard_False;
72   }
73
74   // Read the LastIndex; the attribute should be present
75   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
76   {
77     TCollection_ExtendedString aMessageString =
78       TCollection_ExtendedString("Cannot retrieve the last index"
79                                  " for ByteArray attribute as \"")
80         + aFirstIndex + "\"";
81     WriteMessage (aMessageString);
82     return Standard_False;
83   }
84
85   if (aFirstInd > aLastInd)
86   {
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;
92   }
93
94
95   Handle(TDataStd_ByteArray) aByteArray = Handle(TDataStd_ByteArray)::DownCast(theTarget);
96   Handle(TColStd_HArray1OfByte) array = new TColStd_HArray1OfByte(aFirstInd, aLastInd);
97
98   Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
99   Standard_Integer i = array->Lower(), upper = array->Upper();
100   for (; i <= upper; i++)
101   {
102     if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
103     {
104       TCollection_ExtendedString aMessageString =
105         TCollection_ExtendedString("Cannot retrieve integer member"
106                                    " for ByteArray attribute as \"")
107           + aValueStr + "\"";
108       WriteMessage (aMessageString);
109       return Standard_False;
110     }
111     array->SetValue(i, (Standard_Byte) aValue);
112   }
113   aByteArray->ChangeArray(array);
114   
115 #ifdef DEB
116   //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
117 #endif
118   Standard_Boolean aDelta(Standard_False);
119   
120   if(XmlMDataStd::DocumentVersion() > 2) {
121     Standard_Integer aDeltaValue;
122     if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
123       {
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;
130       } 
131     else
132       aDelta = (Standard_Boolean)aDeltaValue;
133   }
134 #ifdef DEB
135   else if(XmlMDataStd::DocumentVersion() == -1)
136     cout << "Current DocVersion field is not initialized. "  <<endl;
137 #endif
138   aByteArray->SetDelta(aDelta);
139
140   return Standard_True;
141 }
142
143 //=======================================================================
144 //function : Paste
145 //purpose  : transient -> persistent (store)
146 //=======================================================================
147 void XmlMDataStd_ByteArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
148                                         XmlObjMgt_Persistent&        theTarget,
149                                         XmlObjMgt_SRelocationTable&  ) const
150 {
151   Handle(TDataStd_ByteArray) aByteArray = Handle(TDataStd_ByteArray)::DownCast(theSource);
152
153   Standard_Integer aL = aByteArray->Lower();
154   Standard_Integer anU = aByteArray->Upper();
155   TCollection_AsciiString aValueStr;
156
157   theTarget.Element().setAttribute(::FirstIndexString(), aL);
158   theTarget.Element().setAttribute(::LastIndexString(), anU);
159   theTarget.Element().setAttribute(::IsDeltaOn(),aByteArray->GetDelta());
160
161   const Handle(TColStd_HArray1OfByte)& array = aByteArray->InternalArray();
162   Standard_Integer lower = array->Lower(), i = lower, upper = array->Upper();
163   for (; i <= upper; i++)
164   {
165     aValueStr += TCollection_AsciiString((Standard_Integer) array->Value(i));
166     aValueStr += ' ';
167   }
168   XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
169
170 }