b311480e |
1 | // Created on: 1997-12-09 |
2 | // Created by: Jean-Louis Frenkel |
3 | // Copyright (c) 1997-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
17 | #include <PCDM_ReadWriter.ixx> |
7fd59977 |
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> |
23 | #include <UTL.hxx> |
24 | #include <TColStd_HSequenceOfAsciiString.hxx> |
25 | #include <PCDM.hxx> |
26 | #include <PCDM_DOMHeaderParser.hxx> |
27 | |
28 | #define FILE_FORMAT "FILE_FORMAT: " |
29 | |
30 | static TCollection_ExtendedString TryXmlDriverType |
31 | (const TCollection_AsciiString& theFileName); |
32 | |
33 | //======================================================================= |
34 | //function : Open |
35 | //purpose : |
36 | //======================================================================= |
37 | |
38 | void PCDM_ReadWriter::Open (Storage_BaseDriver& aDriver, |
39 | const TCollection_ExtendedString& aFileName, |
40 | const Storage_OpenMode aMode) |
41 | { |
42 | Storage_Error error = UTL::OpenFile(aDriver,aFileName,aMode); |
43 | if(error != Storage_VSOk) { |
44 | Standard_SStream aMsg; aMsg << "could not open the file: "; |
45 | aMsg << aFileName; |
46 | switch (error) { |
47 | case Storage_VSOpenError: aMsg << "; file was not found or permission denied"; break; |
48 | case Storage_VSAlreadyOpen: aMsg<< "; file was already opened"; |
49 | default: |
50 | break; |
51 | } |
52 | aMsg << (char)0; |
53 | Standard_Failure::Raise(aMsg); |
54 | } |
55 | } |
56 | |
57 | //======================================================================= |
58 | //function : Reader |
59 | //purpose : |
60 | //======================================================================= |
61 | |
62 | //Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader(const TCollection_ExtendedString& aFileName) { |
63 | |
64 | Handle(PCDM_ReadWriter) PCDM_ReadWriter::Reader |
65 | (const TCollection_ExtendedString&) |
66 | { |
67 | static Handle(PCDM_ReadWriter_1) theReader=new PCDM_ReadWriter_1; |
68 | return theReader; |
69 | } |
70 | |
71 | //======================================================================= |
72 | //function : Writer |
73 | //purpose : |
74 | //======================================================================= |
75 | |
76 | Handle(PCDM_ReadWriter) PCDM_ReadWriter::Writer () |
77 | { |
78 | static Handle(PCDM_ReadWriter_1) theWriter=new PCDM_ReadWriter_1; |
79 | return theWriter; |
80 | } |
81 | |
82 | //======================================================================= |
83 | //function : WriteFileFormat |
84 | //purpose : |
85 | //======================================================================= |
86 | |
87 | void PCDM_ReadWriter::WriteFileFormat (const Handle(Storage_Data)& aData, |
88 | const Handle(CDM_Document)& aDocument) |
89 | { |
90 | TCollection_AsciiString ligne(FILE_FORMAT); |
91 | ligne += TCollection_AsciiString(aDocument->StorageFormat(),'?'); |
92 | |
93 | aData->AddToUserInfo(ligne); |
94 | } |
95 | |
96 | //======================================================================= |
97 | //function : FileFormat |
98 | //purpose : |
99 | //======================================================================= |
100 | |
101 | TCollection_ExtendedString PCDM_ReadWriter::FileFormat |
102 | (const TCollection_ExtendedString& aFileName) |
103 | { |
104 | TCollection_ExtendedString theFormat; |
105 | |
106 | PCDM_BaseDriverPointer theFileDriver; |
107 | |
d9ff84e8 |
108 | // conversion to UTF-8 is done inside |
109 | TCollection_AsciiString theFileName (aFileName); |
7fd59977 |
110 | if (PCDM::FileDriverType (theFileName, theFileDriver) == PCDM_TOFD_Unknown) |
111 | return ::TryXmlDriverType (theFileName); |
112 | |
113 | static Standard_Boolean theFileIsOpen; |
114 | theFileIsOpen=Standard_False; |
115 | |
116 | try { |
117 | OCC_CATCH_SIGNALS |
118 | |
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) { |
127 | found=Standard_True; |
d9ff84e8 |
128 | theFormat=TCollection_ExtendedString(refUserInfo(i).Token(" ",2).ToCString(), |
129 | Standard_True); |
7fd59977 |
130 | } |
131 | } |
132 | if(!found) theFormat=s->ReadTypeSection(*theFileDriver)->Types()->Value(1); |
133 | } |
134 | catch (Standard_Failure) {} |
135 | |
136 | |
137 | if(theFileIsOpen)theFileDriver->Close(); |
138 | |
139 | delete theFileDriver; |
140 | |
141 | return theFormat; |
142 | } |
143 | |
144 | //======================================================================= |
145 | //function : ::TryXmlDriverType |
146 | //purpose : called from FileFormat() |
147 | //======================================================================= |
148 | |
149 | static TCollection_ExtendedString TryXmlDriverType |
150 | (const TCollection_AsciiString& theFileName) |
151 | { |
152 | TCollection_ExtendedString theFormat; |
153 | PCDM_DOMHeaderParser aParser; |
154 | const char * aDocumentElementName = "document"; |
155 | aParser.SetStartElementName (Standard_CString(aDocumentElementName)); |
156 | |
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())) |
160 | { |
161 | LDOM_Element anElement = aParser.GetElement(); |
162 | if (anElement.getTagName().equals (LDOMString(aDocumentElementName))) |
163 | theFormat = anElement.getAttribute ("format"); |
164 | } |
165 | return theFormat; |
166 | } |