gwenhywfar  5.3.0
parser_xml.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Fri Apr 18 2014
3  copyright : (C) 2014 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <gwenhywfar/parser_xml.h>
30 #include <gwenhywfar/debug.h>
31 
32 
33 
34 
35 int GWEN_ParserXml__Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_PARSER_ELEMENT *eParent, GWEN_XMLNODE *node)
36 {
37  GWEN_PARSER_ELEMENT *e=NULL;
38  const char *s;
39 
40  switch (GWEN_XMLNode_GetType(node)) {
42  e=GWEN_ParserElement_new();
43  GWEN_ParserElement_SetType(e, GWEN_ParserElementType_Element);
44  s=GWEN_XMLNode_GetData(node);
45  if (s && *s)
46  GWEN_ParserElement_SetName(e, s);
47  /* TODO: Read attributes */
48  break;
50  e=GWEN_ParserElement_new();
51  GWEN_ParserElement_SetElementType(e, GWEN_ParserElementType_Data);
52  s=GWEN_XMLNode_GetData(node);
53  if (s && *s)
54  GWEN_ParserElement_SetData(e, s);
55  break;
57  e=NULL;
58  break;
59  }
60 
61  if (e) {
62  GWEN_XMLNODE *n;
63  const char *name;
64 
65  name=GWEN_ParserElement_GetName(e);
66  DBG_INFO(GWEN_LOGDOMAIN, "Adding element \"%s\" (%s)",
67  name?name:"-?-",
68  GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(e)));
69 
70  if (eParent)
71  GWEN_ParserElement_Tree_AddChild(eParent, e);
72  else
73  GWEN_ParserElement_Tree_Add(et, e);
74 
75  n=GWEN_XMLNode_GetChild(node);
76  while (n) {
77  int rv;
78 
79  rv=GWEN_ParserXml__Read(et, e, n);
80  if (rv<0) {
81  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
82  return rv;
83  }
84 
85  n=GWEN_XMLNode_Next(n);
86  }
87  }
88 
89  return 0;
90 }
91 
92 
93 
94 
95 int GWEN_ParserXml_Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_XMLNODE *node)
96 {
97  return GWEN_ParserXml__Read(et, NULL, node);
98 }
99 
100 
101 
102 int GWEN_ParserXml_ReadFile(GWEN_PARSER_ELEMENT_TREE *et, const char *fname)
103 {
104  int rv;
105  GWEN_XMLNODE *rootNode;
106  GWEN_XMLNODE *n;
107 
108  rootNode=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "root");
110  if (rv<0) {
111  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
112  GWEN_XMLNode_free(rootNode);
113  return rv;
114  }
115 
116  n=GWEN_XMLNode_GetChild(rootNode);
117  while (n) {
118  int rv;
119 
120  rv=GWEN_ParserXml_Read(et, n);
121  if (rv<0) {
122  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
123  GWEN_XMLNode_free(rootNode);
124  return rv;
125  }
126  n=GWEN_XMLNode_Next(n);
127  }
128 
129  GWEN_XMLNode_free(rootNode);
130  return 0;
131 }
132 
133 
134 
135 
136 
137 
#define GWEN_XML_FLAGS_DEFAULT
Definition: xml.h:110
#define NULL
Definition: binreloc.c:300
GWENHYWFAR_API int GWEN_XML_ReadFile(GWEN_XMLNODE *n, const char *filepath, uint32_t flags)
Definition: xmlrw.c:1297
int GWEN_ParserXml__Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_PARSER_ELEMENT *eParent, GWEN_XMLNODE *node)
Definition: parser_xml.c:35
#define GWEN_LOGDOMAIN
Definition: logger.h:35
GWEN_XMLNODE * GWEN_XMLNode_new(GWEN_XMLNODE_TYPE t, const char *data)
Definition: xml.c:144
GWEN_XMLNODE * GWEN_XMLNode_GetChild(const GWEN_XMLNODE *n)
Definition: xml.c:409
GWEN_XMLNODE_TYPE GWEN_XMLNode_GetType(const GWEN_XMLNODE *n)
Definition: xml.c:458
GWEN_XMLNODE * GWEN_XMLNode_Next(const GWEN_XMLNODE *n)
Definition: xml.c:465
void GWEN_XMLNode_free(GWEN_XMLNODE *n)
Definition: xml.c:160
const char * GWEN_XMLNode_GetData(const GWEN_XMLNODE *n)
Definition: xml.c:370
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:178
#define GWEN_XML_FLAGS_HANDLE_HEADERS
Definition: xml.h:94
int GWEN_ParserXml_ReadFile(GWEN_PARSER_ELEMENT_TREE *et, const char *fname)
Definition: parser_xml.c:102
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:149
int GWEN_ParserXml_Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_XMLNODE *node)
Definition: parser_xml.c:95