Wraps a set of XML nodes in an XML-document

@param  string $nodes   One or more XML-nodes, as a string
@param  string $charset Charset to specify in XML-document
@return string
434953
protected function wrapInXmlDocument($nodes, $charset = null) {
        $charset = strtoupper($charset ?: Validator::CHARSET_UTF_8);

        $document  = '<?xml version="1.0" encoding="' . $charset . '"?>' . PHP_EOL;
        $document .= '<root>' . $nodes . '</root>';

        return $document;
    }
373988
protected function arrayToDOM($array, $rootElement, $charset = null)
    {
        foreach ($array as $key => $value) {
            if (is_numeric($key)) {
                $key = $rootElement->nodeName;

                // Books => Book
                if (substr($key, -1, 1) === 's') {
                    $key = substr($key, 0, strlen($key) - 1);
                }
            }

            $element = $rootElement->ownerDocument->createElement($key);
            if (is_array($value)) {
                if (!empty($value)) {
                    $element = $this->arrayToDOM($value, $element, $charset);
                }
            } elseif (is_string($value)) {
                $charset = $charset ? $charset : 'utf-8';
                if (function_exists('iconv') && strcasecmp($charset, 'utf-8') !== 0 && strcasecmp($charset, 'utf8') !== 0) {
                    $value = iconv($charset, 'UTF-8', $value);
                }
                $value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
                $child = $element->ownerDocument->createCDATASection($value);
                $element->appendChild($child);
            } elseif ($value instanceof \DateTimeInterface) {
                $element->setAttribute('type', 'xsd:dateTime');
                $child = $element->ownerDocument->createTextNode($value->format(\DateTime::ISO8601));
                $element->appendChild($child);
            } else {
                $child = $element->ownerDocument->createTextNode($value);
                $element->appendChild($child);
            }
            $rootElement->appendChild($element);
        }

        return $rootElement;
    }
391683
static public function build( XML_DOM_Node $tree, $encoding = "utf-8", $namespaces = array() )
	{
		$document	= new DOMDocument( "1.0", $encoding );
		$document->formatOutput = TRUE;
		$root		= $document->createElement( $tree->getNodename() );
		foreach( $namespaces as $prefix => $namespace )
			$root->setAttribute( "xmlns:".$prefix, $namespace );
		$root		= $document->appendChild( $root );
		self::buildRecursive( $document, $root, $tree, $encoding );
		return $document->saveXML();
	}
540508
protected function pStmts(array $nodes, $indent = true)
    {
        $result = '';

        foreach ($nodes as $node) {
            $row = "";

            $cur_ln = $this->cur_ln;

            $comments = $node->getAttribute('comments', array());
            $comments = !empty($comments) ? ($this->pComments($node->getAttribute('comments', array())) . "\n") : "";
            $this->cur_ln += substr_count($comments, "\n");

            if ($node->getLine() > $this->cur_ln) {
                $row .= str_repeat("\n", $node->getLine() - $this->cur_ln);
                $this->cur_ln += substr_count($row, "\n");
            }

            $row .= $comments
                . $this->p($node)
                . ($node instanceof \PhpParser\Node\Expr ? ';' : '');

            $this->cur_ln = $cur_ln + substr_count($row, "\n"); // get rid of cur_ln modifications in deeper context

            $result .= $row;
        }

        if ($indent) {
            return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n    ", $result);
        } else {
            return $result;
        }
    }
1211043
public static function newDocumentHTML($markup = null, $charset = null) 
    {
        $contentType = $charset
        ? ";charset=$charset"
        : '';
        return self::newDocument($markup, "text/html{$contentType}");
    }
957729
protected function wrapOptions(array $nodes)
	{
		$ret = array();

		foreach ( $nodes as $node_element ) {
			/** @var SelectOption $option */
			$option = SelectOption::fromNodeElement($node_element, $this->getPageFactory());
			$ret[] = $option->setSelect($this);
		}

		return $ret;
	}
443838
function wrap(nodes) {
  nodes = nodes || [];
  if ('string' == typeof nodes) {
    nodes = new DOMParser().parseFromString(nodes);
  }

  if (nodes.attrNS && nodes._related) {
    // attempt to re-wrap Collection, return it directly
    return nodes;
  } else if (nodes.documentElement) {
    nodes = [ nodes.documentElement ];
  } else if (nodes.nodeType) {
    nodes = [ nodes ];
  }
  return new Collection(nodes);
}
1082280
public static Map<String, String> toMap(byte[] xmlBytes, String charset) throws Exception {
        SAXReader reader = new SAXReader(false);
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
        InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
        source.setEncoding(charset);
        Document doc = reader.read(source);
        Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
        return params;
    }
