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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <CDM_Document.hxx>
19 #include <CDM_MessageDriver.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>
36 IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
38 #define FILE_FORMAT "FILE_FORMAT: "
40 static TCollection_ExtendedString TryXmlDriverType
41 (const TCollection_AsciiString& theFileName);
43 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
45 //=======================================================================
48 //=======================================================================
50 void PCDM_ReadWriter::Open (Storage_BaseDriver& aDriver,
51 const TCollection_ExtendedString& aFileName,
52 const Storage_OpenMode aMode)
54 Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
55 if(error != Storage_VSOk) {
56 Standard_SStream aMsg; aMsg << "could not open the file: ";
59 case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
60 case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
65 Standard_Failure::Raise(aMsg);
69 //=======================================================================
72 //=======================================================================
74 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
76 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
77 (const TCollection_ExtendedString&)
79 static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
83 //=======================================================================
86 //=======================================================================
88 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
90 static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
94 //=======================================================================
95 //function : WriteFileFormat
97 //=======================================================================
99 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
100 const Handle(CDM_Document)& aDocument)
102 TCollection_AsciiString ligne(FILE_FORMAT);
103 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
105 aData->AddToUserInfo(ligne);
108 //=======================================================================
109 //function : FileFormat
111 //=======================================================================
113 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
114 (const TCollection_ExtendedString& aFileName)
116 TCollection_ExtendedString theFormat;
118 PCDM_BaseDriverPointer theFileDriver;
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);
125 static Standard_Boolean theFileIsOpen;
126 theFileIsOpen=Standard_False;
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) {
140 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
144 if(!found) theFormat=s->ReadTypeSection(*theFileDriver)->Types()->Value(1);
146 catch (Standard_Failure) {}
149 if(theFileIsOpen)theFileDriver->Close();
151 delete theFileDriver;
156 //=======================================================================
157 //function : FileFormat
159 //=======================================================================
161 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
163 TCollection_ExtendedString aFormat;
165 Storage_BaseDriver* aFileDriver;
166 if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_Unknown)
168 return ::TryXmlDriverType (theIStream);
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);
176 aFileDriver->ReadCompleteInfo (theIStream, theData);
178 for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
180 const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
182 if(aLine.Search (FILE_FORMAT) != -1)
184 aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
191 //=======================================================================
192 //function : ::TryXmlDriverType
193 //purpose : called from FileFormat()
194 //=======================================================================
196 static TCollection_ExtendedString TryXmlDriverType
197 (const TCollection_AsciiString& theFileName)
199 TCollection_ExtendedString theFormat;
200 PCDM_DOMHeaderParser aParser;
201 const char * aDocumentElementName = "document";
202 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
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()))
208 LDOM_Element anElement = aParser.GetElement();
209 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
210 theFormat = anElement.getAttribute ("format");
215 //=======================================================================
216 //function : ::TryXmlDriverType
217 //purpose : called from FileFormat()
218 //=======================================================================
220 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
222 TCollection_ExtendedString theFormat;
223 PCDM_DOMHeaderParser aParser;
224 const char * aDocumentElementName = "document";
225 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
227 if (theIStream.good())
229 streampos aDocumentPos = theIStream.tellg();
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))
235 LDOM_Element anElement = aParser.GetElement();
236 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
237 theFormat = anElement.getAttribute ("format");
240 if (!theIStream.good())
245 theIStream.seekg(aDocumentPos);