From 2ff7bcfc5d05fc0bd01201af33f80dae2f9cd688 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 13 Jan 2009 10:56:50 -0500 Subject: Move low-level xml outputting code to a class Made a class for outputting XML code --- lib/xmloutputter.php | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 lib/xmloutputter.php (limited to 'lib/xmloutputter.php') diff --git a/lib/xmloutputter.php b/lib/xmloutputter.php new file mode 100644 index 000000000..9a7d12e4c --- /dev/null +++ b/lib/xmloutputter.php @@ -0,0 +1,223 @@ +. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Low-level generator for XML + * + * This is a thin wrapper around PHP's XMLWriter. The main + * advantage is the element() method, which simplifies outputting + * an element. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * @see Action + * @see HTMLOutputter + */ + +class XMLOutputter +{ + /** + * Wrapped XMLWriter object, which does most of the heavy lifting + * for output. + */ + + var $xw = null; + + /** + * Constructor + * + * Initializes the wrapped XMLWriter. + * + * @param $output URL for outputting, defaults to stdout + * @param $indent Whether to indent output, default true + */ + + function __construct($output='php://output', $indent=true) + { + $this->xw = new XMLWriter(); + $this->xw->openURI($output); + $this->xw->setIndent($indent); + $this->xw->startDocument('1.0', 'UTF-8'); + } + + /** + * Start a new XML document + * + * @param string $doc document element + * @param string $public public identifier + * @param string $system system identifier + * + * @return void + */ + + function startXML($doc=null, $public=null, $system=null) + { + if ($doc) { + $this->xw->writeDTD($doc, $public, $system); + } + } + + /** + * finish an XML document + * + * It's probably a bad idea to continue to use this object + * after calling endXML(). + * + * @return void + */ + + function endXML() + { + $this->xw->endDocument(); + $this->xw->flush(); + } + + /** + * output an XML element + * + * Utility for outputting an XML element. A convenient wrapper + * for a bunch of longer XMLWriter calls. This is best for + * when an element doesn't have any sub-elements; if that's the + * case, use elementStart() and elementEnd() instead. + * + * The $content element will be escaped for XML. If you need + * raw output, use elementStart() and elementEnd() with a call + * to raw() in the middle. + * + * @param string $tag Element type or tagname + * @param array $attrs Array of element attributes, as + * key-value pairs + * @param string $content string content of the element + * + * @return void + */ + + function element($tag, $attrs=null, $content=null) + { + $this->elementStart($tag, $attrs); + if (!is_null($content)) { + $this->xw->text($content); + } + $this->elementEnd($tag); + } + + /** + * output a start tag for an element + * + * Mostly used for when an element has content that's + * not a simple string. + * + * @param string $tag Element type or tagname + * @param array $attrs Array of element attributes + * + * @return void + */ + + function elementStart($tag, $attrs=null) + { + $this->xw->startElement($tag); + if (is_array($attrs)) { + foreach ($attrs as $name => $value) { + $this->xw->writeAttribute($name, $value); + } + } else if (is_string($attrs)) { + $this->xw->writeAttribute('class', $attrs); + } + } + + /** + * output an end tag for an element + * + * Used in conjunction with elementStart(). $tag param + * should match the elementStart() param. + * + * For HTML 4 compatibility, this method will force + * a full end element () even if the element is + * empty, except for a handful of exception tagnames. + * This is a hack. + * + * @param string $tag Element type or tagname. + * + * @return void + */ + + function elementEnd($tag) + { + static $empty_tag = array('base', 'meta', 'link', 'hr', + 'br', 'param', 'img', 'area', + 'input', 'col'); + // XXX: check namespace + if (in_array($tag, $empty_tag)) { + $this->xw->endElement(); + } else { + $this->xw->fullEndElement(); + } + } + + /** + * output plain text + * + * Text will be escaped. If you need it not to be, + * use raw() instead. + * + * @param string $txt Text to output. + * + * @return void + */ + + function text($txt) + { + $this->xw->text($txt); + } + + /** + * output raw xml + * + * This will spit out its argument verbatim -- no escaping is + * done. + * + * @param string $xml XML to output. + * + * @return void + */ + + function raw($xml) + { + $this->xw->writeRaw($xml); + } +} -- cgit v1.2.3-54-g00ecf From 10fcbe9da3456d7f7c07bd1192051602bd8b0762 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 13 Jan 2009 11:47:01 -0500 Subject: Make XMLOutputter work with phpcs A couple of small changes to comply with code standards. Also, note that the second param of element() and elementStart() can be a string. --- lib/xmloutputter.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/xmloutputter.php') diff --git a/lib/xmloutputter.php b/lib/xmloutputter.php index 9a7d12e4c..5bbd35627 100644 --- a/lib/xmloutputter.php +++ b/lib/xmloutputter.php @@ -63,8 +63,8 @@ class XMLOutputter * * Initializes the wrapped XMLWriter. * - * @param $output URL for outputting, defaults to stdout - * @param $indent Whether to indent output, default true + * @param string $output URL for outputting, defaults to stdout + * @param boolean $indent Whether to indent output, default true */ function __construct($output='php://output', $indent=true) @@ -119,6 +119,9 @@ class XMLOutputter * raw output, use elementStart() and elementEnd() with a call * to raw() in the middle. * + * If $attrs is a string instead of an array, it will be treated + * as the class attribute of the element. + * * @param string $tag Element type or tagname * @param array $attrs Array of element attributes, as * key-value pairs @@ -142,6 +145,9 @@ class XMLOutputter * Mostly used for when an element has content that's * not a simple string. * + * If $attrs is a string instead of an array, it will be treated + * as the class attribute of the element. + * * @param string $tag Element type or tagname * @param array $attrs Array of element attributes * -- cgit v1.2.3-54-g00ecf From 3ce62aae1b1a0e176db32fac142218f05bc57731 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 14 Jan 2009 13:57:29 -0500 Subject: Add comment() method, have action use it --- lib/action.php | 5 ++--- lib/xmloutputter.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'lib/xmloutputter.php') diff --git a/lib/action.php b/lib/action.php index 0419828b3..8d0fb2634 100644 --- a/lib/action.php +++ b/lib/action.php @@ -116,8 +116,8 @@ class Action extends HTMLOutputter // lawsuit foreach (array(6,7) as $ver) { if (file_exists(theme_file('ie'.$ver.'.css'))) { // Yes, IE people should be put in jail. - $xw->writeComment('[if lte IE '.$ver.']>comment('[if lte IE '.$ver.']>elementStart('div', array('id' => 'content')); } - // Added @id to li for some control. We might want to move this to htmloutputter.php function common_menu_item($id=null, $url, $text, $title=null, $is_selected=false) { diff --git a/lib/xmloutputter.php b/lib/xmloutputter.php index 5bbd35627..9ca0c91bd 100644 --- a/lib/xmloutputter.php +++ b/lib/xmloutputter.php @@ -226,4 +226,17 @@ class XMLOutputter { $this->xw->writeRaw($xml); } + + /** + * output a comment + * + * @param string $txt text of the comment + * + * @return void + */ + + function comment($txt) + { + $this->xw->writeComment($txt); + } } -- cgit v1.2.3-54-g00ecf From ff901332d2a64766472921d3d3742dc913540508 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 15 Jan 2009 14:21:47 -0500 Subject: Fix the constructor for Action Also, fix the startXML() method --- lib/action.php | 17 +++++++++++++++-- lib/htmloutputter.php | 2 -- lib/xmloutputter.php | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/xmloutputter.php') diff --git a/lib/action.php b/lib/action.php index 8694bf47c..4aff4264d 100644 --- a/lib/action.php +++ b/lib/action.php @@ -58,8 +58,21 @@ class Action extends HTMLOutputter // lawsuit { var $args; - function Action() - { + /** + * Constructor + * + * Just wraps the HTMLOutputter constructor. + * + * @param string $output URI to output to, default = stdout + * @param boolean $indent Whether to indent output, default true + * + * @see XMLOutputter::__construct + * @see HTMLOutputter::__construct + */ + + function __construct($output='php://output', $indent=true) + { + parent::__construct($output, $indent); } // For initializing members of the class diff --git a/lib/htmloutputter.php b/lib/htmloutputter.php index 43e4a59a9..75a995bef 100644 --- a/lib/htmloutputter.php +++ b/lib/htmloutputter.php @@ -122,7 +122,6 @@ class HTMLOutputter extends XMLOutputter 'lang' => $language)); } - /** * Ends an HTML document * @@ -134,7 +133,6 @@ class HTMLOutputter extends XMLOutputter $this->endXML(); } - /** * Output an HTML text input element * diff --git a/lib/xmloutputter.php b/lib/xmloutputter.php index 9ca0c91bd..64935da40 100644 --- a/lib/xmloutputter.php +++ b/lib/xmloutputter.php @@ -72,7 +72,6 @@ class XMLOutputter $this->xw = new XMLWriter(); $this->xw->openURI($output); $this->xw->setIndent($indent); - $this->xw->startDocument('1.0', 'UTF-8'); } /** @@ -87,6 +86,7 @@ class XMLOutputter function startXML($doc=null, $public=null, $system=null) { + $this->xw->startDocument('1.0', 'UTF-8'); if ($doc) { $this->xw->writeDTD($doc, $public, $system); } -- cgit v1.2.3-54-g00ecf