| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Converts HTMLPurifier_ConfigSchema_Interchange to an XML format, |
|---|
| 5 | * which can be further processed to generate documentation. |
|---|
| 6 | */ |
|---|
| 7 | class HTMLPurifier_ConfigSchema_Builder_Xml extends XMLWriter |
|---|
| 8 | { |
|---|
| 9 | |
|---|
| 10 | protected $interchange; |
|---|
| 11 | |
|---|
| 12 | protected function writeHTMLDiv($html) { |
|---|
| 13 | $this->startElement('div'); |
|---|
| 14 | |
|---|
| 15 | $purifier = HTMLPurifier::getInstance(); |
|---|
| 16 | $html = $purifier->purify($html); |
|---|
| 17 | $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); |
|---|
| 18 | $this->writeRaw($html); |
|---|
| 19 | |
|---|
| 20 | $this->endElement(); // div |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | protected function export($var) { |
|---|
| 24 | if ($var === array()) return 'array()'; |
|---|
| 25 | return var_export($var, true); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public function build($interchange) { |
|---|
| 29 | // global access, only use as last resort |
|---|
| 30 | $this->interchange = $interchange; |
|---|
| 31 | |
|---|
| 32 | $this->setIndent(true); |
|---|
| 33 | $this->startDocument('1.0', 'UTF-8'); |
|---|
| 34 | $this->startElement('configdoc'); |
|---|
| 35 | $this->writeElement('title', $interchange->name); |
|---|
| 36 | |
|---|
| 37 | foreach ($interchange->namespaces as $namespace) { |
|---|
| 38 | $this->buildNamespace($namespace); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | $this->endElement(); // configdoc |
|---|
| 42 | $this->flush(); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public function buildNamespace($namespace) { |
|---|
| 46 | $this->startElement('namespace'); |
|---|
| 47 | $this->writeAttribute('id', $namespace->namespace); |
|---|
| 48 | |
|---|
| 49 | $this->writeElement('name', $namespace->namespace); |
|---|
| 50 | $this->startElement('description'); |
|---|
| 51 | $this->writeHTMLDiv($namespace->description); |
|---|
| 52 | $this->endElement(); // description |
|---|
| 53 | |
|---|
| 54 | foreach ($this->interchange->directives as $directive) { |
|---|
| 55 | if ($directive->id->namespace !== $namespace->namespace) continue; |
|---|
| 56 | $this->buildDirective($directive); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $this->endElement(); // namespace |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public function buildDirective($directive) { |
|---|
| 63 | $this->startElement('directive'); |
|---|
| 64 | $this->writeAttribute('id', $directive->id->toString()); |
|---|
| 65 | |
|---|
| 66 | $this->writeElement('name', $directive->id->directive); |
|---|
| 67 | |
|---|
| 68 | $this->startElement('aliases'); |
|---|
| 69 | foreach ($directive->aliases as $alias) $this->writeElement('alias', $alias->toString()); |
|---|
| 70 | $this->endElement(); // aliases |
|---|
| 71 | |
|---|
| 72 | $this->startElement('constraints'); |
|---|
| 73 | if ($directive->version) $this->writeElement('version', $directive->version); |
|---|
| 74 | $this->startElement('type'); |
|---|
| 75 | if ($directive->typeAllowsNull) $this->writeAttribute('allow-null', 'yes'); |
|---|
| 76 | $this->text($directive->type); |
|---|
| 77 | $this->endElement(); // type |
|---|
| 78 | if ($directive->allowed) { |
|---|
| 79 | $this->startElement('allowed'); |
|---|
| 80 | foreach ($directive->allowed as $value => $x) $this->writeElement('value', $value); |
|---|
| 81 | $this->endElement(); // allowed |
|---|
| 82 | } |
|---|
| 83 | $this->writeElement('default', $this->export($directive->default)); |
|---|
| 84 | $this->writeAttribute('xml:space', 'preserve'); |
|---|
| 85 | if ($directive->external) { |
|---|
| 86 | $this->startElement('external'); |
|---|
| 87 | foreach ($directive->external as $project) $this->writeElement('project', $project); |
|---|
| 88 | $this->endElement(); |
|---|
| 89 | } |
|---|
| 90 | $this->endElement(); // constraints |
|---|
| 91 | |
|---|
| 92 | if ($directive->deprecatedVersion) { |
|---|
| 93 | $this->startElement('deprecated'); |
|---|
| 94 | $this->writeElement('version', $directive->deprecatedVersion); |
|---|
| 95 | $this->writeElement('use', $directive->deprecatedUse->toString()); |
|---|
| 96 | $this->endElement(); // deprecated |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | $this->startElement('description'); |
|---|
| 100 | $this->writeHTMLDiv($directive->description); |
|---|
| 101 | $this->endElement(); // description |
|---|
| 102 | |
|---|
| 103 | $this->endElement(); // directive |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | } |
|---|