1 // Created on: 2001-07-09
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <XmlLDrivers_DocumentRetrievalDriver.ixx>
23 #include <XmlLDrivers.hxx>
25 #include <XmlObjMgt_RRelocationTable.hxx>
26 #include <XmlObjMgt_Document.hxx>
27 #include <XmlObjMgt.hxx>
28 #include <XmlMDataStd.hxx>
29 #include <LDOM_LDOMImplementation.hxx>
30 #include <LDOM_DocumentType.hxx>
31 #include <LDOMParser.hxx>
33 #include <TDF_Data.hxx>
34 #include <TDocStd_Owner.hxx>
35 #include <TDocStd_Document.hxx>
36 #include <CDM_MessageDriver.hxx>
37 #include <CDM_MetaData.hxx>
38 #include <TCollection_AsciiString.hxx>
40 #include <OSD_Path.hxx>
47 #include <Standard_Failure.hxx>
48 #include <Standard_ErrorHandler.hxx>
50 #define START_REF "START_REF"
51 #define END_REF "END_REF"
52 #define REFERENCE_COUNTER "REFERENCE_COUNTER"
55 static void take_time (const Standard_Integer, const char *,
56 const Handle(CDM_MessageDriver)&)
63 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
65 Standard_Integer i, j, len ;
67 len = aString.Length() ;
69 // Case of network path, such as \\MACHINE\dir
70 for (i = j = 2 ; j <= len ; i++,j++) {
72 for (i = j = 1 ; j <= len ; i++,j++) {
74 Standard_Character c = aString.Value(j) ;
75 aString.SetValue(i,c) ;
77 while(j < len && aString.Value(j+1) == '/') j++ ;
80 if (aString.Value(len) == '/') len-- ;
84 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
85 TCollection_AsciiString theCFile=UTL::CString(aFileName);
86 TCollection_AsciiString theDirectory;
87 Standard_Integer i=theCFile.SearchFromEnd("/");
89 // if(i==-1) i=theCFile.SearchFromEnd("\\");
90 if(theCFile.SearchFromEnd("\\") > i)
91 i=theCFile.SearchFromEnd("\\");
93 if(i!=-1) theDirectory=theCFile.SubString(1,i);
97 static TCollection_AsciiString AbsolutePath(
98 const TCollection_AsciiString& aDirPath,
99 const TCollection_AsciiString& aRelFilePath)
101 TCollection_AsciiString EmptyString = "" ;
103 if (aRelFilePath.Search(":") == 2 ||
104 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
106 if(aRelFilePath.Search("/") == 1)
108 return aRelFilePath ;
110 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
111 Standard_Integer i,len ;
114 if(DirPath.Search(":") != 2 &&
115 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
117 if (DirPath.Search("/") != 1 )
122 DirPath.ChangeAll('\\','/') ;
123 RelFilePath.ChangeAll('\\','/') ;
126 RemoveExtraSeparator(DirPath) ;
127 len = RemoveExtraSeparator(RelFilePath) ;
129 while (RelFilePath.Search("../") == 1) {
132 RelFilePath = RelFilePath.SubString(4,len) ;
134 if (DirPath.IsEmpty())
136 i = DirPath.SearchFromEnd("/") ;
141 TCollection_AsciiString retx;
148 //=======================================================================
149 //function : XmlLDrivers_DocumentRetrievalDriver
150 //purpose : Constructor
151 //=======================================================================
152 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
154 myReaderStatus = PCDM_RS_OK;
157 //=======================================================================
158 //function : CreateDocument
159 //purpose : pure virtual method definition
160 //=======================================================================
161 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
163 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
166 //=======================================================================
167 //function : SchemaName
168 //purpose : pure virtual method definition
169 //=======================================================================
170 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
172 TCollection_ExtendedString schemaname;
176 //=======================================================================
178 //purpose : pure virtual method definition
179 //=======================================================================
180 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
181 const Handle(CDM_Document&))
185 //=======================================================================
188 //=======================================================================
189 void XmlLDrivers_DocumentRetrievalDriver::Read
190 (const TCollection_ExtendedString& theFileName,
191 const Handle(CDM_Document)& theNewDocument,
192 const Handle(CDM_Application)& theApplication)
194 myReaderStatus = PCDM_RS_DriverFailure;
195 myFileName = theFileName;
196 Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
197 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
199 // 1. Read DOM_Document from file
201 TCollection_AsciiString aName (theFileName,'?');
202 if (aParser.parse(aName.ToCString()))
204 TCollection_AsciiString aData;
205 cout << aParser.GetError(aData) << ": " << aData << endl;
206 myReaderStatus = PCDM_RS_FormatFailure;
209 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
210 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
212 ReadFromDomDocument (anElement, theNewDocument, theApplication);
215 //=======================================================================
216 //function : ReadFromDomDocument
217 //purpose : management of the macro-structure of XML document data
218 //remark : If the application needs to use myRelocTable to retrieve additional
219 // data from LDOM, this method should be reimplemented
220 //=======================================================================
222 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
223 (const XmlObjMgt_Element& theElement,
224 const Handle(CDM_Document)& theNewDocument,
225 const Handle(CDM_Application)& theApplication)
227 const Handle(CDM_MessageDriver) aMsgDriver =
228 theApplication -> MessageDriver();
229 // 1. Read info // to be done
230 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
231 Standard_Integer aCurDocVersion = 0;
232 TCollection_ExtendedString anInfo;
233 const XmlObjMgt_Element anInfoElem =
234 theElement.GetChildByTagName ("info");
235 if (anInfoElem != NULL) {
236 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
237 if(aDocVerStr == NULL)
239 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
240 TCollection_ExtendedString aMsg =
241 TCollection_ExtendedString ("Cannot retrieve the current Document version"
242 " attribute as \"") + aDocVerStr + "\"";
243 if(!aMsgDriver.IsNull())
244 aMsgDriver->Write(aMsg.ToExtString());
247 // oan: OCC22305 - check a document verison and if it's greater than
248 // current version of storage driver set an error status and return
249 if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
251 TCollection_ExtendedString aMsg =
252 TCollection_ExtendedString ("error: wrong file version: ") +
253 aDocVerStr + " while current is " +
254 XmlLDrivers::StorageVersion();
255 myReaderStatus = PCDM_RS_NoVersion;
256 if(!aMsgDriver.IsNull())
257 aMsgDriver->Write(aMsg.ToExtString());
261 if( aCurDocVersion < 2) aCurDocVersion = 2;
263 PropagateDocumentVersion(aCurDocVersion);
265 Standard_Boolean isRef = Standard_False;
266 for (LDOM_Node aNode = anInfoElem.getFirstChild();
267 aNode != NULL; aNode = aNode.getNextSibling()) {
268 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
269 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
272 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
275 TCollection_AsciiString anInf(anInfo,'?');
276 //Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
277 //theNewDocument->SetReferenceCounter(aRefCounter);
279 catch (Standard_Failure) {
280 // cout << "warning: could not read the reference counter in " << aFileName << endl;
281 TCollection_ExtendedString aMsg("Warning: ");
282 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
283 if(!aMsgDriver.IsNull())
284 aMsgDriver->Write(aMsg.ToExtString());
288 if(anInfo == END_REF)
289 isRef = Standard_False;
290 if(isRef) { // Process References
292 Standard_Integer pos=anInfo.Search(" ");
294 // Parce RefId, DocumentVersion and FileName
295 Standard_Integer aRefId;
296 TCollection_ExtendedString aFileName;
297 Standard_Integer aDocumentVersion;
300 TCollection_ExtendedString aRest=anInfo.Split(pos);
301 aRefId = UTL::IntegerValue(anInfo);
303 Standard_Integer pos2 = aRest.Search(" ");
305 aFileName = aRest.Split(pos2);
306 aDocumentVersion = UTL::IntegerValue(aRest);
308 TCollection_AsciiString aPath = UTL::CString(aFileName);
309 TCollection_AsciiString anAbsolutePath;
310 if(!anAbsoluteDirectory.IsEmpty()) {
311 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
312 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
314 if(!aMsgDriver.IsNull()) {
315 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
316 TCollection_ExtendedString aMsg("Warning: ");
317 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
318 aMsgDriver->Write(aMsg.ToExtString());
322 TCollection_ExtendedString theFolder,theName;
323 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
324 TCollection_ExtendedString f(aPath);
327 Standard_Integer i= f.SearchFromEnd("/");
328 TCollection_ExtendedString n = f.Split(i);
329 f.Trunc(f.Length()-1);
333 OSD_Path p = UTL::Path(f);
334 Standard_ExtCharacter chr;
335 TCollection_ExtendedString dir, dirRet, name;
340 for ( int i = 1; i <= dir.Length (); ++i ) {
342 chr = dir.Value ( i );
347 dirRet += _TEXT( "/" );
352 dirRet += _TEXT( ".." );
361 theName = UTL::Name(p); theName+= UTL::Extension(p);
364 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
366 theNewDocument->CreateReference(aMetaData,aRefId,
367 theApplication,aDocumentVersion,Standard_False);
374 if(anInfo == START_REF)
375 isRef = Standard_True;
382 TCollection_ExtendedString aComment;
383 const XmlObjMgt_Element aCommentsElem =
384 theElement.GetChildByTagName ("comments");
385 if (aCommentsElem != NULL)
387 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
388 aNode != NULL; aNode = aNode.getNextSibling())
390 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
392 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
394 theNewDocument->AddComment(aComment);
400 // 2. Read Shapes section
401 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
402 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
403 if(!aNSDriver.IsNull())
404 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
406 // 5. Read document contents
410 #if defined(DEB) && !defined(TAKE_TIMES)
411 TCollection_ExtendedString aMessage ("PasteDocument");
412 aMsgDriver -> Write (aMessage.ToExtString());
414 if (!MakeDocument(theElement, theNewDocument))
415 myReaderStatus = PCDM_RS_MakeFailure;
417 myReaderStatus = PCDM_RS_OK;
419 catch (Standard_Failure)
421 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
422 aMsgDriver -> Write (anErrorString.ToExtString());
425 // Wipe off the shapes written to the <shapes> section
426 ShapeSetCleaning(aNSDriver);
428 // Clean the relocation table.
429 // If the application needs to use myRelocTable to retrieve additional
430 // data from LDOM, this method should be reimplemented avoiding this step
431 myRelocTable.Clear();
432 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
435 //=======================================================================
436 //function : MakeDocument
438 //=======================================================================
439 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
440 (const XmlObjMgt_Element& theElement,
441 const Handle(CDM_Document)& theTDoc)
443 Standard_Boolean aResult = Standard_False;
444 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
445 myRelocTable.Clear();
448 Handle(TDF_Data) aTDF = new TDF_Data();
449 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
451 TDOC->SetData (aTDF);
452 TDocStd_Owner::SetDocument (aTDF, TDOC);
458 //=======================================================================
459 //function : AttributeDrivers
461 //=======================================================================
462 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
463 (const Handle(CDM_MessageDriver)& theMessageDriver)
465 return XmlLDrivers::AttributeDrivers (theMessageDriver);
468 //=======================================================================
469 //function : take_time
471 //purpose : output astronomical time elapsed
472 //=======================================================================
475 #include <sys/timeb.h>
476 #include <sys/types.h>
479 extern "C" int ftime (struct timeb *tp);
481 extern struct timeb tmbuf0;
483 static void take_time (const Standard_Integer isReset, const char * aHeader,
484 const Handle(CDM_MessageDriver)& aMessageDriver)
488 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
489 if (isReset) tmbuf0 = tmbuf;
491 char take_tm_buf [64];
492 Sprintf (take_tm_buf, "%9.2f s ++++",
493 double(tmbuf.time - tmbuf0.time) +
494 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
495 aMessage += take_tm_buf;
497 aMessageDriver -> Write (aMessage.ToExtString());
501 //=======================================================================
502 //function : PropagateDocumentVersion
504 //=======================================================================
505 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
506 const Standard_Integer theDocVersion )
509 // cout << "DocCurVersion =" << theDocVersion <<endl;
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*/)