0026229: Add the possibility in OCAF to open/save a document from/to a stream object
[occt.git] / src / PCDM / PCDM_ReadWriter.cxx
1 // Created on: 1997-12-09
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
18 #include <CDM_Document.hxx>
19 #include <CDM_MessageDriver.hxx>
20 #include <PCDM.hxx>
21 #include <PCDM_DOMHeaderParser.hxx>
22 #include <PCDM_ReadWriter.hxx>
23 #include <PCDM_ReadWriter_1.hxx>
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_Type.hxx>
26 #include <Storage_BaseDriver.hxx>
27 #include <Storage_Data.hxx>
28 #include <Storage_HeaderData.hxx>
29 #include <Storage_Schema.hxx>
30 #include <Storage_TypeData.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
33 #include <TColStd_HSequenceOfAsciiString.hxx>
34 #include <UTL.hxx>
35
36 IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
37
38 #define FILE_FORMAT "FILE_FORMAT: "
39
40 static TCollection_ExtendedString TryXmlDriverType
41                                 (const TCollection_AsciiString& theFileName);
42
43 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
44
45 //=======================================================================
46 //function : Open
47 //purpose  : 
48 //=======================================================================
49
50 void PCDM_ReadWriter::Open (Storage_BaseDriver&                 aDriver,
51                             const TCollection_ExtendedString&   aFileName,
52                             const Storage_OpenMode              aMode)
53 {
54   Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
55   if(error != Storage_VSOk) {
56     Standard_SStream aMsg; aMsg << "could not open the file: ";
57     aMsg << aFileName;
58     switch (error) {
59     case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
60     case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
61     default:
62       break;
63     }
64     aMsg << (char)0;
65     Standard_Failure::Raise(aMsg);
66   }
67 }
68
69 //=======================================================================
70 //function : Reader
71 //purpose  : 
72 //=======================================================================
73
74 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
75
76 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
77                                            (const TCollection_ExtendedString&)
78 {
79   static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
80   return theReader;
81 }
82
83 //=======================================================================
84 //function : Writer
85 //purpose  : 
86 //=======================================================================
87
88 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
89 {
90   static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
91   return theWriter;
92 }
93  
94 //=======================================================================
95 //function : WriteFileFormat
96 //purpose  : 
97 //=======================================================================
98
99 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
100                                        const Handle(CDM_Document)& aDocument)
101
102   TCollection_AsciiString ligne(FILE_FORMAT);
103   ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
104
105   aData->AddToUserInfo(ligne);
106 }
107
108 //=======================================================================
109 //function : FileFormat
110 //purpose  : 
111 //=======================================================================
112
113 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
114                                 (const TCollection_ExtendedString& aFileName)
115 {
116   TCollection_ExtendedString theFormat;
117   
118   PCDM_BaseDriverPointer theFileDriver;
119
120   // conversion to UTF-8 is done inside
121   TCollection_AsciiString theFileName (aFileName);
122   if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
123     return ::TryXmlDriverType (theFileName);
124
125   static Standard_Boolean theFileIsOpen;
126   theFileIsOpen=Standard_False;
127
128   try {
129     OCC_CATCH_SIGNALS
130     
131     Open(*theFileDriver,aFileName,Storage_VSRead);
132     theFileIsOpen=Standard_True;
133     Handle(Storage_Schema) s = new Storage_Schema;
134     Handle(Storage_HeaderData) hd = s->ReadHeaderSection(*theFileDriver);
135     const TColStd_SequenceOfAsciiString &refUserInfo = hd->UserInfo();
136     Standard_Boolean found=Standard_False;
137     for (Standard_Integer i =1; !found && i<=  refUserInfo.Length() ; i++) {
138       if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
139         found=Standard_True;
140         theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
141                                              Standard_True);
142       }
143     }
144     if(!found) theFormat=s->ReadTypeSection(*theFileDriver)->Types()->Value(1);
145   }
146   catch (Standard_Failure) {}
147
148   
149   if(theFileIsOpen)theFileDriver->Close();
150
151   delete theFileDriver;
152
153   return theFormat;
154 }
155
156 //=======================================================================
157 //function : FileFormat
158 //purpose  : 
159 //=======================================================================
160
161 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
162 {
163   TCollection_ExtendedString aFormat;
164
165   Storage_BaseDriver* aFileDriver;
166   if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_Unknown)
167   {
168     return ::TryXmlDriverType (theIStream);
169   }
170   
171   // the stream starts with a magic number, FileDriverType has read
172   // them already but returned the stream pos to initial state,
173   // thus we should read them before reading of info section
174   aFileDriver->ReadMagicNumber(theIStream);
175
176   aFileDriver->ReadCompleteInfo (theIStream, theData);
177
178   for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
179   {
180     const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
181
182     if(aLine.Search (FILE_FORMAT) != -1)
183     {
184       aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
185     }
186   }
187
188   return aFormat;
189 }
190
191 //=======================================================================
192 //function : ::TryXmlDriverType
193 //purpose  : called from FileFormat()
194 //=======================================================================
195
196 static TCollection_ExtendedString TryXmlDriverType
197                                 (const TCollection_AsciiString& theFileName)
198 {
199   TCollection_ExtendedString theFormat;
200   PCDM_DOMHeaderParser       aParser;
201   const char                 * aDocumentElementName = "document";
202   aParser.SetStartElementName (Standard_CString(aDocumentElementName));
203
204   // Parse the file; if there is no error or an error appears before retrieval
205   // of the DocumentElement, the XML format cannot be defined
206   if (aParser.parse (theFileName.ToCString()))
207   {
208     LDOM_Element anElement = aParser.GetElement();
209     if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
210       theFormat = anElement.getAttribute ("format");
211   }
212   return theFormat;
213 }
214
215 //=======================================================================
216 //function : ::TryXmlDriverType
217 //purpose  : called from FileFormat()
218 //=======================================================================
219
220 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
221 {
222   TCollection_ExtendedString theFormat;
223   PCDM_DOMHeaderParser       aParser;
224   const char                 * aDocumentElementName = "document";
225   aParser.SetStartElementName (Standard_CString(aDocumentElementName));
226
227   if (theIStream.good())
228   {
229     streampos aDocumentPos = theIStream.tellg();
230
231     // Parse the file; if there is no error or an error appears before retrieval
232     // of the DocumentElement, the XML format cannot be defined
233     if (aParser.parse (theIStream))
234     {
235       LDOM_Element anElement = aParser.GetElement();
236       if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
237         theFormat = anElement.getAttribute ("format");
238     }
239
240     if (!theIStream.good())
241     {
242       theIStream.clear();
243     }
244
245     theIStream.seekg(aDocumentPos);
246   }
247
248   return theFormat;
249 }