QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QString& filePath ):
    mXMLDoc( xmlDoc ), mProjectPath( filePath )
{
  //accelerate the search for layers, groups and the creation of annotation items
  if ( mXMLDoc )
  {
    QDomNodeList layerNodeList = mXMLDoc->elementsByTagName( "maplayer" );
    QDomElement currentElement;
    int nNodes = layerNodeList.size();
    for ( int i = 0; i < nNodes; ++i )
    {
      currentElement = layerNodeList.at( i ).toElement();
      mProjectLayerElements.push_back( currentElement );
      mProjectLayerElementsByName.insert( layerName( currentElement ), currentElement );
      mProjectLayerElementsById.insert( layerId( currentElement ), currentElement );
    }

    QDomElement legendElement = mXMLDoc->documentElement().firstChildElement( "legend" );
    if ( !legendElement.isNull() )
    {
      QDomNodeList groupNodeList = legendElement.elementsByTagName( "legendgroup" );
      for ( int i = 0; i < groupNodeList.size(); ++i )
      {
        mLegendGroupElements.push_back( groupNodeList.at( i ).toElement() );
      }
    }

    mRestrictedLayers = findRestrictedLayers();
  }
}
QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QString& filePath )
    : mXMLDoc( xmlDoc )
    , mProjectPath( filePath )
    , mUseLayerIDs( false )
{
  //accelerate the search for layers, groups and the creation of annotation items
  if ( mXMLDoc )
  {
    QDomNodeList layerNodeList = mXMLDoc->elementsByTagName( "maplayer" );
    QDomElement currentElement;
    int nNodes = layerNodeList.size();
    for ( int i = 0; i < nNodes; ++i )
    {
      currentElement = layerNodeList.at( i ).toElement();
      mProjectLayerElements.push_back( currentElement );
      mProjectLayerElementsByName.insert( layerName( currentElement ), currentElement );
      mProjectLayerElementsById.insert( layerId( currentElement ), currentElement );
    }

    QDomElement legendElement = mXMLDoc->documentElement().firstChildElement( "legend" );
    if ( !legendElement.isNull() )
    {
      QDomNodeList groupNodeList = legendElement.elementsByTagName( "legendgroup" );
      for ( int i = 0; i < groupNodeList.size(); ++i )
      {
        mLegendGroupElements.push_back( groupNodeList.at( i ).toElement() );
      }
    }

    mUseLayerIDs = findUseLayerIDs();
    mRestrictedLayers = findRestrictedLayers();

    mCustomLayerOrder.clear();

    QDomElement customOrder = mXMLDoc->documentElement().firstChildElement( "layer-tree-canvas" ).firstChildElement( "custom-order" );
    if ( customOrder.attribute( "enabled" ) == "1" )
    {
      QDomNodeList items = customOrder.childNodes();
      for ( int i = 0; i < items.size(); ++i )
      {
        mCustomLayerOrder << items.item( i ).toElement().text();
      }
    }
  }
  // Setting the QgsProject instance fileName
  // to help converting relative pathes to absolute
  if ( mProjectPath != "" )
  {
    QgsProject::instance()->setFileName( mProjectPath );
  }
}