7fd59977 |
1 | // File: PCDM_RetrievalDriver.cxx |
2 | // Created: Thu Aug 7 14:11:39 1997 |
3 | // Author: Jean-Louis Frenkel |
4 | // <rmi@frilox.paris1.matra-dtv.fr> |
5 | |
6 | |
7 | #include <PCDM_RetrievalDriver.ixx> |
8 | #include <PCDM_BaseDriverPointer.hxx> |
9 | #include <PCDM.hxx> |
10 | #include <PCDM_TypeOfFileDriver.hxx> |
11 | #include <Storage_Data.hxx> |
12 | #include <Storage_HeaderData.hxx> |
13 | #include <Storage_Schema.hxx> |
14 | #include <Storage_Root.hxx> |
15 | #include <TCollection_AsciiString.hxx> |
16 | |
17 | #include <TColStd_SequenceOfAsciiString.hxx> |
18 | #include <Standard_TypeMismatch.hxx> |
19 | |
20 | #include <PCDM_ReadWriter.hxx> |
21 | #include <Resource_Manager.hxx> |
22 | #include <Standard_ErrorHandler.hxx> |
23 | #include <UTL.hxx> |
24 | #include <PCDM.hxx> |
25 | #include <Storage_HSeqOfRoot.hxx> |
26 | #include <locale.h> |
27 | |
28 | void PCDM_RetrievalDriver::RaiseIfUnknownTypes(const Handle(Storage_Schema)& aSchema, const TCollection_ExtendedString& aFileName) { |
29 | |
30 | PCDM_BaseDriverPointer theFileDriver; |
31 | if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(aFileName)), theFileDriver) == PCDM_TOFD_Unknown) return; |
32 | |
33 | PCDM_ReadWriter::Open(*theFileDriver,aFileName,Storage_VSRead); |
34 | |
35 | TColStd_SequenceOfAsciiString theUnknownTypes; |
36 | Standard_Boolean unknowns = aSchema->HasUnknownType(*theFileDriver,theUnknownTypes); |
37 | |
38 | theFileDriver->Close(); |
39 | delete theFileDriver; |
40 | |
41 | if(unknowns) { |
42 | Standard_SStream aMsg; aMsg << "cannot read: `" << aFileName << "', because the following types: "; |
43 | for (Standard_Integer i=1; i <= theUnknownTypes.Length(); i++) { |
44 | aMsg << theUnknownTypes(i); |
45 | if(i< theUnknownTypes.Length()) aMsg <<","; |
46 | } |
47 | aMsg << " have been found in it but not in the available Schema: " << aSchema->Name() << (char)0; |
48 | Standard_TypeMismatch::Raise(aMsg); |
49 | } |
50 | } |
51 | |
52 | //======================================================================= |
53 | //function : Read |
54 | //purpose : |
55 | //======================================================================= |
56 | |
57 | void PCDM_RetrievalDriver::Read(const TCollection_ExtendedString& theFileName, |
58 | const Handle(CDM_Document)& aNewDocument, |
59 | const Handle(CDM_Application)& theApplication) { |
60 | |
61 | |
62 | char *oldnum,*plocal ; |
63 | plocal = setlocale(LC_NUMERIC, NULL) ; |
64 | oldnum = new char[strlen(plocal)+1] ; |
65 | strcpy(oldnum,plocal); |
66 | Standard_SStream aMsg; |
67 | Standard_Boolean Failure=Standard_False; |
68 | Handle(Storage_Schema) aSchema; |
69 | TColStd_SequenceOfExtendedString theExtensions; |
70 | myReaderStatus = PCDM_RS_OK; |
71 | { |
72 | try { |
73 | OCC_CATCH_SIGNALS |
74 | aSchema=PCDM::Schema(SchemaName(),theApplication); |
75 | Extensions(theFileName, theExtensions, theApplication->MessageDriver()); |
76 | LoadExtensions(aSchema,theExtensions, theApplication->MessageDriver()); |
77 | } |
78 | catch (Standard_NoSuchObject) { |
79 | aMsg << Standard_NoSuchObject::Caught() << endl; |
80 | myReaderStatus = PCDM_RS_NoSchema; |
81 | Failure=Standard_True; |
82 | } |
83 | catch (Standard_Failure) { |
84 | aMsg << Standard_Failure::Caught() << endl; |
85 | myReaderStatus = PCDM_RS_ExtensionFailure; |
86 | Failure=Standard_True; |
87 | } |
88 | if(Failure) Standard_Failure::Raise(aMsg); |
89 | } |
90 | |
91 | PCDM_BaseDriverPointer theFileDriver; |
92 | if(PCDM::FileDriverType(TCollection_AsciiString(UTL::CString(theFileName)), theFileDriver) == PCDM_TOFD_Unknown) { |
93 | myReaderStatus = PCDM_RS_UnknownFileDriver; |
94 | return; |
95 | } |
96 | { |
97 | try { |
98 | OCC_CATCH_SIGNALS |
99 | RaiseIfUnknownTypes(aSchema, theFileName); |
100 | PCDM_ReadWriter::Open(*theFileDriver,theFileName,Storage_VSRead); |
101 | } |
102 | catch (Standard_TypeMismatch) { |
103 | aMsg << Standard_TypeMismatch::Caught() << endl; |
104 | myReaderStatus = PCDM_RS_TypeNotFoundInSchema; |
105 | Failure=Standard_True; |
106 | } |
107 | catch (Standard_Failure) { |
108 | aMsg << Standard_Failure::Caught() << endl; |
109 | myReaderStatus = PCDM_RS_OpenError; |
110 | Failure=Standard_True; |
111 | } |
112 | if(Failure) Standard_Failure::Raise(aMsg); |
113 | } |
114 | |
115 | Handle(Storage_Data) theData = aSchema->Read(*theFileDriver); |
116 | Storage_Error aStatus = theData->ErrorStatus(); |
117 | if(aStatus != Storage_VSOk) { |
118 | switch (aStatus) { |
119 | case Storage_VSOpenError: |
120 | case Storage_VSNotOpen: |
121 | case Storage_VSAlreadyOpen: |
122 | myReaderStatus = PCDM_RS_OpenError; |
123 | aMsg << "Stream Open Error" << endl; |
124 | break; |
125 | case Storage_VSModeError: |
126 | myReaderStatus = PCDM_RS_WrongStreamMode; |
127 | aMsg << "Stream is opened with a wrong mode for operation" << endl; |
128 | break; |
129 | case Storage_VSSectionNotFound: |
130 | aMsg << "Section is not found" << endl; |
131 | myReaderStatus = PCDM_RS_FormatFailure; |
132 | break; |
133 | case Storage_VSFormatError: |
134 | myReaderStatus = PCDM_RS_FormatFailure; |
135 | aMsg << "Wrong format error" << endl; |
136 | break; |
137 | case Storage_VSUnknownType: |
138 | myReaderStatus = PCDM_RS_TypeFailure; |
139 | aMsg << "Try to read an unknown type" << endl; |
140 | break; |
141 | case Storage_VSTypeMismatch: |
142 | myReaderStatus = PCDM_RS_TypeFailure; |
143 | aMsg << "Try to read a wrong primitive type" << endl; |
144 | break; |
145 | default: |
146 | myReaderStatus = PCDM_RS_DriverFailure; |
147 | aMsg << "Retrieval Driver Failure" << endl; |
148 | } |
149 | Standard_Failure::Raise(aMsg); |
150 | } |
151 | |
152 | theFileDriver->Close(); |
153 | delete theFileDriver; |
154 | |
155 | Handle(Storage_HSeqOfRoot) theDocuments=theData->Roots(); |
156 | |
157 | Handle(Storage_Root) theDocument; |
158 | for(Standard_Integer i=1; i<=theDocuments->Length() && theDocument.IsNull(); i++) { |
159 | theDocument=theDocuments->Value(i); |
160 | } |
161 | |
162 | if(theDocument.IsNull()) { |
163 | Standard_SStream aMsg; |
164 | aMsg << "could not find any document in this file" << (char) 0; |
165 | myReaderStatus = PCDM_RS_NoDocument; |
166 | Standard_Failure::Raise(aMsg); |
167 | } |
168 | Handle(Standard_Persistent) theObject=theDocument->Object(); |
169 | Handle(PCDM_Document) thePDocument = *((Handle(PCDM_Document)*)&theObject); |
170 | |
171 | |
172 | { |
173 | try { |
174 | OCC_CATCH_SIGNALS |
175 | Make(thePDocument,aNewDocument); |
176 | } |
177 | catch (Standard_Failure) { |
178 | aMsg << "Error during Make: "; |
179 | aMsg << Standard_Failure::Caught() << (char)0; |
180 | myReaderStatus = PCDM_RS_MakeFailure; |
181 | Failure=Standard_True; |
182 | } |
183 | } |
184 | |
185 | // read comments |
186 | aNewDocument->SetComments(theData->Comments()); |
187 | |
188 | // on remet le LC_NUMERIC a la precedente valeur |
189 | setlocale(LC_NUMERIC, oldnum) ; |
190 | delete[] oldnum; |
191 | |
192 | PCDM_DriverError_Raise_if(Failure,aMsg); |
193 | } |
194 | |
195 | void PCDM_RetrievalDriver::References(const TCollection_ExtendedString& aFileName, PCDM_SequenceOfReference& theReferences, const Handle(CDM_MessageDriver)& theMsgDriver) { |
196 | |
197 | PCDM_ReadWriter::Reader(aFileName)->ReadReferences(aFileName, theReferences, theMsgDriver); |
198 | } |
199 | |
200 | void PCDM_RetrievalDriver::Extensions(const TCollection_ExtendedString& aFileName, TColStd_SequenceOfExtendedString& theExtensions, const Handle(CDM_MessageDriver)& theMsgDriver) { |
201 | |
202 | PCDM_ReadWriter::Reader(aFileName)->ReadExtensions(aFileName,theExtensions, theMsgDriver); |
203 | } |
204 | |
205 | |
206 | |
207 | Standard_Integer PCDM_RetrievalDriver::DocumentVersion(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) { |
208 | |
209 | return PCDM_ReadWriter::Reader(aFileName)->ReadDocumentVersion(aFileName, theMsgDriver); |
210 | } |
211 | Standard_Integer PCDM_RetrievalDriver::ReferenceCounter(const TCollection_ExtendedString& aFileName, const Handle(CDM_MessageDriver)& theMsgDriver) { |
212 | |
213 | return PCDM_ReadWriter::Reader(aFileName)->ReadReferenceCounter(aFileName, theMsgDriver); |
214 | } |
215 | void PCDM_RetrievalDriver::LoadExtensions(const Handle(Storage_Schema)& , const TColStd_SequenceOfExtendedString& , const Handle(CDM_MessageDriver)&) {} |
216 | |
217 | |
218 | //======================================================================= |
219 | //function : Format |
220 | //purpose : |
221 | //======================================================================= |
222 | |
223 | void PCDM_RetrievalDriver::SetFormat (const TCollection_ExtendedString& aformat) |
224 | { |
225 | myFormat = aformat; |
226 | } |
227 | |
228 | |
229 | //======================================================================= |
230 | //function : Format |
231 | //purpose : |
232 | //======================================================================= |
233 | |
234 | TCollection_ExtendedString PCDM_RetrievalDriver::GetFormat () const |
235 | { |
236 | return myFormat; |
237 | } |