summaryrefslogtreecommitdiff
path: root/libdigidoc/DigiDocStack.c
blob: 9ea32e7d5cab390b1a1de3eba03060145d93e46b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//==================================================
// FILE:	DigiDocStack.c
// PROJECT:     Digi Doc
// DESCRIPTION: Digi Doc functions for simple stack
//  to keep track of xml parsing
// AUTHOR:  Veiko Sinivee, S|E|B IT Partner Estonia
//==================================================
// Copyright (C) AS Sertifitseerimiskeskus
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// GNU Lesser General Public Licence is available at
// http://www.gnu.org/copyleft/lesser.html
//==========< HISTORY >=============================
//      09.09.2004      Veiko Sinivee
//                      Creation
//==================================================

#include <libdigidoc/DigiDocStack.h>
#include <libdigidoc/DigiDocLib.h>
#include <libdigidoc/DigiDocError.h>
#include <libdigidoc/DigiDocDebug.h>
#include <string.h>

//--------------------------------------------------
// Finds the last element on stack
// reader - XML reader cursor to current node
// pStack - address of stack begin. This one elemnt 
// must exist, but might be initially empty
// return error code or ERR_OK
//--------------------------------------------------
ElementEntry* ddocStackFindEnd(ElementEntry* pStack)
{
  ElementEntry* pElem = pStack;
  while(pElem->pNext)
    pElem = (ElementEntry*)pElem->pNext;
  return pElem;
}

//--------------------------------------------------
// Push a new element info onto stack
// reader - XML reader cursor to current node
// pStack - address of stack begin. This one elemnt 
// must exist, but might be initially empty
// pLastElem - address for new elements pointer. 
//   If not NULL then will be used to return the
//   newly allocated elemnt, so you don't have to
//   do a new search.
// return error code or ERR_OK
//--------------------------------------------------
int ddocStackPushElement(ElementEntry* pStack, xmlTextReaderPtr reader,
			 ElementEntry** pLastElem)
{
  ElementEntry* pElem;

  RETURN_IF_NULL_PARAM(reader);
  RETURN_IF_NULL_PARAM(pStack);
  pElem = ddocStackFindEnd(pStack);
  if(pElem->tag && !pElem->pNext) {  // check if this entry is used
    pElem->pNext = (ElementEntry*)malloc(sizeof(ElementEntry));
    if(!pElem)
      SET_LAST_ERROR_RETURN_CODE(ERR_BAD_ALLOC);
    memset(pElem->pNext, 0, sizeof(ElementEntry));
    pElem = (ElementEntry*)pElem->pNext;
  }
  if(!pElem)
    return ERR_NULL_POINTER;
  pElem->tag = xmlTextReaderLocalName(reader);
  pElem->prefix = xmlTextReaderPrefix(reader);
  pElem->nsUri = xmlTextReaderNamespaceUri(reader);
  // atributes
  pElem->id = xmlTextReaderGetAttribute(reader, (const xmlChar*)"Id");
  pElem->uri = xmlTextReaderGetAttribute(reader, (const xmlChar*)"URI");
  pElem->content = xmlTextReaderGetAttribute(reader, (const xmlChar*)"ContentType");
  if(pLastElem) // this may be NULL if users doesn't want the last elem.
    *pLastElem = pElem;
  return ERR_OK;
}

