1 |
<?php |
2 |
|
3 |
require_once("YakkaXml.php"); |
4 |
|
5 |
define("YAKKA_NAMESPACE_PREPROCESS", "http://www.ilker.de/yakka/yakka-template-preprocessor/0.1"); |
6 |
define("YAKKA_NAMESPACE_POSTPROCESS", "http://www.ilker.de/yakka/yakka-template-postprocessor/0.1"); |
7 |
define("YAKKA_NAMESPACE_XSL", "http://www.w3.org/1999/XSL/Transform"); |
8 |
|
9 |
class YakkaTemplateCommandProcessor { |
10 |
var $textProcessor; |
11 |
|
12 |
var $preProcessCommands; |
13 |
var $postProcessCommands; |
14 |
|
15 |
function YakkaTemplateCommandProcessor(&$textProcessor) { |
16 |
$this->textProcessor = &$textProcessor; |
17 |
|
18 |
$this->preProcessCommands = array( |
19 |
"include-template" => "includeTemplate" |
20 |
); |
21 |
|
22 |
$this->postProcessCommands = array( |
23 |
"process-text" => "processText", |
24 |
"pagelink" => "pageLink" |
25 |
); |
26 |
} |
27 |
|
28 |
function preProcess(&$xmlDocument, $templateLocation, $templateId, $filePath) { |
29 |
if (is_object($xmlDocument) && $xmlDocument->is("YakkaXml")) { |
30 |
$xmlDocument->declareNamespace("pre", YAKKA_NAMESPACE_PREPROCESS); |
31 |
|
32 |
while(list($commandName, $command) = each($this->preProcessCommands)) |
33 |
if ($elements = $xmlDocument->selectNodes("//pre:$commandName")) |
34 |
while(list($index, $element) = each($elements)) |
35 |
$this->$command($xmlDocument, $elements[$index], $templateLocation, $templateId, $filePath); |
36 |
} |
37 |
} |
38 |
|
39 |
function postProcess(&$xmlDocument) { |
40 |
if (is_object($xmlDocument) && $xmlDocument->is("YakkaXml")) { |
41 |
$xmlDocument->declareNamespace("post", YAKKA_NAMESPACE_POSTPROCESS); |
42 |
|
43 |
while(list($commandName, $command) = each($this->postProcessCommands)) |
44 |
if ($elements = $xmlDocument->selectNodes("//post:$commandName")) |
45 |
while(list($index, $element) = each($elements)) |
46 |
$this->$command($xmlDocument, $elements[$index]); |
47 |
} |
48 |
} |
49 |
|
50 |
function includeTemplate(&$domDocument, &$element, $templateLocation, $templateId, $filePath) { |
51 |
$parent = &$element->parent_node(); |
52 |
|
53 |
$template = $element->get_attribute("name").".xsl"; |
54 |
$xslInclude = $domDocument->dom->create_element("xsl:include"); |
55 |
$xslInclude->set_attribute("href", $template); |
56 |
|
57 |
$this->replaceElement($element, $xslInclude); |
58 |
} |
59 |
|
60 |
function processText(&$domDocument, &$element) { |
61 |
$processedText = "<div>".$this->textProcessor->process($element->get_content())."</div>"; |
62 |
$processedXml = new YakkaXml($processedText); |
63 |
|
64 |
$this->replaceElement($element, $processedXml->toDomElement()); |
65 |
} |
66 |
|
67 |
function pageLink(&$xmlDocument, &$element) { |
68 |
$dom = $xmlDocument->ToDom(); |
69 |
|
70 |
$page = $element->get_attribute("page"); |
71 |
$method = $element->get_attribute("method") ? $element->get_attribute("method") : null; |
72 |
if (($child = $element->first_child()) && $child->node_type() == XML_TEXT_NODE) |
73 |
$text = $child->node_value(); |
74 |
else |
75 |
$text = $page; |
76 |
|
77 |
$link = $this->textProcessor->CreateLink($page, $method, $text); |
78 |
$linkXml = new YakkaXml($link); |
79 |
|
80 |
$this->replaceElement($element, $linkXml->ToDomElement()); |
81 |
} |
82 |
|
83 |
function replaceElement(&$oldElement, &$newElement) { |
84 |
$parentElement = &$oldElement->parent_node(); |
85 |
$parentElement->insert_before($newElement, $oldElement); |
86 |
$parentElement->remove_child($oldElement); |
87 |
} |
88 |
} |
89 |
|
90 |
?> |