1 /**************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  **************************************************************************/
31 
32 #ifndef IXMLPARSER_H
33 #define IXMLPARSER_H
34 
35 /*!
36  * \file
37  */
38 
39 #include "ixml.h"
40 #include "ixmlmembuf.h"
41 
42 /* Parser definitions */
43 #define QUOT """
44 #define LT "<"
45 #define GT ">"
46 #define APOS "'"
47 #define AMP "&"
48 #define ESC_HEX "&#x"
49 #define ESC_DEC "&#"
50 
51 typedef struct _IXML_NamespaceURI
52 {
53 	char *nsURI;
54 	char *prefix;
55 	struct _IXML_NamespaceURI *nextNsURI;
56 } IXML_NamespaceURI;
57 
58 typedef struct _IXML_ElementStack
59 {
60 	char *element;
61 	char *prefix;
62 	char *namespaceUri;
63 	IXML_NamespaceURI *pNsURI;
64 	struct _IXML_ElementStack *nextElement;
65 } IXML_ElementStack;
66 
67 typedef enum
68 {
69 	eELEMENT,
70 	eATTRIBUTE,
71 	eCONTENT
72 } PARSER_STATE;
73 
74 typedef struct _Parser
75 {
76 	/*! Data buffer. */
77 	char *dataBuffer;
78 	/*! Pointer to the token parsed. */
79 	char *curPtr;
80 	/*! Saves for backup. */
81 	char *savePtr;
82 	ixml_membuf lastElem;
83 	ixml_membuf tokenBuf;
84 	IXML_Node *pNeedPrefixNode;
85 	IXML_ElementStack *pCurElement;
86 	IXML_Node *currentNodePtr;
87 	PARSER_STATE state;
88 	int bHasTopLevel;
89 } Parser;
90 
91 /*!
92  * \brief Check to see whether name is a valid xml name.
93  */
94 int Parser_isValidXmlName(
95 	/*! [in] The string to be checked. */
96 	const DOMString name);
97 
98 /*!
99  * \brief Sets the error character.
100  *
101  * If 'c' is 0 (default), the parser is strict about XML encoding:
102  * invalid UTF-8 sequences or "&" entities are rejected, and the parsing
103  * aborts.
104  *
105  * If 'c' is not 0, the parser is relaxed: invalid UTF-8 characters
106  * are replaced by this character, and invalid "&" entities are left
107  * untranslated. The parsing is then allowed to continue.
108  */
109 void Parser_setErrorChar(
110 	/*! [in] The character to become the error character. */
111 	char c);
112 
113 #ifdef IXML_HAVE_SCRIPTSUPPORT
114 /*!
115  * \brief Sets the handler to call before a node is freed.
116  *
117  * If \b hndlr is set to a function, it will be called before any
118  * node is freed, with the node as its parameter. This allows scripting
119  * languages to do their garbage collection, without maintaining their
120  * own tree structure.
121  */
122 void Parser_setBeforeFree(
123 	/*! [in] The handler callback to call before each node to be freed. */
124 	IXML_BeforeFreeNode_t hndlr);
125 
126 /*!
127  * \brief Gets the handler to call before a node is freed.
128  */
129 IXML_BeforeFreeNode_t Parser_getBeforeFree();
130 #endif
131 
132 /*!
133  * \brief Fees a node contents.
134  */
135 void Parser_freeNodeContent(
136 	/*! [in] The Node to process. */
137 	IXML_Node *IXML_Nodeptr);
138 
139 int Parser_LoadDocument(IXML_Document **retDoc, const char *xmlFile, int file);
140 
141 int Parser_setNodePrefixAndLocalName(IXML_Node *newIXML_NodeIXML_Attr);
142 
143 void ixmlAttr_init(IXML_Attr *attrNode);
144 
145 /*!
146  * \brief Set the given element's tagName.
147  *
148  * \return One of the following:
149  * 	\li \b IXML_SUCCESS, if successfull.
150  * 	\li \b IXML_FAILED, if element of tagname is \b NULL.
151  * 	\li \b IXML_INSUFFICIENT_MEMORY, if there is no memory to allocate the
152  * 		buffer for the element's tagname.
153  */
154 int ixmlElement_setTagName(
155 	/*! [in] The element to change the tagname. */
156 	IXML_Element *element,
157 	/*! [in] The new tagName for the element. */
158 	const char *tagName);
159 
160 /*!
161  * \brief Initializes a NamedNodeMap object.
162  */
163 void ixmlNamedNodeMap_init(
164 	/*! [in] The named node map to process. */
165 	IXML_NamedNodeMap *nnMap);
166 
167 /*!
168  * \brief Add a node to a NamedNodeMap.
169  *
170  * \return IXML_SUCCESS or failure.
171  */
172 int ixmlNamedNodeMap_addToNamedNodeMap(
173 	/* [in] The named node map. */
174 	IXML_NamedNodeMap **nnMap,
175 	/* [in] The node to add. */
176 	IXML_Node *add);
177 
178 /*!
179  * \brief Add a node to nodelist.
180  */
181 int ixmlNodeList_addToNodeList(
182 	/*! [in] The pointer to the nodelist. */
183 	IXML_NodeList **nList,
184 	/*! [in] The node to add. */
185 	IXML_Node *add);
186 
187 /*!
188  * \brief Intializes a node.
189  */
190 void ixmlNode_init(
191 	/*! [in] The \b Node to iniatialize. */
192 	IXML_Node *nodeptr);
193 
194 /*!
195  * \brief Compare two nodes to see whether they are the same node.
196  * Parent, sibling and children node are ignored.
197  *
198  * \return
199  * 	\li 1, the two nodes are the same.
200  * 	\li 0, the two nodes are not the same.
201  */
202 int ixmlNode_compare(
203 	/*! [in] The first \b Node. */
204 	IXML_Node *srcNode,
205 	/*! [in] The second \b Node. */
206 	IXML_Node *destNode);
207 
208 /*!
209  * \brief Returns a nodeList of all descendant Elements with a given tagName,
210  * in the order in which they are encountered in a traversal of this element
211  * tree.
212  */
213 void ixmlNode_getElementsByTagName(
214 	/*! [in] The \b Node tree. */
215 	IXML_Node *n,
216 	/*! [in] The tag name to match. */
217 	const char *tagname,
218 	/*! [out] The output \b NodeList. */
219 	IXML_NodeList **list);
220 
221 /*!
222  * \brief Returns a nodeList of all the descendant Elements with a given local
223  * name and namespace URI in the order in which they are encountered in a
224  * preorder traversal of this Elememt tree.
225  */
226 void ixmlNode_getElementsByTagNameNS(
227 	/*! [in] The \b Element tree. */
228 	IXML_Node *n,
229 	/*! [in] The name space to match. */
230 	const char *namespaceURI,
231 	/*! [in] The local name to match. */
232 	const char *localName,
233 	/*! [out] The output \b NodeList. */
234 	IXML_NodeList **list);
235 
236 /*!
237  * \brief
238  *
239  * \return
240  */
241 int ixmlNode_setNodeName(
242 	/*! [in] The \b Node. */
243 	IXML_Node *node,
244 	/*! [in] . */
245 	const DOMString qualifiedName);
246 
247 /*!
248  * \brief
249  *
250  * \return
251  */
252 int ixmlNode_setNodeProperties(
253 	/*! [in] . */
254 	IXML_Node *destNode,
255 	/*! [in] . */
256 	IXML_Node *src);
257 
258 /*!
259  * \brief Initializes a nodelist
260  */
261 void ixmlNodeList_init(
262 	/*! [in,out] The \b NodeList to initialize. */
263 	IXML_NodeList *nList);
264 
265 #endif /* IXMLPARSER_H */
266