[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/phing/tasks/ext/ -> XmlLintTask.php (source)

   1  <?php
   2  require_once 'phing/Task.php';
   3  
   4  /**
   5   * A XML lint task. Checking syntax of one or more XML files against an XML Schema using the DOM extension.
   6   *
   7   * @author   Knut Urdalen <knut.urdalen@telio.no>
   8   * @package  phing.tasks.ext
   9   */
  10  class XmlLintTask extends Task {
  11  
  12    protected $file;  // the source file (from xml attribute)
  13    protected $schema; // the schema file (from xml attribute)
  14    protected $filesets = array(); // all fileset objects assigned to this task
  15  
  16    /**
  17     * File to be performed syntax check on
  18     *
  19     * @param PhingFile $file
  20     */
  21    public function setFile(PhingFile $file) {
  22      $this->file = $file;
  23    }
  24  
  25    /**
  26     * XML Schema Description file to validate against
  27     *
  28     * @param PhingFile $schema
  29     */
  30    public function setSchema(PhingFile $schema) {
  31      $this->schema = $schema;
  32    }
  33    
  34    /**
  35     * Nested creator, creates a FileSet for this task
  36     *
  37     * @return FileSet The created fileset object
  38     */
  39    function createFileSet() {
  40      $num = array_push($this->filesets, new FileSet());
  41      return $this->filesets[$num-1];
  42    }
  43  
  44    /**
  45     * Execute lint check against PhingFile or a FileSet
  46     */
  47    public function main() {
  48      if(!isset($this->schema)) {
  49        throw new BuildException("Missing attribute 'schema'");
  50      }
  51      $schema = $this->schema->getPath();
  52      if(!file_exists($schema)) {
  53        throw new BuildException("File not found: ".$schema);
  54      }
  55      if(!isset($this->file) and count($this->filesets) == 0) {
  56        throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  57      }
  58  
  59      set_error_handler(array($this, 'errorHandler'));
  60      if($this->file instanceof PhingFile) {
  61        $this->lint($this->file->getPath());
  62      } else { // process filesets
  63        $project = $this->getProject();
  64        foreach($this->filesets as $fs) {
  65      $ds = $fs->getDirectoryScanner($project);
  66      $files = $ds->getIncludedFiles();
  67      $dir = $fs->getDir($this->project)->getPath();
  68      foreach($files as $file) {
  69        $this->lint($dir.DIRECTORY_SEPARATOR.$file);
  70      }
  71        }
  72      }
  73      restore_error_handler();
  74    }
  75  
  76    /**
  77     * Performs validation
  78     *
  79     * @param string $file
  80     * @return void
  81     */
  82    protected function lint($file) {
  83      if(file_exists($file)) {
  84        if(is_readable($file)) {
  85      $dom = new DOMDocument();
  86      $dom->load($file);
  87      if($dom->schemaValidate($this->schema->getPath())) {
  88        $this->log($file.' validated', PROJECT_MSG_INFO);
  89      } else {
  90        $this->log($file.' fails to validate (See messages above)', PROJECT_MSG_ERR);
  91      }
  92        } else {
  93      throw new BuildException('Permission denied: '.$file);
  94        }
  95      } else {
  96        throw new BuildException('File not found: '.$file);
  97      }
  98    }
  99  
 100    /**
 101     * Local error handler to catch validation errors and log them through Phing
 102     *
 103     * @param int    $level
 104     * @param string $message
 105     * @param string $file
 106     * @param int    $line
 107     */
 108    public function errorHandler($level, $message, $file, $line, $context) {
 109      $matches = array();
 110      preg_match('/^.*\(\): (.*)$/', $message, $matches);
 111      $this->log($matches[1], PROJECT_MSG_ERR);
 112    }
 113  
 114  }
 115  
 116  ?>


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7