Xml.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Converts HTMLPurifier_ConfigSchema_Interchange to an XML format,
  4. * which can be further processed to generate documentation.
  5. */
  6. class HTMLPurifier_ConfigSchema_Builder_Xml extends XMLWriter
  7. {
  8. protected $interchange;
  9. private $namespace;
  10. protected function writeHTMLDiv($html) {
  11. $this->startElement('div');
  12. $purifier = HTMLPurifier::getInstance();
  13. $html = $purifier->purify($html);
  14. $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
  15. $this->writeRaw($html);
  16. $this->endElement(); // div
  17. }
  18. protected function export($var) {
  19. if ($var === array()) return 'array()';
  20. return var_export($var, true);
  21. }
  22. public function build($interchange) {
  23. // global access, only use as last resort
  24. $this->interchange = $interchange;
  25. $this->setIndent(true);
  26. $this->startDocument('1.0', 'UTF-8');
  27. $this->startElement('configdoc');
  28. $this->writeElement('title', $interchange->name);
  29. foreach ($interchange->directives as $directive) {
  30. $this->buildDirective($directive);
  31. }
  32. if ($this->namespace) $this->endElement(); // namespace
  33. $this->endElement(); // configdoc
  34. $this->flush();
  35. }
  36. public function buildDirective($directive) {
  37. // Kludge, although I suppose having a notion of a "root namespace"
  38. // certainly makes things look nicer when documentation is built.
  39. // Depends on things being sorted.
  40. if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) {
  41. if ($this->namespace) $this->endElement(); // namespace
  42. $this->namespace = $directive->id->getRootNamespace();
  43. $this->startElement('namespace');
  44. $this->writeAttribute('id', $this->namespace);
  45. $this->writeElement('name', $this->namespace);
  46. }
  47. $this->startElement('directive');
  48. $this->writeAttribute('id', $directive->id->toString());
  49. $this->writeElement('name', $directive->id->getDirective());
  50. $this->startElement('aliases');
  51. foreach ($directive->aliases as $alias) $this->writeElement('alias', $alias->toString());
  52. $this->endElement(); // aliases
  53. $this->startElement('constraints');
  54. if ($directive->version) $this->writeElement('version', $directive->version);
  55. $this->startElement('type');
  56. if ($directive->typeAllowsNull) $this->writeAttribute('allow-null', 'yes');
  57. $this->text($directive->type);
  58. $this->endElement(); // type
  59. if ($directive->allowed) {
  60. $this->startElement('allowed');
  61. foreach ($directive->allowed as $value => $x) $this->writeElement('value', $value);
  62. $this->endElement(); // allowed
  63. }
  64. $this->writeElement('default', $this->export($directive->default));
  65. $this->writeAttribute('xml:space', 'preserve');
  66. if ($directive->external) {
  67. $this->startElement('external');
  68. foreach ($directive->external as $project) $this->writeElement('project', $project);
  69. $this->endElement();
  70. }
  71. $this->endElement(); // constraints
  72. if ($directive->deprecatedVersion) {
  73. $this->startElement('deprecated');
  74. $this->writeElement('version', $directive->deprecatedVersion);
  75. $this->writeElement('use', $directive->deprecatedUse->toString());
  76. $this->endElement(); // deprecated
  77. }
  78. $this->startElement('description');
  79. $this->writeHTMLDiv($directive->description);
  80. $this->endElement(); // description
  81. $this->endElement(); // directive
  82. }
  83. }
  84. // vim: et sw=4 sts=4