summaryrefslogtreecommitdiff
path: root/src/PCDM/PCDM_RetrievalDriver.cxx
blob: 7164f4082759a2a56285f47a14c7802bec517640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// File:	PCDM_RetrievalDriver.cxx
// Created:	Thu Aug  7 14:11:39 1997
// Author:	Jean-Louis Frenkel
//		<rmi@frilox.paris1.matra-dtv.fr>


#include <PCDM_RetrievalDriver.ixx>
#include <PCDM_BaseDriverPointer.hxx>
#include <PCDM.hxx>
#include <PCDM_TypeOfFileDriver.hxx>
#include <Storage_Data.hxx>
#include <Storage_HeaderData.hxx>
#include <Storage_Schema.hxx>
#include <Storage_Root.hxx>
#include <TCollection_AsciiString.hxx>

#include <TColStd_SequenceOfAsciiString.hxx>
#include <Standard_TypeMismatch.hxx>

#include <PCDM_ReadWriter.hxx>
#include <Resource_Manager.hxx>
#include <Standard_ErrorHandler.hxx>
#include <UTL.hxx>
#include <PCDM.hxx>
#include <Storage_HSeqOfRoot.hxx>
#include <locale.h>

void PCDM_RetrievalDriver::RaiseIfUnknownTypes(const Handle(Storage_Schema)& aSchema, const TCollection_ExtendedString& aFileName) {

  PCDM_BaseDriverPointer theFileDriver;
  if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(aFileName)), theFileDriver) == PCDM_TOFD_Unknown) return;
  
  PCDM_ReadWriter::Open(*theFileDriver,aFileName,Storage_VSRead);
  
  TColStd_SequenceOfAsciiString theUnknownTypes;
  Standard_Boolean unknowns = aSchema->HasUnknownType(*theFileDriver,theUnknownTypes);

  theFileDriver->Close();
  delete theFileDriver;

  if(unknowns) {
    Standard_SStream aMsg; aMsg << "cannot read: `" << aFileName << "', because  the following types: ";
    for (Standard_Integer i=1; i <= theUnknownTypes.Length(); i++) {
      aMsg << theUnknownTypes(i);
      if(i< theUnknownTypes.Length()) aMsg <<",";
    }
    aMsg << " have been found in it but not in the available Schema: " << aSchema->Name() << (char)0;
    Standard_TypeMismatch::Raise(aMsg);
  }
}

//=======================================================================
//function : Read
//purpose  : 
//=======================================================================