977780
protected function wrapInHtml4Document($nodes, $charset = null, $parser = null) {
        if ($parser === Validator::PARSER_HTML4TR) {
            $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
        } else {
            $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
        }

        $charset = strtolower($charset ?: Validator::CHARSET_UTF_8);

        $document  = $doctype . PHP_EOL;
        $document .= '<html><head>' . PHP_EOL;
        $document .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '">' . PHP_EOL;
        $document .= '<title>Validation document</title>' . PHP_EOL;
        $document .= '</head><body>' . $nodes . '</body></html>';

        return $document;
    }
472919
public function charset($charset = null)
    {
        if (!func_num_args()) {
            return $this->_stream->charset();
        }
        $this->_stream->charset($charset);
        return $this;
    }
411203
public function setCharset($charset)
    {
        if ($this->getContentType() == '') {
            throw new Exception\RuntimeException(__METHOD__ . ' Content-type must be specified prior to setting charset');
        }
        $this->params['content-type-charset'] = $charset;

        return $this;
    }
169340
public function generateTree($nodes)
    {
        $superRoot = count(array_filter($nodes, function (ReadNodeInterface $node) {
            return '-' == $node->getParentId();
        }))? '-': 'root';

        $list = array();
        $list[$superRoot] = array();
        foreach ($nodes as $node) {
            if ($superRoot !== $node->getParentId() && $this->parentInList($node->getParentId(), $nodes)) {
                $list[$node->getParentId()][] = $node;
                continue;
            }
            $list[$superRoot][] = $node;
        }

        $tree = $this->createTree($list[$superRoot], $list);

        return $tree;
    }
230164
protected static function getCharset($charset)
    {
        // $charset_default = 'ISO-8859-1';
        // $charset_default = ini_get('default_charset');
        $supportedCharsets = array(
            'UTF-8'         => '',
            'ISO-8859-1'    => 'ISO8859-1, ASCII',
            'ISO-8859-15'   => 'ISO8859-15',
            'cp866'         => 'ibm866, 866',
            'cp1251'        => 'Windows-1251, win-1251, 1251',
            'cp1252'        => 'Windows-1252, 1252',
            'KOI8-R'        => 'koi8-ru, koi8r',
            'BIG5'          => '950',
            'GB2312'        => '936',
            'BIG5-HKSCS'    => '',
            'Shift_JIS'     => 'SJIS, 932',
            'EUC-JP'        => 'EUCJP',
        );
        $found = in_array($charset, array_keys($supportedCharsets));
        if ($found) {
            return $charset;
        }
        foreach ($supportedCharsets as $k => $v) {
            if (strpos($v, $charset) !== false) {
                $found = true;
                $charset = $k;
                break;
            }
        }
        return $charset;
    }
771354
public function setCharset($charset)
    {
        $this->charset = $charset;
        $this->setContentType($this->getContentType()); // update charset which is set on content-type

        return $this;
    }
239829
public static function getBodyNodeFromHtmlFragment($html, $charset = 'UTF-8')
    {
        $html = '<html><body>' . $html . '</body></html>';
        $current = libxml_use_internal_errors(true);
        $disableEntities = libxml_disable_entity_loader(true);
        $d = new \DOMDocument('1.0', $charset);
        $d->validateOnParse = true;
        if (function_exists('mb_convert_encoding') && in_array(
                strtolower($charset),
                array_map('strtolower', mb_list_encodings())
            )
        ) {
            $html = mb_convert_encoding($html, 'HTML-ENTITIES', $charset);
        }
        @$d->loadHTML($html);
        libxml_use_internal_errors($current);
        libxml_disable_entity_loader($disableEntities);
        return $d->getElementsByTagName('body')->item(0);
    }
994397
public static function newDocumentXHTML($markup = null, $charset = null) 
    {
        $contentType = $charset
        ? ";charset=$charset"
        : '';
        return self::newDocument($markup, "application/xhtml+xml{$contentType}");
    }
440858
function serialize($charset_encoding='')
		{
			// add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...
			//if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
			//{
				reset($this->me);
				list($typ, $val) = each($this->me);
				return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n";
			//}
		}