Imported Upstream version 0.20.12
[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  * Since: 0.13.0
33  */
34
35 #include <string.h>
36 #include "gupnp-xml-doc.h"
37 #include "gupnp-error.h"
38
39 G_DEFINE_TYPE (GUPnPXMLDoc,
40                gupnp_xml_doc,
41                G_TYPE_OBJECT);
42
43 static void
44 gupnp_xml_doc_init (G_GNUC_UNUSED GUPnPXMLDoc *doc)
45 {
46         /* Empty */
47 }
48
49 static void
50 gupnp_xml_doc_finalize (GObject *object)
51 {
52         GUPnPXMLDoc *doc;
53
54         doc = GUPNP_XML_DOC (object);
55
56         xmlFreeDoc (doc->doc);
57
58         G_OBJECT_CLASS (gupnp_xml_doc_parent_class)->finalize (object);
59 }
60
61 static void
62 gupnp_xml_doc_class_init (GUPnPXMLDocClass *klass)
63 {
64         GObjectClass *object_class;
65
66         object_class = G_OBJECT_CLASS (klass);
67
68         object_class->finalize = gupnp_xml_doc_finalize;
69 }
70
71 /**
72  * gupnp_xml_doc_new:
73  * @xml_doc: Pointer to #xmlDoc to wrap under this object
74  *
75  * Create a new #GUPnPXMLDoc for @xml_doc.
76  *
77  * Return value: A new #GUPnPXMLDoc, or %NULL on an error
78  *
79  * Since: 0.13.0
80  **/
81 GUPnPXMLDoc *
82 gupnp_xml_doc_new (xmlDoc *xml_doc)
83 {
84         GUPnPXMLDoc *doc;
85
86         g_return_val_if_fail (xml_doc != NULL, NULL);
87
88         doc = g_object_new (GUPNP_TYPE_XML_DOC, NULL);
89
90         doc->doc = xml_doc;
91
92         return doc;
93 }
94
95 /**
96  * gupnp_xml_doc_new_from_path:
97  * @path: Path to xml document
98  * @error: Location to put the error into
99  *
100  * Create a new #GUPnPXMLDoc for the XML document at @path.
101  *
102  * Return value: A new #GUPnPXMLDoc, or %NULL on an error
103  *
104  * Since: 0.13.0
105  **/
106 GUPnPXMLDoc *
107 gupnp_xml_doc_new_from_path (const char *path,
108                              GError    **error)
109 {
110         xmlDoc *doc;
111         int flags;
112
113         flags = XML_PARSE_PEDANTIC;
114
115         if (!g_getenv ("GUPNP_DEBUG")) {
116                 flags |= XML_PARSE_NOWARNING | XML_PARSE_NOERROR;
117         }
118
119         g_return_val_if_fail (path != NULL, NULL);
120         doc = xmlReadFile (path, NULL, flags);
121         if (doc == NULL) {
122                 g_set_error (error,
123                              GUPNP_XML_ERROR,
124                              GUPNP_XML_ERROR_PARSE,
125                              "Failed to parse %s\n",
126                              path);
127
128                 return NULL;
129         }
130
131         return gupnp_xml_doc_new (doc);
132 }