1 // File: XmlLDrivers_DocumentRetrievalDriver.cxx
2 // Created: Mon Jul 9 12:29:49 MSK DST 2001
3 // Author: Julia DOROVSKIKH
4 // Copyright: Open Cascade 2001
6 #include <XmlLDrivers_DocumentRetrievalDriver.ixx>
8 #include <XmlLDrivers.hxx>
10 #include <XmlObjMgt_RRelocationTable.hxx>
11 #include <XmlObjMgt_Document.hxx>
12 #include <XmlObjMgt.hxx>
13 #include <XmlMDataStd.hxx>
14 #include <LDOM_LDOMImplementation.hxx>
15 #include <LDOM_DocumentType.hxx>
16 #include <LDOMParser.hxx>
18 #include <TDF_Data.hxx>
19 #include <TDocStd_Owner.hxx>
20 #include <TDocStd_Document.hxx>
21 #include <CDM_MessageDriver.hxx>
22 #include <CDM_MetaData.hxx>
23 #include <TCollection_AsciiString.hxx>
25 #include <OSD_Path.hxx>
32 #include <Standard_Failure.hxx>
33 #include <Standard_ErrorHandler.hxx>
35 #define START_REF "START_REF"
36 #define END_REF "END_REF"
37 #define REFERENCE_COUNTER "REFERENCE_COUNTER"
39 static Standard_Integer myDocumentVersion = 0;
42 static void take_time (const Standard_Integer, const char *,
43 const Handle(CDM_MessageDriver)&)
50 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
52 Standard_Integer i, j, len ;
54 len = aString.Length() ;
56 // Case of network path, such as \\MACHINE\dir
57 for (i = j = 2 ; j <= len ; i++,j++) {
59 for (i = j = 1 ; j <= len ; i++,j++) {
61 Standard_Character c = aString.Value(j) ;
62 aString.SetValue(i,c) ;
64 while(j < len && aString.Value(j+1) == '/') j++ ;
67 if (aString.Value(len) == '/') len-- ;
71 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
72 TCollection_AsciiString theCFile=UTL::CString(aFileName);
73 TCollection_AsciiString theDirectory;
74 Standard_Integer i=theCFile.SearchFromEnd("/");
76 // if(i==-1) i=theCFile.SearchFromEnd("\\");
77 if(theCFile.SearchFromEnd("\\") > i)
78 i=theCFile.SearchFromEnd("\\");
80 if(i!=-1) theDirectory=theCFile.SubString(1,i);
84 static TCollection_AsciiString AbsolutePath(
85 const TCollection_AsciiString& aDirPath,
86 const TCollection_AsciiString& aRelFilePath)
88 TCollection_AsciiString EmptyString = "" ;
90 if (aRelFilePath.Search(":") == 2 ||
91 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
93 if(aRelFilePath.Search("/") == 1)
97 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
98 Standard_Integer i,len ;
101 if(DirPath.Search(":") != 2 &&
102 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
104 if (DirPath.Search("/") != 1 )
109 DirPath.ChangeAll('\\','/') ;
110 RelFilePath.ChangeAll('\\','/') ;
113 RemoveExtraSeparator(DirPath) ;
114 len = RemoveExtraSeparator(RelFilePath) ;
116 while (RelFilePath.Search("../") == 1) {
119 RelFilePath = RelFilePath.SubString(4,len) ;
121 if (DirPath.IsEmpty())
123 i = DirPath.SearchFromEnd("/") ;
128 TCollection_AsciiString retx;
135 //=======================================================================
136 //function : XmlLDrivers_DocumentRetrievalDriver
137 //purpose : Constructor
138 //=======================================================================
139 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
141 myReaderStatus = PCDM_RS_OK;
144 //=======================================================================
145 //function : CreateDocument
146 //purpose : pure virtual method definition
147 //=======================================================================
148 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
150 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
153 //=======================================================================
154 //function : SchemaName
155 //purpose : pure virtual method definition
156 //=======================================================================
157 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
159 TCollection_ExtendedString schemaname;
163 //=======================================================================
165 //purpose : pure virtual method definition
166 //=======================================================================
167 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
168 const Handle(CDM_Document&))
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;
183 const TCollection_AsciiString anOldNumLocale =
184 (Standard_CString) setlocale (LC_NUMERIC, NULL);
185 setlocale(LC_NUMERIC, "C");
187 Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
188 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
190 // 1. Read DOM_Document from file
192 TCollection_AsciiString aName (theFileName,'?');
193 if (aParser.parse(aName.ToCString()))
195 TCollection_AsciiString aData;
196 cout << aParser.GetError(aData) << ": " << aData << endl;
197 myReaderStatus = PCDM_RS_FormatFailure;
200 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
201 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
203 ReadFromDomDocument (anElement, theNewDocument, theApplication);
205 setlocale(LC_NUMERIC, (char *) anOldNumLocale.ToCString()) ;
208 //=======================================================================
209 //function : ReadFromDomDocument
210 //purpose : management of the macro-structure of XML document data
211 //remark : If the application needs to use myRelocTable to retrieve additional
212 // data from LDOM, this method should be reimplemented
213 //=======================================================================
215 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
216 (const XmlObjMgt_Element& theElement,
217 const Handle(CDM_Document)& theNewDocument,
218 const Handle(CDM_Application)& theApplication)
220 const Handle(CDM_MessageDriver) aMsgDriver =
221 theApplication -> MessageDriver();
222 // 1. Read info // to be done
223 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
224 Standard_Integer aCurDocVersion = 0;
225 TCollection_ExtendedString anInfo;
226 const XmlObjMgt_Element anInfoElem =
227 theElement.GetChildByTagName ("info");
228 if (anInfoElem != NULL) {
229 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
230 if(aDocVerStr == NULL)
232 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
233 TCollection_ExtendedString aMsg =
234 TCollection_ExtendedString ("Cannot retrieve the current Document version"
235 " attribute as \"") + aDocVerStr + "\"";
236 if(!aMsgDriver.IsNull())
237 aMsgDriver->Write(aMsg.ToExtString());
240 // oan: OCC22305 - check a document verison and if it's greater than
241 // current version of storage driver set an error status and return
242 if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
244 TCollection_ExtendedString aMsg =
245 TCollection_ExtendedString ("error: wrong file version: ") +
246 aDocVerStr + " while current is " +
247 XmlLDrivers::StorageVersion();
248 myReaderStatus = PCDM_RS_NoVersion;
249 if(!aMsgDriver.IsNull())
250 aMsgDriver->Write(aMsg.ToExtString());
254 if( aCurDocVersion < 2) aCurDocVersion = 2;
256 PropagateDocumentVersion(aCurDocVersion);
258 Standard_Boolean isRef = Standard_False;
259 for (LDOM_Node aNode = anInfoElem.getFirstChild();
260 aNode != NULL; aNode = aNode.getNextSibling()) {
261 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
262 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
265 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
268 TCollection_AsciiString anInf(anInfo,'?');
269 //Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
270 //theNewDocument->SetReferenceCounter(aRefCounter);
272 catch (Standard_Failure) {
273 // cout << "warning: could not read the reference counter in " << aFileName << endl;
274 TCollection_ExtendedString aMsg("Warning: ");
275 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
276 if(!aMsgDriver.IsNull())
277 aMsgDriver->Write(aMsg.ToExtString());
281 if(anInfo == END_REF)
282 isRef = Standard_False;
283 if(isRef) { // Process References
285 Standard_Integer pos=anInfo.Search(" ");
287 // Parce RefId, DocumentVersion and FileName
288 Standard_Integer aRefId;
289 TCollection_ExtendedString aFileName;
290 Standard_Integer aDocumentVersion;
293 TCollection_ExtendedString aRest=anInfo.Split(pos);
294 aRefId = UTL::IntegerValue(anInfo);
296 Standard_Integer pos2 = aRest.Search(" ");
298 aFileName = aRest.Split(pos2);
299 aDocumentVersion = UTL::IntegerValue(aRest);
301 TCollection_AsciiString aPath = UTL::CString(aFileName);
302 TCollection_AsciiString anAbsolutePath;
303 if(!anAbsoluteDirectory.IsEmpty()) {
304 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
305 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
307 if(!aMsgDriver.IsNull()) {
308 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
309 TCollection_ExtendedString aMsg("Warning: ");
310 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
311 aMsgDriver->Write(aMsg.ToExtString());
315 TCollection_ExtendedString theFolder,theName;
316 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
317 TCollection_ExtendedString f(aPath);
320 Standard_Integer i= f.SearchFromEnd("/");
321 TCollection_ExtendedString n = f.Split(i);
322 f.Trunc(f.Length()-1);
326 OSD_Path p = UTL::Path(f);
327 Standard_ExtCharacter chr;
328 TCollection_ExtendedString dir, dirRet, name;
333 for ( int i = 1; i <= dir.Length (); ++i ) {
335 chr = dir.Value ( i );
340 dirRet += _TEXT( "/" );
345 dirRet += _TEXT( ".." );
354 theName = UTL::Name(p); theName+= UTL::Extension(p);
357 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
359 theNewDocument->CreateReference(aMetaData,aRefId,
360 theApplication,aDocumentVersion,Standard_False);
367 if(anInfo == START_REF)
368 isRef = Standard_True;
375 TCollection_ExtendedString aComment;
376 const XmlObjMgt_Element aCommentsElem =
377 theElement.GetChildByTagName ("comments");
378 if (aCommentsElem != NULL)
380 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
381 aNode != NULL; aNode = aNode.getNextSibling())
383 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
385 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
387 theNewDocument->AddComment(aComment);
393 // 2. Read Shapes section
394 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
395 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
396 if(!aNSDriver.IsNull())
397 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
399 // 5. Read document contents
403 #if defined(DEB) && !defined(TAKE_TIMES)
404 TCollection_ExtendedString aMessage ("PasteDocument");
405 aMsgDriver -> Write (aMessage.ToExtString());
407 if (!MakeDocument(theElement, theNewDocument))
408 myReaderStatus = PCDM_RS_MakeFailure;
410 myReaderStatus = PCDM_RS_OK;
412 catch (Standard_Failure)
414 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
415 aMsgDriver -> Write (anErrorString.ToExtString());
418 // Wipe off the shapes written to the <shapes> section
419 ShapeSetCleaning(aNSDriver);
421 // Clean the relocation table.
422 // If the application needs to use myRelocTable to retrieve additional
423 // data from LDOM, this method should be reimplemented avoiding this step
424 myRelocTable.Clear();
425 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
428 //=======================================================================
429 //function : MakeDocument
431 //=======================================================================
432 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
433 (const XmlObjMgt_Element& theElement,
434 const Handle(CDM_Document)& theTDoc)
436 Standard_Boolean aResult = Standard_False;
437 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
438 myRelocTable.Clear();
441 Handle(TDF_Data) aTDF = new TDF_Data();
442 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
444 TDOC->SetData (aTDF);
445 TDocStd_Owner::SetDocument (aTDF, TDOC);
451 //=======================================================================
452 //function : AttributeDrivers
454 //=======================================================================
455 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
456 (const Handle(CDM_MessageDriver)& theMessageDriver)
458 return XmlLDrivers::AttributeDrivers (theMessageDriver);
461 //=======================================================================
462 //function : take_time
464 //purpose : output astronomical time elapsed
465 //=======================================================================
468 #include <sys/timeb.h>
469 #include <sys/types.h>
472 extern "C" int ftime (struct timeb *tp);
474 extern struct timeb tmbuf0;
476 static void take_time (const Standard_Integer isReset, const char * aHeader,
477 const Handle(CDM_MessageDriver)& aMessageDriver)
481 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
482 if (isReset) tmbuf0 = tmbuf;
484 char take_tm_buf [64];
485 sprintf (take_tm_buf, "%9.2f s ++++",
486 double(tmbuf.time - tmbuf0.time) +
487 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
488 aMessage += take_tm_buf;
490 aMessageDriver -> Write (aMessage.ToExtString());
494 //=======================================================================
495 //function : PropagateDocumentVersion
497 //=======================================================================
498 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
499 const Standard_Integer theDocVersion )
502 // cout << "DocCurVersion =" << theDocVersion <<endl;
504 XmlMDataStd::SetDocumentVersion(theDocVersion);
507 //=======================================================================
508 //function : ReadShapeSection
509 //purpose : definition of ReadShapeSection
510 //=======================================================================
511 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
512 const XmlObjMgt_Element& /*theElement*/,
513 const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
515 Handle(XmlMDF_ADriver) aDriver;
516 //empty; to be redefined
520 //=======================================================================
521 //function : ShapeSetCleaning
522 //purpose : definition of ShapeSetCleaning
523 //=======================================================================
524 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
525 const Handle(XmlMDF_ADriver)& /*theDriver*/)