Create formatted output of XML errors

@param string $xmlFile
@throws ConfigException
179321
private function dumpXmlErrors($xmlFile) {

		$severity = [LIBXML_ERR_WARNING => 'Warning', LIBXML_ERR_ERROR => 'Error', LIBXML_ERR_FATAL => 'Fatal'];
		$errors = [];

		foreach(libxml_get_errors() as $error) {
			$errors[] = sprintf("Row %d, column %d: %s (%d) %s", $error->line, $error->column, $severity[$error->level], $error->code, $error->message);
		}

		throw new ConfigException(sprintf("Could not parse XML configuration in '%s'.\n\n%s", $xmlFile, implode("\n", $errors)));

	}
491133
public static function appendErrors(XMLElement $result, array $fields, $errors, $post_values)
    {
        $result->setAttribute('result', 'error');
        $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array(
            'message-id' => EventMessages::ENTRY_ERRORS
        )));

        foreach ($errors as $field_id => $message) {
            $field = FieldManager::fetch($field_id);

            // Do a little bit of a check for files so that we can correctly show
            // whether they are 'missing' or 'invalid'. If it's missing, then we
            // want to remove the data so `__reduceType` will correctly resolve to
            // missing instead of invalid.
            // @see https://github.com/symphonists/s3upload_field/issues/17
            if (isset($_FILES['fields']['error'][$field->get('element_name')])) {
                $upload = $_FILES['fields']['error'][$field->get('element_name')];

                if ($upload === UPLOAD_ERR_NO_FILE) {
                    unset($fields[$field->get('element_name')]);
                }
            }

            if (is_array($fields[$field->get('element_name')])) {
                $type = array_reduce($fields[$field->get('element_name')], array('SectionEvent', '__reduceType'));
            } else {
                $type = ($fields[$field->get('element_name')] == '') ? 'missing' : 'invalid';
            }

            $error = self::createError($field, $type, $message);
            $result->appendChild($error);
        }

        if (isset($post_values) && is_object($post_values)) {
            $result->appendChild($post_values);
        }

        return $result;
    }
494423
protected function validate( $xmlElement )
	{
		$errors	= array();
		foreach( $xmlElement->getDocNamespaces() as $prefix => $namespace )
		{
			$prefix	= $prefix ? $prefix : "atom";
			$xmlElement->registerXPathNamespace( $prefix, $namespace );
		}
		$key	= "//atom:feed/";
		if( !count( $xmlElement->xpath( $key.'atom:author' ) ) )
			foreach( $xmlElement->entry as $entry )
				if( !$entry->author )
					$errors[]	= "feed_author";
		if( count( $xmlElement->xpath( $key.'atom:generator' ) ) > 1 )
			$errors[]	= "feed_generator";
		if( count( $xmlElement->xpath( $key.'atom:icon' ) ) > 1 )
			$errors[]	= "feed_icon";
		if( count( $xmlElement->xpath( $key.'atom:logo' ) ) > 1 )
			$errors[]	= "feed_logo";
		if( count( $xmlElement->xpath( $key.'atom:id' ) ) != 1 )
			$errors[]	= "feed_id";
		if( count( $xmlElement->xpath( $key.'atom:rights' ) ) > 1 )
			$errors[]	= "feed_rights";
		if( count( $xmlElement->xpath( $key.'atom:subtitle' ) ) > 1 )
			$errors[]	= "feed_subtitle";
		if( count( $xmlElement->xpath( $key.'atom:title' ) ) != 1 )
			$errors[]	= "feed_title";
		if( count( $xmlElement->xpath( $key.'atom:updated' ) ) != 1 )
			$errors[]	= "feed_updated";
		$ids	= array();
		foreach( $xmlElement->xpath( $key.'atom:link[@rel="alternate"]' ) as $link )
		{
			$id	= "";
			if( $link->hasAttribute( 'type' ) )
				$id	= $link->getAttribute( 'type' );
			if( $link->hasAttribute( 'hreflang' ) )
				$id	.= "_".$link->getAttribute( 'hreflang');
			if( in_array( $id, $ids ) )
			{
				$errors[]	= "feed_link_unique";
				break;
			}
			$ids[]	= $id;
		}

		$numberEntries	= count( $xmlElement->xpath( $key.'atom:entry' ) );
		for( $i=1; $i<=$numberEntries; $i++ )
		{
			$key	= "//atom:feed/atom:entry[$i]/";
			if( !count( $xmlElement->xpath( $key.'atom:author' ) ) && !count( $xmlElement->xpath( '//atom:feed/atom:author' ) ) )
				$errors[]	= "entry_author";
			if( count( $xmlElement->xpath( $key.'atom:content' ) ) > 1 )
				$errors[]	= "entry_content";
			if( count( $xmlElement->xpath( $key.'atom:id' ) ) != 1 )
				$errors[]	= "entry_id";
			if( !count( $xmlElement->xpath( $key.'atom:content' ) ) && !count( $xmlElement->xpath( $key.'atom:link[@rel="alternate"]' ) ) )
				$errors[]	= "entry_link_alt";
			if( count( $xmlElement->xpath( $key.'atom:published' ) ) > 1 )
				$errors[]	= "entry_published";
			if( count( $xmlElement->xpath( $key.'atom:rights' ) ) > 1 )
				$errors[]	= "entry_rights";
			if( count( $xmlElement->xpath( $key.'atom:source' ) ) > 1 )
				$errors[]	= "entry_source";
			if( count( $xmlElement->xpath( $key.'atom:summary' ) ) > 1 )
				$errors[]	= "entry_summary";
			if( count( $xmlElement->xpath( $key.'atom:title' ) ) != 1 )
				$errors[]	= "entry_title";
			if( count( $xmlElement->xpath( $key.'atom:updated' ) ) != 1 )
				$errors[]	= "entry_updated";

			$keys	= array();
			foreach( $xmlElement->xpath( $key.'atom:link[@rel="alternate"]' ) as $link )
			{
				$key	= "";
				if( $link->hasAttribute( 'type' ) )
					$key	= $link->getAttribute( 'type' );
				if( $link->hasAttribute( 'hreflang' ) )
					$key	.= "_".$link->getAttribute( 'hreflang');
				if( in_array( $key, $keys ) )
				{
					$errors[]	= "entry_link_unique";
					break;
				}
				$keys[]	= $key;
			}
		}
		$this->errors	= $errors;

	}
