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 <Message_Messenger.hxx>
20 #include <Message_ProgressScope.hxx>
21 #include <CDM_MetaData.hxx>
22 #include <LDOM_DocumentType.hxx>
23 #include <LDOM_LDOMImplementation.hxx>
24 #include <LDOMParser.hxx>
25 #include <OSD_Path.hxx>
26 #include <OSD_OpenFile.hxx>
27 #include <PCDM_Document.hxx>
28 #include <PCDM_DOMHeaderParser.hxx>
29 #include <Standard_Type.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TCollection_ExtendedString.hxx>
32 #include <TDF_Data.hxx>
33 #include <TDocStd_Document.hxx>
34 #include <TDocStd_Owner.hxx>
36 #include <XmlLDrivers.hxx>
37 #include <XmlLDrivers_DocumentRetrievalDriver.hxx>
39 #include <XmlMDF_ADriver.hxx>
40 #include <XmlMDF_ADriverTable.hxx>
41 #include <XmlObjMgt.hxx>
42 #include <XmlObjMgt_Document.hxx>
43 #include <XmlObjMgt_RRelocationTable.hxx>
45 IMPLEMENT_STANDARD_RTTIEXT(XmlLDrivers_DocumentRetrievalDriver,PCDM_RetrievalDriver)
52 #include <Standard_Failure.hxx>
53 #include <Standard_ErrorHandler.hxx>
55 #define START_REF "START_REF"
56 #define END_REF "END_REF"
58 #define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
59 #define REFERENCE_COUNTER "REFERENCE_COUNTER: "
62 static void take_time (const Standard_Integer, const char *,
63 const Handle(Message_Messenger)&)
70 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
72 Standard_Integer i, j, len ;
74 len = aString.Length() ;
76 // Case of network path, such as \\MACHINE\dir
77 for (i = j = 2 ; j <= len ; i++,j++) {
79 for (i = j = 1 ; j <= len ; i++,j++) {
81 Standard_Character c = aString.Value(j) ;
82 aString.SetValue(i,c) ;
84 while(j < len && aString.Value(j+1) == '/') j++ ;
87 if (aString.Value(len) == '/') len-- ;
91 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
92 TCollection_AsciiString theCFile=UTL::CString(aFileName);
93 TCollection_AsciiString theDirectory;
94 Standard_Integer i=theCFile.SearchFromEnd("/");
96 // if(i==-1) i=theCFile.SearchFromEnd("\\");
97 if(theCFile.SearchFromEnd("\\") > i)
98 i=theCFile.SearchFromEnd("\\");
100 if(i!=-1) theDirectory=theCFile.SubString(1,i);
104 static TCollection_AsciiString AbsolutePath(
105 const TCollection_AsciiString& aDirPath,
106 const TCollection_AsciiString& aRelFilePath)
108 TCollection_AsciiString EmptyString = "" ;
110 if (aRelFilePath.Search(":") == 2 ||
111 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
113 if(aRelFilePath.Search("/") == 1)
115 return aRelFilePath ;
117 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
118 Standard_Integer i,len ;
121 if(DirPath.Search(":") != 2 &&
122 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
124 if (DirPath.Search("/") != 1 )
129 DirPath.ChangeAll('\\','/') ;
130 RelFilePath.ChangeAll('\\','/') ;
133 RemoveExtraSeparator(DirPath) ;
134 len = RemoveExtraSeparator(RelFilePath) ;
136 while (RelFilePath.Search("../") == 1) {
139 RelFilePath = RelFilePath.SubString(4,len) ;
141 if (DirPath.IsEmpty())
143 i = DirPath.SearchFromEnd("/") ;
148 TCollection_AsciiString retx;
155 //=======================================================================
156 //function : XmlLDrivers_DocumentRetrievalDriver
157 //purpose : Constructor
158 //=======================================================================
159 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
161 myReaderStatus = PCDM_RS_OK;
164 //=======================================================================
167 //=======================================================================
168 void XmlLDrivers_DocumentRetrievalDriver::Read
169 (const TCollection_ExtendedString& theFileName,
170 const Handle(CDM_Document)& theNewDocument,
171 const Handle(CDM_Application)& theApplication,
172 const Message_ProgressRange& theRange)
174 myReaderStatus = PCDM_RS_DriverFailure;
175 myFileName = theFileName;
177 std::ifstream aFileStream;
178 OSD_OpenStream (aFileStream, myFileName, std::ios::in);
180 if (aFileStream.is_open() && aFileStream.good())
182 Read (aFileStream, NULL, theNewDocument, theApplication, theRange);
186 myReaderStatus = PCDM_RS_OpenError;
188 TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
189 theFileName + " cannot be opened for reading";
191 theApplication->MessageDriver()->Send (aMsg.ToExtString(), Message_Fail);
192 throw Standard_Failure("File cannot be opened for reading");
196 //=======================================================================
199 //=======================================================================
200 void XmlLDrivers_DocumentRetrievalDriver::Read (Standard_IStream& theIStream,
201 const Handle(Storage_Data)& /*theStorageData*/,
202 const Handle(CDM_Document)& theNewDocument,
203 const Handle(CDM_Application)& theApplication,
204 const Message_ProgressRange& theRange)
206 Handle(Message_Messenger) aMessageDriver = theApplication -> MessageDriver();
207 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
209 // 1. Read DOM_Document from file
212 // if myFileName is not empty, "document" tag is required to be read
213 // from the received document
214 Standard_Boolean aWithoutRoot = myFileName.IsEmpty();
216 if (aParser.parse(theIStream, Standard_False, aWithoutRoot))
218 TCollection_AsciiString aData;
219 std::cout << aParser.GetError(aData) << ": " << aData << std::endl;
220 myReaderStatus = PCDM_RS_FormatFailure;
223 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
224 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
226 ReadFromDomDocument (anElement, theNewDocument, theApplication, theRange);
229 //=======================================================================
230 //function : ReadFromDomDocument
231 //purpose : management of the macro-structure of XML document data
232 //remark : If the application needs to use myRelocTable to retrieve additional
233 // data from LDOM, this method should be reimplemented
234 //=======================================================================
236 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
237 (const XmlObjMgt_Element& theElement,
238 const Handle(CDM_Document)& theNewDocument,
239 const Handle(CDM_Application)& theApplication,
240 const Message_ProgressRange& theRange)
242 const Handle(Message_Messenger) aMsgDriver =
243 theApplication -> MessageDriver();
244 // 1. Read info // to be done
245 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
246 Standard_Integer aCurDocVersion = TDocStd_FormatVersion_VERSION_2; // minimum supported version
247 TCollection_ExtendedString anInfo;
248 const XmlObjMgt_Element anInfoElem =
249 theElement.GetChildByTagName ("info");
250 if (anInfoElem != NULL) {
251 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
252 if (aDocVerStr != NULL)
254 Standard_Integer anIntegerVersion = 0;
255 if (aDocVerStr.GetInteger (anIntegerVersion))
257 aCurDocVersion = anIntegerVersion;
261 TCollection_ExtendedString aMsg =
262 TCollection_ExtendedString ("Cannot retrieve the current Document version"
263 " attribute as \"") + aDocVerStr + "\"";
264 if (!aMsgDriver.IsNull())
266 aMsgDriver->Send(aMsg.ToExtString(), Message_Fail);
271 // oan: OCC22305 - check a document version and if it's greater than
272 // current version of storage driver set an error status and return
273 if( aCurDocVersion > TDocStd_Document::CurrentStorageFormatVersion() )
275 TCollection_ExtendedString aMsg =
276 TCollection_ExtendedString ("error: wrong file version: ") +
277 aDocVerStr + " while current is " +
278 TDocStd_Document::CurrentStorageFormatVersion();
279 myReaderStatus = PCDM_RS_NoVersion;
280 if(!aMsgDriver.IsNull())
281 aMsgDriver->Send(aMsg.ToExtString(), Message_Fail);
285 Standard_Boolean isRef = Standard_False;
286 for (LDOM_Node aNode = anInfoElem.getFirstChild();
287 aNode != NULL; aNode = aNode.getNextSibling()) {
288 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
289 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
292 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
295 TCollection_AsciiString anInf(anInfo,'?');
296 Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
297 theNewDocument->SetReferenceCounter(aRefCounter);
299 catch (Standard_Failure const&) {
300 // std::cout << "warning: could not read the reference counter in " << aFileName << std::endl;
301 TCollection_ExtendedString aMsg("Warning: ");
302 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
303 if(!aMsgDriver.IsNull())
304 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
307 else if (anInfo.Search(MODIFICATION_COUNTER) != -1) {
311 TCollection_AsciiString anInf(anInfo,'?');
312 Standard_Integer aModCounter = anInf.Token(" ",2).IntegerValue();
313 theNewDocument->SetModifications (aModCounter);
315 catch (Standard_Failure const&) {
316 TCollection_ExtendedString aMsg("Warning: could not read the modification counter\0");
317 if(!aMsgDriver.IsNull())
318 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
322 if(anInfo == END_REF)
323 isRef = Standard_False;
324 if(isRef) { // Process References
326 Standard_Integer pos=anInfo.Search(" ");
328 // Parce RefId, DocumentVersion and FileName
329 Standard_Integer aRefId;
330 TCollection_ExtendedString aFileName;
331 Standard_Integer aDocumentVersion;
334 TCollection_ExtendedString aRest=anInfo.Split(pos);
335 aRefId = UTL::IntegerValue(anInfo);
337 Standard_Integer pos2 = aRest.Search(" ");
339 aFileName = aRest.Split(pos2);
340 aDocumentVersion = UTL::IntegerValue(aRest);
342 TCollection_AsciiString aPath = UTL::CString(aFileName);
343 TCollection_AsciiString anAbsolutePath;
344 if(!anAbsoluteDirectory.IsEmpty()) {
345 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
346 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
348 if(!aMsgDriver.IsNull()) {
349 // std::cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
350 TCollection_ExtendedString aMsg("Warning: ");
351 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
352 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
356 TCollection_ExtendedString theFolder,theName;
357 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
358 TCollection_ExtendedString f(aPath);
361 Standard_Integer i= f.SearchFromEnd("/");
362 TCollection_ExtendedString n = f.Split(i);
363 f.Trunc(f.Length()-1);
367 OSD_Path p = UTL::Path(f);
368 Standard_ExtCharacter chr;
369 TCollection_ExtendedString dir, dirRet, name;
374 for ( int i = 1; i <= dir.Length (); ++i ) {
376 chr = dir.Value ( i );
395 theName = UTL::Name(p); theName+= UTL::Extension(p);
398 Handle(CDM_MetaData) aMetaData =
399 CDM_MetaData::LookUp(theApplication->MetaDataLookUpTable(), theFolder, theName, aPath, aPath, UTL::IsReadOnly(aFileName));
401 theNewDocument->CreateReference(aMetaData,aRefId,
402 theApplication,aDocumentVersion,Standard_False);
409 if(anInfo == START_REF)
410 isRef = Standard_True;
417 TCollection_ExtendedString aComment;
418 const XmlObjMgt_Element aCommentsElem =
419 theElement.GetChildByTagName ("comments");
420 if (aCommentsElem != NULL)
422 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
423 aNode != NULL; aNode = aNode.getNextSibling())
425 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
427 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
429 theNewDocument->AddComment(aComment);
434 Message_ProgressScope aPS(theRange, "Reading document", 2);
435 // 2. Read Shapes section
436 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
437 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver, aPS.Next());
438 if(!aNSDriver.IsNull())
439 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
443 myReaderStatus = PCDM_RS_UserBreak;
447 // 2.1. Keep document format version in RT
448 Handle(Storage_HeaderData) aHeaderData = new Storage_HeaderData();
449 aHeaderData->SetStorageVersion(aCurDocVersion);
450 myRelocTable.Clear();
451 myRelocTable.SetHeaderData(aHeaderData);
453 // 5. Read document contents
458 TCollection_ExtendedString aMessage ("PasteDocument");
459 aMsgDriver ->Send (aMessage.ToExtString(), Message_Trace);
461 if (!MakeDocument(theElement, theNewDocument, aPS.Next()))
462 myReaderStatus = PCDM_RS_MakeFailure;
464 myReaderStatus = PCDM_RS_OK;
466 catch (Standard_Failure const& anException)
468 TCollection_ExtendedString anErrorString (anException.GetMessageString());
469 aMsgDriver ->Send (anErrorString.ToExtString(), Message_Fail);
473 myReaderStatus = PCDM_RS_UserBreak;
477 // Wipe off the shapes written to the <shapes> section
478 ShapeSetCleaning(aNSDriver);
480 // Clean the relocation table.
481 // If the application needs to use myRelocTable to retrieve additional
482 // data from LDOM, this method should be reimplemented avoiding this step
483 myRelocTable.Clear();
484 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
487 //=======================================================================
488 //function : MakeDocument
490 //=======================================================================
491 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
492 (const XmlObjMgt_Element& theElement,
493 const Handle(CDM_Document)& theTDoc,
494 const Message_ProgressRange& theRange)
496 Standard_Boolean aResult = Standard_False;
497 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
500 Handle(TDF_Data) aTDF = new TDF_Data();
501 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers, theRange);
503 TDOC->SetData (aTDF);
504 TDocStd_Owner::SetDocument (aTDF, TDOC);
510 //=======================================================================
511 //function : AttributeDrivers
513 //=======================================================================
514 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
515 (const Handle(Message_Messenger)& theMessageDriver)
517 return XmlLDrivers::AttributeDrivers (theMessageDriver);
520 //=======================================================================
521 //function : take_time
523 //purpose : output astronomical time elapsed
524 //=======================================================================
527 #include <sys/timeb.h>
528 #include <sys/types.h>
531 extern "C" int ftime (struct timeb *tp);
533 extern struct timeb tmbuf0;
535 static void take_time (const Standard_Integer isReset, const char * aHeader,
536 const Handle(Message_Messenger)& aMessageDriver)
540 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
541 if (isReset) tmbuf0 = tmbuf;
543 char take_tm_buf [64];
544 Sprintf (take_tm_buf, "%9.2f s ++++",
545 double(tmbuf.time - tmbuf0.time) +
546 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
547 aMessage += take_tm_buf;
549 aMessageDriver -> Write (aMessage.ToExtString());
553 //=======================================================================
554 //function : ReadShapeSection
555 //purpose : definition of ReadShapeSection
556 //=======================================================================
557 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
558 const XmlObjMgt_Element& /*theElement*/,
559 const Handle(Message_Messenger)& /*aMsgDriver*/,
560 const Message_ProgressRange& /*theRange*/)
562 Handle(XmlMDF_ADriver) aDriver;
563 //empty; to be redefined
567 //=======================================================================
568 //function : ShapeSetCleaning
569 //purpose : definition of ShapeSetCleaning
570 //=======================================================================
571 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
572 const Handle(XmlMDF_ADriver)& /*theDriver*/)