| 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 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | // Represents node in XML document. Node has name and may have content |
| 68 | // and properties. Most common node types are wxXML_TEXT_NODE (name and props |
| 69 | // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is |
| 70 | // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE |
| 71 | // with content="hi"). |
| 72 | // |
| 73 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load |
| 74 | // (default is UTF-8). |
| 75 | class wxXmlNode |
| 76 | { |
| 77 | public: |
| 78 | wxXmlNode(wxXmlNode *parent = NULL, |
| 79 | wxXmlNodeType type = 0, |
| 80 | const wxString& name = wxPyEmptyString, |
| 81 | const wxString& content = wxPyEmptyString, |
| 82 | wxXmlProperty *props = NULL, |
| 83 | wxXmlNode *next = NULL); |
| 84 | ~wxXmlNode(); |
| 85 | |
| 86 | |
| 87 | // user-friendly creation: |
| 88 | %RenameCtor(XmlNodeEasy, wxXmlNode(wxXmlNodeType type, const wxString& name, |
| 89 | const wxString& content = wxPyEmptyString)); |
| 90 | |
| 91 | void AddChild(wxXmlNode *child); |
| 92 | void InsertChild(wxXmlNode *child, wxXmlNode *before_node); |
| 93 | bool RemoveChild(wxXmlNode *child); |
| 94 | void AddProperty(wxXmlProperty *prop); |
| 95 | %Rename(AddPropertyName, void, AddProperty(const wxString& name, const wxString& value)); |
| 96 | bool DeleteProperty(const wxString& name); |
| 97 | |
| 98 | // access methods: |
| 99 | wxXmlNodeType GetType() const; |
| 100 | wxString GetName() const; |
| 101 | wxString GetContent() const; |
| 102 | |
| 103 | wxXmlNode *GetParent() const; |
| 104 | wxXmlNode *GetNext() const; |
| 105 | wxXmlNode *GetChildren() const; |
| 106 | |
| 107 | wxXmlProperty *GetProperties() const; |
| 108 | wxString GetPropVal(const wxString& propName, |
| 109 | const wxString& defaultVal) const; |
| 110 | bool HasProp(const wxString& propName) const; |
| 111 | |
| 112 | void SetType(wxXmlNodeType type); |
| 113 | void SetName(const wxString& name); |
| 114 | void SetContent(const wxString& con); |
| 115 | |
| 116 | void SetParent(wxXmlNode *parent); |
| 117 | void SetNext(wxXmlNode *next); |
| 118 | void SetChildren(wxXmlNode *child); |
| 119 | |
| 120 | void SetProperties(wxXmlProperty *prop); |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | |
| 125 | // This class holds XML data/document as parsed by XML parser. |
| 126 | class wxXmlDocument : public wxObject |
| 127 | { |
| 128 | public: |
| 129 | wxXmlDocument(const wxString& filename, |
| 130 | const wxString& encoding = wxPyUTF8String); |
| 131 | %RenameCtor(XmlDocumentFromStream, wxXmlDocument(wxInputStream& stream, |
| 132 | const wxString& encoding = wxPyUTF8String)); |
| 133 | %RenameCtor(EmptyXmlDocument, wxXmlDocument()); |
| 134 | |
| 135 | ~wxXmlDocument(); |
| 136 | |
| 137 | |
| 138 | // Parses .xml file and loads data. Returns True on success, False |
| 139 | // otherwise. |
| 140 | bool Load(const wxString& filename, |
| 141 | const wxString& encoding = wxPyUTF8String); |
| 142 | %Rename(LoadFromStream, bool, Load(wxInputStream& stream, |
| 143 | const wxString& encoding = wxPyUTF8String)); |
| 144 | |
| 145 | // Saves document as .xml file. |
| 146 | bool Save(const wxString& filename) const; |
| 147 | %Rename(SaveToStream, bool, Save(wxOutputStream& stream) const); |
| 148 | |
| 149 | bool IsOk() const; |
| 150 | |
| 151 | // Returns root node of the document. |
| 152 | wxXmlNode *GetRoot() const; |
| 153 | |
| 154 | // Returns version of document (may be empty). |
| 155 | wxString GetVersion() const; |
| 156 | |
| 157 | // Returns encoding of document (may be empty). |
| 158 | // Note: this is the encoding original file was saved in, *not* the |
| 159 | // encoding of in-memory representation! |
| 160 | wxString GetFileEncoding() const; |
| 161 | |
| 162 | // Write-access methods: |
| 163 | void SetRoot(wxXmlNode *node); |
| 164 | void SetVersion(const wxString& version); |
| 165 | void SetFileEncoding(const wxString& encoding); |
| 166 | |
| 167 | // %extend { |
| 168 | // // Returns encoding of in-memory representation of the document (same |
| 169 | // // as passed to Load or ctor, defaults to UTF-8). NB: this is |
| 170 | // // meaningless in Unicode build where data are stored as wchar_t* |
| 171 | // wxString GetEncoding() { |
| 172 | // %#if wxUSE_UNICODE |
| 173 | // return wxPyEmptyString; |
| 174 | // %#else |
| 175 | // return self->GetEncoding(); |
| 176 | // %#endif |
| 177 | // } |
| 178 | // void SetEncoding(const wxString& enc) { |
| 179 | // %#if wxUSE_UNICODE |
| 180 | // // do nothing |
| 181 | // %#else |
| 182 | // self->SetEncoding(enc); |
| 183 | // %#endif |
| 184 | // } |
| 185 | // } |
| 186 | }; |
| 187 | |
| 188 | //--------------------------------------------------------------------------- |
| 189 | //--------------------------------------------------------------------------- |