Process element attributes

@param \XMLReader $reader
@param \stdClass $tree

@SuppressWarnings(PHPMD.ElseExpression)
978069
public function processAttributes(&$reader, &$tree)
    {
        $name = $reader->name;

        $node = new \stdClass();

        $node->attr = new \stdClass();

        while ($reader->moveToNextAttribute()) {
            $node->attr->$name = $reader->value;
        }

        if (isset($tree->$name) && is_a($tree->$name, 'ArrayObject')) {
            $lastKey = $tree->$name->count() - 1;

            $node->value = $tree->$name->offsetGet($lastKey);
            $tree->$name->offsetSet($lastKey, $node);
        } else {
            $node->value = isset($tree->$name) ? $tree->$name : '';

            $tree->$name = $node;
        }
    }
28126
private function configureTokenReader(TokenReader $reader)
    {
        $count = count($this->parameters);
        for ($i = 0; $i < $count; $i++) {
            $method_name = "Set" . $this->parameters[$i]->getName();
            $value = $this->parameters[$i]->getValue();
            $reader->$method_name($value);
        }
    }
1145973
function parse($text) {
		$text=parent::parse($text);
		// Build tree structure
		for ($ptr=0,$w=5,$len=strlen($text),$tree=[],$tmp='';$ptr<$len;)
			if (preg_match('/^(.{0,'.$w.'}?)<(\/?)(?:F3:)?'.
				'('.$this->tags.')\b((?:\s+[\w.:@!-]+'.
				'(?:\h*=\h*(?:"(?:.*?)"|\'(?:.*?)\'))?|'.
				'\h*\{\{.+?\}\})*)\h*(\/?)>/is',
				substr($text,$ptr),$match)) {
				if (strlen($tmp) || $match[1])
					$tree[]=$tmp.$match[1];
				// Element node
				if ($match[2]) {
					// Find matching start tag
					$stack=[];
					for($i=count($tree)-1;$i>=0;$i--) {
						$item=$tree[$i];
						if (is_array($item) &&
							array_key_exists($match[3],$item) &&
							!isset($item[$match[3]][0])) {
							// Start tag found
							$tree[$i][$match[3]]+=array_reverse($stack);
							$tree=array_slice($tree,0,$i+1);
							break;
						}
						else $stack[]=$item;
					}
				}
				else {
					// Start tag
					$node=&$tree[][$match[3]];
					$node=[];
					if ($match[4]) {
						// Process attributes
						preg_match_all(
							'/(?:(\{\{.+?\}\})|([^\s\/"\'=]+))'.
							'\h*(?:=\h*(?:"(.*?)"|\'(.*?)\'))?/s',
							$match[4],$attr,PREG_SET_ORDER);
						foreach ($attr as $kv)
							if (!empty($kv[1]) && !isset($kv[3]) && !isset($kv[4]))
								$node['@attrib'][]=$kv[1];
							else
								$node['@attrib'][$kv[1]?:$kv[2]]=
									(isset($kv[3]) && $kv[3]!==''?
										$kv[3]:
										(isset($kv[4]) && $kv[4]!==''?
											$kv[4]:NULL));
					}
				}
				$tmp='';
				$ptr+=strlen($match[0]);
				$w=5;
			}
			else {
				// Text node
				$tmp.=substr($text,$ptr,$w);
				$ptr+=$w;
				if ($w<50)
					$w++;
			}
		if (strlen($tmp))
			// Append trailing text
			$tree[]=$tmp;
		// Break references
		unset($node);
		return $tree;
	}
1020012
protected function read(\XMLReader $reader)
    {
        libxml_clear_errors();
        
        $status = @$reader->read();

        $error = libxml_get_last_error();
        if($error)
        {
            libxml_clear_errors();
            throw new Exceptions\ParseException(sprintf('Xml parsing error "%s" in file "%s" on line %s on column %s', $error->message, $error->file, $error->line, $error->column));
        }

        return $status;
    }
1293250
public synchronized void releaseXMLReader(XMLReader reader) {
        // If the reader that's being released is the cached reader
        // for this thread, remove it from the m_isUse list.
        if (m_readers.get() == reader && reader != null) {
            m_inUse.remove(reader);
        }
    }
