| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: _xml.i |
| 3 | // Purpose: SWIG interface for other wxXml classes |
| 4 | // |
| 5 | // Author: Robin Dunn |
| 6 | // |
| 7 | // Created: 4-June-2001 |
| 8 | // RCS-ID: $Id$ |
| 9 | // Copyright: (c) 2003 by Total Control Software |
| 10 | // Licence: wxWindows license |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | // Not a %module |
| 14 | |
| 15 | |
| 16 | //--------------------------------------------------------------------------- |
| 17 | %newgroup |
| 18 | |
| 19 | |
| 20 | // In order to provide wrappers for wxXmlResourceHandler we need to also |
| 21 | // provide the classes for representing and parsing XML. |
| 22 | |
| 23 | |
| 24 | // Represents XML node type. |
| 25 | enum wxXmlNodeType |
| 26 | { |
| 27 | // note: values are synchronized with xmlElementType from libxml |
| 28 | wxXML_ELEMENT_NODE, |
| 29 | wxXML_ATTRIBUTE_NODE, |
| 30 | wxXML_TEXT_NODE, |
| 31 | wxXML_CDATA_SECTION_NODE, |
| 32 | wxXML_ENTITY_REF_NODE, |
| 33 | wxXML_ENTITY_NODE, |
| 34 | wxXML_PI_NODE, |
| 35 | wxXML_COMMENT_NODE, |
| 36 | wxXML_DOCUMENT_NODE, |
| 37 | wxXML_DOCUMENT_TYPE_NODE, |
| 38 | wxXML_DOCUMENT_FRAG_NODE, |
| 39 | wxXML_NOTATION_NODE, |
| 40 | wxXML_HTML_DOCUMENT_NODE |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | |
| 45 | // Represents node property(ies). |
| 46 | // Example: in <img src="hello.gif" id="3"/> "src" is property with value |
| 47 | // "hello.gif" and "id" is property with value "3". |
| 48 | class wxXmlProperty |
| 49 | { |
| 50 | public: |
| 51 | wxXmlProperty(const wxString& name = wxPyEmptyString, |
| 52 | const wxString& value = wxPyEmptyString, |
| 53 | wxXmlProperty *next = NULL); |
| 54 | |
| 55 | wxString GetName() const; |
| 56 | wxString GetValue() const; |
| 57 | wxXmlProperty *GetNext() const; |
| 58 | |
| 59 | void SetName(const wxString& name); |
| 60 | void SetValue(const wxString& value); |
| 61 | void SetNext(wxXmlProperty *next); |
| 62 | |
| 63 | %property(Name, GetName, SetName, doc="See `GetName` and `SetName`"); |
| 64 | %property(Next, GetNext, SetNext, doc="See `GetNext` and `SetNext`"); |
| 65 | %property(Value, GetValue, SetValue, doc="See `GetValue` and `SetValue`"); |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | // Represents node in XML document. Node has name and may have content |
| 72 | // and properties. Most common node types are wxXML_TEXT_NODE (name and props |
| 73 | // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is |
| 74 | // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE |
| 75 | // with content="hi"). |
| 76 | // |
| 77 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load |
| 78 | // (default is UTF-8). |
| 79 | class wxXmlNode |
| 80 | { |
| 81 | public: |
| 82 | wxXmlNode(wxXmlNode *parent = NULL, |
| 83 | wxXmlNodeType type = 0, |
| 84 | const wxString& name = wxPyEmptyString, |
| 85 | const wxString& content = wxPyEmptyString, |
| 86 | wxXmlProperty *props = NULL, |
| 87 | wxXmlNode *next = NULL); |
| 88 | ~wxXmlNode(); |
| 89 | |
| 90 | |
| 91 | // user-friendly creation: |
| 92 | %RenameCtor(XmlNodeEasy, wxXmlNode(wxXmlNodeType type, const wxString& name, |
| 93 | const wxString& content = wxPyEmptyString)); |
| 94 | |
| 95 | void AddChild(wxXmlNode *child); |
| 96 | bool InsertChild(wxXmlNode *child, wxXmlNode *before_node); |
| 97 | bool RemoveChild(wxXmlNode *child); |
| 98 | void AddProperty(wxXmlProperty *prop); |
| 99 | %Rename(AddPropertyName, void, AddProperty(const wxString& name, const wxString& value)); |
| 100 | bool DeleteProperty(const wxString& name); |
| 101 | |
| 102 | // access methods: |
| 103 | wxXmlNodeType GetType() const; |
| 104 | wxString GetName() const; |
| 105 | wxString GetContent() const; |
| 106 | |
| 107 | bool IsWhitespaceOnly() const; |
| 108 | int GetDepth(wxXmlNode *grandparent = NULL) const; |
| 109 | |
| 110 | // Gets node content from wxXML_ENTITY_NODE |
| 111 | // The problem is, <tag>content<tag> is represented as |
| 112 | // wxXML_ENTITY_NODE name="tag", content="" |
| 113 | // |-- wxXML_TEXT_NODE or |
| 114 | // wxXML_CDATA_SECTION_NODE name="" content="content" |
| 115 | wxString GetNodeContent() const; |
| 116 | |
| 117 | wxXmlNode *GetParent() const; |
| 118 | wxXmlNode *GetNext() const; |
| 119 | wxXmlNode *GetChildren() const; |
| 120 | |
| 121 | wxXmlProperty *GetProperties() const; |
| 122 | wxString GetPropVal(const wxString& propName, |
| 123 | const wxString& defaultVal) const; |
| 124 | bool HasProp(const wxString& propName) const; |
| 125 | |
| 126 | void SetType(wxXmlNodeType type); |
| 127 | void SetName(const wxString& name); |
| 128 | void SetContent(const wxString& con); |
| 129 | |
| 130 | void SetParent(wxXmlNode *parent); |
| 131 | void SetNext(wxXmlNode *next); |
| 132 | void SetChildren(wxXmlNode *child); |
| 133 | |
| 134 | void SetProperties(wxXmlProperty *prop); |
| 135 | |
| 136 | %property(Children, GetChildren, SetChildren, doc="See `GetChildren` and `SetChildren`"); |
| 137 | %property(Content, GetContent, SetContent, doc="See `GetContent` and `SetContent`"); |
| 138 | %property(Name, GetName, SetName, doc="See `GetName` and `SetName`"); |
| 139 | %property(Next, GetNext, SetNext, doc="See `GetNext` and `SetNext`"); |
| 140 | %property(Parent, GetParent, SetParent, doc="See `GetParent` and `SetParent`"); |
| 141 | %property(Properties, GetProperties, SetProperties, doc="See `GetProperties` and `SetProperties`"); |
| 142 | %property(Type, GetType, SetType, doc="See `GetType` and `SetType`"); |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | |
| 147 | // special indentation value for wxXmlDocument::Save |
| 148 | enum { |
| 149 | wxXML_NO_INDENTATION |
| 150 | }; |
| 151 | |
| 152 | // flags for wxXmlDocument::Load |
| 153 | enum wxXmlDocumentLoadFlag |
| 154 | { |
| 155 | wxXMLDOC_NONE = 0, |
| 156 | wxXMLDOC_KEEP_WHITESPACE_NODES = 1 |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | |
| 161 | // This class holds XML data/document as parsed by XML parser. |
| 162 | class wxXmlDocument : public wxObject |
| 163 | { |
| 164 | public: |
| 165 | wxXmlDocument(const wxString& filename, |
| 166 | const wxString& encoding = wxPyUTF8String); |
| 167 | %RenameCtor(XmlDocumentFromStream, wxXmlDocument(wxInputStream& stream, |
| 168 | const wxString& encoding = wxPyUTF8String)); |
| 169 | %RenameCtor(EmptyXmlDocument, wxXmlDocument()); |
| 170 | |
| 171 | ~wxXmlDocument(); |
| 172 | |
| 173 | |
| 174 | // Parses .xml file and loads data. Returns True on success, False |
| 175 | // otherwise. |
| 176 | bool Load(const wxString& filename, |
| 177 | const wxString& encoding = wxPyUTF8String, |
| 178 | int flags = wxXMLDOC_NONE); |
| 179 | %Rename(LoadFromStream, bool, Load(wxInputStream& stream, |
| 180 | const wxString& encoding = wxPyUTF8String, |
| 181 | int flags = wxXMLDOC_NONE)); |
| 182 | |
| 183 | // Saves document as .xml file. |
| 184 | bool Save(const wxString& filename, int indentstep=1) const; |
| 185 | %Rename(SaveToStream, bool, Save(wxOutputStream& stream, int indentstep=1) const); |
| 186 | |
| 187 | bool IsOk() const; |
| 188 | |
| 189 | // Returns root node of the document. |
| 190 | wxXmlNode *GetRoot() const; |
| 191 | |
| 192 | // Returns version of document (may be empty). |
| 193 | wxString GetVersion() const; |
| 194 | |
| 195 | // Returns encoding of document (may be empty). |
| 196 | // Note: this is the encoding original file was saved in, *not* the |
| 197 | // encoding of in-memory representation! |
| 198 | wxString GetFileEncoding() const; |
| 199 | |
| 200 | // Write-access methods: |
| 201 | wxXmlNode *DetachRoot(); |
| 202 | void SetRoot(wxXmlNode *node); |
| 203 | void SetVersion(const wxString& version); |
| 204 | void SetFileEncoding(const wxString& encoding); |
| 205 | |
| 206 | // %extend { |
| 207 | // // Returns encoding of in-memory representation of the document (same |
| 208 | // // as passed to Load or ctor, defaults to UTF-8). NB: this is |
| 209 | // // meaningless in Unicode build where data are stored as wchar_t* |
| 210 | // wxString GetEncoding() { |
| 211 | // %#if wxUSE_UNICODE |
| 212 | // return wxPyEmptyString; |
| 213 | // %#else |
| 214 | // return self->GetEncoding(); |
| 215 | // %#endif |
| 216 | // } |
| 217 | // void SetEncoding(const wxString& enc) { |
| 218 | // %#if wxUSE_UNICODE |
| 219 | // // do nothing |
| 220 | // %#else |
| 221 | // self->SetEncoding(enc); |
| 222 | // %#endif |
| 223 | // } |
| 224 | // } |
| 225 | |
| 226 | %property(FileEncoding, GetFileEncoding, SetFileEncoding, doc="See `GetFileEncoding` and `SetFileEncoding`"); |
| 227 | %property(Root, GetRoot, SetRoot, doc="See `GetRoot` and `SetRoot`"); |
| 228 | %property(Version, GetVersion, SetVersion, doc="See `GetVersion` and `SetVersion`"); |
| 229 | }; |
| 230 | |
| 231 | //--------------------------------------------------------------------------- |
| 232 | //--------------------------------------------------------------------------- |