XmlLintTask.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * $Id: XmlLintTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * A XML lint task. Checking syntax of one or more XML files against an XML Schema using the DOM extension.
  24. *
  25. * @author Knut Urdalen <knut.urdalen@telio.no>
  26. * @version $Id: XmlLintTask.php 905 2010-10-05 16:28:03Z mrook $
  27. * @package phing.tasks.ext
  28. */
  29. class XmlLintTask extends Task {
  30. protected $file; // the source file (from xml attribute)
  31. protected $schema; // the schema file (from xml attribute)
  32. protected $filesets = array(); // all fileset objects assigned to this task
  33. /**
  34. * File to be performed syntax check on
  35. *
  36. * @param PhingFile $file
  37. */
  38. public function setFile(PhingFile $file) {
  39. $this->file = $file;
  40. }
  41. /**
  42. * XML Schema Description file to validate against
  43. *
  44. * @param PhingFile $schema
  45. */
  46. public function setSchema(PhingFile $schema) {
  47. $this->schema = $schema;
  48. }
  49. /**
  50. * Nested creator, creates a FileSet for this task
  51. *
  52. * @return FileSet The created fileset object
  53. */
  54. function createFileSet() {
  55. $num = array_push($this->filesets, new FileSet());
  56. return $this->filesets[$num-1];
  57. }
  58. /**
  59. * Execute lint check against PhingFile or a FileSet
  60. */
  61. public function main() {
  62. if(!isset($this->schema)) {
  63. throw new BuildException("Missing attribute 'schema'");
  64. }
  65. $schema = $this->schema->getPath();
  66. if(!file_exists($schema)) {
  67. throw new BuildException("File not found: ".$schema);
  68. }
  69. if(!isset($this->file) and count($this->filesets) == 0) {
  70. throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  71. }
  72. set_error_handler(array($this, 'errorHandler'));
  73. if($this->file instanceof PhingFile) {
  74. $this->lint($this->file->getPath());
  75. } else { // process filesets
  76. $project = $this->getProject();
  77. foreach($this->filesets as $fs) {
  78. $ds = $fs->getDirectoryScanner($project);
  79. $files = $ds->getIncludedFiles();
  80. $dir = $fs->getDir($this->project)->getPath();
  81. foreach($files as $file) {
  82. $this->lint($dir.DIRECTORY_SEPARATOR.$file);
  83. }
  84. }
  85. }
  86. restore_error_handler();
  87. }
  88. /**
  89. * Performs validation
  90. *
  91. * @param string $file
  92. * @return void
  93. */
  94. protected function lint($file) {
  95. if(file_exists($file)) {
  96. if(is_readable($file)) {
  97. $dom = new DOMDocument();
  98. $dom->load($file);
  99. if($dom->schemaValidate($this->schema->getPath())) {
  100. $this->log($file.' validated', Project::MSG_INFO);
  101. } else {
  102. $this->log($file.' fails to validate (See messages above)', Project::MSG_ERR);
  103. }
  104. } else {
  105. throw new BuildException('Permission denied: '.$file);
  106. }
  107. } else {
  108. throw new BuildException('File not found: '.$file);
  109. }
  110. }
  111. /**
  112. * Local error handler to catch validation errors and log them through Phing
  113. *
  114. * @param int $level
  115. * @param string $message
  116. * @param string $file
  117. * @param int $line
  118. */
  119. public function errorHandler($level, $message, $file, $line, $context) {
  120. $matches = array();
  121. preg_match('/^.*\(\): (.*)$/', $message, $matches);
  122. $this->log($matches[1], Project::MSG_ERR);
  123. }
  124. }