[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /***************************************************** 3 * This file is part of Agora, web based content management system. 4 * 5 * Agora is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; version 2 of the License. 8 * 9 * Agora is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details (file "COPYING"). 13 * 14 * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière. 15 * List of authors detailed in "copyright_fr.html" file. 16 * E-mail : agora@sig.premier-ministre.gouv.fr 17 * Web site : http://www.agora.gouv.fr 18 *****************************************************/ 19 // Class designed to build a SearchResult array using 20 // an input XML Stream 21 // 22 23 require_once ("SearchResults.php"); 24 require_once ("SearchResult.php"); 25 26 /** 27 * This class is designed to provide an easy way to change 28 * the parsing technique used to build results given 29 * as an XML input stream. This XML stream MUST VALIDATE 30 * the indexresults.xsd ! 31 * 32 * @author Antoine Angénieux <aangenieux@clever-age.com> 33 * @version $Revision$ 34 */ 35 class ResultBuilder { 36 37 // {{{ properties 38 39 /** 40 * XML parser resource 41 * @var resource 42 * @access private 43 */ 44 var $m_parser; 45 46 /** 47 * Inner variable representing the current result being proceeded. 48 * @var SearchResult 49 * @access private 50 */ 51 var $m_currentResult; 52 53 /** 54 * Inner variable representing final SearchResults instance 55 * @var SearchResults 56 * @access private 57 */ 58 var $m_searchResults; 59 60 /** 61 * Inner variable representing the current tag being proceeded 62 * @var String 63 * @access private 64 */ 65 var $m_currentTag; 66 67 // }}} 68 69 // {{{ constructor 70 71 /** 72 * Constructor for ResultBuilder 73 * @access public 74 */ 75 76 function ResultBuilder() { 77 78 } 79 80 // }}} 81 82 // {{{ buildResults() 83 84 /** 85 * This function is the one that does the job ;) 86 * Any subclass should redefine this default method 87 * in order to use a different parsing technique 88 * or a different implementation of SearchResult 89 * @param String $xmlInputStream is the XML stream 90 * to parse and to transform into 91 * a SearchResult array 92 * @return mixed Instance of SearchResults or PEAR::Error 93 */ 94 function &buildResults($xmlInputStream) { 95 $this->_parseXMLStream($xmlInputStream); 96 if (PEAR::isError($result)) { 97 die($result->getMessage()); 98 } 99 return $this->m_searchResults; 100 } 101 102 // }}} 103 104 // {{{ _parseXMLStream() 105 106 /** 107 * This function does the parsing job on the XML Stream and builds 108 * the SearchResult array 109 * @param String $xmlStream XML stream to parse 110 * @access private 111 */ 112 function _parseXMLStream($xmlStream) { 113 $xp = @xml_parser_create(); 114 if (is_resource($xp)) { 115 $this->m_parser = $xp; 116 } else { 117 return PEAR::raiseError("[".get_class($this)." _parseXMLStream():] Unable to get an XML parser resource !", 118 null, null, null, null, null, false); 119 } 120 xml_set_object($this->m_parser,$this); 121 xml_set_element_handler($this->m_parser, "_tagOpen", "_tagClose"); 122 xml_set_character_data_handler($this->m_parser, "_cdata"); 123 $this->_parse($xmlStream); 124 xml_parser_free($this->m_parser); 125 } 126 // }}} 127 128 // {{{ _parse() 129 130 /** 131 * This method is used to start the XML parsing 132 * @param &$data XML stream to parse 133 * @access private 134 */ 135 function _parse(&$data) { 136 xml_parse($this->m_parser, $data, TRUE); 137 } 138 // }}} 139 140 // {{{ _tagOpen() 141 142 /** 143 * This method id used to handle opening tags during the XML parsing 144 * @param resource $parser a resource reference to the XML parser 145 * @param String $tag representing the tag being treated 146 * @param Array $attributes containing the current element attributes 147 * @access private 148 */ 149 function _tagOpen($parser, $tag, $attributes) { 150 switch($tag) { 151 case "RESULTS" : $this->m_results = TRUE; 152 $this->m_searchResults = &new SearchResults(); 153 break; 154 case "RESULT" : $this->m_result = TRUE; 155 $this->m_currentResult = &new SearchResult(); 156 break; 157 } 158 $this->m_currentTag = $tag; 159 } 160 161 // }}} 162 163 // {{{ _tagClose() 164 165 /** 166 * This method id used to handle closing tags during the XML parsing 167 * @param resource $parser a resource reference to the XML parser 168 * @param String $tag representing the tag being treated 169 * @access private 170 */ 171 function _tagClose($parser, $tag) { 172 switch($tag) { 173 case "NUMBEROFRESULTS" : $this->m_searchResults->numberOfResults = intval($this->m_searchResults->numberOfResults); 174 break; 175 case "FIRSTRESULTINDEX" : $this->m_searchResults->firstResultIndex = intval($this->m_searchResults->firstResultIndex); 176 break; 177 case "LASTRESULTINDEX" : $this->m_searchResults->lastResultIndex = intval($this->m_searchResults->lastResultIndex); 178 break; 179 case "RESULT" : $this->m_result = FALSE; 180 $this->m_searchResults->results[] = &$this->m_currentResult; 181 unset($this->m_currentResult); 182 break; 183 } 184 } 185 // }}} 186 187 // {{{ _cdata() 188 189 /** 190 * This method id used to handle chracter datas encounterd 191 * while parsing an XML stream 192 * @param resource $parser a resource reference to the XML parser 193 * @param String $cdata representing the cdata encountered 194 * @access private 195 */ 196 function _cdata($parser, $cdata) { 197 switch($this->m_currentTag) { 198 case "NUMBEROFRESULTS" : $this->m_searchResults->numberOfResults .= $cdata; 199 break; 200 case "FIRSTRESULTINDEX" : $this->m_searchResults->firstResultIndex .= $cdata; 201 break; 202 case "LASTRESULTINDEX" : $this->m_searchResults->lastResultIndex .= $cdata; 203 break; 204 case "TITLE" : $this->m_currentResult->title .= $cdata; 205 break; 206 case "PERTINENCE" : $this->m_currentResult->pertinence .= $cdata; 207 break; 208 case "PATH" : $this->m_currentResult->path .= $cdata; 209 break; 210 case "FILENAME" : $this->m_currentResult->filename .= $cdata; 211 break; 212 case "ABSTRACT" : $this->m_currentResult->abstract .= $cdata; 213 break; 214 } 215 } 216 217 // }}} 218 219 } 220 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |