0031353: TDocStd_Application does not have api to set progress indicator
[occt.git] / src / PCDM / PCDM_ReadWriter.cxx
CommitLineData
b311480e 1// Created on: 1997-12-09
2// Created by: Jean-Louis Frenkel
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
42cf5bc1 17
18#include <CDM_Document.hxx>
42cf5bc1 19#include <PCDM.hxx>
20#include <PCDM_DOMHeaderParser.hxx>
21#include <PCDM_ReadWriter.hxx>
7fd59977 22#include <PCDM_ReadWriter_1.hxx>
7fd59977 23#include <Standard_ErrorHandler.hxx>
42cf5bc1 24#include <Standard_Type.hxx>
25#include <Storage_BaseDriver.hxx>
26#include <Storage_Data.hxx>
7fd59977 27#include <Storage_HeaderData.hxx>
28#include <Storage_TypeData.hxx>
42cf5bc1 29#include <TCollection_AsciiString.hxx>
30#include <TCollection_ExtendedString.hxx>
7fd59977 31#include <TColStd_HSequenceOfAsciiString.hxx>
42cf5bc1 32#include <UTL.hxx>
7fd59977 33
92efcf78 34IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
35
7fd59977 36#define FILE_FORMAT "FILE_FORMAT: "
37
38static TCollection_ExtendedString TryXmlDriverType
39 (const TCollection_AsciiString& theFileName);
40
4ff92abe 41static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
42
7fd59977 43//=======================================================================
44//function : Open
45//purpose :
46//=======================================================================
47
39c8dc70 48void PCDM_ReadWriter::Open (const Handle(Storage_BaseDriver)& aDriver,
7fd59977 49 const TCollection_ExtendedString& aFileName,
50 const Storage_OpenMode aMode)
51{
52 Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
53 if(error != Storage_VSOk) {
54 Standard_SStream aMsg; aMsg << "could not open the file: ";
55 aMsg << aFileName;
56 switch (error) {
57 case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
58 case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
59 default:
60 break;
61 }
62 aMsg << (char)0;
9775fa61 63 throw Standard_Failure(aMsg.str().c_str());
7fd59977 64 }
65}
66
67//=======================================================================
68//function : Reader
69//purpose :
70//=======================================================================
71
72//Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
73
74Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
75 (const TCollection_ExtendedString&)
76{
77 static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
78 return theReader;
79}
80
81//=======================================================================
82//function : Writer
83//purpose :
84//=======================================================================
85
86Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
87{
88 static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
89 return theWriter;
90}
91
92//=======================================================================
93//function : WriteFileFormat
94//purpose :
95//=======================================================================
96
97void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
98 const Handle(CDM_Document)& aDocument)
99{
100 TCollection_AsciiString ligne(FILE_FORMAT);
101 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
102
103 aData->AddToUserInfo(ligne);
104}
105
106//=======================================================================
107//function : FileFormat
108//purpose :
109//=======================================================================
110
111TCollection_ExtendedString PCDM_ReadWriter::FileFormat
112 (const TCollection_ExtendedString& aFileName)
113{
114 TCollection_ExtendedString theFormat;
115
39c8dc70 116 Handle(Storage_BaseDriver) theFileDriver;
7fd59977 117
d9ff84e8 118 // conversion to UTF-8 is done inside
119 TCollection_AsciiString theFileName (aFileName);
7fd59977 120 if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
121 return ::TryXmlDriverType (theFileName);
122
123 static Standard_Boolean theFileIsOpen;
124 theFileIsOpen=Standard_False;
125
126 try {
127 OCC_CATCH_SIGNALS
128
39c8dc70 129 Open(theFileDriver,aFileName,Storage_VSRead);
7fd59977 130 theFileIsOpen=Standard_True;
7ed7467d 131 Storage_HeaderData hd;
39c8dc70 132 hd.Read (theFileDriver);
7ed7467d 133 const TColStd_SequenceOfAsciiString &refUserInfo = hd.UserInfo();
7fd59977 134 Standard_Boolean found=Standard_False;
135 for (Standard_Integer i =1; !found && i<= refUserInfo.Length() ; i++) {
136 if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
137 found=Standard_True;
d9ff84e8 138 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
139 Standard_True);
7fd59977 140 }
141 }
7ed7467d 142 if (!found)
143 {
144 Storage_TypeData td;
39c8dc70 145 td.Read (theFileDriver);
7ed7467d 146 theFormat = td.Types()->Value(1);
147 }
7fd59977 148 }
a738b534 149 catch (Standard_Failure const&) {}
7fd59977 150
39c8dc70 151 if(theFileIsOpen)
152 {
153 theFileDriver->Close();
154 }
7fd59977 155
156 return theFormat;
157}
158
159//=======================================================================
4ff92abe 160//function : FileFormat
161//purpose :
162//=======================================================================
163
164TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
165{
166 TCollection_ExtendedString aFormat;
167
39c8dc70 168 Handle(Storage_BaseDriver) aFileDriver;
5fce1605 169 if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_XmlFile)
4ff92abe 170 {
171 return ::TryXmlDriverType (theIStream);
172 }
18151f1a 173 if (!aFileDriver)
174 {
175 // type is not recognized, return empty string
176 return aFormat;
177 }
4ff92abe 178
179 aFileDriver->ReadCompleteInfo (theIStream, theData);
180
181 for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
182 {
183 const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
184
185 if(aLine.Search (FILE_FORMAT) != -1)
186 {
187 aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
188 }
189 }
190
191 return aFormat;
192}
193
194//=======================================================================
7fd59977 195//function : ::TryXmlDriverType
196//purpose : called from FileFormat()
197//=======================================================================
198
199static TCollection_ExtendedString TryXmlDriverType
200 (const TCollection_AsciiString& theFileName)
201{
202 TCollection_ExtendedString theFormat;
203 PCDM_DOMHeaderParser aParser;
204 const char * aDocumentElementName = "document";
205 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
206
207 // Parse the file; if there is no error or an error appears before retrieval
208 // of the DocumentElement, the XML format cannot be defined
209 if (aParser.parse (theFileName.ToCString()))
210 {
211 LDOM_Element anElement = aParser.GetElement();
212 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
213 theFormat = anElement.getAttribute ("format");
214 }
215 return theFormat;
216}
4ff92abe 217
218//=======================================================================
219//function : ::TryXmlDriverType
220//purpose : called from FileFormat()
221//=======================================================================
222
223static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
224{
225 TCollection_ExtendedString theFormat;
226 PCDM_DOMHeaderParser aParser;
227 const char * aDocumentElementName = "document";
228 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
229
230 if (theIStream.good())
231 {
4ff92abe 232 // Parse the file; if there is no error or an error appears before retrieval
233 // of the DocumentElement, the XML format cannot be defined
5fce1605 234 if (aParser.parse (theIStream, Standard_True))
4ff92abe 235 {
236 LDOM_Element anElement = aParser.GetElement();
237 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
238 theFormat = anElement.getAttribute ("format");
239 }
4ff92abe 240 }
241
242 return theFormat;
243}