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>
20 #include <PCDM_DOMHeaderParser.hxx>
21 #include <PCDM_ReadWriter.hxx>
22 #include <PCDM_ReadWriter_1.hxx>
23 #include <Standard_ErrorHandler.hxx>
24 #include <Standard_Type.hxx>
25 #include <Storage_BaseDriver.hxx>
26 #include <Storage_Data.hxx>
27 #include <Storage_HeaderData.hxx>
28 #include <Storage_TypeData.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TColStd_HSequenceOfAsciiString.hxx>
34 IMPLEMENT_STANDARD_RTTIEXT(PCDM_ReadWriter,Standard_Transient)
36 #define FILE_FORMAT "FILE_FORMAT: "
38 static TCollection_ExtendedString TryXmlDriverType
39 (const TCollection_AsciiString& theFileName);
41 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream);
43 //=======================================================================
46 //=======================================================================
48 void PCDM_ReadWriter::Open (Storage_BaseDriver& aDriver,
49 const TCollection_ExtendedString& aFileName,
50 const Storage_OpenMode aMode)
52 Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
53 if(error != Storage_VSOk) {
54 Standard_SStream aMsg; aMsg << "could not open the file: ";
57 case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
58 case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
63 throw Standard_Failure(aMsg.str().c_str());
67 //=======================================================================
70 //=======================================================================
72 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
74 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
75 (const TCollection_ExtendedString&)
77 static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
81 //=======================================================================
84 //=======================================================================
86 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
88 static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
92 //=======================================================================
93 //function : WriteFileFormat
95 //=======================================================================
97 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
98 const Handle(CDM_Document)& aDocument)
100 TCollection_AsciiString ligne(FILE_FORMAT);
101 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
103 aData->AddToUserInfo(ligne);
106 //=======================================================================
107 //function : FileFormat
109 //=======================================================================
111 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
112 (const TCollection_ExtendedString& aFileName)
114 TCollection_ExtendedString theFormat;
116 PCDM_BaseDriverPointer theFileDriver;
118 // conversion to UTF-8 is done inside
119 TCollection_AsciiString theFileName (aFileName);
120 if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
121 return ::TryXmlDriverType (theFileName);
123 static Standard_Boolean theFileIsOpen;
124 theFileIsOpen=Standard_False;
129 Open(*theFileDriver,aFileName,Storage_VSRead);
130 theFileIsOpen=Standard_True;
131 Storage_HeaderData hd;
132 hd.Read (*theFileDriver);
133 const TColStd_SequenceOfAsciiString &refUserInfo = hd.UserInfo();
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) {
138 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
145 td.Read (*theFileDriver);
146 theFormat = td.Types()->Value(1);
149 catch (Standard_Failure const&) {}
152 if(theFileIsOpen)theFileDriver->Close();
154 delete theFileDriver;
159 //=======================================================================
160 //function : FileFormat
162 //=======================================================================
164 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
166 TCollection_ExtendedString aFormat;
168 Storage_BaseDriver* aFileDriver = 0L;
169 if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_XmlFile)
171 return ::TryXmlDriverType (theIStream);
175 // type is not recognized, return empty string
179 aFileDriver->ReadCompleteInfo (theIStream, theData);
181 for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
183 const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
185 if(aLine.Search (FILE_FORMAT) != -1)
187 aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
194 //=======================================================================
195 //function : ::TryXmlDriverType
196 //purpose : called from FileFormat()
197 //=======================================================================
199 static TCollection_ExtendedString TryXmlDriverType
200 (const TCollection_AsciiString& theFileName)
202 TCollection_ExtendedString theFormat;
203 PCDM_DOMHeaderParser aParser;
204 const char * aDocumentElementName = "document";
205 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
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()))
211 LDOM_Element anElement = aParser.GetElement();
212 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
213 theFormat = anElement.getAttribute ("format");
218 //=======================================================================
219 //function : ::TryXmlDriverType
220 //purpose : called from FileFormat()
221 //=======================================================================
223 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
225 TCollection_ExtendedString theFormat;
226 PCDM_DOMHeaderParser aParser;
227 const char * aDocumentElementName = "document";
228 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
230 if (theIStream.good())
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
234 if (aParser.parse (theIStream, Standard_True))
236 LDOM_Element anElement = aParser.GetElement();
237 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
238 theFormat = anElement.getAttribute ("format");