1<?PHP
2/**
3 * XML/Beautifier.php
4 *
5 * Format XML files containing unknown entities (like all of peardoc)
6 *
7 * phpDocumentor :: automatic documentation generator
8 *
9 * PHP versions 4 and 5
10 *
11 * Copyright (c) 2004-2006 Gregory Beaver
12 *
13 * LICENSE:
14 *
15 * This library is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU Lesser General
17 * Public License as published by the Free Software Foundation;
18 * either version 2.1 of the License, or (at your option) any
19 * later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * @package    phpDocumentor
31 * @subpackage Parsers
32 * @author     Greg Beaver <cellog@php.net>
33 * @copyright  2004-2006 Gregory Beaver
34 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
35 * @version    CVS: $Id: Beautifier.php 212211 2006-04-30 22:18:14Z cellog $
36 * @filesource
37 * @link       http://www.phpdoc.org
38 * @link       http://pear.php.net/PhpDocumentor
39 * @since      1.3.0
40 */
41
42
43/**
44 * This is just like XML_Beautifier, but uses {@link phpDocumentor_XML_Beautifier_Tokenizer}
45 * @package phpDocumentor
46 * @subpackage Parsers
47 * @since 1.3.0
48 */
49class phpDocumentor_peardoc2_XML_Beautifier extends XML_Beautifier {
50
51   /**
52    * format a file or URL
53    *
54    * @access public
55    * @param  string    $file       filename
56    * @param  mixed     $newFile    filename for beautified XML file (if none is given, the XML string will be returned.)
57    *                               if you want overwrite the original file, use XML_BEAUTIFIER_OVERWRITE
58    * @param  string    $renderer   Renderer to use, default is the plain xml renderer
59    * @return mixed                 XML string of no file should be written, true if file could be written
60    * @throws PEAR_Error
61    * @uses   _loadRenderer() to load the desired renderer
62    */
63    function formatFile($file, $newFile = null, $renderer = "Plain")
64    {
65        if ($this->apiVersion() != '1.0') {
66            return $this->raiseError('API version must be 1.0');
67        }
68        /**
69         * Split the document into tokens
70         * using the XML_Tokenizer
71         */
72        require_once dirname(__FILE__) . '/Tokenizer.php';
73        $tokenizer = new phpDocumentor_XML_Beautifier_Tokenizer();
74
75        $tokens = $tokenizer->tokenize( $file, true );
76
77        if (PEAR::isError($tokens)) {
78            return $tokens;
79        }
80
81        include_once dirname(__FILE__) . '/Plain.php';
82        $renderer = new PHPDoc_XML_Beautifier_Renderer_Plain($this->_options);
83
84        $xml = $renderer->serialize($tokens);
85
86        if ($newFile == null) {
87            return $xml;
88        }
89
90        $fp = @fopen($newFile, "w");
91        if (!$fp) {
92            return PEAR::raiseError("Could not write to output file", XML_BEAUTIFIER_ERROR_NO_OUTPUT_FILE);
93        }
94
95        flock($fp, LOCK_EX);
96        fwrite($fp, $xml);
97        flock($fp, LOCK_UN);
98        fclose($fp);
99        return true;    }
100
101   /**
102    * format an XML string
103    *
104    * @access public
105    * @param  string    $string     XML
106    * @return string    formatted XML string
107    * @throws PEAR_Error
108    */
109    function formatString($string, $renderer = "Plain")
110    {
111        if ($this->apiVersion() != '1.0') {
112            return $this->raiseError('API version must be 1.0');
113        }
114        /**
115         * Split the document into tokens
116         * using the XML_Tokenizer
117         */
118        require_once dirname(__FILE__) . '/Tokenizer.php';
119        $tokenizer = new phpDocumentor_XML_Beautifier_Tokenizer();
120
121        $tokens = $tokenizer->tokenize( $string, false );
122
123        if (PEAR::isError($tokens)) {
124            return $tokens;
125        }
126
127        include_once dirname(__FILE__) . '/Plain.php';
128        $renderer = new PHPDoc_XML_Beautifier_Renderer_Plain($this->_options);
129
130        $xml = $renderer->serialize($tokens);
131
132        return $xml;
133    }
134}
135?>