262105
protected function parseDimensionsElement(\XMLReader $reader)
    {
        $dimensions = [];
        $currentDimension = null;

        while ($reader->read()) {
            switch ($reader->nodeType) {
                case \XMLReader::ELEMENT:
                    $currentDimension = $reader->name;
                    break;
                case \XMLReader::END_ELEMENT:
                    if ($reader->name === 'dimensions') {
                        return $dimensions;
                    }
                    break;
                case \XMLReader::CDATA:
                case \XMLReader::TEXT:
                    $dimensions[$currentDimension][] = $reader->value;
                    break;
            }
        }

        return $dimensions;
    }
928168
private function moveReaderToCurrent()
    {
        if (
            ($this->reader->nodeType === XMLReader::NONE)
            or ($this->reader->nodeType !== XMLReader::ELEMENT)
            or ($this->localName && $this->localName !== $this->reader->localName)
        ) {
            self::next();
        }
    }
276614
private function readerOperation(\XmlReader $reader, $method, array $args = [])
    {
        // clear any previous errors
        libxml_clear_errors();

        // remember current settings
        $errors = libxml_use_internal_errors(true);
        $entities = libxml_disable_entity_loader(true);

        // perform the operation
        $retval = call_user_func_array([$reader, $method], $args);

        // get the last error, if any
        $error = $this->getXmlError();

        // reset everything, clear the error buffer again
        libxml_clear_errors();
        libxml_use_internal_errors($errors);
        libxml_disable_entity_loader($entities);

        if (!empty($error)) {
            throw new ReadException($error);
        }

        return $retval;
    }
679322
public function initReader( $locale )
    {
        $this->xmlParser = $this->openTranslationFile( $locale, 'SimpleXMLIterator' );
        $this->xmlParser->rewind();
    }
245354
protected function parseRecursive( $root, $tree )
	{
		$nodes = array();
		if( $child = $root->firstChild )
		{
			while( $child )
			{
				$attributes	= $child->hasAttributes()? $child->attributes : array();
				switch( $child->nodeType )
				{
					case XML_ELEMENT_NODE:
						$node = new XML_DOM_Node( $child->nodeName );
						if( !$this->parseRecursive( $child, $node ) )
						{
	#						$node->setContent( utf8_decode( $child->textContent ) );
							$node->setContent( $child->textContent );
						}
						foreach( $attributes as $attribute)
							$node->setAttribute( $attribute->nodeName, stripslashes( $attribute->nodeValue ) );
						$tree->addChild( $node );
						break;
					case XML_TEXT_NODE:
						if( strlen( trim( $content = $child->textContent ) ) )
						{
							return false;
						}
						else if( isset( $attributes['type'] ) && preg_match( "/.*ml/i", $attributes['type'] ) )
						{
							return false;
						}
						break;
					case XML_CDATA_SECTION_NODE:
						$tree->setContent( stripslashes( $child->textContent ) );
						break;
					default:
						break;
				}
				$child = $child->nextSibling;
			}
		}
		return true;
	}
460108
protected function moveToNextNode(\XMLReader $reader)
    {
        if (!$this->nextNode instanceof \Closure) {
            throw new \LogicException('No callback set to get next node');
        }

        ++$this->key;

        return call_user_func($this->nextNode, $reader);
    }
650723
public static void skipElement(final XMLStreamReader reader) throws XMLStreamException, IOException {
        if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
            return;
        }
        final String namespace = reader.getNamespaceURI();
        final String name = reader.getLocalName();
        for (;;) {
            switch (reader.nextTag()) {
            case XMLStreamConstants.START_ELEMENT:
                // call ourselves recursively if we encounter START_ELEMENT
                skipElement(reader);
                break;
            case XMLStreamConstants.END_ELEMENT:
            case XMLStreamConstants.END_DOCUMENT:
                // discard events until we encounter matching END_ELEMENT
                reader.require(XMLStreamConstants.END_ELEMENT, namespace, name);
                reader.next();
                return;
            }
        }
    }
1315789
public static function convertFromXMLString($root_element, $xml)
    {
        $doc = new DOMDocument('1.0', 'utf-8');
        $doc->loadXML($xml);

        return self::convertFromDOMDocument($root_element, $doc);
    }
