1 // Created on: 2004-11-23
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 // The original implementation Copyright: (C) RINA S.p.A
17 
18 #include <TObj_Application.hxx>
19 
20 #include <Standard_SStream.hxx>
21 #include <Standard_ErrorHandler.hxx>
22 #include <Standard_Dump.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TColStd_SequenceOfExtendedString.hxx>
25 #include <Message_Msg.hxx>
26 #include <Message_MsgFile.hxx>
27 #include <Resource_Manager.hxx>
28 #include <stdio.h>
29 
30 #include "TObj_TObj_msg.pxx"
31 
IMPLEMENT_STANDARD_RTTIEXT(TObj_Application,TDocStd_Application)32 IMPLEMENT_STANDARD_RTTIEXT(TObj_Application,TDocStd_Application)
33 
34 //=======================================================================
35 //function : GetInstance
36 //purpose  :
37 //=======================================================================
38 Handle(TObj_Application) TObj_Application::GetInstance()
39 {
40   static Handle(TObj_Application) THE_TOBJ_APP(new TObj_Application);
41   return THE_TOBJ_APP;
42 }
43 
44 //=======================================================================
45 //function : TObj_Application
46 //purpose  :
47 //=======================================================================
48 
TObj_Application()49 TObj_Application::TObj_Application () : myIsError(Standard_False)
50 {
51   if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
52   {
53     // load messages into global map on first instantiation
54     Message_MsgFile::LoadFromString (TObj_TObj_msg, sizeof(TObj_TObj_msg) - 1);
55     if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
56     {
57       throw Standard_ProgramError("Critical Error - message resources for TObj_Application are invalid or undefined!");
58     }
59   }
60 
61   myMessenger = new Message_Messenger;
62   myIsVerbose = Standard_False;
63 }
64 
65 //=======================================================================
66 //function : ResourcesName
67 //purpose  :
68 //=======================================================================
69 
ResourcesName()70 Standard_CString TObj_Application::ResourcesName()
71 {
72   return Standard_CString("TObj");
73 }
74 
75 //=======================================================================
76 //function : SaveDocument
77 //purpose  : Saving the OCAF document
78 //=======================================================================
79 
SaveDocument(const Handle (TDocStd_Document)& theSourceDoc,const TCollection_ExtendedString & theTargetFile)80 Standard_Boolean TObj_Application::SaveDocument
81                         (const Handle(TDocStd_Document)&   theSourceDoc,
82                          const TCollection_ExtendedString& theTargetFile)
83 {
84   const PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, theTargetFile);
85   myIsError = (aStatus != PCDM_SS_OK);
86   if (myIsError)
87     SetError (aStatus, theTargetFile);
88 
89   // Release free memory
90   Standard::Purge();
91   return myIsError ? Standard_False : Standard_True;
92 }
93 
94 //=======================================================================
95 //function : SaveDocument
96 //purpose  : Saving the OCAF document to a stream
97 //=======================================================================
98 
SaveDocument(const Handle (TDocStd_Document)& theSourceDoc,Standard_OStream & theOStream)99 Standard_Boolean TObj_Application::SaveDocument
100                         (const Handle(TDocStd_Document)& theSourceDoc,
101                          Standard_OStream&               theOStream)
102 {
103   const PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, theOStream);
104   myIsError = (aStatus != PCDM_SS_OK);
105   if (myIsError)
106     SetError (aStatus, "");
107 
108   // Release free memory
109   Standard::Purge();
110   return myIsError ? Standard_False : Standard_True;
111 }
112 
113 //=======================================================================
114 //function : LoadDocument
115 //purpose  : Loading the OCAF document
116 //=======================================================================
117 
LoadDocument(const TCollection_ExtendedString & theSourceFile,Handle (TDocStd_Document)& theTargetDoc)118 Standard_Boolean TObj_Application::LoadDocument
119                         (const TCollection_ExtendedString& theSourceFile,
120                          Handle(TDocStd_Document)&         theTargetDoc)
121 {
122   PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
123   {
124     try
125     {
126       aStatus = Open (theSourceFile, theTargetDoc);
127     }
128     catch (Standard_Failure const& anException) {
129 #ifdef OCCT_DEBUG
130       ErrorMessage (Message_Msg("TObj_Appl_Exception") <<
131                     anException.GetMessageString());
132 #endif
133       (void) anException;
134     }
135   }
136   myIsError = (aStatus != PCDM_RS_OK);
137   if (myIsError)
138     SetError (aStatus, theSourceFile);
139 
140   // Release free memory
141   Standard::Purge();
142   return myIsError ? Standard_False : Standard_True;
143 }
144 
145 //=======================================================================
146 //function : LoadDocument
147 //purpose  : Loading the OCAF document from a stream
148 //=======================================================================
149 
LoadDocument(Standard_IStream & theIStream,Handle (TDocStd_Document)& theTargetDoc)150 Standard_Boolean TObj_Application::LoadDocument
151                         (Standard_IStream&         theIStream,
152                          Handle(TDocStd_Document)& theTargetDoc)
153 {
154   PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
155   {
156     try
157     {
158       aStatus = Open (theIStream, theTargetDoc);
159     }
160     catch (Standard_Failure const& anException) {
161 #ifdef OCCT_DEBUG
162       ErrorMessage(Message_Msg("TObj_Appl_Exception") << anException.GetMessageString());
163 #endif
164       (void) anException;
165     }
166   }
167   myIsError = (aStatus != PCDM_RS_OK);
168   if (myIsError)
169     SetError (aStatus, "");
170 
171   // Release free memory
172   Standard::Purge();
173   return myIsError ? Standard_False : Standard_True;
174 }
175 
176 //=======================================================================
177 //function : CreateNewDocument
178 //purpose  :
179 //=======================================================================
180 
CreateNewDocument(Handle (TDocStd_Document)& theDoc,const TCollection_ExtendedString & theFormat)181 Standard_Boolean TObj_Application::CreateNewDocument
182                         (Handle(TDocStd_Document)&         theDoc,
183                          const TCollection_ExtendedString& theFormat)
184 {
185   myIsError = Standard_False;
186 
187   // Create the Document
188   NewDocument (theFormat, theDoc);
189 
190   return myIsError ? Standard_False : Standard_True;
191 }
192 
193 //=======================================================================
194 //function : ErrorMessage
195 //purpose  :
196 //=======================================================================
197 
ErrorMessage(const TCollection_ExtendedString & theMsg,const Message_Gravity theLevel)198 void TObj_Application::ErrorMessage (const TCollection_ExtendedString &theMsg,
199 					 const Message_Gravity theLevel)
200 {
201   myMessenger->Send ( theMsg, theLevel );
202 }
203 
204 //=======================================================================
205 //function : DumpJson
206 //purpose  :
207 //=======================================================================
DumpJson(Standard_OStream & theOStream,Standard_Integer theDepth) const208 void TObj_Application::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
209 {
210   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
211 
212   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDocStd_Application)
213 }
214 
215 //=======================================================================
216 //function : SetError
217 //purpose  : Sets an error occurred on storage of a document.
218 //=======================================================================
219 
SetError(const PCDM_StoreStatus theStatus,const TCollection_ExtendedString & theInfo)220 void TObj_Application::SetError (const PCDM_StoreStatus theStatus, const TCollection_ExtendedString& theInfo)
221 {
222   switch (theStatus)
223   {
224     case PCDM_SS_DriverFailure:
225       ErrorMessage(Message_Msg("TObj_Appl_SDriverFailure") << theInfo);
226       break;
227     case PCDM_SS_WriteFailure:
228       ErrorMessage(Message_Msg("TObj_Appl_SWriteFailure") << theInfo);
229       break;
230     case PCDM_SS_Failure:
231       ErrorMessage(Message_Msg("TObj_Appl_SFailure") << theInfo);
232       break;
233     case PCDM_SS_Doc_IsNull:
234       ErrorMessage(Message_Msg("TObj_Appl_SDocIsNull") << theInfo);
235       break;
236     case PCDM_SS_No_Obj:
237       ErrorMessage(Message_Msg("TObj_Appl_SNoObj") << theInfo);
238       break;
239     case PCDM_SS_Info_Section_Error:
240       ErrorMessage(Message_Msg("TObj_Appl_SInfoSectionError") << theInfo);
241       break;
242     default:
243       ErrorMessage(Message_Msg("TObj_Appl_SUnknownFailure") << theInfo);
244       break;
245   }
246 }
247 
248 //=======================================================================
249 //function : SetError
250 //purpose  : Sets an error occurred on reading of a document.
251 //=======================================================================
252 
SetError(const PCDM_ReaderStatus theStatus,const TCollection_ExtendedString & theInfo)253 void TObj_Application::SetError(const PCDM_ReaderStatus theStatus, const TCollection_ExtendedString& theInfo)
254 {
255   switch (theStatus)
256   {
257     case PCDM_RS_UnknownDocument:
258       ErrorMessage(Message_Msg("TObj_Appl_RUnknownDocument") << theInfo);
259       break;
260     case PCDM_RS_AlreadyRetrieved:
261       ErrorMessage(Message_Msg("TObj_Appl_RAlreadyRetrieved") << theInfo);
262       break;
263     case PCDM_RS_AlreadyRetrievedAndModified:
264       ErrorMessage(Message_Msg("TObj_Appl_RAlreadyRetrievedAndModified") << theInfo);
265       break;
266     case PCDM_RS_NoDriver:
267       ErrorMessage(Message_Msg("TObj_Appl_RNoDriver") << theInfo);
268       break;
269     case PCDM_RS_UnknownFileDriver:
270       ErrorMessage(Message_Msg("TObj_Appl_RNoDriver") << theInfo);
271       break;
272     case PCDM_RS_OpenError:
273       ErrorMessage(Message_Msg("TObj_Appl_ROpenError") << theInfo);
274       break;
275     case PCDM_RS_NoVersion:
276       ErrorMessage(Message_Msg("TObj_Appl_RNoVersion") << theInfo);
277       break;
278     case PCDM_RS_NoModel:
279       ErrorMessage(Message_Msg("TObj_Appl_RNoModel") << theInfo);
280       break;
281     case PCDM_RS_NoDocument:
282       ErrorMessage(Message_Msg("TObj_Appl_RNoDocument") << theInfo);
283       break;
284     case PCDM_RS_FormatFailure:
285       ErrorMessage(Message_Msg("TObj_Appl_RFormatFailure") << theInfo);
286       break;
287     case PCDM_RS_TypeNotFoundInSchema:
288       ErrorMessage(Message_Msg("TObj_Appl_RTypeNotFound") << theInfo);
289       break;
290     case PCDM_RS_UnrecognizedFileFormat:
291       ErrorMessage(Message_Msg("TObj_Appl_RBadFileFormat") << theInfo);
292       break;
293     case PCDM_RS_MakeFailure:
294       ErrorMessage(Message_Msg("TObj_Appl_RMakeFailure") << theInfo);
295       break;
296     case PCDM_RS_PermissionDenied:
297       ErrorMessage(Message_Msg("TObj_Appl_RPermissionDenied") << theInfo);
298       break;
299     case PCDM_RS_DriverFailure:
300       ErrorMessage(Message_Msg("TObj_Appl_RDriverFailure") << theInfo);
301       break;
302     case PCDM_RS_ReaderException:
303       ErrorMessage(Message_Msg("TObj_Appl_RException") << theInfo);
304       break;
305     default:
306       ErrorMessage(Message_Msg("TObj_Appl_RUnknownFail") << theInfo);
307       break;
308   }
309 }
310