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"
54 static Standard_Integer myDocumentVersion = 0;
57 static void take_time (const Standard_Integer, const char *,
58 const Handle(CDM_MessageDriver)&)
65 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
67 Standard_Integer i, j, len ;
69 len = aString.Length() ;
71 // Case of network path, such as \\MACHINE\dir
72 for (i = j = 2 ; j <= len ; i++,j++) {
74 for (i = j = 1 ; j <= len ; i++,j++) {
76 Standard_Character c = aString.Value(j) ;
77 aString.SetValue(i,c) ;
79 while(j < len && aString.Value(j+1) == '/') j++ ;
82 if (aString.Value(len) == '/') len-- ;
86 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
87 TCollection_AsciiString theCFile=UTL::CString(aFileName);
88 TCollection_AsciiString theDirectory;
89 Standard_Integer i=theCFile.SearchFromEnd("/");
91 // if(i==-1) i=theCFile.SearchFromEnd("\\");
92 if(theCFile.SearchFromEnd("\\") > i)
93 i=theCFile.SearchFromEnd("\\");
95 if(i!=-1) theDirectory=theCFile.SubString(1,i);
99 static TCollection_AsciiString AbsolutePath(
100 const TCollection_AsciiString& aDirPath,
101 const TCollection_AsciiString& aRelFilePath)
103 TCollection_AsciiString EmptyString = "" ;
105 if (aRelFilePath.Search(":") == 2 ||
106 (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
108 if(aRelFilePath.Search("/") == 1)
110 return aRelFilePath ;
112 TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath ;
113 Standard_Integer i,len ;
116 if(DirPath.Search(":") != 2 &&
117 (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
119 if (DirPath.Search("/") != 1 )
124 DirPath.ChangeAll('\\','/') ;
125 RelFilePath.ChangeAll('\\','/') ;
128 RemoveExtraSeparator(DirPath) ;
129 len = RemoveExtraSeparator(RelFilePath) ;
131 while (RelFilePath.Search("../") == 1) {
134 RelFilePath = RelFilePath.SubString(4,len) ;
136 if (DirPath.IsEmpty())
138 i = DirPath.SearchFromEnd("/") ;
143 TCollection_AsciiString retx;
150 //=======================================================================
151 //function : XmlLDrivers_DocumentRetrievalDriver
152 //purpose : Constructor
153 //=======================================================================
154 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
156 myReaderStatus = PCDM_RS_OK;
159 //=======================================================================
160 //function : CreateDocument
161 //purpose : pure virtual method definition
162 //=======================================================================
163 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
165 return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
168 //=======================================================================
169 //function : SchemaName
170 //purpose : pure virtual method definition
171 //=======================================================================
172 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
174 TCollection_ExtendedString schemaname;
178 //=======================================================================
180 //purpose : pure virtual method definition
181 //=======================================================================
182 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
183 const Handle(CDM_Document&))
187 //=======================================================================
190 //=======================================================================
191 void XmlLDrivers_DocumentRetrievalDriver::Read
192 (const TCollection_ExtendedString& theFileName,
193 const Handle(CDM_Document)& theNewDocument,
194 const Handle(CDM_Application)& theApplication)
196 myReaderStatus = PCDM_RS_DriverFailure;
197 myFileName = theFileName;
198 const TCollection_AsciiString anOldNumLocale =
199 (Standard_CString) setlocale (LC_NUMERIC, NULL);
200 setlocale(LC_NUMERIC, "C");
202 Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
203 ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
205 // 1. Read DOM_Document from file
207 TCollection_AsciiString aName (theFileName,'?');
208 if (aParser.parse(aName.ToCString()))
210 TCollection_AsciiString aData;
211 cout << aParser.GetError(aData) << ": " << aData << endl;
212 myReaderStatus = PCDM_RS_FormatFailure;
215 const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
216 ::take_time (0, " +++++ Fin parsing XML : ", aMessageDriver);
218 ReadFromDomDocument (anElement, theNewDocument, theApplication);
220 setlocale(LC_NUMERIC, (char *) anOldNumLocale.ToCString()) ;
223 //=======================================================================
224 //function : ReadFromDomDocument
225 //purpose : management of the macro-structure of XML document data
226 //remark : If the application needs to use myRelocTable to retrieve additional
227 // data from LDOM, this method should be reimplemented
228 //=======================================================================
230 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
231 (const XmlObjMgt_Element& theElement,
232 const Handle(CDM_Document)& theNewDocument,
233 const Handle(CDM_Application)& theApplication)
235 const Handle(CDM_MessageDriver) aMsgDriver =
236 theApplication -> MessageDriver();
237 // 1. Read info // to be done
238 TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
239 Standard_Integer aCurDocVersion = 0;
240 TCollection_ExtendedString anInfo;
241 const XmlObjMgt_Element anInfoElem =
242 theElement.GetChildByTagName ("info");
243 if (anInfoElem != NULL) {
244 XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
245 if(aDocVerStr == NULL)
247 else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
248 TCollection_ExtendedString aMsg =
249 TCollection_ExtendedString ("Cannot retrieve the current Document version"
250 " attribute as \"") + aDocVerStr + "\"";
251 if(!aMsgDriver.IsNull())
252 aMsgDriver->Write(aMsg.ToExtString());
255 // oan: OCC22305 - check a document verison and if it's greater than
256 // current version of storage driver set an error status and return
257 if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
259 TCollection_ExtendedString aMsg =
260 TCollection_ExtendedString ("error: wrong file version: ") +
261 aDocVerStr + " while current is " +
262 XmlLDrivers::StorageVersion();
263 myReaderStatus = PCDM_RS_NoVersion;
264 if(!aMsgDriver.IsNull())
265 aMsgDriver->Write(aMsg.ToExtString());
269 if( aCurDocVersion < 2) aCurDocVersion = 2;
271 PropagateDocumentVersion(aCurDocVersion);
273 Standard_Boolean isRef = Standard_False;
274 for (LDOM_Node aNode = anInfoElem.getFirstChild();
275 aNode != NULL; aNode = aNode.getNextSibling()) {
276 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
277 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
280 if(anInfo.Search(REFERENCE_COUNTER) != -1) {
283 TCollection_AsciiString anInf(anInfo,'?');
284 //Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
285 //theNewDocument->SetReferenceCounter(aRefCounter);
287 catch (Standard_Failure) {
288 // cout << "warning: could not read the reference counter in " << aFileName << endl;
289 TCollection_ExtendedString aMsg("Warning: ");
290 aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
291 if(!aMsgDriver.IsNull())
292 aMsgDriver->Write(aMsg.ToExtString());
296 if(anInfo == END_REF)
297 isRef = Standard_False;
298 if(isRef) { // Process References
300 Standard_Integer pos=anInfo.Search(" ");
302 // Parce RefId, DocumentVersion and FileName
303 Standard_Integer aRefId;
304 TCollection_ExtendedString aFileName;
305 Standard_Integer aDocumentVersion;
308 TCollection_ExtendedString aRest=anInfo.Split(pos);
309 aRefId = UTL::IntegerValue(anInfo);
311 Standard_Integer pos2 = aRest.Search(" ");
313 aFileName = aRest.Split(pos2);
314 aDocumentVersion = UTL::IntegerValue(aRest);
316 TCollection_AsciiString aPath = UTL::CString(aFileName);
317 TCollection_AsciiString anAbsolutePath;
318 if(!anAbsoluteDirectory.IsEmpty()) {
319 anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
320 if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
322 if(!aMsgDriver.IsNull()) {
323 // cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
324 TCollection_ExtendedString aMsg("Warning: ");
325 aMsg = aMsg.Cat("reference found; ReferenceIdentifier: ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
326 aMsgDriver->Write(aMsg.ToExtString());
330 TCollection_ExtendedString theFolder,theName;
331 //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
332 TCollection_ExtendedString f(aPath);
335 Standard_Integer i= f.SearchFromEnd("/");
336 TCollection_ExtendedString n = f.Split(i);
337 f.Trunc(f.Length()-1);
341 OSD_Path p = UTL::Path(f);
342 Standard_ExtCharacter chr;
343 TCollection_ExtendedString dir, dirRet, name;
348 for ( int i = 1; i <= dir.Length (); ++i ) {
350 chr = dir.Value ( i );
355 dirRet += _TEXT( "/" );
360 dirRet += _TEXT( ".." );
369 theName = UTL::Name(p); theName+= UTL::Extension(p);
372 Handle(CDM_MetaData) aMetaData = CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
374 theNewDocument->CreateReference(aMetaData,aRefId,
375 theApplication,aDocumentVersion,Standard_False);
382 if(anInfo == START_REF)
383 isRef = Standard_True;
390 TCollection_ExtendedString aComment;
391 const XmlObjMgt_Element aCommentsElem =
392 theElement.GetChildByTagName ("comments");
393 if (aCommentsElem != NULL)
395 for (LDOM_Node aNode = aCommentsElem.getFirstChild();
396 aNode != NULL; aNode = aNode.getNextSibling())
398 if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
400 if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
402 theNewDocument->AddComment(aComment);
408 // 2. Read Shapes section
409 if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);
410 const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
411 if(!aNSDriver.IsNull())
412 ::take_time (0, " +++++ Fin reading Shapes : ", aMsgDriver);
414 // 5. Read document contents
418 #if defined(DEB) && !defined(TAKE_TIMES)
419 TCollection_ExtendedString aMessage ("PasteDocument");
420 aMsgDriver -> Write (aMessage.ToExtString());
422 if (!MakeDocument(theElement, theNewDocument))
423 myReaderStatus = PCDM_RS_MakeFailure;
425 myReaderStatus = PCDM_RS_OK;
427 catch (Standard_Failure)
429 TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
430 aMsgDriver -> Write (anErrorString.ToExtString());
433 // Wipe off the shapes written to the <shapes> section
434 ShapeSetCleaning(aNSDriver);
436 // Clean the relocation table.
437 // If the application needs to use myRelocTable to retrieve additional
438 // data from LDOM, this method should be reimplemented avoiding this step
439 myRelocTable.Clear();
440 ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
443 //=======================================================================
444 //function : MakeDocument
446 //=======================================================================
447 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
448 (const XmlObjMgt_Element& theElement,
449 const Handle(CDM_Document)& theTDoc)
451 Standard_Boolean aResult = Standard_False;
452 Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
453 myRelocTable.Clear();
456 Handle(TDF_Data) aTDF = new TDF_Data();
457 aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
459 TDOC->SetData (aTDF);
460 TDocStd_Owner::SetDocument (aTDF, TDOC);
466 //=======================================================================
467 //function : AttributeDrivers
469 //=======================================================================
470 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
471 (const Handle(CDM_MessageDriver)& theMessageDriver)
473 return XmlLDrivers::AttributeDrivers (theMessageDriver);
476 //=======================================================================
477 //function : take_time
479 //purpose : output astronomical time elapsed
480 //=======================================================================
483 #include <sys/timeb.h>
484 #include <sys/types.h>
487 extern "C" int ftime (struct timeb *tp);
489 extern struct timeb tmbuf0;
491 static void take_time (const Standard_Integer isReset, const char * aHeader,
492 const Handle(CDM_MessageDriver)& aMessageDriver)
496 TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
497 if (isReset) tmbuf0 = tmbuf;
499 char take_tm_buf [64];
500 sprintf (take_tm_buf, "%9.2f s ++++",
501 double(tmbuf.time - tmbuf0.time) +
502 double(tmbuf.millitm - tmbuf0.millitm)/1000.);
503 aMessage += take_tm_buf;
505 aMessageDriver -> Write (aMessage.ToExtString());
509 //=======================================================================
510 //function : PropagateDocumentVersion
512 //=======================================================================
513 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
514 const Standard_Integer theDocVersion )
517 // cout << "DocCurVersion =" << theDocVersion <<endl;
519 XmlMDataStd::SetDocumentVersion(theDocVersion);
522 //=======================================================================
523 //function : ReadShapeSection
524 //purpose : definition of ReadShapeSection
525 //=======================================================================
526 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
527 const XmlObjMgt_Element& /*theElement*/,
528 const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
530 Handle(XmlMDF_ADriver) aDriver;
531 //empty; to be redefined
535 //=======================================================================
536 //function : ShapeSetCleaning
537 //purpose : definition of ShapeSetCleaning
538 //=======================================================================
539 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
540 const Handle(XmlMDF_ADriver)& /*theDriver*/)