File indexing completed on 2024-12-22 05:36:19

0001 <?php
0002 
0003 /**
0004  * Converts HTMLPurifier_ConfigSchema_Interchange to an XML format,
0005  * which can be further processed to generate documentation.
0006  */
0007 class HTMLPurifier_ConfigSchema_Builder_Xml extends XMLWriter
0008 {
0009 
0010     /**
0011      * @type HTMLPurifier_ConfigSchema_Interchange
0012      */
0013     protected $interchange;
0014 
0015     /**
0016      * @type string
0017      */
0018     private $namespace;
0019 
0020     /**
0021      * @param string $html
0022      */
0023     protected function writeHTMLDiv($html)
0024     {
0025         $this->startElement('div');
0026 
0027         $purifier = HTMLPurifier::getInstance();
0028         $html = $purifier->purify($html);
0029         $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
0030         $this->writeRaw($html);
0031 
0032         $this->endElement(); // div
0033     }
0034 
0035     /**
0036      * @param mixed $var
0037      * @return string
0038      */
0039     protected function export($var)
0040     {
0041         if ($var === array()) {
0042             return 'array()';
0043         }
0044         return var_export($var, true);
0045     }
0046 
0047     /**
0048      * @param HTMLPurifier_ConfigSchema_Interchange $interchange
0049      */
0050     public function build($interchange)
0051     {
0052         // global access, only use as last resort
0053         $this->interchange = $interchange;
0054 
0055         $this->setIndent(true);
0056         $this->startDocument('1.0', 'UTF-8');
0057         $this->startElement('configdoc');
0058         $this->writeElement('title', $interchange->name);
0059 
0060         foreach ($interchange->directives as $directive) {
0061             $this->buildDirective($directive);
0062         }
0063 
0064         if ($this->namespace) {
0065             $this->endElement();
0066         } // namespace
0067 
0068         $this->endElement(); // configdoc
0069         $this->flush();
0070     }
0071 
0072     /**
0073      * @param HTMLPurifier_ConfigSchema_Interchange_Directive $directive
0074      */
0075     public function buildDirective($directive)
0076     {
0077         // Kludge, although I suppose having a notion of a "root namespace"
0078         // certainly makes things look nicer when documentation is built.
0079         // Depends on things being sorted.
0080         if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) {
0081             if ($this->namespace) {
0082                 $this->endElement();
0083             } // namespace
0084             $this->namespace = $directive->id->getRootNamespace();
0085             $this->startElement('namespace');
0086             $this->writeAttribute('id', $this->namespace);
0087             $this->writeElement('name', $this->namespace);
0088         }
0089 
0090         $this->startElement('directive');
0091         $this->writeAttribute('id', $directive->id->toString());
0092 
0093         $this->writeElement('name', $directive->id->getDirective());
0094 
0095         $this->startElement('aliases');
0096         foreach ($directive->aliases as $alias) {
0097             $this->writeElement('alias', $alias->toString());
0098         }
0099         $this->endElement(); // aliases
0100 
0101         $this->startElement('constraints');
0102         if ($directive->version) {
0103             $this->writeElement('version', $directive->version);
0104         }
0105         $this->startElement('type');
0106         if ($directive->typeAllowsNull) {
0107             $this->writeAttribute('allow-null', 'yes');
0108         }
0109         $this->text($directive->type);
0110         $this->endElement(); // type
0111         if ($directive->allowed) {
0112             $this->startElement('allowed');
0113             foreach ($directive->allowed as $value => $x) {
0114                 $this->writeElement('value', $value);
0115             }
0116             $this->endElement(); // allowed
0117         }
0118         $this->writeElement('default', $this->export($directive->default));
0119         $this->writeAttribute('xml:space', 'preserve');
0120         if ($directive->external) {
0121             $this->startElement('external');
0122             foreach ($directive->external as $project) {
0123                 $this->writeElement('project', $project);
0124             }
0125             $this->endElement();
0126         }
0127         $this->endElement(); // constraints
0128 
0129         if ($directive->deprecatedVersion) {
0130             $this->startElement('deprecated');
0131             $this->writeElement('version', $directive->deprecatedVersion);
0132             $this->writeElement('use', $directive->deprecatedUse->toString());
0133             $this->endElement(); // deprecated
0134         }
0135 
0136         $this->startElement('description');
0137         $this->writeHTMLDiv($directive->description);
0138         $this->endElement(); // description
0139 
0140         $this->endElement(); // directive
0141     }
0142 }
0143 
0144 // vim: et sw=4 sts=4