gwenhywfar  4.99.8beta
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  GWEN_PARSER_ELEMENT *e=NULL;
37  const char *s;
38 
39  switch(GWEN_XMLNode_GetType(node)) {
41  e=GWEN_ParserElement_new();
42  GWEN_ParserElement_SetType(e, GWEN_ParserElementType_Element);
43  s=GWEN_XMLNode_GetData(node);
44  if (s && *s)
45  GWEN_ParserElement_SetName(e, s);
46  /* TODO: Read attributes */
47  break;
49  e=GWEN_ParserElement_new();
50  GWEN_ParserElement_SetElementType(e, GWEN_ParserElementType_Data);
51  s=GWEN_XMLNode_GetData(node);
52  if (s && *s)
53  GWEN_ParserElement_SetData(e, s);
54  break;
56  e=NULL;
57  break;
58  }
59 
60  if (e) {
61  GWEN_XMLNODE *n;
62  const char *name;
63 
64  name=GWEN_ParserElement_GetName(e);
65  DBG_INFO(GWEN_LOGDOMAIN, "Adding element \"%s\" (%s)",
66  name?name:"-?-",
67  GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(e)));
68 
69  if (eParent)
70  GWEN_ParserElement_Tree_AddChild(eParent, e);
71  else
72  GWEN_ParserElement_Tree_Add(et, e);
73 
74  n=GWEN_XMLNode_GetChild(node);
75  while(n) {
76  int rv;
77 
78  rv=GWEN_ParserXml__Read(et, e, n);
79  if (rv<0) {
80  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
81  return rv;
82  }
83 
84  n=GWEN_XMLNode_Next(n);
85  }
86  }
87 
88  return 0;
89 }
90 
91 
92 
93 
94 int GWEN_ParserXml_Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_XMLNODE *node) {
95  return GWEN_ParserXml__Read(et, NULL, node);
96 }
97 
98 
99 
100 int GWEN_ParserXml_ReadFile(GWEN_PARSER_ELEMENT_TREE *et, const char *fname) {
101  int rv;
102  GWEN_XMLNODE *rootNode;
103  GWEN_XMLNODE *n;
104 
105  rootNode=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "root");
107  if (rv<0) {
108  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
109  GWEN_XMLNode_free(rootNode);
110  return rv;
111  }
112 
113  n=GWEN_XMLNode_GetChild(rootNode);
114  while(n) {
115  int rv;
116 
117  rv=GWEN_ParserXml_Read(et, n);
118  if (rv<0) {
119  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
120  GWEN_XMLNode_free(rootNode);
121  return rv;
122  }
123  n=GWEN_XMLNode_Next(n);
124  }
125 
126  GWEN_XMLNode_free(rootNode);
127  return 0;
128 }
129 
130 
131 
132 
133 
134 
#define GWEN_XML_FLAGS_DEFAULT
Definition: xml.h:110
#define NULL
Definition: binreloc.c:290
GWENHYWFAR_API int GWEN_XML_ReadFile(GWEN_XMLNODE *n, const char *filepath, uint32_t flags)
Definition: xmlrw.c:973
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:137
GWEN_XMLNODE * GWEN_XMLNode_GetChild(const GWEN_XMLNODE *n)
Definition: xml.c:386
GWEN_XMLNODE_TYPE GWEN_XMLNode_GetType(const GWEN_XMLNODE *n)
Definition: xml.c:431
GWEN_XMLNODE * GWEN_XMLNode_Next(const GWEN_XMLNODE *n)
Definition: xml.c:437
void GWEN_XMLNode_free(GWEN_XMLNODE *n)
Definition: xml.c:152
const char * GWEN_XMLNode_GetData(const GWEN_XMLNODE *n)
Definition: xml.c:351
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
#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:100
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:148
int GWEN_ParserXml_Read(GWEN_PARSER_ELEMENT_TREE *et, GWEN_XMLNODE *node)
Definition: parser_xml.c:94