osgEarth 2.1.1
|
00001 /* -*-c++-*- */ 00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph 00003 * Copyright 2008-2010 Pelican Mapping 00004 * http://osgearth.org 00005 * 00006 * osgEarth is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/> 00018 */ 00019 #include <osgEarth/Config> 00020 #include <osgEarth/XmlUtils> 00021 #include <sstream> 00022 #include <iomanip> 00023 00024 using namespace osgEarth; 00025 00026 Config& emptyConfig() 00027 { 00028 static Config _emptyConfig; 00029 return _emptyConfig; 00030 } 00031 00032 void 00033 Config::setURIContext( const URIContext& context ) 00034 { 00035 _uriContext = context; 00036 for( ConfigSet::iterator i = _children.begin(); i != _children.end(); i++ ) 00037 { 00038 i->setURIContext( context.add(i->_uriContext) ); 00039 //URI newURI( i->uriContext(), context ); 00040 //i->setURIContext( *newURI ); 00041 } 00042 } 00043 00044 bool 00045 Config::loadXML( std::istream& in ) 00046 { 00047 osg::ref_ptr<XmlDocument> xml = XmlDocument::load( in ); 00048 if ( xml.valid() ) 00049 *this = xml->getConfig(); 00050 return xml.valid(); 00051 } 00052 00053 const Config& 00054 Config::child( const std::string& childName ) const 00055 { 00056 for( ConfigSet::const_iterator i = _children.begin(); i != _children.end(); i++ ) { 00057 if ( i->key() == childName ) 00058 return *i; 00059 } 00060 return emptyConfig(); 00061 } 00062 00063 void 00064 Config::merge( const Config& rhs ) 00065 { 00066 for( Properties::const_iterator a = rhs._attrs.begin(); a != rhs._attrs.end(); ++a ) 00067 _attrs[ a->first ] = a->second; 00068 00069 for( ConfigSet::const_iterator c = rhs._children.begin(); c != rhs._children.end(); ++c ) 00070 addChild( *c ); 00071 } 00072 00073 std::string 00074 Config::toString( int indent ) const 00075 { 00076 std::stringstream buf; 00077 buf << std::fixed; 00078 for( int i=0; i<indent; i++ ) buf << " "; 00079 buf << "{ " << (_key.empty()? "anonymous" : _key) << ": "; 00080 if ( !_defaultValue.empty() ) buf << _defaultValue; 00081 if ( !_attrs.empty() ) { 00082 buf << std::endl; 00083 for( int i=0; i<indent+1; i++ ) buf << " "; 00084 buf << "attrs: [ "; 00085 for( Properties::const_iterator a = _attrs.begin(); a != _attrs.end(); a++ ) 00086 buf << a->first << "=" << a->second << ", "; 00087 buf << " ]"; 00088 } 00089 if ( !_children.empty() ) { 00090 for( ConfigSet::const_iterator c = _children.begin(); c != _children.end(); c++ ) 00091 buf << std::endl << (*c).toString( indent+1 ); 00092 } 00093 00094 buf << " }"; 00095 00096 std::string bufStr; 00097 bufStr = buf.str(); 00098 return bufStr; 00099 } 00100 00101 std::string 00102 Config::toHashString() const 00103 { 00104 std::stringstream buf; 00105 buf << std::fixed; 00106 buf << "{" << (_key.empty()? "anonymous" : _key) << ":"; 00107 if ( !_defaultValue.empty() ) buf << _defaultValue; 00108 if ( !_attrs.empty() ) { 00109 buf << "["; 00110 for( Properties::const_iterator a = _attrs.begin(); a != _attrs.end(); a++ ) 00111 buf << a->first << "=" << a->second << ","; 00112 buf << "]"; 00113 } 00114 if ( !_children.empty() ) { 00115 for( ConfigSet::const_iterator c = _children.begin(); c != _children.end(); c++ ) 00116 buf << (*c).toHashString(); 00117 } 00118 00119 buf << "}"; 00120 00121 std::string bufStr; 00122 bufStr = buf.str(); 00123 return bufStr; 00124 }