/**
  * Cast a given value
  *
  * Error codes returned are:
  * <ul>
  *   <li>invalid_chars - if input contains characters not allowed for XML</li>
  *   <li>not_well_formed - if input cannot be parsed to XML</li>
  * </ul>
  *
  * @param   array value
  * @return  string error or array on success
  */
 public function check($value)
 {
     foreach ($value as $v) {
         if (strlen($v) > strcspn($v, Node::XML_ILLEGAL_CHARS)) {
             return 'invalid_chars';
         }
         try {
             $p = new XMLParser();
             $p->parse('<doc>' . $v . '</doc>');
         } catch (\xml\XMLFormatException $e) {
             return 'not_well_formed';
         }
     }
     return null;
 }
Example #2
0
 /**
  * Construct an XML tree from a file.
  *
  * <code>
  *   $tree= Tree::fromFile(new File('foo.xml'));
  * </code>
  *
  * @param   io.File file
  * @param   string c default __CLASS__ class name
  * @return  xml.Tree
  * @throws  xml.XMLFormatException in case of a parser error
  * @throws  io.IOException in case reading the file fails
  */
 public static function fromFile($file, $c = __CLASS__)
 {
     $parser = new XMLParser();
     $tree = new $c();
     $parser->setCallback($tree);
     $parser->parse(FileUtil::getContents($file));
     // Fetch actual encoding from parser
     $tree->setEncoding($parser->getEncoding());
     unset($parser);
     return $tree;
 }