//--------------------------------------------------
// Push a new element info onto stack
// tagName - elements tag name, Possibly with ns-prefix
// atts - array of atributa names and values
// pStack - address of stack begin. This one elemnt 
// must exist, but might be initially empty
// pLastElem - address for new elements pointer. 
//   If not NULL then will be used to return the
//   newly allocated elemnt, so you don't have to
//   do a new search.
// return error code or ERR_OK
//--------------------------------------------------
int ddocStackPushElementSAX(ElementEntry* pStack, const xmlChar* tagName,
			    const xmlChar** atts, ElementEntry** pLastElem)
{
  ElementEntry* pElem;
  char *p1, *p2;
  xmlChar *n, *v;
  int i;

  ddocDebug(5, "ddocStackPushElementSAX", "tag: %s", (char*)tagName);
  RETURN_IF_NULL_PARAM(tagName)
  RETURN_IF_NULL_PARAM(pStack)
  pElem = ddocStackFindEnd(pStack);
  if(pElem->tag && !pElem->pNext) {  // check if this entry is used
    pElem->pNext = (ElementEntry*)malloc(sizeof(ElementEntry));
    if(!pElem)
      SET_LAST_ERROR_RETURN_CODE(ERR_BAD_ALLOC);
    memset(pElem->pNext, 0, sizeof(ElementEntry));
    pElem = (ElementEntry*)pElem->pNext;
  }
  if(!pElem)
    return ERR_NULL_POINTER;
  p1 = (char*)strdup((char*)tagName);
  if(!p1)
    SET_LAST_ERROR_RETURN_CODE(ERR_BAD_ALLOC)
  // check for ns-prefix
  p2 = strchr(p1, ':');
  if(p2) {
    *p2 = 0;
    p2++;
    pElem->prefix = xmlStrdup((xmlChar*)p1);
    if(!pElem->prefix) {
      free(p1);
      SET_LAST_ERROR_RETURN_CODE(ERR_BAD_ALLOC);
    }
    pElem->tag = xmlStrdup((const xmlChar*)p2);
  }
  else
    pElem->tag = xmlStrdup((const xmlChar*)p1);
  free(p1);
  if(!pElem->tag)
    SET_LAST_ERROR_RETURN_CODE(ERR_BAD_ALLOC);
  for (i = 0; (atts != NULL) && (atts[i] != NULL); i += 2) {
    n = (xmlChar*)atts[i];
    v = (xmlChar*)atts[i+1];
    if(n && !strncmp((char*)n, "xmlns", 5) && v)
      pElem->nsUri = xmlStrdup((xmlChar*)v);
    if(n && !strncmp((char*)n, "Id", 2) && v)
      pElem->id = xmlStrdup((xmlChar*)v);
    if(n && !strncmp((char*)n, "URI", 3) && v)
      pElem->uri = xmlStrdup((xmlChar*)v);
    if(n && !strncmp((char*)n, "ContentType", 11) && v)
      pElem->content = xmlStrdup((xmlChar*)v);
  }
  if(pLastElem) // this may be NULL if users doesn't want the last elem.
    *pLastElem = pElem;
  return ERR_OK;
}

//--------------------------------------------------
// Pop the last element from the stack
// pStack - address of stack begin. This one elemnt 
// must exist, and will never be deleted. 
// bCleanup - flag: 1=cleanup the whole stack, 0=just the last element
// return error code or ERR_OK
// pLastElem - address for new elements pointer. 
//   If not NULL then will be used to return the
//   last element on stack.
//--------------------------------------------------
int ddocStackPopElement(ElementEntry* pStack, int bCleanup, 
			ElementEntry** pLastElem)
{
  ElementEntry *pElem = 0, *pOldElem = 0;

  RETURN_IF_NULL_PARAM(pStack);
  do {
    pOldElem = pElem = pStack;
    while(pElem->pNext) {
      pOldElem = pElem;
      pElem = (ElementEntry *)pElem->pNext;
    }
    if(pElem && pElem != pStack) {
      ddocDebug(5, "ddocStackPopElement", "Pop: %s, cleanup: %d", pElem->tag, bCleanup);
      xmlFree(pElem->tag);
      pElem->tag = 0;
      xmlFree(pElem->prefix);
      pElem->prefix = 0;
      xmlFree(pElem->nsUri);
      pElem->nsUri = 0;
      xmlFree(pElem->id);
      pElem->id = 0;
      xmlFree(pElem->uri);
      pElem->uri = 0;
      xmlFree(pElem->content);
      pElem->content = 0;
      free(pElem);
      pElem = 0; // mark freed
      pOldElem->pNext = 0;
      if(!bCleanup)
	break;  // remove just the last one?
    }
  } while(pElem && pElem != pStack);
  if(bCleanup) {
    // now free up also the final first elements
    // dynamically allocated part
    xmlFree(pStack->tag);
    pStack->tag = 0;
    xmlFree(pStack->prefix);
    pStack->prefix = 0;
    xmlFree(pStack->nsUri);
    pStack->nsUri = 0;
    xmlFree(pStack->id);
    pStack->id = 0;
    xmlFree(pStack->uri);
    pStack->uri = 0;
    xmlFree(pStack->content);
    pStack->content = 0;
    pStack->pNext = 0;
  }
  if(pLastElem && pElem) // this may be NULL if users doesn't want the last elem.
    *pLastElem = pElem;
  return ERR_OK;
}


