Back to index

salome-gui  6.5.0
Public Member Functions | Protected Member Functions | Private Member Functions
QtxResourceMgr::XmlFormat Class Reference

Reader/writer for .xml resources files. More...

Inheritance diagram for QtxResourceMgr::XmlFormat:
Inheritance graph
[legend]
Collaboration diagram for QtxResourceMgr::XmlFormat:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 XmlFormat ()
 Constructor.
 ~XmlFormat ()
 Destructor.

Protected Member Functions

virtual bool load (const QString &, QMap< QString, Section > &)
 Load resources from xml-file.
virtual bool save (const QString &, const QMap< QString, Section > &)
 Save resources to the xml-file.

Private Member Functions

QString docTag () const
 Get document tag name.
QString sectionTag () const
 Get section tag name.
QString parameterTag () const
 Get parameter tag name.
QString importTag () const
 Get import tag name.
QString nameAttribute () const
 Get parameter tag's "name" attribute name.
QString valueAttribute () const
 Get parameter tag's "value" attribute name.
bool load (const QString &, QMap< QString, Section > &, QSet< QString > &)
 Load resources from xml-file.

Detailed Description

Reader/writer for .xml resources files.

Definition at line 671 of file QtxResourceMgr.cxx.


Constructor & Destructor Documentation

Constructor.

Definition at line 695 of file QtxResourceMgr.cxx.

: Format( "xml" )
{
}

Destructor.

Definition at line 703 of file QtxResourceMgr.cxx.

{
}

Member Function Documentation

QString QtxResourceMgr::XmlFormat::docTag ( ) const [private]

Get document tag name.

Returns:
XML document tag name

Definition at line 926 of file QtxResourceMgr.cxx.

{
  QString tag = option( "doc_tag" );
  if ( tag.isEmpty() )
    tag = QString( "document" );
  return tag;
}
QString QtxResourceMgr::XmlFormat::importTag ( ) const [private]

Get import tag name.

Returns:
XML import tag name

Definition at line 962 of file QtxResourceMgr.cxx.

{
  QString tag = option( "import_tag" );
  if ( tag.isEmpty() )
   tag = QString( "import" );
  return tag;
}
bool QtxResourceMgr::XmlFormat::load ( const QString &  fname,
QMap< QString, Section > &  secMap 
) [protected, virtual]

Load resources from xml-file.

Parameters:
fnameresources file name
secMapresources map to be filled in
Returns:
true on success and false on error

Definition at line 713 of file QtxResourceMgr.cxx.

{
  QSet<QString> importHistory;
  return load( fname, secMap, importHistory );
}
bool QtxResourceMgr::XmlFormat::load ( const QString &  fname,
QMap< QString, Section > &  secMap,
QSet< QString > &  importHistory 
) [private]

Load resources from xml-file.

Parameters:
fnameresources file name
secMapresources map to be filled in
importHistorylist of already imported resources files (to prevent import loops)
Returns:
true on success and false on error

Definition at line 726 of file QtxResourceMgr.cxx.

