Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.XDOMDocument.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Utils;
8 
10 {
11  public $libXMLErrors = array();
12 
13  public function __construct($message, &$libXMLErrors = array())
14  {
15  $this->message = $message;
16  if (count($libXMLErrors) <= 0) {
17  $libXMLErrors[] = new \libXMLError();
18  }
19  $this->libXMLErrors = $libXMLErrors;
20  }
21 }
22 
23 class XDOMDocument extends \DOMDocument
24 {
25  public $libXMLErrorIgnoreCodes = array();
26  protected static $libXmlIntErr = null;
27  protected static function enableLibXMLErrors()
28  {
29  if (self::$libXmlIntErr !== null) {
30  throw new XDOMDocumentException(sprintf("libXMLIntErr is already initialized."));
31  }
32  self::$libXmlIntErr = libxml_use_internal_errors(true);
33  libxml_clear_errors();
34  }
35  protected static function restoreLibXMLErrors()
36  {
37  if (self::$libXmlIntErr === null) {
38  throw new XDOMDocumentException(sprintf("Uninitialized libXmlIntErr"));
39  }
40  libxml_clear_errors();
41  libxml_use_internal_errors(self::$libXmlIntErr);
42  self::$libXmlIntErr = null;
43  }
44  public function setLibXMLErrorIgnoreCodes($ignoreCodes)
45  {
46  $this->libXMLErrorIgnoreCodes = $ignoreCodes;
47  }
48  public function getLibXMLErrorIgnoreCodes()
49  {
51  }
52  /**
53  * @return \LibXMLError[]
54  */
55  protected function getLibXMLErrors()
56  {
57  $libXMLErrors = libxml_get_errors();
58  $libXMLErrorsFiltered = array();
59  foreach ($libXMLErrors as & $libXMLError) {
60  if (in_array($libXMLError->code, $this->libXMLErrorIgnoreCodes)) {
61  continue;
62  }
63  $libXMLErrorsFiltered[] = $libXMLError;
64  }
65  unset($libXMLError);
66  return $libXMLErrorsFiltered;
67  }
68  public function load($filename, $options = 0, \libXMLError & $error = null)
69  {
70  self::enableLibXMLErrors();
71  /*
72  * Explicitly mask errors so PHPUnit work properly
73  */
74  $ret = @parent::load($filename, $options = 0);
75  $errors = $this->getLibXMLErrors();
76  self::restoreLibXMLErrors();
77  if ($ret === false) {
78  if (count($errors) <= 0) {
79  $error = new \libXMLError();
80  $error->message = 'general parsing error';
81  $error->level = LIBXML_ERR_FATAL;
82  $error->code = 0;
83  $error->line = 0;
84  $error->column = 0;
85  $error->file = $filename;
86  $errors[] = $error;
87  }
88  throw new XDOMDocumentException(sprintf("Error loading XML file '%s': %s", $filename, $error[0]->message) , $errors);
89  }
90  return true;
91  }
92  public function loadXML($source, $options = 0, &$errors = array())
93  {
94  self::enableLibXMLErrors();
95  /*
96  * Explicitly mask errors so PHPUnit work properly
97  */
98  $ret = @parent::loadXML($source, $options);
99  $errors = $this->getLibXMLErrors();
100  self::restoreLibXMLErrors();
101  if ($ret === false) {
102  if (count($errors) <= 0) {
103  /*
104  * If source is empty, libxml_get_last_error report no errors at all
105  * so we mimic the expected error message.
106  */
107  if ($source == '') {
108  $error = new \libXMLError();
109  $error->message = 'general parsing error';
110  $error->level = LIBXML_ERR_FATAL;
111  $error->code = 0;
112  $error->line = 0;
113  $error->column = 0;
114  $error->file = '';
115  } else {
116  $error = new \libXMLError();
117  $error->message = 'Empty string supplied as input';
118  $error->level = LIBXML_ERR_ERROR;
119  $error->code = 0;
120  $error->line = 0;
121  $error->column = 0;
122  $error->file = '';
123  }
124  $errors[] = $error;
125  }
126  throw new XDOMDocumentException(sprintf("Error loading XML data: %s", $errors[0]->message) , $errors);
127  }
128  return true;
129  }
130  /**
131  * @param string $filename
132  * @param int $options
133  * @param \libXMLError[] $errors
134  * @return bool
135  * @throws XDOMDocumentException
136  */
137  public function loadHTMLFile($filename, $options = 0, &$errors = array())
138  {
139  self::enableLibXMLErrors();
140  /*
141  * Explicitly mask errors so PHPUnit work properly
142  */
143  $ret = @parent::loadHTMLFile($filename, $options);
144  $errors = $this->getLibXMLErrors();
145  self::restoreLibXMLErrors();
146  if ($ret === false) {
147  if (count($errors) <= 0) {
148  $error = new \libXMLError();
149  $error->message = 'general parsing error';
150  $error->level = LIBXML_ERR_FATAL;
151  $error->code = 0;
152  $error->line = 0;
153  $error->column = 0;
154  $error->file = $filename;
155  $errors[] = $error;
156  }
157  throw new XDOMDocumentException(sprintf("Error loading HTML file '%s': %s", $filename, $errors[0]->message) , $errors);
158  }
159  return true;
160  }
161  public function loadHTML($source, $options = 0, &$errors = array())
162  {
163  self::enableLibXMLErrors();
164  /*
165  * Explicitly mask errors so PHPUnit work properly
166  */
167  $ret = @parent::loadHTML($source, $options);
168  $errors = $this->getLibXMLErrors();
169  self::restoreLibXMLErrors();
170  if ($ret === false) {
171  if (count($errors) <= 0) {
172  /*
173  * If source is empty, libxml_get_last_error report no errors at all
174  * so we mimic the expected error message.
175  */
176  if ($source == '') {
177  $error = new \libXMLError();
178  $error->message = 'general parsing error';
179  $error->level = LIBXML_ERR_FATAL;
180  $error->code = 0;
181  $error->line = 0;
182  $error->column = 0;
183  $error->file = '';
184  } else {
185  $error = new \libXMLError();
186  $error->message = 'Empty string supplied as input';
187  $error->level = LIBXML_ERR_ERROR;
188  $error->code = 0;
189  $error->line = 0;
190  $error->column = 0;
191  $error->file = '';
192  }
193  $errors[] = $error;
194  }
195  throw new XDOMDocumentException(sprintf("Error loading HTML data: %s", $errors[0]->message) , $errors);
196  }
197  return true;
198  }
199 }
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
$ret
$message
$filename
load($filename, $options=0,\libXMLError &$error=null)
loadXML($source, $options=0, &$errors=array())
__construct($message, &$libXMLErrors=array())
setLibXMLErrorIgnoreCodes($ignoreCodes)
loadHTML($source, $options=0, &$errors=array())
loadHTMLFile($filename, $options=0, &$errors=array())
← centre documentaire © anakeen