18 private $xml_parser =
false;
23 private $in_file =
false;
28 private $in_fh =
false;
33 private $out_file =
false;
38 private $out_fh =
false;
45 private $fileIndex = 0;
52 private $compatibilityEscape =
true;
62 if (!is_dir($splitdir) || !is_writable($splitdir)) {
63 $this->errmsg = sprintf(_(
"Invalid directory '%s'.") , $splitdir);
67 $this->splitdir = $splitdir;
69 $this->xml_parser = xml_parser_create(
'UTF-8');
70 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 0);
71 xml_parser_set_option($this->xml_parser, XML_OPTION_SKIP_WHITE, 0);
73 xml_set_object($this->xml_parser, $this);
74 xml_set_element_handler($this->xml_parser,
"startElement",
"endElement");
75 xml_set_character_data_handler($this->xml_parser,
"characterData");
90 if ((
$data = fread($this->in_fh, 8192)) ===
false) {
91 $this->errmsg = sprintf(_(
"Error reading from file '%s'") , $this->in_file);
94 $eof = feof($this->in_fh);
95 if (!xml_parse($this->xml_parser,
$data, $eof)) {
96 $this->errmsg = sprintf(_(
"XML error %s at line %d") , xml_error_string(xml_get_error_code($this->xml_parser)) , xml_get_current_line_number($this->xml_parser));
101 catch(Exception $e) {
114 if ($this->xml_parser !==
false) {
115 xml_parser_free($this->xml_parser);
117 $this->xml_parser =
false;
118 $this->closeOutputFile();
119 $this->closeInputFile();
129 $str = str_replace(array(
152 $str = str_replace(array(
174 static private function compatibilityEscapeEntities($str)
176 $str = str_replace(array(
194 private function open(
$file)
196 $this->in_file =
$file;
197 $this->in_fh = fopen($this->in_file,
"r");
198 if ($this->in_fh ===
false) {
199 $this->errmsg = sprintf(_(
"Could not open '%s' for reading.") , $this->in_file);
210 private function writeOutputFile($str)
212 if ($this->out_fh !==
false) {
213 $ret = fwrite($this->out_fh, $str);
214 if (
$ret ===
false) {
215 $this->errmsg = sprintf(_(
"Error writing to ouput file '%s'.") , $this->out_file);
226 private function printStartNode(array $node)
229 foreach ($node[
'attrs'] as $name =>
$value) {
235 $str = sprintf(
"<%s%s%s>", $node[
'name'], count(
$attrList) > 0 ?
' ' :
'', join(
" ",
$attrList));
236 $this->writeOutputFile($str);
244 private function printEndNode(array $node)
246 $str = sprintf(
"</%s>\n", $node[
'name']);
247 $this->writeOutputFile($str);
255 private function printData(
$data)
258 if ($this->compatibilityEscape) {
259 $str = $this::compatibilityEscapeEntities($str);
261 $str = $this::escapeEntities($str);
263 $this->writeOutputFile($str);
273 private function characterData(&$parser,
$data)
275 if ($this->depth >= 1) {
276 $this->printData(
$data);
289 private function startElement(&$parser, $name, $attrs)
296 if ($this->depth == 1 && $node[
'name'] !=
'documents') {
297 $this->errmsg = sprintf(_(
"XML Root node is not a '%s' node (root node is '%s').") ,
'documents', $node[
'name']);
300 if ($this->depth == 2) {
301 $this->openOutputFile($node);
303 $this->printStartNode($node);
313 private function endElement(&$parser, $name)
319 $this->printEndNode($node);
320 if ($this->depth == 1) {
321 $this->closeOutputFile();
334 private function openOutputFile(array $node)
336 if ($this->out_fh !==
false) {
337 $this->errmsg = sprintf(_(
"Output file '%s' is already opened.") , $this->out_file);
338 error_log($this->errmsg);
342 if (isset($node[
'attrs'][
'name']) && preg_match(
'/^[a-zA-Z0-9_-]+$/', $node[
'attrs'][
'name'])) {
343 $fname = $node[
'attrs'][
'name'];
344 } elseif (isset($node[
'attrs'][
'id']) && preg_match(
'/^[0-9]+$/', $node[
'attrs'][
'id'])) {
345 $fname = $node[
'attrs'][
'id'];
347 $fname = uniqid(
"new");
351 $this->errmsg = sprintf(_(
"Could not generate output file name for node '%s'.") , $node[
'name']);
355 $this->out_file = sprintf(
"%s%s%05d%s.xml", $this->splitdir, DIRECTORY_SEPARATOR, $this->fileIndex++, $fname);
356 if (is_file($this->out_file)) {
357 $this->errmsg = sprintf(_(
"Output file '%s' already exists.") , $this->out_file);
360 $this->out_fh = fopen($this->out_file,
'wx');
361 if ($this->out_fh ===
false) {
362 $this->errmsg = sprintf(_(
"Xml import : Cannot create file %s") , $this->out_file);
365 $this->writeOutputFile(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
372 private function closeInputFile()
374 if ($this->in_fh !==
false) {
375 fclose($this->in_fh);
377 $this->in_fh =
false;
378 $this->in_file =
false;
385 private function closeOutputFile()
387 if ($this->out_fh !==
false) {
388 fclose($this->out_fh);
390 $this->out_fh =
false;
391 $this->out_file =
false;
static escapeEntities($str)
Exception class use exceptionCode to identifiy correctly exception.
if(is_numeric($parms['famid'])) $attrList
static unescapeEntities($str)