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());
239 if(aCurDocVersion < 2) aCurDocVersion = 2;
241 PropagateDocumentVersion(aCurDocVersion);
243 Standard_Boolean isRef = Standard_False;
244 for (LDOM_Node aNode = anInfoElem.getFirstChild();
245 aNode != NULL; aNode = aNode.getNextSibling()) {
246 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
247 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
250 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
253 TCollection_AsciiString anInf(anInfo,'?');
254 Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
255 //theNewDocument->SetReferenceCounter(aRefCounter);
257 catch (Standard_Failure) {
258 // cout << "warning: could not read the reference counter in " << aFileName << endl;
259 TCollection_ExtendedString aMsg("Warning: ");
260 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
261 if(!aMsgDriver.IsNull())
262 aMsgDriver->Write(aMsg.ToExtString());
266 if(anInfo == END_REF)
267 isRef = Standard_False;
268 if(isRef) { // Process References
270 Standard_Integer pos=anInfo.Search(" ");
272 // Parce RefId, DocumentVersion and FileName
273 Standard_Integer aRefId;
274 TCollection_ExtendedString aFileName;
275 Standard_Integer aDocumentVersion;
278 TCollection_ExtendedString aRest=anInfo.Split(pos);
279 aRefId = UTL::IntegerValue(anInfo);
281 Standard_Integer pos2 = aRest.Search(" ");
283 aFileName = aRest.Split(pos2);
284 aDocumentVersion = UTL::IntegerValue(aRest);
286 TCollection_AsciiString aPath = UTL::CString(aFileName);
287 TCollection_AsciiString anAbsolutePath;
288 if(!anAbsoluteDirectory.IsEmpty()) {
289 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
290 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
292 if(!aMsgDriver.IsNull()) {
293 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
294 TCollection_ExtendedString aMsg("Warning: ");
295 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
296 aMsgDriver->Write(aMsg.ToExtString());
300 TCollection_ExtendedString theFolder,theName;
301 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
302 TCollection_ExtendedString f(aPath);
305 Standard_Integer i= f.SearchFromEnd("/");
306 TCollection_ExtendedString n = f.Split(i);
307 f.Trunc(f.Length()-1);
311 OSD_Path p = UTL::Path(f);
312 Standard_ExtCharacter chr;
313 TCollection_ExtendedString dir, dirRet, name;
318 for ( int i = 1; i <= dir.Length (); ++i ) {
320 chr = dir.Value ( i );
325 dirRet += _TEXT( "/" );
330 dirRet += _TEXT( ".." );
339 theName = UTL::Name(p); theName+= UTL::Extension(p);
342 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
344 theNewDocument->CreateReference(aMetaData,aRefId,
345 theApplication,aDocumentVersion,Standard_False);
352 if(anInfo == START_REF)
353 isRef = Standard_True;
360 TCollection_ExtendedString aComment;
361 const XmlObjMgt_Element aCommentsElem =
362 theElement.GetChildByTagName ("comments");
363 if (aCommentsElem != NULL)
365 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
366 aNode != NULL; aNode = aNode.getNextSibling())
368 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
370 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
372 theNewDocument->AddComment(aComment);
378 // 2. Read Shapes section
379 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
380 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
381 if(!aNSDriver.IsNull())
382 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
384 // 5. Read document contents
388 #if defined(DEB) && !defined(TAKE_TIMES)
389 TCollection_ExtendedString aMessage ("PasteDocument");
390 aMsgDriver -> Write (aMessage.ToExtString());
392 if (!MakeDocument(theElement, theNewDocument))
393 myReaderStatus = PCDM_RS_MakeFailure;
395 myReaderStatus = PCDM_RS_OK;
397 catch (Standard_Failure)
399 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
400 aMsgDriver -> Write (anErrorString.ToExtString());
403 // Wipe off the shapes written to the <shapes> section
404 ShapeSetCleaning(aNSDriver);
406 // Clean the relocation table.
407 // If the application needs to use myRelocTable to retrieve additional
408 // data from LDOM, this method should be reimplemented avoiding this step
409 myRelocTable.Clear();
410 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
413 //=======================================================================
414 //function : MakeDocument
416 //=======================================================================
417 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
418 (const XmlObjMgt_Element& theElement,
419 const Handle(CDM_Document)& theTDoc)
421 Standard_Boolean aResult = Standard_False;
422 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
423 myRelocTable.Clear();
426 Handle(TDF_Data) aTDF = new TDF_Data();
427 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
429 TDOC->SetData (aTDF);
430 TDocStd_Owner::SetDocument (aTDF, TDOC);
436 //=======================================================================
437 //function : AttributeDrivers
439 //=======================================================================
440 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
441 (const Handle(CDM_MessageDriver)& theMessageDriver)
443 return XmlLDrivers::AttributeDrivers (theMessageDriver);
446 //=======================================================================
447 //function : take_time
449 //purpose : output astronomical time elapsed
450 //=======================================================================
453 #include <sys/timeb.h>
454 #include <sys/types.h>
457 extern "C" int ftime (struct timeb *tp);
459 extern struct timeb tmbuf0;
461 static void take_time (const Standard_Integer isReset, const char * aHeader,
462 const Handle(CDM_MessageDriver)& aMessageDriver)
466 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
467 if (isReset) tmbuf0 = tmbuf;
469 char take_tm_buf [64];
470 sprintf (take_tm_buf, "%9.2f s ++++",
471 double(tmbuf.time - tmbuf0.time) +
472 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
473 aMessage += take_tm_buf;
475 aMessageDriver -> Write (aMessage.ToExtString());
479 //=======================================================================
480 //function : PropagateDocumentVersion
482 //=======================================================================
483 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
484 const Standard_Integer theDocVersion )
487 // cout << "DocCurVersion =" << theDocVersion <<endl;
489 XmlMDataStd::SetDocumentVersion(theDocVersion);
492 //=======================================================================
493 //function : ReadShapeSection
494 //purpose : definition of ReadShapeSection
495 //=======================================================================
496 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
497 const XmlObjMgt_Element& /*theElement*/,
498 const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
500 Handle(XmlMDF_ADriver) aDriver;
501 //empty; to be redefined
505 //=======================================================================
506 //function : ShapeSetCleaning
507 //purpose : definition of ShapeSetCleaning
508 //=======================================================================
509 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
510 const Handle(XmlMDF_ADriver)& /*theDriver*/)