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