0029220: Application Framework - replace CDM_MessageDriver interface by Message_Messe...
[occt.git] / src / TObj / TObj_Application.cxx
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 <TCollection_ExtendedString.hxx>
23 #include <TColStd_SequenceOfExtendedString.hxx>
24 #include <Message_Msg.hxx>
25 #include <Message_MsgFile.hxx>
26 #include <Resource_Manager.hxx>
27
28 #include <stdio.h>
29
30 #include "TObj_TObj_msg.pxx"
31
32 IMPLEMENT_STANDARD_RTTIEXT(TObj_Application,TDocStd_Application)
33
34 //=======================================================================
35 //function : GetInstance
36 //purpose  :
37 //=======================================================================
38
39 Handle(TObj_Application) TObj_Application::GetInstance()
40 {
41   static Handle(TObj_Application) anInstance = new TObj_Application;
42   return anInstance;
43 }
44
45 //=======================================================================
46 //function : TObj_Application
47 //purpose  : 
48 //=======================================================================
49
50 TObj_Application::TObj_Application () : myIsError(Standard_False)
51 {
52   if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
53   {
54     // load messages into global map on first instantiation
55     Message_MsgFile::LoadFromString (TObj_TObj_msg, sizeof(TObj_TObj_msg) - 1);
56     if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
57     {
58       throw Standard_ProgramError("Critical Error - message resources for TObj_Application are invalid or undefined!");
59     }
60   }
61
62   myMessenger = new Message_Messenger;
63   myIsVerbose = Standard_False;
64 }
65
66 //=======================================================================
67 //function : ResourcesName
68 //purpose  : 
69 //=======================================================================
70
71 Standard_CString TObj_Application::ResourcesName()
72 {
73   return Standard_CString("TObj");
74 }
75
76 //=======================================================================
77 //function : SaveDocument
78 //purpose  : Saving the OCAF document
79 //=======================================================================
80
81 Standard_Boolean TObj_Application::SaveDocument
82                         (const Handle(TDocStd_Document)&   theSourceDoc,
83                          const TCollection_ExtendedString& theTargetFile)
84 {
85   myIsError = Standard_False;
86   PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, theTargetFile);
87   myIsError = aStatus != PCDM_SS_OK;
88   if (myIsError)
89   {
90     switch (aStatus)
91     {
92     case PCDM_SS_DriverFailure:
93       ErrorMessage (Message_Msg("TObj_Appl_SDriverFailure") << theTargetFile);
94       break;
95     case PCDM_SS_WriteFailure:
96       ErrorMessage (Message_Msg("TObj_Appl_SWriteFailure") << theTargetFile);
97       break;
98     case PCDM_SS_Failure:
99       ErrorMessage (Message_Msg("TObj_Appl_SFailure") << theTargetFile);
100       break;
101     case PCDM_SS_Doc_IsNull:
102       ErrorMessage (Message_Msg("TObj_Appl_SDocIsNull") << theTargetFile);
103       break;
104     case PCDM_SS_No_Obj:
105       ErrorMessage (Message_Msg("TObj_Appl_SNoObj") << theTargetFile);
106       break;
107     case PCDM_SS_Info_Section_Error:
108       ErrorMessage (Message_Msg("TObj_Appl_SInfoSectionError") << theTargetFile);
109       break;
110     default:
111       ErrorMessage (Message_Msg("TObj_Appl_SUnknownFailure") << theTargetFile);
112       break;
113     }
114   }
115
116   // Release free memory
117   Standard::Purge();
118   return myIsError ? Standard_False : Standard_True;
119 }
120
121 //=======================================================================
122 //function : LoadDocument
123 //purpose  : Loading the OCAF document
124 //=======================================================================
125
126 Standard_Boolean TObj_Application::LoadDocument
127                         (const TCollection_ExtendedString& theSourceFile,
128                          Handle(TDocStd_Document)&         theTargetDoc)
129 {
130   myIsError = Standard_False;
131   PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
132   {
133     try
134     {
135       aStatus = Open (theSourceFile, theTargetDoc);
136     }
137     catch (Standard_Failure const& anException) {
138 #ifdef OCCT_DEBUG
139       ErrorMessage (Message_Msg("TObj_Appl_Exception") << 
140                     anException.GetMessageString());
141 #endif
142       (void)anException;
143     }
144   }
145   myIsError = aStatus != PCDM_RS_OK;
146   if (myIsError)
147   {
148     switch (aStatus)
149     {
150     case PCDM_RS_UnknownDocument:
151       ErrorMessage (Message_Msg("TObj_Appl_RUnknownDocument") << theSourceFile);
152       break;
153     case PCDM_RS_AlreadyRetrieved:
154       ErrorMessage (Message_Msg("TObj_Appl_RAlreadyRetrieved") << theSourceFile);
155       break;
156     case PCDM_RS_AlreadyRetrievedAndModified:
157       ErrorMessage (Message_Msg("TObj_Appl_RAlreadyRetrievedAndModified") << theSourceFile);
158       break;
159     case PCDM_RS_NoDriver:
160       ErrorMessage (Message_Msg("TObj_Appl_RNoDriver") << theSourceFile);
161       break;
162     case PCDM_RS_UnknownFileDriver:
163       ErrorMessage (Message_Msg("TObj_Appl_RNoDriver") << theSourceFile);
164       break;
165     case PCDM_RS_OpenError:
166       ErrorMessage (Message_Msg("TObj_Appl_ROpenError") << theSourceFile);
167       break;
168     case PCDM_RS_NoVersion:
169       ErrorMessage (Message_Msg("TObj_Appl_RNoVersion") << theSourceFile);
170       break;
171     case PCDM_RS_NoModel:
172       ErrorMessage (Message_Msg("TObj_Appl_RNoModel") << theSourceFile);
173       break;
174     case PCDM_RS_NoDocument:
175       ErrorMessage (Message_Msg("TObj_Appl_RNoDocument") << theSourceFile);
176       break;
177     case PCDM_RS_FormatFailure:
178       ErrorMessage (Message_Msg("TObj_Appl_RFormatFailure") << theSourceFile);
179       break;
180     case PCDM_RS_TypeNotFoundInSchema:
181       ErrorMessage (Message_Msg("TObj_Appl_RTypeNotFound") << theSourceFile);
182       break;
183     case PCDM_RS_UnrecognizedFileFormat:
184       ErrorMessage (Message_Msg("TObj_Appl_RBadFileFormat") << theSourceFile);
185       break;
186     case PCDM_RS_MakeFailure:
187       ErrorMessage (Message_Msg("TObj_Appl_RMakeFailure") << theSourceFile);
188       break;
189     case PCDM_RS_PermissionDenied:
190       ErrorMessage (Message_Msg("TObj_Appl_RPermissionDenied") << theSourceFile);
191       break;
192     case PCDM_RS_DriverFailure:
193       ErrorMessage (Message_Msg("TObj_Appl_RDriverFailure") << theSourceFile);
194       break;
195     case PCDM_RS_ReaderException:
196       ErrorMessage (Message_Msg("TObj_Appl_RException") << theSourceFile);
197       break;
198     default:
199       ErrorMessage (Message_Msg("TObj_Appl_RUnknownFail") << theSourceFile);
200       break;
201     }
202   }
203
204   // Release free memory
205   Standard::Purge();
206   return myIsError ? Standard_False : Standard_True;
207 }
208
209 //=======================================================================
210 //function : CreateNewDocument
211 //purpose  : 
212 //=======================================================================
213
214 Standard_Boolean TObj_Application::CreateNewDocument
215                         (Handle(TDocStd_Document)&         theDoc,
216                          const TCollection_ExtendedString& theFormat)
217 {
218   myIsError = Standard_False;
219
220   // Create the Document
221   NewDocument (theFormat, theDoc);
222
223   return myIsError ? Standard_False : Standard_True;
224 }
225
226 //=======================================================================
227 //function : ErrorMessage
228 //purpose  : 
229 //=======================================================================
230
231 void TObj_Application::ErrorMessage (const TCollection_ExtendedString &theMsg,
232                                          const Message_Gravity theLevel)
233 {
234   myMessenger->Send ( theMsg, theLevel );
235 }
236