aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Tool/Xmllint.php
blob: 84881384c56b932bf5146578c38d6205c5828b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace phorkie;

class Tool_Xmllint
{
    public static $arSupportedExtensions = array(
        'htm', 'html', 'xml'
    );

    public function run(File $file)
    {
        $fpath = $file->getFullPath();
        $fpathlen = strlen($fpath);

        $res = new Tool_Result();
        $cmd = 'xmllint --noout ' . escapeshellarg($fpath) . ' 2>&1';
        exec($cmd, $output, $retval);
        if ($retval == 0) {
            $res->annotations['general'][] = new Tool_Result_Line(
                'XML is well-formed', 'ok'
            );
            return $res;
        }

        for ($i = 0; $i < count($output); $i += 3) {
            $line = $output[$i];
            if (substr($line, 0, $fpathlen) != $fpath) {
                throw new Exception('xmllint does not behave as expected: ' . $line);
            }
            list($linenum, $msg) = explode(':', substr($line, $fpathlen + 1), 2);
            $res->annotations[$linenum][] = new Tool_Result_Line(
                $msg, 'error'
            );
        }

        $res->annotations['general'][] = new Tool_Result_Line(
            'XML is not well-formed', 'error'
        );

        return $res;
    }
}

?>