fae4fa1f27d508bf82e60fbdea44f6ef92da4c56
[profile/ivi/GUPnP.git] / libgupnp / gupnp-xml-doc.c
1 /*
2  * Copyright (C) 2006, 2007 OpenedHand Ltd.
3  * Copyright (C) 2009 Nokia Corporation.
4  *
5  * Author: Jorn Baayen <jorn@openedhand.com>
6  *         Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7  *                               <zeeshan.ali@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 /**
26  * SECTION:gupnp-xml-doc
27  * @short_description: GObject wrapper for xmlDoc.
28  *
29  * GObject wrapper for xmlDoc, so that we can use refcounting and weak
30  * references.
31  */
32
33 #include <string.h>
34 #include "gupnp-xml-doc.h"
35 #include "gupnp-error.h"
36
37 G_DEFINE_TYPE (GUPnPXMLDoc,
38                gupnp_xml_doc,
39                G_TYPE_OBJECT);
40
41 static void
42 gupnp_xml_doc_init (G_GNUC_UNUSED GUPnPXMLDoc *doc)
43 {
44         /* Empty */
45 }
46
47 static void
48 gupnp_xml_doc_finalize (GObject *object)
49 {
50         GUPnPXMLDoc *doc;
51
52         doc = GUPNP_XML_DOC (object);
53
54         xmlFreeDoc (doc->doc);
55
56         G_OBJECT_CLASS (gupnp_xml_doc_parent_class)->finalize (object);
57 }
58
59 static void
60 gupnp_xml_doc_class_init (GUPnPXMLDocClass *klass)
61 {
62         GObjectClass *object_class;
63
64         object_class = G_OBJECT_CLASS (klass);
65
66         object_class->finalize = gupnp_xml_doc_finalize;
67 }
68
69 /**
70  * gupnp_xml_doc_new:
71  * @xml_doc: Pointer to #xmlDoc to wrap under this object
72  *
73  * Create a new #GUPnPXMLDoc for @xml_doc.
74  *
75  * Return value: A new #GUPnPXMLDoc, or %NULL on an error
76  **/
77 GUPnPXMLDoc *
78 gupnp_xml_doc_new (xmlDoc *xml_doc)
79 {
80         GUPnPXMLDoc *doc;
81
82         g_return_val_if_fail (xml_doc != NULL, NULL);
83
84         doc = g_object_new (GUPNP_TYPE_XML_DOC, NULL);
85
86         doc->doc = xml_doc;
87
88         return doc;
89 }
90
91 /**
92  * gupnp_xml_doc_new_from_path:
93  * @path: Path to xml document
94  * @error: Location to put the error into
95  *
96  * Create a new #GUPnPXMLDoc for the XML document at @path.
97  *
98  * Return value: A new #GUPnPXMLDoc, or %NULL on an error
99  **/
100 GUPnPXMLDoc *
101 gupnp_xml_doc_new_from_path (const char *path,
102                              GError    **error)
103 {
104         xmlDoc *doc;
105         int flags;
106
107         flags = XML_PARSE_PEDANTIC;
108
109         if (!g_getenv ("GUPNP_DEBUG")) {
110                 flags |= XML_PARSE_NOWARNING | XML_PARSE_NOERROR;
111         }
112
113         g_return_val_if_fail (path != NULL, NULL);
114         doc = xmlReadFile (path, NULL, flags);
115         if (doc == NULL) {
116                 g_set_error (error,
117                              GUPNP_XML_ERROR,
118                              GUPNP_XML_ERROR_PARSE,
119                              "Failed to parse %s\n",
120                              path);
121
122                 return NULL;
123         }
124
125         return gupnp_xml_doc_new (doc);
126 }