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 <PCDM_Document.hxx>
26 #include <Standard_Type.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_ExtendedString.hxx>
29 #include <TDF_Data.hxx>
30 #include <TDocStd_Document.hxx>
31 #include <TDocStd_Owner.hxx>
33 #include <XmlLDrivers.hxx>
34 #include <XmlLDrivers_DocumentRetrievalDriver.hxx>
35 #include <XmlMDataStd.hxx>
37 #include <XmlMDF_ADriver.hxx>
38 #include <XmlMDF_ADriverTable.hxx>
39 #include <XmlObjMgt.hxx>
40 #include <XmlObjMgt_Document.hxx>
41 #include <XmlObjMgt_RRelocationTable.hxx>
43 IMPLEMENT_STANDARD_RTTIEXT(XmlLDrivers_DocumentRetrievalDriver,PCDM_RetrievalDriver)
50 #include <Standard_Failure.hxx>
51 #include <Standard_ErrorHandler.hxx>
53 #define START_REF "START_REF"
54 #define END_REF "END_REF"
55 #define REFERENCE_COUNTER "REFERENCE_COUNTER"
58 static void take_time (const Standard_Integer, const char *,
59 const Handle(CDM_MessageDriver)&)
66 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
68 Standard_Integer i, j, len ;
70 len = aString.Length() ;
72 // Case of network path, such as \\MACHINE\dir
73 for (i = j = 2 ; j <= len ; i++,j++) {
75 for (i = j = 1 ; j <= len ; i++,j++) {
77 Standard_Character c = aString.Value(j) ;
78 aString.SetValue(i,c) ;
80 while(j < len && aString.Value(j+1) == '/') j++ ;
83 if (aString.Value(len) == '/') len-- ;
87 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
88 TCollection_AsciiString theCFile=UTL::CString(aFileName);
89 TCollection_AsciiString theDirectory;
90 Standard_Integer i=theCFile.SearchFromEnd("/");
92 // if(i==-1) i=theCFile.SearchFromEnd("\\");
93 if(theCFile.SearchFromEnd("\\") > i)
94 i=theCFile.SearchFromEnd("\\");
96 if(i!=-1) theDirectory=theCFile.SubString(1,i);
100 static TCollection_AsciiString AbsolutePath(
101 const TCollection_AsciiString& aDirPath,
102 const TCollection_AsciiString& aRelFilePath)
104 TCollection_AsciiString EmptyString = "" ;
106 if (aRelFilePath.Search(":") == 2 ||
107 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
109 if(aRelFilePath.Search("/") == 1)
111 return aRelFilePath ;
113 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
114 Standard_Integer i,len ;
117 if(DirPath.Search(":") != 2 &&
118 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
120 if (DirPath.Search("/") != 1 )
125 DirPath.ChangeAll('\\','/') ;
126 RelFilePath.ChangeAll('\\','/') ;
129 RemoveExtraSeparator(DirPath) ;
130 len = RemoveExtraSeparator(RelFilePath) ;
132 while (RelFilePath.Search("../") == 1) {
135 RelFilePath = RelFilePath.SubString(4,len) ;
137 if (DirPath.IsEmpty())
139 i = DirPath.SearchFromEnd("/") ;
144 TCollection_AsciiString retx;
151 //=======================================================================
152 //function : XmlLDrivers_DocumentRetrievalDriver
153 //purpose : Constructor
154 //=======================================================================
155 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
157 myReaderStatus = PCDM_RS_OK;
160 //=======================================================================
161 //function : CreateDocument
162 //purpose : pure virtual method definition
163 //=======================================================================
164 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
166 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
169 //=======================================================================
170 //function : SchemaName
171 //purpose : pure virtual method definition
172 //=======================================================================
173 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
175 TCollection_ExtendedString schemaname;
179 //=======================================================================
181 //purpose : pure virtual method definition
182 //=======================================================================
183 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
184 const Handle(CDM_Document)&)
188 //=======================================================================
191 //=======================================================================
192 void XmlLDrivers_DocumentRetrievalDriver::Read
193 (const TCollection_ExtendedString& theFileName,
194 const Handle(CDM_Document)& theNewDocument,
195 const Handle(CDM_Application)& theApplication)
197 myReaderStatus = PCDM_RS_DriverFailure;
198 myFileName = theFileName;
199 Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
200 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
202 // 1. Read DOM_Document from file
204 TCollection_AsciiString aName (theFileName,'?');
205 if (aParser.parse(aName.ToCString()))
207 TCollection_AsciiString aData;
208 cout << aParser.GetError(aData) << ": " << aData << endl;
209 myReaderStatus = PCDM_RS_FormatFailure;
212 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
213 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
215 ReadFromDomDocument (anElement, theNewDocument, theApplication);
218 //=======================================================================
219 //function : ReadFromDomDocument
220 //purpose : management of the macro-structure of XML document data
221 //remark : If the application needs to use myRelocTable to retrieve additional
222 // data from LDOM, this method should be reimplemented
223 //=======================================================================
225 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
226 (const XmlObjMgt_Element& theElement,
227 const Handle(CDM_Document)& theNewDocument,
228 const Handle(CDM_Application)& theApplication)
230 const Handle(CDM_MessageDriver) aMsgDriver =
231 theApplication -> MessageDriver();
232 // 1. Read info // to be done
233 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
234 Standard_Integer aCurDocVersion = 0;
235 TCollection_ExtendedString anInfo;
236 const XmlObjMgt_Element anInfoElem =
237 theElement.GetChildByTagName ("info");
238 if (anInfoElem != NULL) {
239 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
240 if(aDocVerStr == NULL)
242 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
243 TCollection_ExtendedString aMsg =
244 TCollection_ExtendedString ("Cannot retrieve the current Document version"
245 " attribute as \"") + aDocVerStr + "\"";
246 if(!aMsgDriver.IsNull())
247 aMsgDriver->Write(aMsg.ToExtString());
250 // oan: OCC22305 - check a document verison and if it's greater than
251 // current version of storage driver set an error status and return
252 if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
254 TCollection_ExtendedString aMsg =
255 TCollection_ExtendedString ("error: wrong file version: ") +
256 aDocVerStr + " while current is " +
257 XmlLDrivers::StorageVersion();
258 myReaderStatus = PCDM_RS_NoVersion;
259 if(!aMsgDriver.IsNull())
260 aMsgDriver->Write(aMsg.ToExtString());
264 if( aCurDocVersion < 2) aCurDocVersion = 2;
266 PropagateDocumentVersion(aCurDocVersion);
268 Standard_Boolean isRef = Standard_False;
269 for (LDOM_Node aNode = anInfoElem.getFirstChild();
270 aNode != NULL; aNode = aNode.getNextSibling()) {
271 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
272 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
275 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
278 TCollection_AsciiString anInf(anInfo,'?');
279 //Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
280 //theNewDocument->SetReferenceCounter(aRefCounter);
282 catch (Standard_Failure) {
283 // cout << "warning: could not read the reference counter in " << aFileName << endl;
284 TCollection_ExtendedString aMsg("Warning: ");
285 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
286 if(!aMsgDriver.IsNull())
287 aMsgDriver->Write(aMsg.ToExtString());
291 if(anInfo == END_REF)
292 isRef = Standard_False;
293 if(isRef) { // Process References
295 Standard_Integer pos=anInfo.Search(" ");
297 // Parce RefId, DocumentVersion and FileName
298 Standard_Integer aRefId;
299 TCollection_ExtendedString aFileName;
300 Standard_Integer aDocumentVersion;
303 TCollection_ExtendedString aRest=anInfo.Split(pos);
304 aRefId = UTL::IntegerValue(anInfo);
306 Standard_Integer pos2 = aRest.Search(" ");
308 aFileName = aRest.Split(pos2);
309 aDocumentVersion = UTL::IntegerValue(aRest);
311 TCollection_AsciiString aPath = UTL::CString(aFileName);
312 TCollection_AsciiString anAbsolutePath;
313 if(!anAbsoluteDirectory.IsEmpty()) {
314 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
315 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
317 if(!aMsgDriver.IsNull()) {
318 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
319 TCollection_ExtendedString aMsg("Warning: ");
320 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
321 aMsgDriver->Write(aMsg.ToExtString());
325 TCollection_ExtendedString theFolder,theName;
326 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
327 TCollection_ExtendedString f(aPath);
330 Standard_Integer i= f.SearchFromEnd("/");
331 TCollection_ExtendedString n = f.Split(i);
332 f.Trunc(f.Length()-1);
336 OSD_Path p = UTL::Path(f);
337 Standard_ExtCharacter chr;
338 TCollection_ExtendedString dir, dirRet, name;
343 for ( int i = 1; i <= dir.Length (); ++i ) {
345 chr = dir.Value ( i );
350 dirRet += _TEXT( "/" );
355 dirRet += _TEXT( ".." );
364 theName = UTL::Name(p); theName+= UTL::Extension(p);
367 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
369 theNewDocument->CreateReference(aMetaData,aRefId,
370 theApplication,aDocumentVersion,Standard_False);
377 if(anInfo == START_REF)
378 isRef = Standard_True;
385 TCollection_ExtendedString aComment;
386 const XmlObjMgt_Element aCommentsElem =
387 theElement.GetChildByTagName ("comments");
388 if (aCommentsElem != NULL)
390 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
391 aNode != NULL; aNode = aNode.getNextSibling())
393 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
395 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
397 theNewDocument->AddComment(aComment);
403 // 2. Read Shapes section
404 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
405 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
406 if(!aNSDriver.IsNull())
407 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
409 // 5. Read document contents
414 TCollection_ExtendedString aMessage ("PasteDocument");
415 aMsgDriver -> Write (aMessage.ToExtString());
417 if (!MakeDocument(theElement, theNewDocument))
418 myReaderStatus = PCDM_RS_MakeFailure;
420 myReaderStatus = PCDM_RS_OK;
422 catch (Standard_Failure)
424 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
425 aMsgDriver -> Write (anErrorString.ToExtString());
428 // Wipe off the shapes written to the <shapes> section
429 ShapeSetCleaning(aNSDriver);
431 // Clean the relocation table.
432 // If the application needs to use myRelocTable to retrieve additional
433 // data from LDOM, this method should be reimplemented avoiding this step
434 myRelocTable.Clear();
435 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
438 //=======================================================================
439 //function : MakeDocument
441 //=======================================================================
442 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
443 (const XmlObjMgt_Element& theElement,
444 const Handle(CDM_Document)& theTDoc)
446 Standard_Boolean aResult = Standard_False;
447 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
448 myRelocTable.Clear();
451 Handle(TDF_Data) aTDF = new TDF_Data();
452 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
454 TDOC->SetData (aTDF);
455 TDocStd_Owner::SetDocument (aTDF, TDOC);
461 //=======================================================================
462 //function : AttributeDrivers
464 //=======================================================================
465 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
466 (const Handle(CDM_MessageDriver)& theMessageDriver)
468 return XmlLDrivers::AttributeDrivers (theMessageDriver);
471 //=======================================================================
472 //function : take_time
474 //purpose : output astronomical time elapsed
475 //=======================================================================
478 #include <sys/timeb.h>
479 #include <sys/types.h>
482 extern "C" int ftime (struct timeb *tp);
484 extern struct timeb tmbuf0;
486 static void take_time (const Standard_Integer isReset, const char * aHeader,
487 const Handle(CDM_MessageDriver)& aMessageDriver)
491 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
492 if (isReset) tmbuf0 = tmbuf;
494 char take_tm_buf [64];
495 Sprintf (take_tm_buf, "%9.2f s ++++",
496 double(tmbuf.time - tmbuf0.time) +
497 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
498 aMessage += take_tm_buf;
500 aMessageDriver -> Write (aMessage.ToExtString());
504 //=======================================================================
505 //function : PropagateDocumentVersion
507 //=======================================================================
508 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
509 const Standard_Integer theDocVersion )
511 XmlMDataStd::SetDocumentVersion(theDocVersion);
514 //=======================================================================
515 //function : ReadShapeSection
516 //purpose : definition of ReadShapeSection
517 //=======================================================================
518 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
519 const XmlObjMgt_Element& /*theElement*/,
520 const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
522 Handle(XmlMDF_ADriver) aDriver;
523 //empty; to be redefined
527 //=======================================================================
528 //function : ShapeSetCleaning
529 //purpose : definition of ShapeSetCleaning
530 //=======================================================================
531 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
532 const Handle(XmlMDF_ADriver)& /*theDriver*/)