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.
16 #include <XmlLDrivers_DocumentStorageDriver.ixx>
18 #include <XmlLDrivers.hxx>
19 #include <XmlLDrivers_NamespaceDef.hxx>
21 #include <XmlMDF_ADriverTable.hxx>
23 #include <XmlObjMgt.hxx>
24 #include <XmlObjMgt_SRelocationTable.hxx>
26 #include <LDOM_XmlWriter.hxx>
27 #include <LDOM_LDOMImplementation.hxx>
28 #include <LDOM_DocumentType.hxx>
29 #include <XmlObjMgt_Document.hxx>
31 #include <Storage_Data.hxx>
33 #include <PCDM_ReadWriter.hxx>
34 #include <CDM_NullMessageDriver.hxx>
35 #include <CDM_Document.hxx>
36 #include <CDM_Application.hxx>
37 #include <TDocStd_Document.hxx>
38 #include <TColStd_SequenceOfAsciiString.hxx>
41 #include <Standard_Failure.hxx>
42 #include <Standard_ErrorHandler.hxx>
44 #include <OSD_File.hxx>
45 #include <OSD_Environment.hxx>
47 #define STORAGE_VERSION "STORAGE_VERSION: "
48 #define REFERENCE_COUNTER "REFERENCE_COUNTER: "
49 #define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
50 #define START_REF "START_REF"
51 #define END_REF "END_REF"
53 #define FAILSTR "Failed to write xsi:schemaLocation : "
56 static void take_time (const Standard_Integer, const char *,
57 const Handle(CDM_MessageDriver)&)
64 //=======================================================================
65 //function : XmlLDrivers_DocumentStorageDriver
66 //purpose : Constructor
67 //=======================================================================
68 XmlLDrivers_DocumentStorageDriver::XmlLDrivers_DocumentStorageDriver
69 (const TCollection_ExtendedString& theCopyright)
70 : myCopyright (theCopyright)
74 //=======================================================================
75 //function : SchemaName
77 //=======================================================================
78 TCollection_ExtendedString XmlLDrivers_DocumentStorageDriver::SchemaName() const
80 TCollection_ExtendedString schemaname;
84 //=======================================================================
85 //function : AddNamespace
87 //=======================================================================
89 void XmlLDrivers_DocumentStorageDriver::AddNamespace
90 (const TCollection_AsciiString& thePrefix,
91 const TCollection_AsciiString& theURI)
93 for (Standard_Integer i = 1; i <= mySeqOfNS.Length(); i++)
94 if (thePrefix == mySeqOfNS(i).Prefix()) return;
95 mySeqOfNS.Append (XmlLDrivers_NamespaceDef(thePrefix, theURI));
98 //=======================================================================
101 //=======================================================================
102 void XmlLDrivers_DocumentStorageDriver::Write
103 (const Handle(CDM_Document)& theDocument,
104 const TCollection_ExtendedString& theFileName)
106 Handle(CDM_MessageDriver) aMessageDriver =
107 theDocument -> Application() -> MessageDriver();
108 ::take_time (~0, " +++++ Start STORAGE procedures ++++++", aMessageDriver);
110 // Create new DOM_Document
111 XmlObjMgt_Document aDOMDoc = XmlObjMgt_Document::createDocument ("document");
113 // Fill the document with data
114 XmlObjMgt_Element anElement = aDOMDoc.getDocumentElement();
116 if (WriteToDomDocument (theDocument, anElement, theFileName) == Standard_False) {
117 // Write DOM_Document into XML file,
118 TCollection_AsciiString aFileName (theFileName, '?');
119 FILE * aFile = fopen(aFileName.ToCString(), "wt");
122 LDOM_XmlWriter aWriter (aFile);
123 aWriter.SetIndentation(1);
126 ::take_time (0, " +++++ Fin formatting to XML : ", aMessageDriver);
129 SetIsError (Standard_True);
130 TCollection_ExtendedString aMsg =
131 TCollection_ExtendedString("Error: the file ") + aFileName +
132 " cannot be opened for writing";
133 aMessageDriver -> Write (aMsg.ToExtString());
134 Standard_Failure::Raise("File cannot be opened for writing");
139 //=======================================================================
140 //function : WriteToDomDocument
141 //purpose : management of the macro-structure of XML document data
142 //remark : If the application needs to use myRelocTable to store additional
143 // data to XML, this method should be reimplemented avoiding step 3
144 //=======================================================================
146 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteToDomDocument
147 (const Handle(CDM_Document)& theDocument,
148 XmlObjMgt_Element& theElement,
149 const TCollection_ExtendedString& theFileName)
151 SetIsError(Standard_False);
152 Handle(CDM_MessageDriver) aMessageDriver =
153 theDocument -> Application() -> MessageDriver();
154 // 1. Write header information
156 XmlObjMgt_Document aDOMDoc = theElement.getOwnerDocument();
159 TCollection_AsciiString aStorageFormat (theDocument->StorageFormat(), '?');
160 theElement.setAttribute ("format", aStorageFormat.ToCString());
161 // theElement.setAttribute ("schema", "XSD");
163 theElement.setAttribute ("xmlns" , "http://www.opencascade.org/OCAF/XML");
164 for (i = 1; i <= mySeqOfNS.Length(); i++) {
165 TCollection_AsciiString aPrefix =
166 TCollection_AsciiString("xmlns:") + mySeqOfNS(i).Prefix().ToCString();
167 theElement.setAttribute (aPrefix.ToCString(),
168 mySeqOfNS(i).URI().ToCString());
170 theElement.setAttribute ("xmlns:xsi",
171 "http://www.w3.org/2001/XMLSchema-instance");
172 //mkv 15.09.05 OCC10001
173 //theElement.setAttribute ("xsi:schemaLocation",
174 // "http://www.opencascade.org/OCAF/XML"
175 // " http://www.nnov.matra-dtv.fr/~agv/XmlOcaf.xsd");
177 // the order of search : by CSF_XmlOcafResource and then by CASROOT
178 TCollection_AsciiString anHTTP = "http://www.opencascade.org/OCAF/XML";
179 Standard_Boolean aToSetCSFVariable = Standard_False;
180 const char * aCSFVariable [2] = {
181 "CSF_XmlOcafResource",
184 TCollection_AsciiString aResourceDir = "";
185 aResourceDir = getenv (aCSFVariable[0]);
186 if (aResourceDir.IsEmpty()) {
187 // now try by CASROOT
188 aResourceDir = getenv (aCSFVariable[1]);
189 if ( !aResourceDir.IsEmpty() ) {
190 aResourceDir += "/src/XmlOcafResource" ;
191 aToSetCSFVariable = Standard_True; //CSF variable to be set later
195 TCollection_ExtendedString aWarn = FAILSTR "Neither ";
196 aWarn = (aWarn + aCSFVariable[0] + ", nor " + aCSFVariable[1]
197 + " variables have been set");
198 aMessageDriver->Write (aWarn.ToExtString());
202 if (!aResourceDir.IsEmpty()) {
203 TCollection_AsciiString aResourceFileName = aResourceDir + "/XmlOcaf.xsd";
204 // search directory name that has been constructed, now check whether
205 // it and the file exist
206 OSD_File aResourceFile ( aResourceFileName );
207 if ( aResourceFile.Exists() ) {
208 if (aToSetCSFVariable) {
209 OSD_Environment aCSFVarEnv ( aCSFVariable[0], aResourceDir );
212 TCollection_ExtendedString aWarn1 = "Variable ";
213 aWarn1 = (aWarn1 + aCSFVariable[0]
214 + " has not been explicitly defined. Set to " + aResourceDir);
215 aMessageDriver->Write (aWarn1.ToExtString());
217 if ( aCSFVarEnv.Failed() ) {
218 TCollection_ExtendedString aWarn = FAILSTR "Failed to initialize ";
219 aWarn = aWarn + aCSFVariable[0] + " with " + aResourceDir;
220 aMessageDriver->Write (aWarn.ToExtString());
226 TCollection_ExtendedString aWarn = FAILSTR "Schema definition file ";
227 aWarn += (aResourceFileName + " was not found");
228 aMessageDriver->Write (aWarn.ToExtString());
231 anHTTP = anHTTP + ' ' + aResourceFileName;
233 theElement.setAttribute ("xsi:schemaLocation", anHTTP.ToCString() );
236 XmlObjMgt_Element anInfoElem = aDOMDoc.createElement("info");
237 theElement.appendChild(anInfoElem);
239 TCollection_AsciiString aCreationDate = XmlLDrivers::CreationDate();
241 // anInfoElem.setAttribute("dbv", 0);
242 anInfoElem.setAttribute("date", aCreationDate.ToCString());
243 anInfoElem.setAttribute("schemav", 0);
244 // anInfoElem.setAttribute("appv", anAppVersion.ToCString());
247 anInfoElem.setAttribute("DocVersion", XmlLDrivers::StorageVersion().ToCString());
249 // User info with Copyright
250 TColStd_SequenceOfAsciiString aUserInfo;
251 if (myCopyright.Length() > 0)
252 aUserInfo.Append (TCollection_AsciiString(myCopyright,'?'));
254 Handle(Storage_Data) theData = new Storage_Data;
255 //PCDM_ReadWriter::WriteFileFormat( theData, theDocument );
256 PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,theDocument);
257 PCDM_ReadWriter::Writer()->WriteReferences(theData,theDocument,theFileName);
258 PCDM_ReadWriter::Writer()->WriteExtensions(theData,theDocument);
259 PCDM_ReadWriter::Writer()->WriteVersion(theData,theDocument);
261 const TColStd_SequenceOfAsciiString& aRefs = theData->UserInfo();
262 for(i = 1; i <= aRefs.Length(); i++)
263 aUserInfo.Append(aRefs.Value(i));
265 for (i = 1; i <= aUserInfo.Length(); i++)
267 XmlObjMgt_Element aUIItem = aDOMDoc.createElement ("iitem");
268 anInfoElem.appendChild (aUIItem);
269 LDOM_Text aUIText = aDOMDoc.createTextNode (aUserInfo(i).ToCString());
270 aUIItem.appendChild (aUIText);
273 // 1.c Comments section
274 TColStd_SequenceOfExtendedString aComments;
275 theDocument->Comments(aComments);
277 XmlObjMgt_Element aCommentsElem = aDOMDoc.createElement ("comments");
278 theElement.appendChild (aCommentsElem);
280 for (i = 1; i <= aComments.Length(); i++)
282 XmlObjMgt_Element aCItem = aDOMDoc.createElement ("citem");
283 aCommentsElem.appendChild (aCItem);
284 XmlObjMgt::SetExtendedString (aCItem, aComments(i));
287 // 2a. Write document contents
288 Standard_Integer anObjNb = 0;
293 anObjNb = MakeDocument(theDocument, theElement);
295 catch (Standard_Failure)
297 SetIsError (Standard_True);
298 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
299 aMessageDriver -> Write (anErrorString.ToExtString());
302 if (anObjNb <= 0 && IsError() == Standard_False) {
303 SetIsError (Standard_True);
304 TCollection_ExtendedString anErrorString ("error occurred");
305 aMessageDriver -> Write (anErrorString.ToExtString());
307 // 2b. Write number of objects into the info section
308 anInfoElem.setAttribute("objnb", anObjNb);
309 ::take_time (0, " +++++ Fin DOM data for OCAF : ", aMessageDriver);
311 // 3. Clear relocation table
312 // If the application needs to use myRelocTable to store additional
313 // data to XML, this method should be reimplemented avoiding this step
314 myRelocTable.Clear();
316 // 4. Write Shapes section
317 if(WriteShapeSection(theElement))
318 ::take_time (0, " +++ Fin DOM data for Shapes : ", aMessageDriver);
322 //=======================================================================
323 //function : MakeDocument
325 //=======================================================================
326 Standard_Integer XmlLDrivers_DocumentStorageDriver::MakeDocument
327 (const Handle(CDM_Document)& theTDoc,
328 XmlObjMgt_Element& theElement)
330 TCollection_ExtendedString aMessage;
331 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
332 myRelocTable.Clear();
335 // myRelocTable.SetDocument (theElement.getOwnerDocument());
336 Handle(TDF_Data) aTDF = TDOC->GetData();
338 // Find MessageDriver and pass it to AttributeDrivers()
339 Handle(CDM_Application) anApplication= theTDoc -> Application();
340 Handle(CDM_MessageDriver) aMessageDriver;
341 if (anApplication.IsNull())
342 aMessageDriver = new CDM_NullMessageDriver;
344 aMessageDriver = anApplication -> MessageDriver();
345 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMessageDriver);
347 // Retrieve from DOM_Document
348 XmlMDF::FromTo (aTDF, theElement, myRelocTable, myDrivers);
349 #if defined(DEB) && !defined(TAKE_TIMES)
350 aMessage = "First step successfull";
351 aMessageDriver -> Write (aMessage.ToExtString());
353 return myRelocTable.Extent();
356 cout << "First step failed" << endl; // No MessageDriver available
361 //=======================================================================
362 //function : AttributeDrivers
364 //=======================================================================
365 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentStorageDriver::AttributeDrivers
366 (const Handle(CDM_MessageDriver)& theMessageDriver)
368 return XmlLDrivers::AttributeDrivers (theMessageDriver);
371 //=======================================================================
372 //function : take_time
374 //purpose : output astronomical time elapsed
375 //=======================================================================
378 #include <sys/timeb.h>
379 #include <sys/types.h>
382 extern "C" int ftime (struct timeb *tp);
386 static void take_time (const Standard_Integer isReset, const char * aHeader,
387 const Handle(CDM_MessageDriver)& aMessageDriver)
391 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
392 if (isReset) tmbuf0 = tmbuf;
394 char take_tm_buf [64];
395 Sprintf (take_tm_buf, "%9.2f s ++++",
396 double(tmbuf.time - tmbuf0.time) +
397 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
398 aMessage += take_tm_buf;
400 aMessageDriver -> Write (aMessage.ToExtString());
404 //=======================================================================
405 //function : WriteShapeSection
406 //purpose : defines WriteShapeSection
407 //=======================================================================
408 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteShapeSection
409 (XmlObjMgt_Element& /*theElement*/)
411 // empty; should be redefined in subclasses
412 return Standard_False;