690642
public function setXML($xml, $isFile = false)
    {
        $this->_xml = ($isFile ? file_get_contents($xml) : $xml);
    }
618901
public function saveXMLFile() {

        $structure = $this->getStructure();

        $result = file_put_contents($this->path, $structure->xmlOutput());

        return $result;
    }
528097
protected function write()
	{
		$ww		= new XML_WDDX_FileWriter( $this->fileName );
		foreach( $this->getConfigValues() as $sectionName => $sectionData )
			foreach( $sectionData as $key => $value)
				$ww->add( $key, $value );
		$ww->write();
	}
139613
protected function loadElementsFromXmlFileWithPath($xmlFile, $nodePath)
    {
        $xml = file_get_contents($xmlFile);
        // Remove whitespaces between closing tag and opening tag.
        $xml = preg_replace('/>\s+</', '><', $xml);

        $doc = new \DOMDocument();
        $doc->loadXML($xml);

        $xpath = new \DOMXpath($doc);
        $elements = $xpath->query($nodePath);

        return $elements;
    }
991117
function xmlDefaultHandler($parser,$data)
		{
			//strip '<!--' and '-->' off comments
			$data = repl(array("&lt;!--", "--&gt;"), "", htmlspecialchars($data));
			$count = $this->xmlCount - 1;
			if(!empty($this->xmlDData[$count])) $this->xmlDData[$count] .= $data;
			else $this->xmlDData[$count] = $data;
		}
130762
public function transform(PhingFile $xmlFile, Writer $out)
    {
        $this->sqlWriter = $out;

        // Reset some vars just in case this is being run multiple times.
        $this->currTableName = $this->currBuilder = null;

        $this->builderClazz = $this->generatorConfig->getBuilderClassname('datasql');

        try {
            $fr = new FileReader($xmlFile);
        } catch (Exception $e) {
            throw new BuildException("XML File not found: " . $xmlFile->getAbsolutePath());
        }

        $br = new BufferedReader($fr);

        $this->parser = new ExpatParser($br);
        $this->parser->parserSetOption(XML_OPTION_CASE_FOLDING, 0);
        $this->parser->setHandler($this);

        try {
            $this->parser->parse();
        } catch (Exception $e) {
            print $e->getMessage() . "\n";
            $br->close();
        }
        $br->close();
    }
390218
public function validateIsoFile($originFile)
	{
		$fileContents = (string) file_get_contents($this->originPath . '/' . $originFile);
		$isoFile = explode("\n", $fileContents);

		if (sizeof($isoFile) != 3)
		{
			$this->output->addMessage(Output::FATAL, 'Must contain exactly 3 lines: 1. English name, 2. Native name, 3. Author information', $originFile);
		}
	}
