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