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 (const Handle(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 return (new PCDM_ReadWriter_1);
80 //=======================================================================
83 //=======================================================================
85 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
87 return (new PCDM_ReadWriter_1);
90 //=======================================================================
91 //function : WriteFileFormat
93 //=======================================================================
95 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
96 const Handle(CDM_Document)& aDocument)
98 TCollection_AsciiString ligne(FILE_FORMAT);
99 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
101 aData->AddToUserInfo(ligne);
104 //=======================================================================
105 //function : FileFormat
107 //=======================================================================
109 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
110 (const TCollection_ExtendedString& aFileName)
112 TCollection_ExtendedString theFormat;
114 Handle(Storage_BaseDriver) theFileDriver;
116 // conversion to UTF-8 is done inside
117 TCollection_AsciiString theFileName (aFileName);
118 if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
119 return ::TryXmlDriverType (theFileName);
121 Standard_Boolean theFileIsOpen(Standard_False);
125 Open(theFileDriver,aFileName,Storage_VSRead);
126 theFileIsOpen=Standard_True;
127 Storage_HeaderData hd;
128 hd.Read (theFileDriver);
129 const TColStd_SequenceOfAsciiString &refUserInfo = hd.UserInfo();
130 Standard_Boolean found=Standard_False;
131 for (Standard_Integer i =1; !found && i<= refUserInfo.Length() ; i++) {
132 if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
134 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
141 td.Read (theFileDriver);
142 theFormat = td.Types()->Value(1);
145 catch (Standard_Failure const&) {}
149 theFileDriver->Close();
155 //=======================================================================
156 //function : FileFormat
158 //=======================================================================
160 TCollection_ExtendedString PCDM_ReadWriter::FileFormat (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
162 TCollection_ExtendedString aFormat;
164 Handle(Storage_BaseDriver) aFileDriver;
165 if (PCDM::FileDriverType (theIStream, aFileDriver) == PCDM_TOFD_XmlFile)
167 return ::TryXmlDriverType (theIStream);
171 // type is not recognized, return empty string
175 aFileDriver->ReadCompleteInfo (theIStream, theData);
177 for (Standard_Integer i = 1; i <= theData->HeaderData()->UserInfo().Length(); i++)
179 const TCollection_AsciiString& aLine = theData->HeaderData()->UserInfo().Value(i);
181 if(aLine.Search (FILE_FORMAT) != -1)
183 aFormat = TCollection_ExtendedString (aLine.Token(" ",2).ToCString(), Standard_True);
190 //=======================================================================
191 //function : ::TryXmlDriverType
192 //purpose : called from FileFormat()
193 //=======================================================================
195 static TCollection_ExtendedString TryXmlDriverType
196 (const TCollection_AsciiString& theFileName)
198 TCollection_ExtendedString theFormat;
199 PCDM_DOMHeaderParser aParser;
200 const char * aDocumentElementName = "document";
201 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
203 // Parse the file; if there is no error or an error appears before retrieval
204 // of the DocumentElement, the XML format cannot be defined
205 if (aParser.parse (theFileName.ToCString()))
207 LDOM_Element anElement = aParser.GetElement();
208 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
209 theFormat = anElement.getAttribute ("format");
214 //=======================================================================
215 //function : ::TryXmlDriverType
216 //purpose : called from FileFormat()
217 //=======================================================================
219 static TCollection_ExtendedString TryXmlDriverType (Standard_IStream& theIStream)
221 TCollection_ExtendedString theFormat;
222 PCDM_DOMHeaderParser aParser;
223 const char * aDocumentElementName = "document";
224 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
226 if (theIStream.good())
228 // Parse the file; if there is no error or an error appears before retrieval
229 // of the DocumentElement, the XML format cannot be defined
230 if (aParser.parse (theIStream, Standard_True))
232 LDOM_Element anElement = aParser.GetElement();
233 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
234 theFormat = anElement.getAttribute ("format");