/** * Converts DomDocument to array with all attributes in it * * @param DOMDocument|DOMElement|DomNode $root * @return array */ function convertDomToArray($root) { $result = []; if ($root->hasAttributes()) { $attrs = $root->attributes; foreach ($attrs as $attr) { $result['@attributes'][$attr->name] = $attr->value; } } if ($root->hasChildNodes()) { $children = $root->childNodes; if ($children->length == 1) { $child = $children->item(0); if ($child->nodeType == XML_TEXT_NODE) { $result['_value'] = $child->nodeValue; return count($result) == 1 ? $result['_value'] : $result; } } $groups = []; foreach ($children as $child) { if (!isset($result[$child->nodeName])) { $result[$child->nodeName] = $this->convertDomToArray($child); } else { if (!isset($groups[$child->nodeName])) { $result[$child->nodeName] = array($result[$child->nodeName]); $groups[$child->nodeName] = 1; } $result[$child->nodeName][] = $this->convertDomToArray($child); } } } return $result; }
protected function _build_tree_r(DomNode $node) { $frame = new Frame($node); $id = $frame->get_id(); $this->_registry[$id] = $frame; if (!$node->hasChildNodes()) { return $frame; } $children = array(); for ($i = 0; $i < $node->childNodes->length; $i++) { $children[] = $node->childNodes->item($i); } foreach ($children as $child) { $node_name = mb_strtolower($child->nodeName); if (in_array($node_name, self::$_HIDDEN_TAGS)) { if ($node_name !== "head" && $node_name !== "style") { $child->parentNode->removeChild($child); } continue; } if ($node_name === "#text" && $child->nodeValue == "") { $child->parentNode->removeChild($child); continue; } if ($node_name === "img" && $child->getAttribute("src") == "") { $child->parentNode->removeChild($child); continue; } $frame->append_child($this->_build_tree_r($child), false); } return $frame; }
/** * Move the children of this node into this node's parent, just before this node in the DOM tree */ function promote_children() { while ($this->node->hasChildNodes()) { $child = $this->node->firstChild; $this->node->removeChild($child); $this->node->parentNode->insertBefore($child, $this->node); } }
/** * Returns an array with hashes from a file-node * * @param DomNode $node * @return array */ public function getHashesFromFileNode(DomNode $node) { $hashes = array(); if ($node->hasChildNodes()) { $children = $node->childNodes; /* @var $children DOMNodeList */ foreach ($children as $childnode) { /* @var $childnode DOMNode */ if ($childnode->nodeName === 'hash') { $attributes = $childnode->attributes; /* @var $attributes DOMNamedNodeMap */ $hash = $attributes->getNamedItem('hash'); /* @var $hash DOMAttr */ $hashes[$hash->value] = $childnode->nodeValue; } } } return $hashes; }
protected function parseHtmlChildren(DomNode $node) { $output = ''; if ($node->hasChildNodes()) { $children = $node->childNodes; foreach ($children as $child) { switch ($child->nodeType) { case XML_ELEMENT_NODE: $output .= $this->parseHtmlChildren($child); break; case XML_TEXT_NODE: $output .= preg_replace('/[\\r\\n ]+/', ' ', $child->data); break; case XML_CDATA_SECTION_NODE: $output .= $child->data; break; } } } return $this->parseHtmlNode($node, $output); }
/** * Recursively adds {@link Frame} objects to the tree * * Recursively build a tree of Frame objects based on a dom tree. * No layout information is calculated at this time, although the * tree may be adjusted (i.e. nodes and frames for generated content * and images may be created). * * @param DomNode $node the current DomNode being considered * @return Frame */ protected function _build_tree_r(DomNode $node) { $frame = new Frame($node); $id = $frame->get_id(); $this->_registry[$id] = $frame; if (!$node->hasChildNodes()) { return $frame; } // Fixes 'cannot access undefined property for object with // overloaded access', fix by Stefan radulian // <*****@*****.**> //foreach ($node->childNodes as $child) { // Store the children in an array so that the tree can be modified $children = array(); for ($i = 0; $i < $node->childNodes->length; $i++) { $children[] = $node->childNodes->item($i); } foreach ($children as $child) { $node_name = mb_strtolower($child->nodeName); // Skip non-displaying nodes if (in_array($node_name, self::$_HIDDEN_TAGS)) { if ($node_name !== "head" && $node_name !== "style") { $child->parentNode->removeChild($child); } continue; } // Skip empty text nodes if ($node_name === "#text" && $child->nodeValue == "") { $child->parentNode->removeChild($child); continue; } // Skip empty image nodes if ($node_name === "img" && $child->getAttribute("src") == "") { $child->parentNode->removeChild($child); continue; } $frame->append_child($this->_build_tree_r($child), false); } return $frame; }
/** * Returns an associative array containing all possible values of the * specified <configenum> tag. * * The keys contain the actual enum values while the values contain their * corresponding descriptions. * * @param DomNode $node The DomNode representation of the <configenum> * tag whose values should be returned. * * @return array An associative array with all possible enum values. */ protected function _getEnumValues($node) { $values = array(); if (!$node->hasChildNodes()) { return $values; } foreach ($node->childNodes as $vnode) { if ($vnode->nodeType == XML_ELEMENT_NODE && $vnode->tagName == 'values') { if (!$vnode->hasChildNodes()) { return array(); } foreach ($vnode->childNodes as $value) { if ($value->nodeType == XML_ELEMENT_NODE) { if ($value->tagName == 'configspecial') { return $this->_handleSpecials($value); } if ($value->tagName == 'value') { $text = $value->textContent; $desc = $value->getAttribute('desc'); $values[$text] = empty($desc) ? $text : $desc; } } } } } return $values; }
/** * Do serialization over complex schema types * @param $sig_param_node sig model for the param * @param $user_val the user value given to the type * @param $parant_ele just root element of the element * @param $payload_dom payload dom document * @param $classmap (array) users classmap * @param $root_node root element of the document, in order to add namespace * @param $prefix_i - next available prefix index * @param namespace_map, array, map to the namespace to prefix */ function wsf_serialize_complex_types(DomNode $sig_param_node, $user_val, $parent_node, $payload_dom, $root_node, $classmap, &$prefix_i, &$namespace_map, $mtom_on, &$attachment_map) { $target_namespace = NULL; $min_occurs = 1; $max_occurs = 1; $sig_param_attris = $sig_param_node->attributes; $param_type = NULL; $param_name = NULL; if ($sig_param_attris->getNamedItem(WSF_NAME)) { $param_name = $sig_param_attris->getNamedItem(WSF_NAME)->value; } if ($sig_param_attris->getNamedItem(WSF_TARGETNAMESPACE)) { $target_namespace = $sig_param_attris->getNamedItem(WSF_TARGETNAMESPACE)->value; } if ($sig_param_attris->getNamedItem(WSF_MIN_OCCURS)) { $min_occurs = $sig_param_attris->getNamedItem(WSF_MIN_OCCURS)->value; } if ($sig_param_attris->getNamedItem(WSF_MAX_OCCURS)) { $max_occurs = $sig_param_attris->getNamedItem(WSF_MAX_OCCURS)->value; } if ($sig_param_attris->getNamedItem(WSF_TYPE)) { $param_type = $sig_param_attris->getNamedItem(WSF_TYPE)->value; } if ($target_namespace == NULL) { $node_name = $param_name; } else { if (array_key_exists($target_namespace, $namespace_map) && $namespace_map[$target_namespace] != NULL) { $prefix = $namespace_map[$target_namespace]; } else { $prefix = "ns" . $prefix_i; $prefix_i++; $root_node->setAttribute("xmlns:" . $prefix, $target_namespace); $namespace_map[$target_namespace] = $prefix; } $node_name = $prefix . ":" . $param_name; } if ($max_occurs > 1 || $max_occurs == "unbounded") { if ($user_val === NULL) { wsf_set_nil_element(NULL, $parent_node, $root_node, $prefix_i, $namespace_map); } else { if (is_array($user_val)) { foreach ($user_val as $user_val_item) { /* type conversion is needed */ if ($user_val_item === NULL) { wsf_set_nil_element(NULL, $parent_node, $root_node, $prefix_i, $namespace_map); continue; } $param_node = $payload_dom->createElement($node_name); if (is_object($user_val_item)) { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_class_map($payload_dom, $sig_param_node, $param_node, $root_node, $user_val_item, $classmap, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_class_map($payload_dom, $param_node, $user_val_item, $prefix); } } else { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_array($payload_dom, $sig_param_node, $param_node, $root_node, $user_val_item, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_array($payload_dom, $param_node, $user_val_item, $prefix); } } $parent_node->appendChild($param_node); } } else { /* in a case this is not an array */ $param_node = $payload_dom->createElement($node_name); if (is_object($user_val)) { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_class_map($payload_dom, $sig_param_node, $param_node, $root_node, $user_val, $classmap, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_class_map($payload_dom, $param_node, $user_val, $prefix); } } else { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_array($payload_dom, $sig_param_node, $param_node, $root_node, $user_val, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_array($payload_dom, $param_node, $user_val, $prefix); } } $parent_node->appendChild($param_node); } } } else { /* in a case this is not an array */ $param_node = $payload_dom->createElement($node_name); if (is_object($user_val)) { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_class_map($payload_dom, $sig_param_node, $param_node, $root_node, $user_val, $classmap, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_class_map($payload_dom, $param_node, $user_val, $prefix); } } else { if ($sig_param_node->hasChildNodes()) { wsf_create_payload_for_array($payload_dom, $sig_param_node, $param_node, $root_node, $user_val, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { wsf_create_payload_for_unknown_array($payload_dom, $param_node, $user_val, $prefix); } } $parent_node->appendChild($param_node); } }
public function getChildNodes(\DomNode $node, array $types = array()) { if (!$node->hasChildNodes()) { return FALSE; } $childs = $node->childNodes; $return = array(); foreach ($childs as $child) { $nodeName = $child->nodeName; if (is_a($child, "DomElement")) { if ($hasChilds = $this->getChildNodes($child, $types)) { foreach ($hasChilds as $childChild) { $return[] = $childChild; continue; } } if (sizeof($types)) { if (in_array($nodeName, $types)) { $return[] = $child; } } else { $return[] = $child; } } } return $return; }
/** * Recursively adds {@link Frame} objects to the tree * * Recursively build a tree of Frame objects based on a dom tree. * No layout information is calculated at this time, although the * tree may be adjusted (i.e. nodes and frames for generated content * and images may be created). * * @param DomNode $node the current DomNode being considered * @return Frame */ protected function _build_tree_r(DomNode $node) { $frame = new Frame($node); $id = $frame->get_id(); $this->_registry[ $id ] = $frame; if ( !$node->hasChildNodes() ) return $frame; // Fixes 'cannot access undefined property for object with // overloaded access', fix by Stefan radulian // <*****@*****.**> //foreach ($node->childNodes as $child) { // Store the children in an array so that the tree can be modified $children = array(); for ($i = 0; $i < $node->childNodes->length; $i++) $children[] = $node->childNodes->item($i); foreach ($children as $child) { // Skip non-displaying nodes if ( in_array( mb_strtolower($child->nodeName), self::$_HIDDEN_TAGS) ) { if ( mb_strtolower($child->nodeName) != "head" && mb_strtolower($child->nodeName) != "style" ) $child->parentNode->removeChild($child); continue; } // Skip empty text nodes if ( $child->nodeName == "#text" && $child->nodeValue == "" ) { $child->parentNode->removeChild($child); continue; } // Add a container frame for images if ( $child->nodeName == "img" ) { $img_node = $child->ownerDocument->createElement("img_inner"); // Move attributes to inner node foreach ( $child->attributes as $attr => $attr_node ) { // Skip style, but move all other attributes if ( $attr == "style" ) continue; $img_node->setAttribute($attr, $attr_node->value); } foreach ( $child->attributes as $attr => $node ) { if ( $attr == "style" ) continue; $child->removeAttribute($attr); } $child->appendChild($img_node); } $frame->append_child($this->_build_tree_r($child), false); } return $frame; }
/** * infer attributes * @param $parent_node parent node containing the attributes * @param $sig_node sig node containing the attribute signature * @return $parset_tree the parsed tree of attributes */ function wsf_infer_attributes(DomNode $parent_node, DomNode $sig_node) { $parse_tree = array(); if ($sig_node->hasChildNodes()) { foreach ($sig_node->childNodes as $sig_param_node) { if ($sig_param_node->nodeName == WSF_PARAM) { $is_simple = FALSE; $is_attribute = FALSE; $param_name = NULL; $param_type = NULL; if ($sig_param_node->attributes->getNamedItem(WSF_NAME)) { $param_name = $sig_param_node->attributes->getNamedItem(WSF_NAME)->value; } if ($sig_param_node->attributes->getNamedItem(WSF_TYPE)) { $param_type = $sig_param_node->attributes->getNamedItem(WSF_TYPE)->value; } if ($sig_param_node->attributes->getNamedItem(WSF_WSDL_SIMPLE) && $sig_param_node->attributes->getNamedItem(WSF_WSDL_SIMPLE)->value == "yes") { $is_simple = TRUE; } if ($sig_param_node->attributes->getNamedItem(WSF_ATTRIBUTE) && $sig_param_node->attributes->getNamedItem(WSF_ATTRIBUTE)->value == "yes") { $is_attribute = TRUE; } if ($is_attribute) { ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, $param_name . ":" . wsf_test_serialize_node($parent_node)); if ($parent_node && $parent_node->attributes->getNamedItem($param_name)) { $original_value = $parent_node->attributes->getNamedItem($param_name)->value; $is_list = FALSE; if ($sig_param_node->attributes->getNamedItem(WSF_LIST) && $sig_param_node->attributes->getNamedItem(WSF_LIST)->value == "yes") { $is_list = TRUE; } $converted_value = wsf_wsdl_deserialize_string_value($param_type, $original_value, $sig_param_node, NULL); $parse_tree[$param_name] = $converted_value; } } } else { if ($sig_param_node->nodeName == WSF_INHERITED_CONTENT) { $parse_tree = array_merge($parse_tree, wsf_infer_attributes($parent_node, $sig_param_node)); } } } } return $parse_tree; }
/** * Recursively adds {@link Frame} objects to the tree * * Recursively build a tree of Frame objects based on a dom tree. * No layout information is calculated at this time, although the * tree may be adjusted (i.e. nodes and frames for generated content * and images may be created). * * @param DomNode $node the current DomNode being considered * @return Frame */ protected function _build_tree_r(DomNode $node) { $frame = new Frame($node); $frame->set_id($id = uniqid(rand())); $this->_registry[$id] = $frame; if (!$node->hasChildNodes()) { return $frame; } foreach ($node->childNodes as $child) { // Skip non-displaying nodes if (in_array($child->nodeName, self::$_HIDDEN_TAGS)) { continue; } // Skip empty #text nodes if ($child->nodeName == "#text" && $child->nodeValue == "") { continue; } // Add a container frame for images if ($child->nodeName == "img") { $img_node = $child->ownerDocument->createElement("img_inner"); // Move attributes to inner node foreach ($child->attributes as $attr => $attr_node) { // Skip style, but move all other attributes if ($attr == "style") { continue; } $img_node->setAttribute($attr, $attr_node->value); } foreach ($child->attributes as $attr => $node) { if ($attr == "style") { continue; } $child->removeAttribute($attr); } $child->appendChild($img_node); } $frame->append_child($this->_build_tree_r($child), false); } return $frame; }
static function convertDomNodeToDataArray(DomNode $node) { $data = array(); $values = array(); $has_value = false; if ($node->hasChildNodes()) { foreach ($node->childNodes as $child_node) { /* * A html dom node always contains one dom document type child * node with no content (DOMDocumentType#internalSubset is for * example <!DOCTYPE html>). Ignore it! */ if ($child_node instanceof DOMDocumentType) { continue; } if ($child_node->nodeType === XML_TEXT_NODE) { $has_value = true; $values[] = $child_node->nodeValue; } else { $key = $child_node->nodeName; if (isset($data[$key])) { if (!is_array($data[$key]) || !isset($data[$key][0])) { $data[$key] = array($data[$key]); } $data[$key][] = self::convertDomNodeToDataArray($child_node); } else { $data[$key] = self::convertDomNodeToDataArray($child_node); } } } } if ($node->hasAttributes()) { foreach ($node->attributes as $attribute_node) { $key = '@' . $attribute_node->nodeName; $data[$key] = $attribute_node->nodeValue; } } if ($has_value) { $value = implode('', $values); if (trim($value) != '') { if (empty($data)) { $data = $value; } else { $data['@'] = $value; } } } return $data; }
/** * Detection for nodes with header(h1-h6) as child. * @param DomNode $node * @return boolean */ protected static function isSectionNode($node) { if ($node->nodeType == XML_ELEMENT_NODE && $node->hasChildNodes()) { foreach ($node->childNodes as $sub) { if (self::isTitle($sub)) { return true; } } } return false; }