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.
17 #include <PCDM_ReadWriter.ixx>
18 #include <PCDM_ReadWriter_1.hxx>
19 #include <Storage_Schema.hxx>
20 #include <Standard_ErrorHandler.hxx>
21 #include <Storage_HeaderData.hxx>
22 #include <Storage_TypeData.hxx>
24 #include <TColStd_HSequenceOfAsciiString.hxx>
26 #include <PCDM_DOMHeaderParser.hxx>
28 #define FILE_FORMAT "FILE_FORMAT: "
30 static TCollection_ExtendedString TryXmlDriverType
31 (const TCollection_AsciiString& theFileName);
33 //=======================================================================
36 //=======================================================================
38 void PCDM_ReadWriter::Open (Storage_BaseDriver& aDriver,
39 const TCollection_ExtendedString& aFileName,
40 const Storage_OpenMode aMode)
42 Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode);
43 if(error != Storage_VSOk) {
44 Standard_SStream aMsg; aMsg << "could not open the file: ";
47 case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break;
48 case Storage_VSAlreadyOpen: aMsg<< "; file was already opened";
53 Standard_Failure::Raise(aMsg);
57 //=======================================================================
60 //=======================================================================
62 //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) {
64 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader
65 (const TCollection_ExtendedString&)
67 static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1;
71 //=======================================================================
74 //=======================================================================
76 Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer ()
78 static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1;
82 //=======================================================================
83 //function : WriteFileFormat
85 //=======================================================================
87 void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData,
88 const Handle(CDM_Document)& aDocument)
90 TCollection_AsciiString ligne(FILE_FORMAT);
91 ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?');
93 aData->AddToUserInfo(ligne);
96 //=======================================================================
97 //function : FileFormat
99 //=======================================================================
101 TCollection_ExtendedString PCDM_ReadWriter::FileFormat
102 (const TCollection_ExtendedString& aFileName)
104 TCollection_ExtendedString theFormat;
106 PCDM_BaseDriverPointer theFileDriver;
108 // conversion to UTF-8 is done inside
109 TCollection_AsciiString theFileName (aFileName);
110 if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown)
111 return ::TryXmlDriverType (theFileName);
113 static Standard_Boolean theFileIsOpen;
114 theFileIsOpen=Standard_False;
119 Open(*theFileDriver,aFileName,Storage_VSRead);
120 theFileIsOpen=Standard_True;
121 Handle(Storage_Schema) s = new Storage_Schema;
122 Handle(Storage_HeaderData) hd = s->ReadHeaderSection(*theFileDriver);
123 const TColStd_SequenceOfAsciiString &refUserInfo = hd->UserInfo();
124 Standard_Boolean found=Standard_False;
125 for (Standard_Integer i =1; !found && i<= refUserInfo.Length() ; i++) {
126 if(refUserInfo(i).Search(FILE_FORMAT) != -1) {
128 theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(),
132 if(!found) theFormat=s->ReadTypeSection(*theFileDriver)->Types()->Value(1);
134 catch (Standard_Failure) {}
137 if(theFileIsOpen)theFileDriver->Close();
139 delete theFileDriver;
144 //=======================================================================
145 //function : ::TryXmlDriverType
146 //purpose : called from FileFormat()
147 //=======================================================================
149 static TCollection_ExtendedString TryXmlDriverType
150 (const TCollection_AsciiString& theFileName)
152 TCollection_ExtendedString theFormat;
153 PCDM_DOMHeaderParser aParser;
154 const char * aDocumentElementName = "document";
155 aParser.SetStartElementName (Standard_CString(aDocumentElementName));
157 // Parse the file; if there is no error or an error appears before retrieval
158 // of the DocumentElement, the XML format cannot be defined
159 if (aParser.parse (theFileName.ToCString()))
161 LDOM_Element anElement = aParser.GetElement();
162 if (anElement.getTagName().equals (LDOMString(aDocumentElementName)))
163 theFormat = anElement.getAttribute ("format");