Skip to content
Snippets Groups Projects
Commit c76426b5 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Fix namespaces.

parent 44818e22
No related branches found
No related tags found
No related merge requests found
...@@ -14,18 +14,21 @@ class XML ...@@ -14,18 +14,21 @@ class XML
/** /**
* Format a DOM element. * Format a DOM element.
* *
* This function takes in a DOM element, and inserts whitespace to make it more * This function takes in a DOM element, and inserts whitespace to make it more readable. Note that whitespace
* readable. Note that whitespace added previously will be removed. * added previously will be removed.
* *
* @param DOMElement $root The root element which should be formatted. * @param \DOMElement $root The root element which should be formatted.
* @param string $indentBase The indentation this element should be assumed to * @param string $indentBase The indentation this element should be assumed to have. Defaults to an empty
* have. Default is an empty string. * string.
* *
* @throws \SimpleSAML_Error_Exception If $root is not a DOMElement or $indentBase is not a string.
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/ */
public static function formatDOMElement(DOMElement $root, $indentBase = '') public static function formatDOMElement(\DOMElement $root, $indentBase = '')
{ {
assert(is_string($indentBase)); if (!is_string($indentBase)) {
throw new \SimpleSAML_Error_Exception('Invalid input parameters');
}
// check what this element contains // check what this element contains
$fullText = ''; // all text in this element $fullText = ''; // all text in this element
...@@ -34,10 +37,10 @@ class XML ...@@ -34,10 +37,10 @@ class XML
for ($i = 0; $i < $root->childNodes->length; $i++) { for ($i = 0; $i < $root->childNodes->length; $i++) {
$child = $root->childNodes->item($i); $child = $root->childNodes->item($i);
if ($child instanceof DOMText) { if ($child instanceof \DOMText) {
$textNodes[] = $child; $textNodes[] = $child;
$fullText .= $child->wholeText; $fullText .= $child->wholeText;
} elseif ($child instanceof DOMComment || $child instanceof DOMElement) { } elseif ($child instanceof \DOMComment || $child instanceof \DOMElement) {
$childNodes[] = $child; $childNodes[] = $child;
} else { } else {
// unknown node type. We don't know how to format this // unknown node type. We don't know how to format this
...@@ -47,7 +50,7 @@ class XML ...@@ -47,7 +50,7 @@ class XML
$fullText = trim($fullText); $fullText = trim($fullText);
if (strlen($fullText) > 0) { if (strlen($fullText) > 0) {
// we contain text // we contain textelf
$hasText = true; $hasText = true;
} else { } else {
$hasText = false; $hasText = false;
...@@ -67,7 +70,7 @@ class XML ...@@ -67,7 +70,7 @@ class XML
if ($hasText) { if ($hasText) {
// only text - add a single text node to the element with the full text // only text - add a single text node to the element with the full text
$root->appendChild(new DOMText($fullText)); $root->appendChild(new \DOMText($fullText));
return; return;
} }
...@@ -82,16 +85,16 @@ class XML ...@@ -82,16 +85,16 @@ class XML
$childIndentation = $indentBase.' '; $childIndentation = $indentBase.' ';
foreach ($childNodes as $node) { foreach ($childNodes as $node) {
// add indentation before node // add indentation before node
$root->insertBefore(new DOMText("\n".$childIndentation), $node); $root->insertBefore(new \DOMText("\n".$childIndentation), $node);
// format child elements // format child elements
if ($node instanceof DOMElement) { if ($node instanceof \DOMElement) {
self::formatDOMElement($node, $childIndentation); self::formatDOMElement($node, $childIndentation);
} }
} }
// add indentation before closing tag // add indentation before closing tag
$root->appendChild(new DOMText("\n".$indentBase)); $root->appendChild(new \DOMText("\n".$indentBase));
} }
/** /**
...@@ -104,19 +107,19 @@ class XML ...@@ -104,19 +107,19 @@ class XML
* to ''. * to ''.
* *
* @return string The formatted string. * @return string The formatted string.
* * @throws \SimpleSAML_Error_Exception If the input does not parse correctly as an XML string or parameters are not
* @throws SimpleSAML_Error_Exception If the input does not parse correctly as an XML string. * strings.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/ */
public static function formatXMLString($xml, $indentBase = '') public static function formatXMLString($xml, $indentBase = '')
{ {
assert('is_string($xml)'); if (!is_string($xml) || !is_string($indentBase)) {
assert('is_string($indentBase)'); throw new \SimpleSAML_Error_Exception('Invalid input parameters');
}
$doc = new DOMDocument(); $doc = new \DOMDocument();
if (!$doc->loadXML($xml)) { if (!$doc->loadXML($xml)) {
throw new SimpleSAML_Error_Exception('Error parsing XML string.'); throw new \SimpleSAML_Error_Exception('Error parsing XML string.');
} }
$root = $doc->firstChild; $root = $doc->firstChild;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment