View source
<?php
define('PATTERNS_FORMAT_XML', 'xml');
function patterns_xmlparser_patterns_parser() {
$parser = array();
$parser['format'] = PATTERNS_FORMAT_XML;
$parser['parser'] = 'patterns_xmlparser';
return $parser;
}
function patterns_xmlparser_load($path, $local = TRUE) {
if ($local && !file_exists($path)) {
return FALSE;
}
if (!($xml = file_get_contents($path))) {
return FALSE;
}
return patterns_xmlparser_parse($xml);
}
function patterns_xmlparser_parse($xml) {
$parse = drupal_xml_parser_create($xml);
xml_parser_set_option($parse, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct($parse, $xml, $vals, $index)) {
return FALSE;
}
$pattern = current(_patterns_xmlparser_parse_tag($vals));
if (empty($pattern) || $pattern['xml_tag'] != 'pattern') {
return FALSE;
}
$pattern = patterns_xmlparser_rearrange_data($pattern);
return $pattern;
}
function patterns_xmlparser_dump($pattern = NULL, $append = NULL) {
if (empty($pattern)) {
return FALSE;
}
$xml = new SimpleXMLElement("<pattern/>");
patterns_xmlparser_array_to_xml($pattern, $xml);
$doc = dom_import_simplexml($xml)->ownerDocument;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$dump = $doc
->saveXML();
return is_null($append) ? $dump : $append . "\n" . $dump;
}
function patterns_xmlparser_dump_comment($text = NULL, $append = NULL) {
$text = !empty($text) ? $text : ' ';
$dump = '<!-- ' . $text . ' -->';
return empty($append) ? $dump : $append . "\n" . $dump;
}
function patterns_xmlparser_array_to_xml($array, &$xml) {
foreach ($array as $key => $value) {
if (is_array($value)) {
if (!is_numeric($key)) {
$subnode = $xml
->addChild("{$key}");
patterns_xmlparser_array_to_xml($value, $subnode);
}
else {
patterns_xmlparser_array_to_xml($value, $xml);
}
}
elseif (!empty($key) && $key == 'tag') {
$xml
->addAttribute("{$key}", "{$value}");
}
else {
if (is_numeric($key) || empty($key)) {
$key = 'value';
}
@$xml
->addChild("{$key}", "{$value}");
}
}
}
function patterns_xmlparser_rearrange_data($pattern) {
foreach ($pattern as $key => $value) {
if (is_string($key)) {
unset($pattern[$key]);
}
else {
if ($value['xml_tag'] == PATTERNS_SECTION_INFO) {
$pattern[PATTERNS_SECTION_INFO] = _patterns_xmlparser_rearrange_data($value);
unset($pattern[PATTERNS_SECTION_INFO]['xml_tag']);
unset($pattern[$key]);
}
elseif ($value['xml_tag'] == PATTERNS_SECTION_MODULES) {
$pattern[PATTERNS_SECTION_MODULES] = _patterns_xmlparser_rearrange_data($value);
unset($pattern[PATTERNS_SECTION_MODULES]['xml_tag']);
unset($pattern[$key]);
}
else {
if (isset($value['tag'])) {
$pattern[] = _patterns_xmlparser_process_action($value);
unset($pattern[$key]);
continue;
}
$section = $value['xml_tag'];
$pattern[$section] = array();
unset($value['xml_tag']);
foreach ($value as $action) {
$pattern[$section][] = _patterns_xmlparser_process_action($action);
}
unset($pattern[$key]);
}
}
}
return $pattern;
}
function _patterns_xmlparser_process_action($action = NULL) {
if (is_null($action)) {
return array();
}
$tag = array(
'tag' => @$action['tag'],
);
$_action = $action['xml_tag'];
if ($_action == PATTERNS_INCLUDE) {
if (!isset($action[0]['value'])) {
$inclusion = patterns_xmlparser_rearrange_data($action[0]);
}
else {
$inclusion = $action[0]['value'];
}
$_action_data = array(
'pattern' => $inclusion,
);
}
else {
$_action_data = _patterns_xmlparser_rearrange_data($action);
unset($_action_data['xml_tag']);
}
return array(
$_action => $_action_data,
);
}
function _patterns_xmlparser_rearrange_data($data, $parent = '') {
foreach ($data as $key => $value) {
if (isset($value['value'])) {
if ($value['value'] == 'false') {
$value['value'] = FALSE;
}
elseif ($value['value'] == 'true') {
$value['value'] = TRUE;
}
}
if (is_numeric($key) && is_array($value) && count($value) == 2 && isset($value['xml_tag']) && isset($value['value'])) {
unset($data[$key]);
if (isset($data[$value['xml_tag']])) {
if (!is_array($data[$value['xml_tag']])) {
$val = $data[$value['xml_tag']];
$data[$value['xml_tag']] = array();
$data[$value['xml_tag']][] = $val;
}
$data[$value['xml_tag']][] = $value['value'];
}
else {
$data[$value['xml_tag']] = $value['value'];
}
}
elseif (is_numeric($key)) {
$tag = $value['xml_tag'];
unset($value['xml_tag']);
$value = _patterns_xmlparser_rearrange_data($value, $tag);
if (isset($value['value'])) {
$data[$tag] = $value['value'];
}
unset($data[$key]);
}
}
foreach ($data as $key => $value) {
if (is_array($value) && count($value) == 1 && $value[0]) {
$data[$key] = $data[$key][0];
}
}
if (isset($data['_numeric_keys'])) {
unset($data['_numeric_keys']);
foreach ($data as $key => $value) {
$data[substr($key, 1)] = $value;
unset($data[$key]);
}
}
return $data;
}
function _patterns_xmlparser_parse_tag($data, &$index = 0) {
$pattern = array();
while (isset($data[$index]) && ($current = $data[$index])) {
$type = $current['type'];
$current['xml_tag'] = $current['tag'];
unset($current['tag']);
if (!empty($current['attributes'])) {
foreach ((array) $current['attributes'] as $key => $value) {
$current[$key] = $value;
}
}
unset($current['type'], $current['level'], $current['attributes']);
if (isset($current['value']) && !trim($current['value']) && $current['value'] != "0") {
unset($current['value']);
}
if (isset($current['value'])) {
if (is_numeric($current['value'])) {
$current['value'] = (int) $current['value'];
}
}
switch ($type) {
case 'open':
$index++;
$current += _patterns_xmlparser_parse_tag($data, $index);
$pattern[] = $current;
break;
case 'close':
$index++;
return $pattern;
break;
case 'complete':
if (!empty($current['attributes']['serialized'])) {
$value = unserialize($current['value']);
if (isset($value)) {
$current['value'] = $value;
}
}
if (!isset($current['value'])) {
$current['value'] = '';
}
$pattern[] = $current;
break;
}
$index++;
}
return $pattern;
}