503806
private void prettyprint(String xmlLogsFile, FileOutputStream htmlReportFile)
            throws Exception {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        // Fortify Mod: prevent external entity injection
        tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Fortify Mod: prevent external entity injection
        factory.setExpandEntityReferences(false);
        factory.setNamespaceAware(true);
        factory.setXIncludeAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document document = parser.parse(xmlLogsFile);
        Transformer transformer = tFactory.newTransformer(new StreamSource(
                xsltFileHandler));
        transformer.transform(new DOMSource(document), new StreamResult(
                htmlReportFile));
    }
1287147
public function writeInnerXml(\XMLWriter $xmlWriter)
    {
        Validate::notNull($xmlWriter, 'xmlWriter');

        $this->writeOptionalAttribute($xmlWriter, 'href', $this->href);
        $this->writeOptionalAttribute($xmlWriter, 'rel', $this->rel);
        $this->writeOptionalAttribute($xmlWriter, 'type', $this->type);
        $this->writeOptionalAttribute($xmlWriter, 'hreflang', $this->hreflang);
        $this->writeOptionalAttribute($xmlWriter, 'title', $this->title);
        $this->writeOptionalAttribute($xmlWriter, 'length', $this->length);

        if (!empty($this->undefinedContent)) {
            $xmlWriter->writeRaw($this->undefinedContent);
        }
    }
1328102
public final String rewriteXml(
        String xmlFile,
        String aloneTags[]
    ) {
        return rewriteXml(
            xmlFile,
            aloneTags,
            false
        );
    }
1029020
protected function process($xml, $xsl)
    {
        $processor = new XSLTProcessor();

        // Create and setup document.
        $xmlDom = new DOMDocument();
        $xmlDom->resolveExternals = $this->resolveDocumentExternals;

        // Create and setup stylesheet.
        $xslDom = new DOMDocument();
        $xslDom->resolveExternals = $this->resolveStylesheetExternals;

        if ($this->html) {
            $result = @$xmlDom->loadHTML($xml);
        } else {
            $result = @$xmlDom->loadXML($xml);
        }

        if ($result === false) {
            throw new BuildException('Invalid syntax detected.');
        }

        $xslDom->loadXML($xsl);

        if (defined('XSL_SECPREF_WRITE_FILE')) {
            $processor->setSecurityPrefs(XSL_SECPREF_WRITE_FILE | XSL_SECPREF_CREATE_DIRECTORY);
        }
        $processor->importStylesheet($xslDom);

        // ignoring param "type" attrib, because
        // we're only supporting direct XSL params right now
        foreach ($this->xsltParams as $param) {
            $this->log("Setting XSLT param: " . $param->getName() . "=>" . $param->getExpression(), Project::MSG_DEBUG);
            $processor->setParameter(null, $param->getName(), $param->getExpression());
        }

        $errorlevel = error_reporting();
        error_reporting($errorlevel & ~E_WARNING);
        @$result = $processor->transformToXml($xmlDom);
        error_reporting($errorlevel);

        if (false === $result) {
            //$errno = xslt_errno($processor);
            //$err   = xslt_error($processor);
            throw new BuildException("XSLT Error");
        } else {
            return $result;
        }
    }
1051774
public function outputError($elTpl, $error)
    {
        if ($error && !$this->options['group_errors']) {
            $elTpl = str_replace(array('<qf:error>', '</qf:error>', '{error}'),
                                  array('', '', $error), $elTpl);
        } else {
            if ($error && $this->options['group_errors']) {
                $this->errors[] = $error;
            }
            $elTpl = preg_replace('!<qf:error>.*</qf:error>!isU', '', $elTpl);
        }
        return $elTpl;
    }
854390
protected function init() {
        if (!file_exists(dirname($this->fullpath))) {
            throw new xml_output_exception('directory_not_exists', dirname($this->fullpath));
        }
        if (file_exists($this->fullpath)) {
            throw new xml_output_exception('file_already_exists', $this->fullpath);
        }
        if (!is_writable(dirname($this->fullpath))) {
            throw new xml_output_exception('directory_not_writable', dirname($this->fullpath));
        }
        // Open the OS file for writing
        if (! $this->fhandle = fopen($this->fullpath, 'w')) {
            throw new xml_output_exception('error_opening_file');
        }
    }
399673
def _write_xml(xmlfile, srcs):
        
        root = ElementTree.Element('source_library')
        root.set('title', 'source_library')

        for src in srcs:
            src.write_xml(root)

        output_file = open(xmlfile, 'w')
        output_file.write(utils.prettify_xml(root))