4 * @file classes/rt/RTXMLParser.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
13 * @brief Class to parse Reading Tools data from an XML format.
16 // $Id: RTXMLParser.inc.php,v 1.6 2009/04/08 21:34:54 asmecher Exp $
19 import('xml.XMLParser');
20 import('rt.RTStruct');
24 /** @var XMLParser the parser to use */
29 * Parse an RT version XML file.
30 * @param $file string path to the XML file
33 function &parse($file) {
34 $parser = new XMLParser();
35 $tree = $parser->parse($file);
39 if ($tree !== false) {
40 $version =& $this->parseVersion($tree);
49 * Parse all RT version XML files in a directory.
50 * @param $dir string path to the directory
51 * @return array RTVersion
53 function &parseAll($dir) {
56 if(($fd = opendir($dir)) !== false) {
57 while (($file = readdir($fd)) !== false) {
58 if (preg_match('/\.xml$/', $file)) {
59 if (($version = $this->parse($dir . '/' . $file))) {
60 array_push($versions, $version);
77 * Parse version entity.
78 * @param $version XMLNode
81 function &parseVersion(&$version) {
82 $newVersion = new RTVersion();
85 $newVersion->key
= $version->getAttribute('id');
86 $newVersion->locale
= $version->getAttribute('locale');
88 foreach ($version->getChildren() as $attrib) {
89 switch ($attrib->getName()) {
91 $newVersion->title
= $attrib->getValue();
93 case 'version_description':
94 $newVersion->description
= $attrib->getValue();
97 $newContext =& $this->parseContext($attrib);
98 $newContext->order
= $numContexts++
;
99 $newVersion->addContext($newContext);
108 * Parse context entity.
109 * @param $context XMLNode
112 function &parseContext(&$context) {
113 $newContext = new RTContext();
116 foreach ($context->getChildren() as $attrib) {
117 switch ($attrib->getName()) {
118 case 'context_title':
119 $newContext->title
= $attrib->getValue();
121 case 'context_abbrev':
122 $newContext->abbrev
= $attrib->getValue();
124 case 'context_description':
125 $newContext->description
= $attrib->getValue();
127 case 'cites_context':
128 $newContext->citedBy
= true;
131 $newContext->authorTerms
= true;
134 $newContext->geoTerms
= true;
137 $newContext->defineTerms
= true;
140 $newSearch =& $this->parseSearch($attrib);
141 $newSearch->order
= $numSearches++
;
142 $newContext->addSearch($newSearch);
151 * Parse search entity.
152 * @param $context XMLNode
155 function &parseSearch(&$search) {
156 $newSearch = new RTSearch();
158 foreach ($search->getChildren() as $attrib) {
159 switch ($attrib->getName()) {
161 $newSearch->title
= $attrib->getValue();
163 case 'search_description':
164 $newSearch->description
= $attrib->getValue();
167 $newSearch->url
= $attrib->getValue();
170 $newSearch->searchUrl
= $attrib->getValue();
173 $newSearch->searchPost
= $attrib->getValue();