Xml.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  9. * @type HTMLPurifier_ConfigSchema_Interchange
  10. */
  11. protected $interchange;
  12. /**
  13. * @type string
  14. */
  15. private $namespace;
  16. /**
  17. * @param string $html
  18. */
  19. protected function writeHTMLDiv($html)
  20. {
  21. $this->startElement('div');
  22. $purifier = HTMLPurifier::getInstance();
  23. $html = $purifier->purify($html);
  24. $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
  25. $this->writeRaw($html);
  26. $this->endElement(); // div
  27. }
  28. /**
  29. * @param mixed $var
  30. * @return string
  31. */
  32. protected function export($var)
  33. {
  34. if ($var === array()) {
  35. return 'array()';
  36. }
  37. return var_export($var, true);
  38. }
  39. /**
  40. * @param HTMLPurifier_ConfigSchema_Interchange $interchange
  41. */
  42. public function build($interchange)
  43. {
  44. // global access, only use as last resort
  45. $this->interchange = $interchange;
  46. $this->setIndent(true);
  47. $this->startDocument('1.0', 'UTF-8');
  48. $this->startElement('configdoc');
  49. $this->writeElement('title', $interchange->name);
  50. foreach ($interchange->directives as $directive) {
  51. $this->buildDirective($directive);
  52. }
  53. if ($this->namespace) {
  54. $this->endElement();
  55. } // namespace
  56. $this->endElement(); // configdoc
  57. $this->flush();
  58. }
  59. /**
  60. * @param HTMLPurifier_ConfigSchema_Interchange_Directive $directive
  61. */
  62. public function buildDirective($directive)
  63. {
  64. // Kludge, although I suppose having a notion of a "root namespace"
  65. // certainly makes things look nicer when documentation is built.
  66. // Depends on things being sorted.
  67. if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) {
  68. if ($this->namespace) {
  69. $this->endElement();
  70. } // namespace
  71. $this->namespace = $directive->id->getRootNamespace();
  72. $this->startElement('namespace');
  73. $this->writeAttribute('id', $this->namespace);
  74. $this->writeElement('name', $this->namespace);
  75. }
  76. $this->startElement('directive');
  77. $this->writeAttribute('id', $directive->id->toString());
  78. $this->writeElement('name', $directive->id->getDirective());
  79. $this->startElement('aliases');
  80. foreach ($directive->aliases as $alias) {
  81. $this->writeElement('alias', $alias->toString());
  82. }
  83. $this->endElement(); // aliases
  84. $this->startElement('constraints');
  85. if ($directive->version) {
  86. $this->writeElement('version', $directive->version);
  87. }
  88. $this->startElement('type');
  89. if ($directive->typeAllowsNull) {
  90. $this->writeAttribute('allow-null', 'yes');
  91. }
  92. $this->text($directive->type);
  93. $this->endElement(); // type
  94. if ($directive->allowed) {
  95. $this->startElement('allowed');
  96. foreach ($directive->allowed as $value => $x) {
  97. $this->writeElement('value', $value);
  98. }
  99. $this->endElement(); // allowed
  100. }
  101. $this->writeElement('default', $this->export($directive->default));
  102. $this->writeAttribute('xml:space', 'preserve');
  103. if ($directive->external) {
  104. $this->startElement('external');
  105. foreach ($directive->external as $project) {
  106. $this->writeElement('project', $project);
  107. }
  108. $this->endElement();
  109. }
  110. $this->endElement(); // constraints
  111. if ($directive->deprecatedVersion) {
  112. $this->startElement('deprecated');
  113. $this->writeElement('version', $directive->deprecatedVersion);
  114. $this->writeElement('use', $directive->deprecatedUse->toString());
  115. $this->endElement(); // deprecated
  116. }
  117. $this->startElement('description');
  118. $this->writeHTMLDiv($directive->description);
  119. $this->endElement(); // description
  120. $this->endElement(); // directive
  121. }
  122. }
  123. // vim: et sw=4 sts=4