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 <CDM_MetaData.hxx>
21 #include <LDOM_DocumentType.hxx>
22 #include <LDOM_LDOMImplementation.hxx>
23 #include <LDOMParser.hxx>
24 #include <OSD_Path.hxx>
25 #include <OSD_OpenFile.hxx>
26 #include <PCDM_Document.hxx>
27 #include <PCDM_DOMHeaderParser.hxx>
28 #include <Standard_Type.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDocStd_Document.hxx>
33 #include <TDocStd_Owner.hxx>
35 #include <XmlLDrivers.hxx>
36 #include <XmlLDrivers_DocumentRetrievalDriver.hxx>
38 #include <XmlMDF_ADriver.hxx>
39 #include <XmlMDF_ADriverTable.hxx>
40 #include <XmlObjMgt.hxx>
41 #include <XmlObjMgt_Document.hxx>
42 #include <XmlObjMgt_RRelocationTable.hxx>
44 IMPLEMENT_STANDARD_RTTIEXT(XmlLDrivers_DocumentRetrievalDriver,PCDM_RetrievalDriver)
51 #include <Standard_Failure.hxx>
52 #include <Standard_ErrorHandler.hxx>
54 #define START_REF "START_REF"
55 #define END_REF "END_REF"
57 #define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
58 #define REFERENCE_COUNTER "REFERENCE_COUNTER: "
61 static void take_time (const Standard_Integer, const char *,
62 const Handle(Message_Messenger)&)
69 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
71 Standard_Integer i, j, len ;
73 len = aString.Length() ;
75 // Case of network path, such as \\MACHINE\dir
76 for (i = j = 2 ; j <= len ; i++,j++) {
78 for (i = j = 1 ; j <= len ; i++,j++) {
80 Standard_Character c = aString.Value(j) ;
81 aString.SetValue(i,c) ;
83 while(j < len && aString.Value(j+1) == '/') j++ ;
86 if (aString.Value(len) == '/') len-- ;
90 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
91 TCollection_AsciiString theCFile=UTL::CString(aFileName);
92 TCollection_AsciiString theDirectory;
93 Standard_Integer i=theCFile.SearchFromEnd("/");
95 // if(i==-1) i=theCFile.SearchFromEnd("\\");
96 if(theCFile.SearchFromEnd("\\") > i)
97 i=theCFile.SearchFromEnd("\\");
99 if(i!=-1) theDirectory=theCFile.SubString(1,i);
103 static TCollection_AsciiString AbsolutePath(
104 const TCollection_AsciiString& aDirPath,
105 const TCollection_AsciiString& aRelFilePath)
107 TCollection_AsciiString EmptyString = "" ;
109 if (aRelFilePath.Search(":") == 2 ||
110 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
112 if(aRelFilePath.Search("/") == 1)
114 return aRelFilePath ;
116 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
117 Standard_Integer i,len ;
120 if(DirPath.Search(":") != 2 &&
121 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
123 if (DirPath.Search("/") != 1 )
128 DirPath.ChangeAll('\\','/') ;
129 RelFilePath.ChangeAll('\\','/') ;
132 RemoveExtraSeparator(DirPath) ;
133 len = RemoveExtraSeparator(RelFilePath) ;
135 while (RelFilePath.Search("../") == 1) {
138 RelFilePath = RelFilePath.SubString(4,len) ;
140 if (DirPath.IsEmpty())
142 i = DirPath.SearchFromEnd("/") ;
147 TCollection_AsciiString retx;
154 //=======================================================================
155 //function : XmlLDrivers_DocumentRetrievalDriver
156 //purpose : Constructor
157 //=======================================================================
158 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
160 myReaderStatus = PCDM_RS_OK;
163 //=======================================================================
164 //function : CreateDocument
165 //purpose : pure virtual method definition
166 //=======================================================================
167 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
169 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
172 //=======================================================================
175 //=======================================================================
176 void XmlLDrivers_DocumentRetrievalDriver::Read
177 (const TCollection_ExtendedString& theFileName,
178 const Handle(CDM_Document)& theNewDocument,
179 const Handle(CDM_Application)& theApplication)
181 myReaderStatus = PCDM_RS_DriverFailure;
182 myFileName = theFileName;
184 std::ifstream aFileStream;
185 OSD_OpenStream (aFileStream, myFileName, std::ios::in);
187 if (aFileStream.is_open() && aFileStream.good())
189 Read (aFileStream, NULL, theNewDocument, theApplication);
193 myReaderStatus = PCDM_RS_OpenError;
195 TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
196 theFileName + " cannot be opened for reading";
198 theApplication->MessageDriver()->Send (aMsg.ToExtString(), Message_Fail);
199 throw Standard_Failure("File cannot be opened for reading");
203 //=======================================================================
206 //=======================================================================
207 void XmlLDrivers_DocumentRetrievalDriver::Read (Standard_IStream& theIStream,
208 const Handle(Storage_Data)& /*theStorageData*/,
209 const Handle(CDM_Document)& theNewDocument,
210 const Handle(CDM_Application)& theApplication)
212 Handle(Message_Messenger) aMessageDriver = theApplication -> MessageDriver();
213 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
215 // 1. Read DOM_Document from file
218 // if myFileName is not empty, "document" tag is required to be read
219 // from the received document
220 Standard_Boolean aWithoutRoot = myFileName.IsEmpty();
222 if (aParser.parse(theIStream, Standard_False, aWithoutRoot))
224 TCollection_AsciiString aData;
225 cout << aParser.GetError(aData) << ": " << aData << endl;
226 myReaderStatus = PCDM_RS_FormatFailure;
229 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
230 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
232 ReadFromDomDocument (anElement, theNewDocument, theApplication);
235 //=======================================================================
236 //function : ReadFromDomDocument
237 //purpose : management of the macro-structure of XML document data
238 //remark : If the application needs to use myRelocTable to retrieve additional
239 // data from LDOM, this method should be reimplemented
240 //=======================================================================
242 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
243 (const XmlObjMgt_Element& theElement,
244 const Handle(CDM_Document)& theNewDocument,
245 const Handle(CDM_Application)& theApplication)
247 const Handle(Message_Messenger) aMsgDriver =
248 theApplication -> MessageDriver();
249 // 1. Read info // to be done
250 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
251 Standard_Integer aCurDocVersion = 0;
252 TCollection_ExtendedString anInfo;
253 const XmlObjMgt_Element anInfoElem =
254 theElement.GetChildByTagName ("info");
255 if (anInfoElem != NULL) {
256 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
257 if(aDocVerStr == NULL)
259 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
260 TCollection_ExtendedString aMsg =
261 TCollection_ExtendedString ("Cannot retrieve the current Document version"
262 " attribute as \"") + aDocVerStr + "\"";
263 if(!aMsgDriver.IsNull())
264 aMsgDriver->Send(aMsg.ToExtString(), Message_Fail);
267 // oan: OCC22305 - check a document verison and if it's greater than
268 // current version of storage driver set an error status and return
269 if( aCurDocVersion > XmlLDrivers::StorageVersion() )
271 TCollection_ExtendedString aMsg =
272 TCollection_ExtendedString ("error: wrong file version: ") +
273 aDocVerStr + " while current is " +
274 XmlLDrivers::StorageVersion();
275 myReaderStatus = PCDM_RS_NoVersion;
276 if(!aMsgDriver.IsNull())
277 aMsgDriver->Send(aMsg.ToExtString(), Message_Fail);
281 if( aCurDocVersion < 2) aCurDocVersion = 2;
282 Standard_Boolean isRef = Standard_False;
283 for (LDOM_Node aNode = anInfoElem.getFirstChild();
284 aNode != NULL; aNode = aNode.getNextSibling()) {
285 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
286 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
289 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
292 TCollection_AsciiString anInf(anInfo,'?');
293 Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
294 theNewDocument->SetReferenceCounter(aRefCounter);
296 catch (Standard_Failure) {
297 // cout << "warning: could not read the reference counter in " << aFileName << endl;
298 TCollection_ExtendedString aMsg("Warning: ");
299 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
300 if(!aMsgDriver.IsNull())
301 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
304 else if (anInfo.Search(MODIFICATION_COUNTER) != -1) {
308 TCollection_AsciiString anInf(anInfo,'?');
309 Standard_Integer aModCounter = anInf.Token(" ",2).IntegerValue();
310 theNewDocument->SetModifications (aModCounter);
312 catch (Standard_Failure) {
313 TCollection_ExtendedString aMsg("Warning: could not read the modification counter\0");
314 if(!aMsgDriver.IsNull())
315 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
319 if(anInfo == END_REF)
320 isRef = Standard_False;
321 if(isRef) { // Process References
323 Standard_Integer pos=anInfo.Search(" ");
325 // Parce RefId, DocumentVersion and FileName
326 Standard_Integer aRefId;
327 TCollection_ExtendedString aFileName;
328 Standard_Integer aDocumentVersion;
331 TCollection_ExtendedString aRest=anInfo.Split(pos);
332 aRefId = UTL::IntegerValue(anInfo);
334 Standard_Integer pos2 = aRest.Search(" ");
336 aFileName = aRest.Split(pos2);
337 aDocumentVersion = UTL::IntegerValue(aRest);
339 TCollection_AsciiString aPath = UTL::CString(aFileName);
340 TCollection_AsciiString anAbsolutePath;
341 if(!anAbsoluteDirectory.IsEmpty()) {
342 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
343 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
345 if(!aMsgDriver.IsNull()) {
346 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
347 TCollection_ExtendedString aMsg("Warning: ");
348 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
349 aMsgDriver->Send(aMsg.ToExtString(), Message_Warning);
353 TCollection_ExtendedString theFolder,theName;
354 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
355 TCollection_ExtendedString f(aPath);
358 Standard_Integer i= f.SearchFromEnd("/");
359 TCollection_ExtendedString n = f.Split(i);
360 f.Trunc(f.Length()-1);
364 OSD_Path p = UTL::Path(f);
365 Standard_ExtCharacter chr;
366 TCollection_ExtendedString dir, dirRet, name;
371 for ( int i = 1; i <= dir.Length (); ++i ) {
373 chr = dir.Value ( i );
392 theName = UTL::Name(p); theName+= UTL::Extension(p);
395 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
397 theNewDocument->CreateReference(aMetaData,aRefId,
398 theApplication,aDocumentVersion,Standard_False);
405 if(anInfo == START_REF)
406 isRef = Standard_True;
413 TCollection_ExtendedString aComment;
414 const XmlObjMgt_Element aCommentsElem =
415 theElement.GetChildByTagName ("comments");
416 if (aCommentsElem != NULL)
418 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
419 aNode != NULL; aNode = aNode.getNextSibling())
421 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
423 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
425 theNewDocument->AddComment(aComment);
431 // 2. Read Shapes section
432 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
433 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
434 if(!aNSDriver.IsNull())
435 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
437 // 2.1. Keep document format version in RT
438 Handle(Storage_HeaderData) aHeaderData = new Storage_HeaderData();
439 aHeaderData->SetStorageVersion(aCurDocVersion);
440 myRelocTable.Clear();
441 myRelocTable.SetHeaderData(aHeaderData);
443 // 5. Read document contents
448 TCollection_ExtendedString aMessage ("PasteDocument");
449 aMsgDriver ->Send (aMessage.ToExtString(), Message_Trace);
451 if (!MakeDocument(theElement, theNewDocument))
452 myReaderStatus = PCDM_RS_MakeFailure;
454 myReaderStatus = PCDM_RS_OK;
456 catch (Standard_Failure const& anException)
458 TCollection_ExtendedString anErrorString (anException.GetMessageString());
459 aMsgDriver ->Send (anErrorString.ToExtString(), Message_Fail);
462 // Wipe off the shapes written to the <shapes> section
463 ShapeSetCleaning(aNSDriver);
465 // Clean the relocation table.
466 // If the application needs to use myRelocTable to retrieve additional
467 // data from LDOM, this method should be reimplemented avoiding this step
468 myRelocTable.Clear();
469 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
472 //=======================================================================
473 //function : MakeDocument
475 //=======================================================================
476 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
477 (const XmlObjMgt_Element& theElement,
478 const Handle(CDM_Document)& theTDoc)
480 Standard_Boolean aResult = Standard_False;
481 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
484 Handle(TDF_Data) aTDF = new TDF_Data();
485 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
487 TDOC->SetData (aTDF);
488 TDocStd_Owner::SetDocument (aTDF, TDOC);
494 //=======================================================================
495 //function : AttributeDrivers
497 //=======================================================================
498 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
499 (const Handle(Message_Messenger)& theMessageDriver)
501 return XmlLDrivers::AttributeDrivers (theMessageDriver);
504 //=======================================================================
505 //function : take_time
507 //purpose : output astronomical time elapsed
508 //=======================================================================
511 #include <sys/timeb.h>
512 #include <sys/types.h>
515 extern "C" int ftime (struct timeb *tp);
517 extern struct timeb tmbuf0;
519 static void take_time (const Standard_Integer isReset, const char * aHeader,
520 const Handle(Message_Messenger)& aMessageDriver)
524 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
525 if (isReset) tmbuf0 = tmbuf;
527 char take_tm_buf [64];
528 Sprintf (take_tm_buf, "%9.2f s ++++",
529 double(tmbuf.time - tmbuf0.time) +
530 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
531 aMessage += take_tm_buf;
533 aMessageDriver -> Write (aMessage.ToExtString());
537 //=======================================================================
538 //function : ReadShapeSection
539 //purpose : definition of ReadShapeSection
540 //=======================================================================
541 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
542 const XmlObjMgt_Element& /*theElement*/,
543 const Handle(Message_Messenger)& /*aMsgDriver*/)
545 Handle(XmlMDF_ADriver) aDriver;
546 //empty; to be redefined
550 //=======================================================================
551 //function : ShapeSetCleaning
552 //purpose : definition of ShapeSetCleaning
553 //=======================================================================
554 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
555 const Handle(XmlMDF_ADriver)& /*theDriver*/)