Ticket #465: jResponseXml.class.2.php

File jResponseXml.class.2.php, 4.9 kB (added by sylvain261, 9 months ago)

Avec les bons cariage return et les comments en anglaiu

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