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_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>
37 #include <XmlMDataStd.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(CDM_MessageDriver)&)
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 //=======================================================================
165 //function : CreateDocument
166 //purpose : pure virtual method definition
167 //=======================================================================
168 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
170 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
173 //=======================================================================
176 //=======================================================================
177 void XmlLDrivers_DocumentRetrievalDriver::Read
178 (const TCollection_ExtendedString& theFileName,
179 const Handle(CDM_Document)& theNewDocument,
180 const Handle(CDM_Application)& theApplication)
182 myReaderStatus = PCDM_RS_DriverFailure;
183 myFileName = theFileName;
185 std::ifstream aFileStream;
186 OSD_OpenStream (aFileStream, myFileName, std::ios::in);
188 if (aFileStream.is_open() && aFileStream.good())
190 Read (aFileStream, NULL, theNewDocument, theApplication);
194 myReaderStatus = PCDM_RS_OpenError;
196 TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
197 theFileName + " cannot be opened for reading";
199 theApplication->MessageDriver()->Write (aMsg.ToExtString());
200 Standard_Failure::Raise("File cannot be opened for reading");
204 //=======================================================================
207 //=======================================================================
208 void XmlLDrivers_DocumentRetrievalDriver::Read (Standard_IStream& theIStream,
209 const Handle(Storage_Data)& /*theStorageData*/,
210 const Handle(CDM_Document)& theNewDocument,
211 const Handle(CDM_Application)& theApplication)
213 Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
214 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
216 // 1. Read DOM_Document from file
219 if (aParser.parse(theIStream))
221 TCollection_AsciiString aData;
222 cout << aParser.GetError(aData) << ": " << aData << endl;
223 myReaderStatus = PCDM_RS_FormatFailure;
226 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
227 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
229 ReadFromDomDocument (anElement, theNewDocument, theApplication);
232 //=======================================================================
233 //function : ReadFromDomDocument
234 //purpose : management of the macro-structure of XML document data
235 //remark : If the application needs to use myRelocTable to retrieve additional
236 // data from LDOM, this method should be reimplemented
237 //=======================================================================
239 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
240 (const XmlObjMgt_Element& theElement,
241 const Handle(CDM_Document)& theNewDocument,
242 const Handle(CDM_Application)& theApplication)
244 const Handle(CDM_MessageDriver) aMsgDriver =
245 theApplication -> MessageDriver();
246 // 1. Read info // to be done
247 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
248 Standard_Integer aCurDocVersion = 0;
249 TCollection_ExtendedString anInfo;
250 const XmlObjMgt_Element anInfoElem =
251 theElement.GetChildByTagName ("info");
252 if (anInfoElem != NULL) {
253 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
254 if(aDocVerStr == NULL)
256 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
257 TCollection_ExtendedString aMsg =
258 TCollection_ExtendedString ("Cannot retrieve the current Document version"
259 " attribute as \"") + aDocVerStr + "\"";
260 if(!aMsgDriver.IsNull())
261 aMsgDriver->Write(aMsg.ToExtString());
264 // oan: OCC22305 - check a document verison and if it's greater than
265 // current version of storage driver set an error status and return
266 if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
268 TCollection_ExtendedString aMsg =
269 TCollection_ExtendedString ("error: wrong file version: ") +
270 aDocVerStr + " while current is " +
271 XmlLDrivers::StorageVersion();
272 myReaderStatus = PCDM_RS_NoVersion;
273 if(!aMsgDriver.IsNull())
274 aMsgDriver->Write(aMsg.ToExtString());
278 if( aCurDocVersion < 2) aCurDocVersion = 2;
280 PropagateDocumentVersion(aCurDocVersion);
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->Write(aMsg.ToExtString());
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->Write(aMsg.ToExtString());
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->Write(aMsg.ToExtString());
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 );
378 dirRet += _TEXT( "/" );
383 dirRet += _TEXT( ".." );
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 // 5. Read document contents
442 TCollection_ExtendedString aMessage ("PasteDocument");
443 aMsgDriver -> Write (aMessage.ToExtString());
445 if (!MakeDocument(theElement, theNewDocument))
446 myReaderStatus = PCDM_RS_MakeFailure;
448 myReaderStatus = PCDM_RS_OK;
450 catch (Standard_Failure)
452 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
453 aMsgDriver -> Write (anErrorString.ToExtString());
456 // Wipe off the shapes written to the <shapes> section
457 ShapeSetCleaning(aNSDriver);
459 // Clean the relocation table.
460 // If the application needs to use myRelocTable to retrieve additional
461 // data from LDOM, this method should be reimplemented avoiding this step
462 myRelocTable.Clear();
463 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
466 //=======================================================================
467 //function : MakeDocument
469 //=======================================================================
470 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
471 (const XmlObjMgt_Element& theElement,
472 const Handle(CDM_Document)& theTDoc)
474 Standard_Boolean aResult = Standard_False;
475 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
476 myRelocTable.Clear();
479 Handle(TDF_Data) aTDF = new TDF_Data();
480 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
482 TDOC->SetData (aTDF);
483 TDocStd_Owner::SetDocument (aTDF, TDOC);
489 //=======================================================================
490 //function : AttributeDrivers
492 //=======================================================================
493 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
494 (const Handle(CDM_MessageDriver)& theMessageDriver)
496 return XmlLDrivers::AttributeDrivers (theMessageDriver);
499 //=======================================================================
500 //function : take_time
502 //purpose : output astronomical time elapsed
503 //=======================================================================
506 #include <sys/timeb.h>
507 #include <sys/types.h>
510 extern "C" int ftime (struct timeb *tp);
512 extern struct timeb tmbuf0;
514 static void take_time (const Standard_Integer isReset, const char * aHeader,
515 const Handle(CDM_MessageDriver)& aMessageDriver)
519 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
520 if (isReset) tmbuf0 = tmbuf;
522 char take_tm_buf [64];
523 Sprintf (take_tm_buf, "%9.2f s ++++",
524 double(tmbuf.time - tmbuf0.time) +
525 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
526 aMessage += take_tm_buf;
528 aMessageDriver -> Write (aMessage.ToExtString());
532 //=======================================================================
533 //function : PropagateDocumentVersion
535 //=======================================================================
536 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
537 const Standard_Integer theDocVersion )
539 XmlMDataStd::SetDocumentVersion(theDocVersion);
542 //=======================================================================
543 //function : ReadShapeSection
544 //purpose : definition of ReadShapeSection
545 //=======================================================================
546 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
547 const XmlObjMgt_Element& /*theElement*/,
548 const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
550 Handle(XmlMDF_ADriver) aDriver;
551 //empty; to be redefined
555 //=======================================================================
556 //function : ShapeSetCleaning
557 //purpose : definition of ShapeSetCleaning
558 //=======================================================================
559 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
560 const Handle(XmlMDF_ADriver)& /*theDriver*/)