void PCDM_RetrievalDriver::Read(const TCollection_ExtendedString& theFileName, 
				const Handle(CDM_Document)& aNewDocument,
				const Handle(CDM_Application)& theApplication) {


  char *oldnum,*plocal ;
  plocal =   setlocale(LC_NUMERIC, NULL) ;
  oldnum = new char[strlen(plocal)+1] ;
  strcpy(oldnum,plocal);
  Standard_SStream aMsg;
  Standard_Boolean Failure=Standard_False;
  Handle(Storage_Schema) aSchema;
  TColStd_SequenceOfExtendedString theExtensions;
  myReaderStatus = PCDM_RS_OK;
  {
    try {
      OCC_CATCH_SIGNALS
      aSchema=PCDM::Schema(SchemaName(),theApplication);
      Extensions(theFileName, theExtensions, theApplication->MessageDriver());
      LoadExtensions(aSchema,theExtensions, theApplication->MessageDriver());
    } 
    catch (Standard_NoSuchObject) {
      aMsg << Standard_NoSuchObject::Caught() << endl;
      myReaderStatus = PCDM_RS_NoSchema;
      Failure=Standard_True;
    }
    catch (Standard_Failure) {
      aMsg << Standard_Failure::Caught() << endl;
      myReaderStatus = PCDM_RS_ExtensionFailure;
      Failure=Standard_True;
    }
    if(Failure) Standard_Failure::Raise(aMsg);
  }

  PCDM_BaseDriverPointer theFileDriver;
  if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(theFileName)), theFileDriver) == PCDM_TOFD_Unknown) {
    myReaderStatus = PCDM_RS_UnknownFileDriver;
    return;
  }
  {
    try {
      OCC_CATCH_SIGNALS
      RaiseIfUnknownTypes(aSchema, theFileName);
      PCDM_ReadWriter::Open(*theFileDriver,theFileName,Storage_VSRead);
    } 
    catch (Standard_TypeMismatch) {
      aMsg << Standard_TypeMismatch::Caught() << endl;
      myReaderStatus = PCDM_RS_TypeNotFoundInSchema;
      Failure=Standard_True;
    }
    catch (Standard_Failure) {
      aMsg << Standard_Failure::Caught() << endl;
      myReaderStatus = PCDM_RS_OpenError;
      Failure=Standard_True;
    }
    if(Failure) Standard_Failure::Raise(aMsg);
  } 
  
  Handle(Storage_Data) theData = aSchema->Read(*theFileDriver);
  Storage_Error aStatus = theData->ErrorStatus();
  if(aStatus != Storage_VSOk) {
    switch (aStatus) {
    case Storage_VSOpenError:
    case Storage_VSNotOpen:
    case Storage_VSAlreadyOpen:
      myReaderStatus = PCDM_RS_OpenError;
      aMsg << "Stream Open Error" << endl;
      break;
    case Storage_VSModeError:
      myReaderStatus = PCDM_RS_WrongStreamMode;
      aMsg << "Stream is opened with a wrong mode for operation" << endl;
      break;
    case Storage_VSSectionNotFound:
      aMsg << "Section is not found" << endl;
      myReaderStatus = PCDM_RS_FormatFailure;
      break;
    case Storage_VSFormatError:
      myReaderStatus = PCDM_RS_FormatFailure;
      aMsg << "Wrong format error" << endl;
      break;
    case Storage_VSUnknownType:
      myReaderStatus = PCDM_RS_TypeFailure;
      aMsg << "Try to read an unknown type" << endl;
      break;
    case Storage_VSTypeMismatch:
      myReaderStatus = PCDM_RS_TypeFailure;
      aMsg << "Try to read a wrong primitive type" << endl;
      break;
    default:
      myReaderStatus = PCDM_RS_DriverFailure;
      aMsg << "Retrieval Driver Failure" << endl;
    }
    Standard_Failure::Raise(aMsg);    
  }

  theFileDriver->Close();
  delete theFileDriver;
  
  Handle(Storage_HSeqOfRoot) theDocuments=theData->Roots();
  
  Handle(Storage_Root) theDocument;
  for(Standard_Integer i=1; i<=theDocuments->Length() && theDocument.IsNull(); i++) {
    theDocument=theDocuments->Value(i);
  }
  
  if(theDocument.IsNull()) {
    Standard_SStream aMsg;
    aMsg << "could not find any document in this file" << (char) 0;
    myReaderStatus = PCDM_RS_NoDocument;
    Standard_Failure::Raise(aMsg);
  }
  Handle(Standard_Persistent) theObject=theDocument->Object();
  Handle(PCDM_Document) thePDocument = *((Handle(PCDM_Document)*)&theObject);


  {
    try {
      OCC_CATCH_SIGNALS
      Make(thePDocument,aNewDocument);
    }
    catch (Standard_Failure) {
      aMsg << "Error during Make: ";
      aMsg << Standard_Failure::Caught() << (char)0;
      myReaderStatus = PCDM_RS_MakeFailure;
      Failure=Standard_True;
    }
  }

// read comments
  aNewDocument->SetComments(theData->Comments());

// on remet le LC_NUMERIC a la precedente valeur
  setlocale(LC_NUMERIC, oldnum) ;
  delete[] oldnum;
  
  PCDM_DriverError_Raise_if(Failure,aMsg);
}

void PCDM_RetrievalDriver::References(const TCollection_ExtendedString& aFileName, PCDM_SequenceOfReference& theReferences, const Handle(CDM_MessageDriver)& theMsgDriver) {

  PCDM_ReadWriter::Reader(aFileName)->ReadReferences(aFileName, theReferences, theMsgDriver);
}

void PCDM_RetrievalDriver::Extensions(const TCollection_ExtendedString& aFileName, TColStd_SequenceOfExtendedString& theExtensions,  const Handle(CDM_MessageDriver)& theMsgDriver) {
  
  PCDM_ReadWriter::Reader(aFileName)->ReadExtensions(aFileName,theExtensions, theMsgDriver);
}



Standard_Integer PCDM_RetrievalDriver::DocumentVersion(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) {

  return PCDM_ReadWriter::Reader(aFileName)->ReadDocumentVersion(aFileName, theMsgDriver);
}
Standard_Integer PCDM_RetrievalDriver::ReferenceCounter(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) {

  return PCDM_ReadWriter::Reader(aFileName)->ReadReferenceCounter(aFileName, theMsgDriver);
}
void PCDM_RetrievalDriver::LoadExtensions(const Handle(Storage_Schema)& , const TColStd_SequenceOfExtendedString& ,  const Handle(CDM_MessageDriver)&) {}


//=======================================================================
//function : Format
//purpose  : 
//=======================================================================

void PCDM_RetrievalDriver::SetFormat (const TCollection_ExtendedString& aformat)
{
  myFormat = aformat;
}


//=======================================================================
//function : Format
//purpose  : 
//=======================================================================

TCollection_ExtendedString PCDM_RetrievalDriver::GetFormat () const
{
  return myFormat;
}