-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathxml_saxy_shared.php
More file actions
291 lines (258 loc) · 8.12 KB
/
Copy pathxml_saxy_shared.php
File metadata and controls
291 lines (258 loc) · 8.12 KB
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
<?php
/**
* SAXY_Parser_Base is a base class for SAXY and SAXY Lite
* @package saxy-xmlparser
* @version 1.0
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/saxy/ SAXY Home Page
* SAXY is Free Software
**/
/** the initial characters of a cdata section */
define('SAXY_SEARCH_CDATA', '![CDATA[');
/** the length of the initial characters of a cdata section */
define('SAXY_CDATA_LEN', 8);
/** the initial characters of a notation */
define('SAXY_SEARCH_NOTATION', '!NOTATION');
/** the initial characters of a doctype */
define('SAXY_SEARCH_DOCTYPE', '!DOCTYPE');
/** saxy parse state, just before parsing an attribute */
define('SAXY_STATE_ATTR_NONE', 0);
/** saxy parse state, parsing an attribute key */
define('SAXY_STATE_ATTR_KEY', 1);
/** saxy parse state, parsing an attribute value */
define('SAXY_STATE_ATTR_VALUE', 2);
/**
* The base SAX Parser class
*
* @package saxy-xmlparser
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class SAXY_Parser_Base {
/** @var int The current state of the parser */
var $state;
/** @var int A temporary container for parsed characters */
var $charContainer;
/** @var Object A reference to the start event handler */
var $startElementHandler;
/** @var Object A reference to the end event handler */
var $endElementHandler;
/** @var Object A reference to the data event handler */
var $characterDataHandler;
/** @var Object A reference to the CDATA Section event handler */
var $cDataSectionHandler = null;
/** @var boolean True if predefined entities are to be converted into characters */
var $convertEntities = true;
/** @var Array Translation table for predefined entities */
var $predefinedEntities = array('&' => '&', '<' => '<', '>' => '>',
'"' => '"', ''' => "'");
/** @var Array User defined translation table for entities */
var $definedEntities = array();
/** @var boolean True if whitespace is to be preserved during parsing. NOT YET IMPLEMENTED! */
var $preserveWhitespace = false;
/**
* Constructor for SAX parser
*/
function SAXY_Parser_Base() {
$this->charContainer = '';
} //SAXY_Parser_Base
/**
* Sets a reference to the handler for the start element event
* @param mixed A reference to the start element handler
*/
function xml_set_element_handler($startHandler, $endHandler) {
$this->startElementHandler = $startHandler;
$this->endElementHandler = $endHandler;
} //xml_set_element_handler
/**
* Sets a reference to the handler for the data event
* @param mixed A reference to the data handler
*/
function xml_set_character_data_handler($handler) {
$this->characterDataHandler =& $handler;
} //xml_set_character_data_handler
/**
* Sets a reference to the handler for the CDATA Section event
* @param mixed A reference to the CDATA Section handler
*/
function xml_set_cdata_section_handler($handler) {
$this->cDataSectionHandler =& $handler;
} //xml_set_cdata_section_handler
/**
* Sets whether predefined entites should be replaced with their equivalent characters during parsing
* @param boolean True if entity replacement is to occur
*/
function convertEntities($truthVal) {
$this->convertEntities = $truthVal;
} //convertEntities
/**
* Appends an array of entity mappings to the existing translation table
*
* Intended mainly to facilitate the conversion of non-ASCII entities into equivalent characters
*
* @param array A list of entity mappings in the format: array('&' => '&');
*/
function appendEntityTranslationTable($table) {
$this->definedEntities = $table;
} //appendEntityTranslationTable
/**
* Gets the nth character from the end of the string
* @param string The text to be queried
* @param int The index from the end of the string
* @return string The found character
*/
function getCharFromEnd($text, $index) {
$len = strlen($text);
$char = $text{($len - 1 - $index)};
return $char;
} //getCharFromEnd
/**
* Parses the attributes string into an array of key / value pairs
* @param string The attribute text
* @return Array An array of key / value pairs
*/
function parseAttributes($attrText) {
$attrText = trim($attrText);
$attrArray = array();
$maybeEntity = false;
$total = strlen($attrText);
$keyDump = '';
$valueDump = '';
$currentState = SAXY_STATE_ATTR_NONE;
$quoteType = '';
for ($i = 0; $i < $total; $i++) {
$currentChar = $attrText{$i};
if ($currentState == SAXY_STATE_ATTR_NONE) {
if (trim($currentChar != '')) {
$currentState = SAXY_STATE_ATTR_KEY;
}
}
switch ($currentChar) {
case "\t":
if ($currentState == SAXY_STATE_ATTR_VALUE) {
$valueDump .= $currentChar;
}
else {
$currentChar = '';
}
break;
case "\x0B": //vertical tab
case "\n":
case "\r":
$currentChar = '';
break;
case '=':
if ($currentState == SAXY_STATE_ATTR_VALUE) {
$valueDump .= $currentChar;
}
else {
$currentState = SAXY_STATE_ATTR_VALUE;
$quoteType = '';
$maybeEntity = false;
}
break;
case '"':
if ($currentState == SAXY_STATE_ATTR_VALUE) {
if ($quoteType == '') {
$quoteType = '"';
}
else {
if ($quoteType == $currentChar) {
if ($this->convertEntities && $maybeEntity) {
$valueDump = strtr($valueDump, $this->predefinedEntities);
$valueDump = strtr($valueDump, $this->definedEntities);
}
$keyDump = trim($keyDump);
$attrArray[$keyDump] = $valueDump;
$keyDump = $valueDump = $quoteType = '';
$currentState = SAXY_STATE_ATTR_NONE;
}
else {
$valueDump .= $currentChar;
}
}
}
break;
case "'":
if ($currentState == SAXY_STATE_ATTR_VALUE) {
if ($quoteType == '') {
$quoteType = "'";
}
else {
if ($quoteType == $currentChar) {
if ($this->convertEntities && $maybeEntity) {
$valueDump = strtr($valueDump, $this->predefinedEntities);
$valueDump = strtr($valueDump, $this->definedEntities);
}
$keyDump = trim($keyDump);
$attrArray[$keyDump] = $valueDump;
$keyDump = $valueDump = $quoteType = '';
$currentState = SAXY_STATE_ATTR_NONE;
}
else {
$valueDump .= $currentChar;
}
}
}
break;
case '&':
//might be an entity
$maybeEntity = true;
$valueDump .= $currentChar;
break;
default:
if ($currentState == SAXY_STATE_ATTR_KEY) {
$keyDump .= $currentChar;
}
else {
$valueDump .= $currentChar;
}
}
}
return $attrArray;
} //parseAttributes
/**
* Parses character data
* @param string The character data
*/
function parseBetweenTags($betweenTagText) {
if (trim($betweenTagText) != ''){
$this->fireCharacterDataEvent($betweenTagText);
}
} //parseBetweenTags
/**
* Fires a start element event
* @param string The start element tag name
* @param Array The start element attributes
*/
function fireStartElementEvent($tagName, $attributes) {
call_user_func($this->startElementHandler, array(&$this), $tagName, $attributes);
} //fireStartElementEvent
/**
* Fires an end element event
* @param string The end element tag name
*/
function fireEndElementEvent($tagName) {
call_user_func($this->endElementHandler, array(&$this), $tagName);
} //fireEndElementEvent
/**
* Fires a character data event
* @param string The character data
*/
function fireCharacterDataEvent($data) {
if ($this->convertEntities && ((strpos($data, "&") != -1))) {
$data = strtr($data, $this->predefinedEntities);
$data = strtr($data, $this->definedEntities);
}
call_user_func($this->characterDataHandler, array(&$this), $data);
} //fireCharacterDataEvent
/**
* Fires a CDATA Section event
* @param string The CDATA Section data
*/
function fireCDataSectionEvent($data) {
call_user_func($this->cDataSectionHandler, array(&$this), $data);
} //fireCDataSectionEvent
} //SAXY_Parser_Base
?>