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_TypeData.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TCollection_ExtendedString.hxx>
32 #include <TColStd_HSequenceOfAsciiString.hxx>
35 IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
37 #define FILE_FORMAT "FILE_FORMAT: "
39 static TCollection_ExtendedString TryXmlDriverType
40 (const TCollection_AsciiString& theFileName);
42 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
44 //=======================================================================
47 //=======================================================================
49 void PCDM_ReadWriter::Open (Storage_BaseDriver& aDriver,
50 const TCollection_ExtendedString& aFileName,
51 const Storage_OpenMode aMode)
53 Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
54 if(error != Storage_VSOk) {
55 Standard_SStream aMsg; aMsg << "could not open the file: ";
58 case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
59 case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
64 Standard_Failure::Raise(aMsg);
68 //=======================================================================
71 //=======================================================================
73 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
75 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
76 (const TCollection_ExtendedString&)
78 static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
82 //=======================================================================
85 //=======================================================================
87 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
89 static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
93 //=======================================================================
94 //function : WriteFileFormat
96 //=======================================================================
98 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
99 const Handle(CDM_Document)& aDocument)
101 TCollection_AsciiString ligne(FILE_FORMAT);
102 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
104 aData->AddToUserInfo(ligne);
107 //=======================================================================
108 //function : FileFormat
110 //=======================================================================
112 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
113 (const TCollection_ExtendedString& aFileName)
115 TCollection_ExtendedString theFormat;
117 PCDM_BaseDriverPointer theFileDriver;
119 // conversion to UTF-8 is done inside
120 TCollection_AsciiString theFileName (aFileName);
121 if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
122 return ::TryXmlDriverType (theFileName);
124 static Standard_Boolean theFileIsOpen;
125 theFileIsOpen=Standard_False;
130 Open(*theFileDriver,aFileName,Storage_VSRead);
131 theFileIsOpen=Standard_True;
132 Storage_HeaderData hd;
133 hd.Read (*theFileDriver);
134 const TColStd_SequenceOfAsciiString &refUserInfo = hd.UserInfo();
135 Standard_Boolean found=Standard_False;
136 for (Standard_Integer i =1; !found && i<= refUserInfo.Length() ; i++) {
137 if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
139 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
146 td.Read (*theFileDriver);
147 theFormat = td.Types()->Value(1);
150 catch (Standard_Failure) {}
153 if(theFileIsOpen)theFileDriver->Close();
155 delete theFileDriver;
160 //=======================================================================
161 //function : FileFormat
163 //=======================================================================
165 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
167 TCollection_ExtendedString aFormat;
169 Storage_BaseDriver* aFileDriver = 0L;
170 if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_XmlFile)
172 return ::TryXmlDriverType (theIStream);
176 // type is not recognized, return empty string
180 aFileDriver->ReadCompleteInfo (theIStream, theData);
182 for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
184 const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
186 if(aLine.Search (FILE_FORMAT) != -1)
188 aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
195 //=======================================================================
196 //function : ::TryXmlDriverType
197 //purpose : called from FileFormat()
198 //=======================================================================
200 static TCollection_ExtendedString TryXmlDriverType
201 (const TCollection_AsciiString& theFileName)
203 TCollection_ExtendedString theFormat;
204 PCDM_DOMHeaderParser aParser;
205 const char * aDocumentElementName = "document";
206 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
208 // Parse the file; if there is no error or an error appears before retrieval
209 // of the DocumentElement, the XML format cannot be defined
210 if (aParser.parse (theFileName.ToCString()))
212 LDOM_Element anElement = aParser.GetElement();
213 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
214 theFormat = anElement.getAttribute ("format");
219 //=======================================================================
220 //function : ::TryXmlDriverType
221 //purpose : called from FileFormat()
222 //=======================================================================
224 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
226 TCollection_ExtendedString theFormat;
227 PCDM_DOMHeaderParser aParser;
228 const char * aDocumentElementName = "document";
229 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
231 if (theIStream.good())
233 // Parse the file; if there is no error or an error appears before retrieval
234 // of the DocumentElement, the XML format cannot be defined
235 if (aParser.parse (theIStream, Standard_True))
237 LDOM_Element anElement = aParser.GetElement();
238 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
239 theFormat = anElement.getAttribute ("format");