a1f4596f18d4acc2b83c7ec9ff7a96814606d321
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntegerArrayDriver.cxx
1 // Created on: 2001-08-24
2 // Created by: Alexnder GRIGORIEV
3 // Copyright (c) 2001-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 //AGV 150202: Changed prototype XmlObjMgt::SetStringValue()
21
22 #include <XmlMDataStd_IntegerArrayDriver.ixx>
23 #include <TDataStd_IntegerArray.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_IntegerArrayDriver
32 //purpose  : Constructor
33 //=======================================================================
34
35 XmlMDataStd_IntegerArrayDriver::XmlMDataStd_IntegerArrayDriver
36                         (const Handle(CDM_MessageDriver)& theMsgDriver)
37       : XmlMDF_ADriver (theMsgDriver, NULL)
38 {}
39
40 //=======================================================================
41 //function : NewEmpty
42 //purpose  : 
43 //=======================================================================
44 Handle(TDF_Attribute) XmlMDataStd_IntegerArrayDriver::NewEmpty() const
45 {
46   return (new TDataStd_IntegerArray());
47 }
48
49 //=======================================================================
50 //function : Paste
51 //purpose  : persistent -> transient (retrieve)
52 //=======================================================================
53 Standard_Boolean XmlMDataStd_IntegerArrayDriver::Paste
54                                 (const XmlObjMgt_Persistent&  theSource,
55                                  const Handle(TDF_Attribute)& theTarget,
56                                  XmlObjMgt_RRelocationTable&  ) const
57 {
58   Standard_Integer aFirstInd, aLastInd, aValue, ind;
59   const XmlObjMgt_Element& anElement = theSource;
60
61   // Read the FirstIndex; if the attribute is absent initialize to 1
62   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
63   if (aFirstIndex == NULL)
64     aFirstInd = 1;
65   else if (!aFirstIndex.GetInteger(aFirstInd)) {
66     TCollection_ExtendedString aMessageString =
67       TCollection_ExtendedString("Cannot retrieve the first index"
68                                  " for IntegerArray 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     TCollection_ExtendedString aMessageString =
77       TCollection_ExtendedString("Cannot retrieve the last index"
78                                  " for IntegerArray attribute as \"")
79         + aFirstIndex + "\"";
80     WriteMessage (aMessageString);
81     return Standard_False;
82   }
83
84   Handle(TDataStd_IntegerArray) anIntArray =
85     Handle(TDataStd_IntegerArray)::DownCast(theTarget);
86   anIntArray->Init(aFirstInd, aLastInd);
87
88   if(aFirstInd == aLastInd) {
89     Standard_Integer anInteger;
90     if(!XmlObjMgt::GetStringValue(anElement).GetInteger( anInteger)) {
91       TCollection_ExtendedString aMessageString =
92         TCollection_ExtendedString("Cannot retrieve integer member"
93                                    " for IntegerArray attribute as \"");
94       WriteMessage (aMessageString);
95       return Standard_False;
96     }
97     anIntArray->SetValue(aFirstInd, anInteger);
98     
99   }
100   else {
101     // Warning: check implementation of XmlObjMgt_DOMString !! For LDOM this is OK
102     Standard_CString aValueStr =
103       Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
104     
105     for (ind = aFirstInd; ind <= aLastInd; ind++)
106     {
107       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) {
108         TCollection_ExtendedString aMessageString =
109           TCollection_ExtendedString("Cannot retrieve integer member"
110                                      " for IntegerArray attribute as \"")
111             + aValueStr + "\"";
112         WriteMessage (aMessageString);
113         return Standard_False;
114       }
115       anIntArray->SetValue(ind, aValue);
116     }
117   }
118 #ifdef DEB
119   //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
120 #endif
121   Standard_Boolean aDelta(Standard_False);
122   
123   if(XmlMDataStd::DocumentVersion() > 2) {
124     Standard_Integer aDeltaValue;
125     if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
126       {
127         TCollection_ExtendedString aMessageString =
128           TCollection_ExtendedString("Cannot retrieve the isDelta value"
129                                  " for IntegerArray attribute as \"")
130         + aDeltaValue + "\"";
131         WriteMessage (aMessageString);
132         return Standard_False;
133       } 
134     else
135       aDelta = (Standard_Boolean)aDeltaValue;
136   }
137 #ifdef DEB
138   else if(XmlMDataStd::DocumentVersion() == -1)
139     cout << "Current DocVersion field is not initialized. "  <<endl;
140 #endif
141   anIntArray->SetDelta(aDelta);
142   
143   return Standard_True;
144 }
145
146 //=======================================================================
147 //function : Paste
148 //purpose  : transient -> persistent (store)
149 //=======================================================================
150 void XmlMDataStd_IntegerArrayDriver::Paste
151                                 (const Handle(TDF_Attribute)& theSource,
152                                  XmlObjMgt_Persistent&        theTarget,
153                                  XmlObjMgt_SRelocationTable&  ) const
154 {
155   Handle(TDataStd_IntegerArray) anIntArray =
156     Handle(TDataStd_IntegerArray)::DownCast(theSource);
157
158   Standard_Integer aL = anIntArray->Lower(), anU = anIntArray->Upper();
159   TCollection_AsciiString aValueStr;
160
161   if (aL != 1) theTarget.Element().setAttribute (::FirstIndexString(), aL);
162   theTarget.Element().setAttribute(::LastIndexString(), anU);
163   theTarget.Element().setAttribute(::IsDeltaOn(), anIntArray->GetDelta());
164
165   Standard_Integer i = aL;
166   while (1) {
167     aValueStr += TCollection_AsciiString(anIntArray->Value(i));
168     if (i >= anU) break;
169     aValueStr += ' ';
170     ++i;
171   }
172   // No occurrence of '&', '<' and other irregular XML characters
173   XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
174 }