1 // Created on: 2001-07-09
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <CDM_Application.hxx>
18 #include <CDM_Document.hxx>
19 #include <CDM_MessageDriver.hxx>
20 #include <CDM_NullMessageDriver.hxx>
21 #include <LDOM_DocumentType.hxx>
22 #include <LDOM_LDOMImplementation.hxx>
23 #include <LDOM_XmlWriter.hxx>
24 #include <OSD_Environment.hxx>
25 #include <OSD_File.hxx>
26 #include <OSD_OpenFile.hxx>
28 #include <PCDM_ReadWriter.hxx>
29 #include <Standard_ErrorHandler.hxx>
30 #include <Standard_Failure.hxx>
31 #include <Standard_Type.hxx>
32 #include <Storage_Data.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TCollection_ExtendedString.hxx>
35 #include <TColStd_SequenceOfAsciiString.hxx>
36 #include <TDocStd_Document.hxx>
37 #include <XmlLDrivers.hxx>
38 #include <XmlLDrivers_DocumentStorageDriver.hxx>
39 #include <XmlLDrivers_NamespaceDef.hxx>
41 #include <XmlMDF_ADriverTable.hxx>
42 #include <XmlObjMgt.hxx>
43 #include <XmlObjMgt_Document.hxx>
44 #include <XmlObjMgt_SRelocationTable.hxx>
47 IMPLEMENT_STANDARD_RTTIEXT(XmlLDrivers_DocumentStorageDriver,PCDM_StorageDriver)
49 #define STORAGE_VERSION "STORAGE_VERSION: "
50 #define REFERENCE_COUNTER "REFERENCE_COUNTER: "
51 #define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
52 #define START_REF "START_REF"
53 #define END_REF "END_REF"
55 #define FAILSTR "Failed to write xsi:schemaLocation : "
58 static void take_time (const Standard_Integer, const char *,
59 const Handle(CDM_MessageDriver)&)
66 //=======================================================================
67 //function : XmlLDrivers_DocumentStorageDriver
68 //purpose : Constructor
69 //=======================================================================
70 XmlLDrivers_DocumentStorageDriver::XmlLDrivers_DocumentStorageDriver
71 (const TCollection_ExtendedString& theCopyright)
72 : myCopyright (theCopyright)
76 //=======================================================================
77 //function : SchemaName
79 //=======================================================================
80 TCollection_ExtendedString XmlLDrivers_DocumentStorageDriver::SchemaName() const
82 TCollection_ExtendedString schemaname;
86 //=======================================================================
87 //function : AddNamespace
89 //=======================================================================
91 void XmlLDrivers_DocumentStorageDriver::AddNamespace
92 (const TCollection_AsciiString& thePrefix,
93 const TCollection_AsciiString& theURI)
95 for (Standard_Integer i = 1; i <= mySeqOfNS.Length(); i++)
96 if (thePrefix == mySeqOfNS(i).Prefix()) return;
97 mySeqOfNS.Append (XmlLDrivers_NamespaceDef(thePrefix, theURI));
100 //=======================================================================
103 //=======================================================================
104 void XmlLDrivers_DocumentStorageDriver::Write (const Handle(CDM_Document)& theDocument,
105 const TCollection_ExtendedString& theFileName)
107 myFileName = theFileName;
109 std::ofstream aFileStream;
110 OSD_OpenStream (aFileStream, theFileName, std::ios::out);
112 if (aFileStream.is_open() && aFileStream.good())
114 Write (theDocument, aFileStream);
118 SetIsError (Standard_True);
119 SetStoreStatus(PCDM_SS_WriteFailure);
121 TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
122 theFileName + " cannot be opened for writing";
124 theDocument->Application()->MessageDriver()->Write (aMsg.ToExtString());
125 Standard_Failure::Raise("File cannot be opened for writing");
129 //=======================================================================
132 //=======================================================================
133 Standard_EXPORT void XmlLDrivers_DocumentStorageDriver::Write (const Handle(CDM_Document)& theDocument,
134 Standard_OStream& theOStream)
136 Handle(CDM_MessageDriver) aMessageDriver = theDocument->Application()->MessageDriver();
137 ::take_time (~0, " +++++ Start STORAGE procedures ++++++", aMessageDriver);
139 // Create new DOM_Document
140 XmlObjMgt_Document aDOMDoc = XmlObjMgt_Document::createDocument ("document");
142 // Fill the document with data
143 XmlObjMgt_Element anElement = aDOMDoc.getDocumentElement();
145 if (WriteToDomDocument (theDocument, anElement) == Standard_False) {
147 LDOM_XmlWriter aWriter;
148 aWriter.SetIndentation(1);
150 if (theOStream.good())
152 aWriter.Write (theOStream, aDOMDoc);
156 SetIsError (Standard_True);
157 SetStoreStatus(PCDM_SS_WriteFailure);
159 TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the stream is bad and") +
160 " cannot be used for writing";
161 theDocument->Application()->MessageDriver()->Write (aMsg.ToExtString());
163 Standard_Failure::Raise("File cannot be opened for writing");
166 ::take_time (0, " +++++ Fin formatting to XML : ", aMessageDriver);
170 //=======================================================================
171 //function : WriteToDomDocument
172 //purpose : management of the macro-structure of XML document data
173 //remark : If the application needs to use myRelocTable to store additional
174 // data to XML, this method should be reimplemented avoiding step 3
175 //=======================================================================
177 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteToDomDocument (const Handle(CDM_Document)& theDocument,
178 XmlObjMgt_Element& theElement)
180 SetIsError(Standard_False);
181 Handle(CDM_MessageDriver) aMessageDriver =
182 theDocument -> Application() -> MessageDriver();
183 // 1. Write header information
185 XmlObjMgt_Document aDOMDoc = theElement.getOwnerDocument();
188 TCollection_AsciiString aStorageFormat (theDocument->StorageFormat(), '?');
189 theElement.setAttribute ("format", aStorageFormat.ToCString());
190 // theElement.setAttribute ("schema", "XSD");
192 theElement.setAttribute ("xmlns" , "http://www.opencascade.org/OCAF/XML");
193 for (i = 1; i <= mySeqOfNS.Length(); i++) {
194 TCollection_AsciiString aPrefix =
195 TCollection_AsciiString("xmlns:") + mySeqOfNS(i).Prefix().ToCString();
196 theElement.setAttribute (aPrefix.ToCString(),
197 mySeqOfNS(i).URI().ToCString());
199 theElement.setAttribute ("xmlns:xsi",
200 "http://www.w3.org/2001/XMLSchema-instance");
201 //mkv 15.09.05 OCC10001
202 //theElement.setAttribute ("xsi:schemaLocation",
203 // "http://www.opencascade.org/OCAF/XML"
204 // " http://www.nnov.matra-dtv.fr/~agv/XmlOcaf.xsd");
206 // the order of search : by CSF_XmlOcafResource and then by CASROOT
207 TCollection_AsciiString anHTTP = "http://www.opencascade.org/OCAF/XML";
208 Standard_Boolean aToSetCSFVariable = Standard_False;
209 const char * aCSFVariable [2] = {
210 "CSF_XmlOcafResource",
213 TCollection_AsciiString aResourceDir = "";
214 aResourceDir = getenv (aCSFVariable[0]);
215 if (aResourceDir.IsEmpty()) {
216 // now try by CASROOT
217 aResourceDir = getenv (aCSFVariable[1]);
218 if ( !aResourceDir.IsEmpty() ) {
219 aResourceDir += "/src/XmlOcafResource" ;
220 aToSetCSFVariable = Standard_True; //CSF variable to be set later
224 TCollection_ExtendedString aWarn = FAILSTR "Neither ";
225 aWarn = (aWarn + aCSFVariable[0] + ", nor " + aCSFVariable[1]
226 + " variables have been set");
227 aMessageDriver->Write (aWarn.ToExtString());
231 if (!aResourceDir.IsEmpty()) {
232 TCollection_AsciiString aResourceFileName = aResourceDir + "/XmlOcaf.xsd";
233 // search directory name that has been constructed, now check whether
234 // it and the file exist
235 OSD_File aResourceFile ( aResourceFileName );
236 if ( aResourceFile.Exists() ) {
237 if (aToSetCSFVariable) {
238 OSD_Environment aCSFVarEnv ( aCSFVariable[0], aResourceDir );
241 TCollection_ExtendedString aWarn1 = "Variable ";
242 aWarn1 = (aWarn1 + aCSFVariable[0]
243 + " has not been explicitly defined. Set to " + aResourceDir);
244 aMessageDriver->Write (aWarn1.ToExtString());
246 if ( aCSFVarEnv.Failed() ) {
247 TCollection_ExtendedString aWarn = FAILSTR "Failed to initialize ";
248 aWarn = aWarn + aCSFVariable[0] + " with " + aResourceDir;
249 aMessageDriver->Write (aWarn.ToExtString());
255 TCollection_ExtendedString aWarn = FAILSTR "Schema definition file ";
256 aWarn += (aResourceFileName + " was not found");
257 aMessageDriver->Write (aWarn.ToExtString());
260 anHTTP = anHTTP + ' ' + aResourceFileName;
262 theElement.setAttribute ("xsi:schemaLocation", anHTTP.ToCString() );
265 XmlObjMgt_Element anInfoElem = aDOMDoc.createElement("info");
266 theElement.appendChild(anInfoElem);
268 TCollection_AsciiString aCreationDate = XmlLDrivers::CreationDate();
270 // anInfoElem.setAttribute("dbv", 0);
271 anInfoElem.setAttribute("date", aCreationDate.ToCString());
272 anInfoElem.setAttribute("schemav", 0);
273 // anInfoElem.setAttribute("appv", anAppVersion.ToCString());
276 anInfoElem.setAttribute("DocVersion", XmlLDrivers::StorageVersion().ToCString());
278 // User info with Copyright
279 TColStd_SequenceOfAsciiString aUserInfo;
280 if (myCopyright.Length() > 0)
281 aUserInfo.Append (TCollection_AsciiString(myCopyright,'?'));
283 Handle(Storage_Data) theData = new Storage_Data;
284 //PCDM_ReadWriter::WriteFileFormat( theData, theDocument );
285 PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,theDocument);
286 PCDM_ReadWriter::Writer()->WriteReferences(theData,theDocument, myFileName);
287 PCDM_ReadWriter::Writer()->WriteExtensions(theData,theDocument);
288 PCDM_ReadWriter::Writer()->WriteVersion(theData,theDocument);
290 const TColStd_SequenceOfAsciiString& aRefs = theData->UserInfo();
291 for(i = 1; i <= aRefs.Length(); i++)
292 aUserInfo.Append(aRefs.Value(i));
294 for (i = 1; i <= aUserInfo.Length(); i++)
296 XmlObjMgt_Element aUIItem = aDOMDoc.createElement ("iitem");
297 anInfoElem.appendChild (aUIItem);
298 LDOM_Text aUIText = aDOMDoc.createTextNode (aUserInfo(i).ToCString());
299 aUIItem.appendChild (aUIText);
302 // 1.c Comments section
303 TColStd_SequenceOfExtendedString aComments;
304 theDocument->Comments(aComments);
306 XmlObjMgt_Element aCommentsElem = aDOMDoc.createElement ("comments");
307 theElement.appendChild (aCommentsElem);
309 for (i = 1; i <= aComments.Length(); i++)
311 XmlObjMgt_Element aCItem = aDOMDoc.createElement ("citem");
312 aCommentsElem.appendChild (aCItem);
313 XmlObjMgt::SetExtendedString (aCItem, aComments(i));
316 // 2a. Write document contents
317 Standard_Integer anObjNb = 0;
322 anObjNb = MakeDocument(theDocument, theElement);
324 catch (Standard_Failure)
326 SetIsError (Standard_True);
327 SetStoreStatus(PCDM_SS_Failure);
328 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
329 aMessageDriver -> Write (anErrorString.ToExtString());
332 if (anObjNb <= 0 && IsError() == Standard_False) {
333 SetIsError (Standard_True);
334 SetStoreStatus(PCDM_SS_No_Obj);
335 TCollection_ExtendedString anErrorString ("error occurred");
336 aMessageDriver -> Write (anErrorString.ToExtString());
338 // 2b. Write number of objects into the info section
339 anInfoElem.setAttribute("objnb", anObjNb);
340 ::take_time (0, " +++++ Fin DOM data for OCAF : ", aMessageDriver);
342 // 3. Clear relocation table
343 // If the application needs to use myRelocTable to store additional
344 // data to XML, this method should be reimplemented avoiding this step
345 myRelocTable.Clear();
347 // 4. Write Shapes section
348 if(WriteShapeSection(theElement))
349 ::take_time (0, " +++ Fin DOM data for Shapes : ", aMessageDriver);
353 //=======================================================================
354 //function : MakeDocument
356 //=======================================================================
357 Standard_Integer XmlLDrivers_DocumentStorageDriver::MakeDocument
358 (const Handle(CDM_Document)& theTDoc,
359 XmlObjMgt_Element& theElement)
361 TCollection_ExtendedString aMessage;
362 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
363 myRelocTable.Clear();
366 // myRelocTable.SetDocument (theElement.getOwnerDocument());
367 Handle(TDF_Data) aTDF = TDOC->GetData();
369 // Find MessageDriver and pass it to AttributeDrivers()
370 Handle(CDM_Application) anApplication= theTDoc -> Application();
371 Handle(CDM_MessageDriver) aMessageDriver;
372 if (anApplication.IsNull())
373 aMessageDriver = new CDM_NullMessageDriver;
375 aMessageDriver = anApplication -> MessageDriver();
376 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMessageDriver);
378 // Retrieve from DOM_Document
379 XmlMDF::FromTo (aTDF, theElement, myRelocTable, myDrivers);
381 aMessage = "First step successfull";
382 aMessageDriver -> Write (aMessage.ToExtString());
384 return myRelocTable.Extent();
387 cout << "First step failed" << endl; // No MessageDriver available
392 //=======================================================================
393 //function : AttributeDrivers
395 //=======================================================================
396 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentStorageDriver::AttributeDrivers
397 (const Handle(CDM_MessageDriver)& theMessageDriver)
399 return XmlLDrivers::AttributeDrivers (theMessageDriver);
402 //=======================================================================
403 //function : take_time
405 //purpose : output astronomical time elapsed
406 //=======================================================================
409 #include <sys/timeb.h>
410 #include <sys/types.h>
413 extern "C" int ftime (struct timeb *tp);
417 static void take_time (const Standard_Integer isReset, const char * aHeader,
418 const Handle(CDM_MessageDriver)& aMessageDriver)
422 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
423 if (isReset) tmbuf0 = tmbuf;
425 char take_tm_buf [64];
426 Sprintf (take_tm_buf, "%9.2f s ++++",
427 double(tmbuf.time - tmbuf0.time) +
428 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
429 aMessage += take_tm_buf;
431 aMessageDriver -> Write (aMessage.ToExtString());
435 //=======================================================================
436 //function : WriteShapeSection
437 //purpose : defines WriteShapeSection
438 //=======================================================================
439 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteShapeSection
440 (XmlObjMgt_Element& /*theElement*/)
442 // empty; should be redefined in subclasses
443 return Standard_False;