Ticket #465: jResponseXml.class.php

File jResponseXml.class.php, 5.0 kB (added by sylvain261, 11 months ago)
Line 
1 <?php
2 /**
3 * @package     jelix
4 * @subpackage  core_response
5 * @author      Loic Mathaud
6 * @contributor Laurent Jouanneau
7 * @copyright   2005-2006 loic Mathaud
8 * @copyright   2007 Laurent Jouanneau
9 * @link        http://www.jelix.org
10 * @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
11 */
12
13 /**
14 *
15 */
16 require_once(JELIX_LIB_TPL_PATH.'jTpl.class.php');
17
18 /**
19 * XML response generator
20 * @package  jelix
21 * @subpackage core_response
22 */
23 class jResponseXml extends jResponse {
24     /**
25     * Id of the response
26     * @var string
27     */
28     protected $_type = 'xml';
29
30
31     /**
32      * the template container
33      * @var jTpl
34      */
35     public $content = null;
36
37     /**
38      * selector of the template file
39      * @var string
40      */
41     public $contentTpl = '';
42
43     /**
44      * The charset
45      * @var string
46      */
47     protected $_charset;
48
49     private $_css = array();
50     private $_xsl = array();
51
52     /**
53      * says what part of the html head has been send
54      * @var integer
55      */
56     protected $_headSent = 0;
57
58     /**
59      * Indique si les headers XML doivent être générés ou non
60      * Utile dans le cas où le flux XML à renvoyer contient déjà l'entête XML
61      * @var boolean
62      */
63     public $sendXMLHeader = TRUE;
64
65
66     /**
67     * constructor..
68     */
69     function __construct (){
70         global $gJConfig;
71         $this->_charset = $gJConfig->charset;
72         $this->content = new jTpl();
73         parent::__construct();
74     }
75
76     /**
77      * generate the xml content and send it to the browser
78      * @return boolean    true if ok
79      */
80     final public function output(){
81         $this->_httpHeaders['Content-Type']='text/xml;charset='.$this->_charset;
82         $this->sendHttpHeaders();
83
84         if($this->sendXMLHeader){
85             echo '<?xml version="1.0" encoding="'. $this->_charset .'"?>', "\n";
86             $this->outputXmlHeader();
87         }
88         $this->_headSent = true;
89
90
91         if(is_string($this->content)) {
92             // utilisation chaine de caractères xml
93             $xml_string = $this->content;
94         }else if (!empty($this->contentTpl)) {
95             // utilisation d'un template
96             $xml_string = $this->content->fetch($this->contentTpl);
97         }else{
98             throw new jException('jelix~errors.repxml.no.content');
99         }
100
101         if (simplexml_load_string($xml_string)) {
102             echo $xml_string;
103         } else {
104             // xml mal formé
105             throw new jException('jelix~errors.repxml.invalid.content');
106         }
107         return true;
108     }
109
110     /**
111      * output errors if any
112      */
113     final public function outputErrors() {
114         if (!$this->_headSent) {
115             if (!$this->_httpHeadersSent) {
116                 header("HTTP/1.0 500 Internal Server Error");
117                 header('Content-Type: text/xml;charset='.$this->_charset);
118             }
119             echo '<?xml version="1.0" encoding="'. $this->_charset .'"?>';
120         }
121
122         echo '<errors xmlns="http://jelix.org/ns/xmlerror/1.0">';
123         if ($this->hasErrors()) {
124             foreach ($GLOBALS['gJCoord']->errorMessages  as $e) {
125                 echo '<error xmlns="http://jelix.org/ns/xmlerror/1.0" type="'. $e[0] .'" code="'. $e[1] .'" file="'. $e[3] .'" line="'. $e[4] .'">'.htmlspecialchars($e[2], ENT_NOQUOTES, $this->_charset). '</error>'. "\n";
126             }
127         } else {
128             echo '<error>Unknow Error</error>';
129         }
130         echo '</errors>';
131     }
132
133     /**
134      * to add a link to css stylesheet
135      * @since 1.0b1
136      */
137     public function addCSSStyleSheet($src, $params = array()) {
138         if (!isset($this->_css[$src])) {
139             $this->_css[$src] = $params;
140         }
141     }
142
143     /**
144      * to add a link to an xsl stylesheet
145      * @since 1.0b1
146      */
147     public function addXSLStyleSheet($src, $params = array()) {
148         if (!isset($this->_xsl[$src])) {
149             $this->_xsl[$src] = $params;
150         }
151     }
152
153     /**
154      * output all processing instructions (stylesheet, xsl..) before the XML content
155      */
156     protected function outputXmlHeader() {
157         // XSL stylesheet
158         foreach ($this->_xsl as $src => $params) {
159             //the extra params we may found in there.
160             $more = '';
161             foreach ($params as $param_name => $param_value) {
162                 $more .= $param_name.'="'. htmlspecialchars($param_value).'" ';
163             }
164             echo ' <?xml-stylesheet type="text/xsl" href="', $src,'" ', $more,' ?>';
165         }
166
167         // CSS stylesheet
168         foreach ($this->_css as $src => $params) {
169             //the extra params we may found in there.
170             $more = '';
171             foreach ($params as $param_name => $param_value) {
172                 $more .= $param_name.'="'. htmlspecialchars($param_value).'" ';
173             }
174             echo ' <?xml-stylesheet type="text/css" href="', $src,'" ', $more,' ?>';
175         }
176     }
177 }
178
179 ?>
Download in other formats: Original Format