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 00020 #ifndef OSGEARTH_XML_UTILS_H 00021 #define OSGEARTH_XML_UTILS_H 00022 00023 #include <osgEarth/Common> 00024 #include <osgEarth/Config> 00025 #include <osgEarth/URI> 00026 #include <osg/Referenced> 00027 #include <osg/ref_ptr> 00028 #include <string> 00029 #include <vector> 00030 #include <map> 00031 #include <stack> 00032 00033 // XML support utilites. 00034 00035 namespace osgEarth 00036 { 00037 extern std::string trim( const std::string& in ); 00038 00039 class OSGEARTH_EXPORT XmlNode : public osg::Referenced 00040 { 00041 public: 00042 XmlNode(); 00043 00044 virtual ~XmlNode() { } 00045 00046 virtual bool isElement() const =0; 00047 00048 virtual bool isText() const =0; 00049 }; 00050 00051 typedef std::vector<osg::ref_ptr<XmlNode> > XmlNodeList; 00052 00053 typedef std::map<std::string,std::string> XmlAttributes; 00054 00055 class OSGEARTH_EXPORT XmlElement : public XmlNode 00056 { 00057 public: 00058 XmlElement( const std::string& name ); 00059 00060 XmlElement( const std::string& name, const XmlAttributes& attrs ); 00061 00062 XmlElement( const Config& conf ); 00063 00064 virtual ~XmlElement() { } 00065 00066 const std::string& getName() const; 00067 00068 void setName( const std::string& name ); 00069 00070 XmlAttributes& getAttrs(); 00071 00072 const XmlAttributes& getAttrs() const; 00073 00074 const std::string& getAttr( const std::string& key ) const; 00075 00076 XmlNodeList& getChildren(); 00077 00078 const XmlNodeList& getChildren() const; 00079 00080 XmlElement* getSubElement( const std::string& name ) const; 00081 00082 XmlNodeList getSubElements( const std::string& name ) const; 00083 00084 std::string getText() const; 00085 00086 std::string getSubElementText( const std::string& tag ) const; 00087 00088 void addSubElement(const std::string& tag, const std::string& text); 00089 00090 void addSubElement(const std::string& tag, const Properties& attrs, const std::string& text); 00091 00092 virtual Config getConfig() const; 00093 00094 public: // XmlNode 00095 virtual bool isElement() const { return true; } 00096 00097 virtual bool isText() const { return false; } 00098 00099 private: 00100 std::string name; 00101 XmlAttributes attrs; 00102 XmlNodeList children; 00103 }; 00104 00105 typedef std::vector<osg::ref_ptr<XmlElement> > XmlElementList; 00106 00107 typedef std::stack<osg::ref_ptr<XmlElement> > XmlElementStack; 00108 00109 typedef std::stack<XmlElement*> XmlElementNoRefStack; 00110 00111 class OSGEARTH_EXPORT XmlText : public XmlNode 00112 { 00113 public: 00114 XmlText( const std::string& value ); 00115 00116 virtual ~XmlText() { } 00117 00118 const std::string& getValue() const; 00119 00120 public: // XmlNode 00121 virtual bool isElement() const { return false; } 00122 00123 virtual bool isText() const { return true; } 00124 00125 private: 00126 std::string value; 00127 }; 00128 00129 class OSGEARTH_EXPORT XmlDocument : public XmlElement 00130 { 00131 public: 00132 XmlDocument(); 00133 00134 XmlDocument( const Config& conf ); 00135 00136 virtual ~XmlDocument(); 00137 00138 static XmlDocument* load( const std::string& location ); 00139 00140 static XmlDocument* load( const URI& uri ); 00141 00142 static XmlDocument* load( std::istream& in, const URIContext& context =URIContext() ); 00143 00144 void store( std::ostream& out ) const; 00145 00146 const std::string& getName() const; 00147 00148 virtual Config getConfig() const; 00149 00150 protected: 00151 URI _sourceURI; 00152 std::string _name; 00153 osg::ref_ptr<XmlElement> _root; 00154 }; 00155 } 00156 00157 #endif // OSGEARTH_XML_UTILS_H