gwenhywfar  4.8.0beta
xmlglobalize.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Wed Feb 27 2008
3  copyright : (C) 2008 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 
26 
27 
29  const char *s) {
31 
32  assert(l);
33  ns=GWEN_XMLNode_NameSpace_List_First(l);
34  while(ns) {
35  const char *d;
36 
38  if (d && strcasecmp(d, s)==0)
39  return ns;
40  ns=GWEN_XMLNode_NameSpace_List_Next(ns);
41  }
42 
43  return NULL;
44 }
45 
46 
47 
49  const char *prefix) {
50  while(n) {
51  if (n->type==GWEN_XMLNodeTypeTag) {
53 
54  DBG_ERROR(0, "Checking in node [%s]", GWEN_XMLNode_GetData(n));
55  ns=GWEN_XMLNode_NameSpace_List_First(n->nameSpaces);
56  while(ns) {
57  const char *d;
58 
60  if (d && strcasecmp(d, prefix)==0)
61  return ns;
62  ns=GWEN_XMLNode_NameSpace_List_Next(ns);
63  }
64  }
65 
66  n=n->parent;
67  }
68 
69  return NULL;
70 }
71 
72 
73 
75  GWEN_XMLNODE_NAMESPACE_LIST *l,
76  char **pValue) {
78  char *dcopy=NULL;
79  char *v;
80  const char *prefix;
81  const char *name;
82 
83  /* split into prefix and value */
84  dcopy=strdup(*pValue);
85  v=strchr(dcopy, ':');
86  if (v) {
87  *v=0;
88  prefix=dcopy;
89  name=v+1;
90  }
91  else {
92  prefix="";
93  name=dcopy;
94  }
95 
96  /* find definition for namespace in this and parent nodes */
98  if (ns) {
100 
101  /* find new namespace in list of redefined namespaces */
103  if (newNs) {
104  char *newValue;
105 
106  /* translate prefix part of the name */
107  newValue=(char*)malloc(strlen(GWEN_XMLNode_NameSpace_GetName(newNs))+
108  strlen(name)+1+1);
109  assert(newValue);
110  strcpy(newValue, GWEN_XMLNode_NameSpace_GetName(newNs));
111  strcat(newValue, ":");
112  strcat(newValue, name);
113  free(*pValue);
114  *pValue=newValue;
115  }
116  else {
118  "Namespace for [%s] not in list, should not happen.",
120  abort();
121  }
122  free(dcopy);
123  }
124  else {
125  DBG_ERROR(GWEN_LOGDOMAIN, "No definition for namespace \"%s\" found", prefix);
126  free(dcopy);
127  return GWEN_ERROR_NO_DATA;
128  }
129 
130  return 0;
131 }
132 
133 
134 
136  GWEN_XMLNODE_NAMESPACE_LIST *l,
137  uint32_t *pLastId) {
138  GWEN_XMLNODE *nn;
139 
140  if (n->type==GWEN_XMLNodeTypeTag) {
142  GWEN_XMLPROPERTY *pr;
143  int rv;
144 
145  ns=GWEN_XMLNode_NameSpace_List_First(n->nameSpaces);
146  while(ns) {
147  const char *url;
148 
150  if (url) {
151  if (GWEN_XMLGL__FindNameSpaceByUrl(l, url)==NULL) {
152  char namebuf[32];
153  GWEN_XMLNODE_NAMESPACE *newNs;
154 
155  snprintf(namebuf, sizeof(namebuf)-1, "_%d_", ++(*pLastId));
156  newNs=GWEN_XMLNode_NameSpace_new(namebuf, url);
157  GWEN_XMLNode_NameSpace_List_Add(newNs, l);
158  }
159  }
160  ns=GWEN_XMLNode_NameSpace_List_Next(ns);
161  }
162 
163  /* translate some properties */
164  pr=n->properties;
165  while(pr) {
166  if (pr->name && pr->value) {
167  if (strcasecmp(pr->name, "type")==0 ||
168  strcasecmp(pr->name, "ref")==0 ||
169  strcasecmp(pr->name, "base")==0) {
170  rv=GWEN_XMLGL__TranslateName(n, l, &(pr->value));
171  if (rv) {
172  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
173  return rv;
174  }
175  }
176  }
177  pr=pr->next;
178  }
179 
180  /* translate this node */
181  if (n->data) {
182  rv=GWEN_XMLGL__TranslateName(n, l, &(n->data));
183  if (rv) {
184  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
185  return rv;
186  }
187  }
188  }
189 
190  /* sample and rename children */
191  nn=GWEN_XMLNode_List_First(n->children);
192  while(nn) {
193  int rv;
194 
195  rv=GWEN_XMLGL__SampleNameSpaces(nn, l, pLastId);
196  if (rv)
197  return rv;
198  nn=GWEN_XMLNode_List_Next(nn);
199  }
200 
201  return 0;
202 }
203 
204 
205 
207  GWEN_XMLNODE *nn;
208 
209  GWEN_XMLNode_NameSpace_List_Clear(n->nameSpaces);
210 
211  nn=GWEN_XMLNode_List_First(n->children);
212  while(nn) {
214  nn=GWEN_XMLNode_List_Next(nn);
215  }
216 }
217 
218 
219 
221  GWEN_XMLNODE_NAMESPACE_LIST *l,
222  uint32_t *pLastId) {
223  int rv;
224 
225  rv=GWEN_XMLGL__SampleNameSpaces(n, l, pLastId);
226  if (rv<0) {
227  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
228  return rv;
229  }
231 
232  return 0;
233 }
234 
235 
236 
238  GWEN_XMLNODE_NAMESPACE_LIST *l;
239  uint32_t lastId=0;
240  int rv;
241 
242  l=GWEN_XMLNode_NameSpace_List_new();
243  rv=GWEN_XMLNode_GlobalizeWithList(n, l, &lastId);
244  if (rv<0) {
245  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
246  GWEN_XMLNode_NameSpace_List_free(l);
247  return rv;
248  }
249  GWEN_XMLNode_NameSpace_List_free(n->nameSpaces);
250  n->nameSpaces=l;
251 
252  return 0;
253 }
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265