1239266
public static void parseCommonStoreAttributes(XMLExtendedStreamReader reader, int i,
                                                 StoreConfigurationBuilder<?, ?> builder) throws XMLStreamException {
      ParseUtils.requireNoNamespaceAttribute(reader, i);
      String value = reader.getAttributeValue(i);
      org.infinispan.tools.config.v6.Attribute attribute = org.infinispan.tools.config.v6.Attribute.forName(reader.getAttributeLocalName(i));
      switch (attribute) {
         case FETCH_PERSISTENT_STATE:
            builder.fetchPersistentState(Boolean.parseBoolean(value));
            break;
         case IGNORE_MODIFICATIONS:
            builder.ignoreModifications(Boolean.parseBoolean(value));
            break;
         case PURGE_ON_STARTUP:
            builder.purgeOnStartup(Boolean.parseBoolean(value));
            break;
         default:
            throw ParseUtils.unexpectedAttribute(reader, i);
      }
   }
440307
public function encode($xml)
    {
        if (empty($xml)) {
            return '';
        }

        libxml_clear_errors();
        $reader = new XMLReader();
        $reader->xml($xml);

        $binary = '';
        while (@$reader->read()) {
            switch ($reader->nodeType) {
                case XMLReader::NONE:
                    break;

                case XMLReader::ELEMENT:
                    $index = $this->getDictionaryIndex($reader->localName);
                    if ($reader->prefix && $index !== false) {
                        $binary .= chr(Constants::RECORD_TYPE_DICTIONARY_ELEMENT);
                        $this->writeString($binary, $reader->prefix);
                        $this->writeDictionaryString($binary, $index);
                    } elseif ($reader->prefix) {
                        $binary .= chr(Constants::RECORD_TYPE_ELEMENT);
                        $this->writeString($binary, $reader->prefix);
                        $this->writeString($binary, $reader->localName);
                    } elseif ($index !== false) {
                        $binary .= chr(Constants::RECORD_TYPE_SHORT_DICTIONARY_ELEMENT);
                        $this->writeDictionaryString($binary, $index);
                    } else {
                        $binary .= chr(Constants::RECORD_TYPE_SHORT_ELEMENT);
                        $this->writeString($binary, $reader->localName);
                    }
                    $emptyElement = $reader->isEmptyElement;
                    $this->writeAttributes($binary, $reader);
                    if ($emptyElement) {
                        $binary .= chr(Constants::RECORD_TYPE_END_ELEMENT);
                    }
                    break;

                case XMLReader::TEXT:
                    $this->writeTextRecord($binary, $reader->value);
                    break;

                case XMLReader::COMMENT:
                    $binary .= chr(Constants::RECORD_TYPE_COMMENT);
                    $this->writeString($binary, $reader->value);
                    break;

                case XMLReader::WHITESPACE:
                    break;

                case XMLReader::SIGNIFICANT_WHITESPACE:
                    $this->writeTextRecord($binary, $reader->value);
                    break;

                case XMLReader::END_ELEMENT:
                    // TODO: Use *_WITH_END_ELEMENT if possible
                    $binary .= chr(Constants::RECORD_TYPE_END_ELEMENT);
                    break;

                case XMLReader::ATTRIBUTE:
                    throw new EncodingException('Invalid encoding state.');

                case XMLReader::DOC:
                case XMLReader::DOC_TYPE:
                case XMLReader::DOC_FRAGMENT:
                case XMLReader::NOTATION:
                case XMLReader::XML_DECLARATION:
                case XMLReader::CDATA:
                case XMLReader::ENTITY:
                case XMLReader::ENTITY_REF:
                case XMLReader::END_ENTITY:
                case XMLReader::PI:
                default:
                    throw new EncodingException(sprintf('Unsupported XML node type %d.', $reader->nodeType));
            }
        }

        $last_error = libxml_get_last_error();
        if ($last_error !== false) {
            throw new EncodingException(sprintf('XML Parsing Error "%s".', rtrim($last_error->message)));
        }

        return $binary;
    }
362101
public static function convertFromDOMDocument($root_element, DOMDocument $doc)
    {
        $xpath = new DOMXPath($doc);
        $root = new XMLElement($root_element);

        foreach ($xpath->query('.') as $node) {
            self::convertNode($root, $node);
        }

        return $root;
    }
712499
protected function processNode(Node $node, \XMLReader $xmlReader)
    {
        $node = parent::processNode( $node, $xmlReader );
        return $this->nodeToDictionary( $node );
    }