friendica-github/vendor/ezyang/htmlpurifier/library/HTMLPurifier/ConfigSchema/Builder/Xml.php

145 lines
4.3 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
/**
* Converts HTMLPurifier_ConfigSchema_Interchange to an XML format,
* which can be further processed to generate documentation.
*/
class HTMLPurifier_ConfigSchema_Builder_Xml extends XMLWriter
{
2016-02-09 10:06:17 +00:00
/**
* @type HTMLPurifier_ConfigSchema_Interchange
*/
2010-09-09 03:14:17 +00:00
protected $interchange;
2016-02-09 10:06:17 +00:00
/**
* @type string
*/
2010-09-09 03:14:17 +00:00
private $namespace;
2016-02-09 10:06:17 +00:00
/**
* @param string $html
*/
protected function writeHTMLDiv($html)
{
2010-09-09 03:14:17 +00:00
$this->startElement('div');
$purifier = HTMLPurifier::getInstance();
$html = $purifier->purify($html);
$this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$this->writeRaw($html);
$this->endElement(); // div
}
2016-02-09 10:06:17 +00:00
/**
* @param mixed $var
* @return string
*/
protected function export($var)
{
if ($var === array()) {
return 'array()';
}
2010-09-09 03:14:17 +00:00
return var_export($var, true);
}
2016-02-09 10:06:17 +00:00
/**
* @param HTMLPurifier_ConfigSchema_Interchange $interchange
*/
public function build($interchange)
{
2010-09-09 03:14:17 +00:00
// global access, only use as last resort
$this->interchange = $interchange;
$this->setIndent(true);
$this->startDocument('1.0', 'UTF-8');
$this->startElement('configdoc');
$this->writeElement('title', $interchange->name);
foreach ($interchange->directives as $directive) {
$this->buildDirective($directive);
}
2016-02-09 10:06:17 +00:00
if ($this->namespace) {
$this->endElement();
} // namespace
2010-09-09 03:14:17 +00:00
$this->endElement(); // configdoc
$this->flush();
}
2016-02-09 10:06:17 +00:00
/**
* @param HTMLPurifier_ConfigSchema_Interchange_Directive $directive
*/
public function buildDirective($directive)
{
2010-09-09 03:14:17 +00:00
// Kludge, although I suppose having a notion of a "root namespace"
// certainly makes things look nicer when documentation is built.
// Depends on things being sorted.
if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) {
2016-02-09 10:06:17 +00:00
if ($this->namespace) {
$this->endElement();
} // namespace
2010-09-09 03:14:17 +00:00
$this->namespace = $directive->id->getRootNamespace();
$this->startElement('namespace');
$this->writeAttribute('id', $this->namespace);
$this->writeElement('name', $this->namespace);
}
$this->startElement('directive');
$this->writeAttribute('id', $directive->id->toString());
$this->writeElement('name', $directive->id->getDirective());
$this->startElement('aliases');
2016-02-09 10:06:17 +00:00
foreach ($directive->aliases as $alias) {
$this->writeElement('alias', $alias->toString());
}
2010-09-09 03:14:17 +00:00
$this->endElement(); // aliases
$this->startElement('constraints');
2016-02-09 10:06:17 +00:00
if ($directive->version) {
$this->writeElement('version', $directive->version);
}
$this->startElement('type');
if ($directive->typeAllowsNull) {
$this->writeAttribute('allow-null', 'yes');
}
$this->text($directive->type);
$this->endElement(); // type
if ($directive->allowed) {
$this->startElement('allowed');
foreach ($directive->allowed as $value => $x) {
$this->writeElement('value', $value);
2010-09-09 03:14:17 +00:00
}
2016-02-09 10:06:17 +00:00
$this->endElement(); // allowed
}
$this->writeElement('default', $this->export($directive->default));
$this->writeAttribute('xml:space', 'preserve');
if ($directive->external) {
$this->startElement('external');
foreach ($directive->external as $project) {
$this->writeElement('project', $project);
2010-09-09 03:14:17 +00:00
}
2016-02-09 10:06:17 +00:00
$this->endElement();
}
2010-09-09 03:14:17 +00:00
$this->endElement(); // constraints
if ($directive->deprecatedVersion) {
$this->startElement('deprecated');
2016-02-09 10:06:17 +00:00
$this->writeElement('version', $directive->deprecatedVersion);
$this->writeElement('use', $directive->deprecatedUse->toString());
2010-09-09 03:14:17 +00:00
$this->endElement(); // deprecated
}
$this->startElement('description');
2016-02-09 10:06:17 +00:00
$this->writeHTMLDiv($directive->description);
2010-09-09 03:14:17 +00:00
$this->endElement(); // description
$this->endElement(); // directive
}
}
// vim: et sw=4 sts=4