Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TXmlTransform class file
4
 *
5
 * @author Knut Urdalen <knut.urdalen@gmail.com>
6
 * @author Qiang Xue <qiang.xue@gmail.com>
7
 * @link http://www.pradosoft.com
8
 * @copyright Copyright &copy; 2005-2008 PradoSoft
9
 * @license http://www.pradosoft.com/license/
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TXmlTransform class
15
 *
16
 * TXmlTransform uses the PHP's XSL extension to perform
17
 * {@link http://www.w3.org/TR/xslt XSL transformations} using the
18
 * {@link http://xmlsoft.org/XSLT/ libxslt library}.
19
 *
20
 * To associate an XML style sheet with TXmlTransform set the
21
 * {@link setTransformPath TransformPath} property to the namespace or path to the style sheet
22
 * or set the {@link setTransformContent TransformContent} property to the XML style sheet
23
 * data as a string.
24
 *
25
 * To associate the XML data to be transformed set the {@link setDocumentPath DocumentPath}
26
 * property to the namespace or path to the XML document or set the
27
 * {@link setDocumentContent DocumentContent} property to the XML data as a string.
28
 *
29
 * To add additional parameters to the transformation process you can use the {@link getParameters Parameters}
30
 * property.
31
 *
32
 * @author Knut Urdalen <knut.urdalen@gmail.com>
33
 * @author Qiang Xue <qiang.xue@gmail.com>
34
 * @package System.Web.UI.WebControls
35
 * @since 3.1
36
 */
37
class TXmlTransform extends TControl {
38
 
39
  const EXT_XML_FILE = '.xml';
40
  const EXT_XSL_FILE = '.xsl';
41
 
42
  /**
43
   * Constructor
44
   *
45
   * Initializes the TXmlTransform object and ensure that the XSL extension is available
46
   * @throws TConfigurationException If XSL extension is not available
47
   */
48
  public function __construct() {
49
    if(!class_exists('XSLTProcessor', false)) {
50
      throw new TConfigurationException('xmltransform_xslextension_required');
51
    }
52
  }
53
 
54
  /**
55
   * @return string The path to the XML style sheet.
56
   */
57
  public function getTransformPath() {
58
    return $this->getViewState('TransformPath', '');
59
  }
60
 
61
  /**
62
   * @param string The path to the XML style sheet.  It must be in namespace format.
63
   */
64
  public function setTransformPath($value) {
65
    if(!is_file($value)) {
66
      $value = Prado::getPathOfNamespace($value, self::EXT_XSL_FILE);
67
      if($value === null) {
68
	throw new TInvalidDataValueException('xmltransform_transformpath_invalid', $value);
69
      }
70
    }
71
    $this->setViewState('TransformPath', $value, '');
72
  }
73
 
74
  /**
75
   * @return string XML style sheet as string
76
   */
77
  public function getTransformContent() {
78
    return $this->getViewState('TransformContent', '');
79
  }
80
 
81
  /**
82
   * @param string $value XML style sheet as string
83
   */
84
  public function setTransformContent($value) {
85
    $this->setViewState('TransformContent', $value, '');
86
  }
87
 
88
  /**
89
   * @return string The path to the XML document. It must be in namespace format.
90
   */
91
  public function getDocumentPath() {
92
    return $this->getViewState('DocumentPath', '');
93
  }
94
 
95
  /**
96
   * @param string Namespace or path to XML document
97
   * @throws TInvalidDataValueException
98
   */
99
  public function setDocumentPath($value) {
100
    if(!is_file($value)) {
101
      $value = Prado::getPathOfNamespace($value, self::EXT_XML_FILE);
102
      if($value === null) {
103
	throw new TInvalidDataValueException('xmltransform_documentpath_invalid', $value);
104
      }
105
    }
106
    $this->setViewState('DocumentPath', $value, '');
107
  }
108
 
109
  /**
110
   * @return string XML data
111
   */
112
  public function getDocumentContent() {
113
    return $this->getViewState('DocumentContent', '');
114
  }
115
 
116
  /**
117
   * @param string $value XML data. If not empty, it takes precedence over {@link setDocumentPath DocumentPath}.
118
   */
119
  public function setDocumentContent($value) {
120
    $this->setViewState('DocumentContent', $value, '');
121
  }
122
 
123
  /**
124
   * Returns the list of parameters to be applied to the transform.
125
   * @return TAttributeCollection the list of custom parameters
126
   */
127
  public function getParameters() {
128
    if($params = $this->getViewState('Parameters',null)) {
129
      return $params;
130
    } else {
131
      $params = new TAttributeCollection();
132
      $this->setViewState('Parameters', $params, null);
133
      return $params;
134
    }
135
  }
136
 
137
  private function getTransformXmlDocument() {
138
    if(($content = $this->getTransformContent()) !== '') {
139
      $document = new DOMDocument();
140
      $document->loadXML($content);
141
      return $document;
142
    } else if(($path = $this->getTransformPath()) !== '') {
143
      $document = new DOMDocument();
144
      $document->load($path);
145
      return $document;
146
    } else {
147
      throw new TConfigurationException('xmltransform_transform_required');
148
    }
149
  }
150
 
151
  private function getSourceXmlDocument() {
152
    if(($content = $this->getDocumentContent()) !== '') {
153
      $document = new DOMDocument();
154
      $document->loadXML($content);
155
      return $document;
156
    } else if(($path = $this->getDocumentPath()) !== '') {
157
      $document = new DOMDocument();
158
      $document->load($path);
159
      return $document;
160
    } else {
161
      return null;
162
    }
163
  }
164
 
165
  /**
166
   * Performs XSL transformation and render the output.
167
   * @param THtmlWriter The writer used for the rendering purpose
168
   */
169
  public function render($writer) {
170
    if(($document=$this->getSourceXmlDocument()) === null) {
171
      $textWriter = new TTextWriter();
172
      parent::render(new THtmlWriter($textWriter));
173
      $document = new DOMDocument();
174
      $document->loadXML($textWriter->flush());
175
    }
176
    $stylesheet = $this->getTransformXmlDocument();
177
 
178
    // Perform XSL transformation
179
    $xslt = new XSLTProcessor();
180
    $xslt->importStyleSheet($stylesheet);
181
 
182
    // Check for parameters
183
    $parameters = $this->getParameters();
184
    foreach($parameters as $name => $value) {
185
      $xslt->setParameter('', $name, $value);
186
    }
187
    $output = $xslt->transformToXML($document);
188
 
189
    // Write output
190
    $writer->write($output);
191
  }
192
}
193