//--------------------------------------------------
// Retrieve the current/last/stack top elements tag (localname)
// pStack - address of stack begin.
//--------------------------------------------------
const xmlChar* ddocStackCurrentTag(ElementEntry* pStack)
{
  ElementEntry* pElem = 0;
  const xmlChar* pTag = 0;

  if(pStack) {
    pElem = ddocStackFindEnd(pStack);
    if(pElem) 
      pTag = (const xmlChar*)pElem->tag;
  }
  return pTag;
}

//--------------------------------------------------
// Retrieve the current/last/stack top elements ns prefix
// pStack - address of stack begin.
//--------------------------------------------------
const xmlChar* ddocStackCurrentNsPrefix(ElementEntry* pStack)
{
  ElementEntry* pElem = 0;
  const xmlChar* pPrefix = 0;

  if(pStack) {
    pElem = ddocStackFindEnd(pStack);
    if(pElem) 
      pPrefix = (const xmlChar*)pElem->prefix;
  }
  return pPrefix;
}


//--------------------------------------------------
// Retrieve the current/last/stack top elements ns prefix
// pStack - address of stack begin.
//--------------------------------------------------
const xmlChar* ddocStackCurrentNsUri(ElementEntry* pStack)
{
  ElementEntry* pElem = 0;
  const xmlChar* pUri = 0;

  if(pStack) {
    pElem = ddocStackFindEnd(pStack);
    if(pElem) 
      pUri = (const xmlChar*)pElem->nsUri;
  }
  return pUri;
}

//--------------------------------------------------
// Checks if there is a parent element with the given
// localname on the stack.
// pStack - address of stack begin.
// return 1 if there is such a parent elem or 0 if not.
//--------------------------------------------------
int ddocStackHasParentWithName(ElementEntry* pStack, 
			       const xmlChar* parentsName, ElementEntry* pCurrElem)
{
  int bFound = 0;
  ElementEntry *pElem = pStack;

  while(pElem && pElem != pCurrElem) {
    if(pElem->tag && !xmlStrcmp(pElem->tag, parentsName)) {
      bFound = 1;
      break;
    } else
      pElem = (ElementEntry *)pElem->pNext;
  }
  return bFound;
}

//--------------------------------------------------
// Checks if there is a parent element with the given
// localname on the stack.
// pStack - address of stack begin.
// return 1 if there is such a parent elem or 0 if not.
//--------------------------------------------------
ElementEntry* ddocStackGetParentWithName(ElementEntry* pStack, 
			       const xmlChar* parentsName, ElementEntry* pCurrElem)
{
  ElementEntry *pParent = 0;
  ElementEntry *pElem = pStack;

  while(pElem && pElem != pCurrElem) {
    if(pElem->tag && !xmlStrcmp(pElem->tag, parentsName)) {
      pParent = pElem;
      break;
    } else
      pElem = (ElementEntry *)pElem->pNext;
  }
  return pParent;
}