{
  QString aFName = fname.trimmed();
  if ( !QFileInfo( aFName ).exists() )
  {
    if ( QFileInfo( aFName + ".xml" ).exists() )
      aFName += ".xml";
    else if ( QFileInfo( aFName + ".XML" ).exists() )
      aFName += ".XML";
    else
      return false; // file does not exist
  }
  QFileInfo aFinfo( aFName );
  aFName = aFinfo.canonicalFilePath();

  if ( !importHistory.contains(  aFName ) )
    importHistory.insert( aFName );
  else
    return true;   // already imported (prevent import loops)

  bool res = false;

#ifndef QT_NO_DOM

  QFile file( aFName );
  if ( !file.open( QFile::ReadOnly ) )
  {
    qDebug() << "QtxResourceMgr: File is not accessible:" << aFName;
    return false;
  }

  QDomDocument doc;

  res = doc.setContent( &file );
  file.close();

  if ( !res )
  {
    qDebug() << "QtxResourceMgr: File is empty:" << aFName;
    return false;
  }

  QDomElement root = doc.documentElement();
  if ( root.isNull() || root.tagName() != docTag() )
  {
    qDebug() << "QtxResourceMgr: Invalid root in file:" << aFName;
    return false;
  }

  QDomNode sectNode = root.firstChild();
  while ( res && !sectNode.isNull() )
  {
    res = sectNode.isElement();
    if ( res )
    {
      QDomElement sectElem = sectNode.toElement();
      if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
      {
        QString section = sectElem.attribute( nameAttribute() );
        QDomNode paramNode = sectNode.firstChild();
        while ( res && !paramNode.isNull() )
        {
          res = paramNode.isElement();
          if ( res )
          {
            QDomElement paramElem = paramNode.toElement();
            if ( paramElem.tagName() == parameterTag() &&
                 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
            {
              QString paramName = paramElem.attribute( nameAttribute() );
              QString paramValue = paramElem.attribute( valueAttribute() );
              secMap[section].insert( paramName, paramValue );
            }
            else
            {
              qDebug() << "QtxResourceMgr: Invalid parameter element in file:" << aFName;
              res = false;
            }
          }
          else
          {
            res = paramNode.isComment();
            if ( !res )
              qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
          }

          paramNode = paramNode.nextSibling();
        }
      }
      else if ( sectElem.tagName() == importTag() && sectElem.hasAttribute( nameAttribute() ) )
      {
         QString impFile = QDir::convertSeparators( Qtx::makeEnvVarSubst( sectElem.attribute( nameAttribute() ), Qtx::Always ) );
             QFileInfo impFInfo( impFile );
             if ( impFInfo.isRelative() )
                impFInfo.setFile( aFinfo.absoluteDir(), impFile );

        QMap<QString, Section> impMap;
        if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
            {
            qDebug() << "QtxResourceMgr: Error with importing file:" << sectElem.attribute( nameAttribute() );
            }
            else
            {
                QMap<QString, Section>::const_iterator it = impMap.constBegin();
                for ( ; it != impMap.constEnd() ; ++it )
                {
                   if ( !secMap.contains( it.key() ) )
                   {
                      // insert full section
                      secMap.insert( it.key(), it.value() );
                   }
                   else
                   {
                      // insert all parameters from the section
                      Section::ConstIterator paramIt = it.value().begin();
                      for ( ; paramIt != it.value().end() ; ++paramIt )
                      {
                             if ( !secMap[it.key()].contains( paramIt.key() ) )
                                secMap[it.key()].insert( paramIt.key(), paramIt.value() );
                      }
                   }
                }
         }
      }
      else
      {
         qDebug() << "QtxResourceMgr: Invalid section in file:" << aFName;
         res = false;
      }
    }
    else
    {
      res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
      if ( !res )
        qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
    }

    sectNode = sectNode.nextSibling();
  }

#endif
  
  if ( res )
    qDebug() << "QtxResourceMgr: File" << fname << "is loaded successfully";
  return res;
}
QString QtxResourceMgr::XmlFormat::nameAttribute ( ) const [private]

Get parameter tag's "name" attribute name.

Returns:
XML parameter tag's "name" attribute name

Definition at line 974 of file QtxResourceMgr.cxx.

{
  QString str = option( "name_attribute" );
  if ( str.isEmpty() )
    str = QString( "name" );
  return str;
}
QString QtxResourceMgr::XmlFormat::parameterTag ( ) const [private]

Get parameter tag name.

Returns:
XML parameter tag name

Definition at line 950 of file QtxResourceMgr.cxx.

{
  QString tag = option( "parameter_tag" );
  if ( tag.isEmpty() )
    tag = QString( "parameter" );
  return tag;
}
bool QtxResourceMgr::XmlFormat::save ( const QString &  fname,
const QMap< QString, Section > &  secMap 
) [protected, virtual]

Save resources to the xml-file.

Parameters:
fnameresources file name
secMapresources map
Returns:
true on success and false on error

Definition at line 879 of file QtxResourceMgr.cxx.

{
  bool res = false;

#ifndef QT_NO_DOM

  if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
    return false;

  QFile file( fname );
  if ( !file.open( QFile::WriteOnly ) )
    return false;

  QDomDocument doc( docTag() );
  QDomElement root = doc.createElement( docTag() );
  doc.appendChild( root );

  for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
  {
    QDomElement sect = doc.createElement( sectionTag() );
    sect.setAttribute( nameAttribute(), it.key() );
    root.appendChild( sect );
    for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
    {
      QDomElement val = doc.createElement( parameterTag() );
      val.setAttribute( nameAttribute(), iter.key() );
      val.setAttribute( valueAttribute(), iter.value() );
      sect.appendChild( val );
    }
  }

  QTextStream ts( &file );
  QStringList docStr = doc.toString().split( "\n" );
  for ( QStringList::ConstIterator itr = docStr.begin(); itr != docStr.end(); ++itr )
    ts << *itr << endl;

  file.close();

#endif

  return res;
}
QString QtxResourceMgr::XmlFormat::sectionTag ( ) const [private]

Get section tag name.

Returns:
XML section tag name

Definition at line 938 of file QtxResourceMgr.cxx.

{
  QString tag = option( "section_tag" );
  if ( tag.isEmpty() )
    tag = QString( "section" );
  return tag;
}
QString QtxResourceMgr::XmlFormat::valueAttribute ( ) const [private]

Get parameter tag's "value" attribute name.

Returns:
XML parameter tag's "value" attribute name

Definition at line 986 of file QtxResourceMgr.cxx.

{
  QString str = option( "value_attribute" );
  if ( str.isEmpty() )
    str = QString( "value" );
  return str;
}

The documentation for this class was generated from the following file: