comparison class.template.php @ 0:c86959030ae8

Import initial
author Franck Deroche <webmaster@defr.net>
date Sat, 10 Mar 2007 21:48:42 +0100
parents
children 0f40a8a39c68
comparison
equal deleted inserted replaced
-1:000000000000 0:c86959030ae8
1 <?php
2 class Template {
3 var $xmlDocument;
4 var $xmlXPath;
5
6 const NS = 'http://defr.net/2007/template';
7
8 function __construct($fileName) {
9 $this->xmlDocument->validateOnParse = true;
10 $this->xmlDocument = DOMDocument::loadXML(file_get_contents($fileName));
11 $this->xmlXPath = new DOMXPath($this->xmlDocument);
12 }
13
14 function apply($selector, $obj) {
15 $rootSelector = $this->parseSelector($selector);
16 // Cloning test
17 if(is_array($obj)) {
18 foreach($obj as $array) {
19 foreach($rootSelector->nodes as $node) {
20 $nodeName = key($array);
21 $tmp = $this->getClonedNode($node, $nodeName);
22 $futureNode = $tmp->clone;
23 $node->insertBefore($futureNode, $tmp->orig);
24 foreach($array as $sel => $test) {
25 $content = $this->parseReplacement($test);
26 $localSelector = $this->parseSelector($sel);
27 if(isset($localSelector->attribute))
28 $futureNode->setAttribute($localSelector->attribute, $test);
29 else {
30 if($futureNode->namespaceURI == Template::NS)
31 $this->replaceNode($futureNode, $content);
32 else
33 $this->setNodeContent($futureNode, $content);
34 }
35 }
36 }
37 }
38 } else
39 {
40 $content = $this->parseReplacement($obj);
41 foreach($rootSelector->nodes as $node) {
42 if(isset($rootSelector->attribute)) {
43 $node->setAttribute($rootSelector->attribute, $obj);
44 } else {
45 if($node->namespaceURI == Template::NS)
46 $this->replaceNode($node, $content);
47 else
48 $this->setNodeContent($node, $content);
49 }
50 }
51 }
52 }
53
54 function getClonedNode($node, $childNodeName) {
55 $canditates = $node->getElementsByTagName($childNodeName);
56 foreach($canditates as $canditate) {
57 if($canditate->nodeName == $childNodeName && $canditate->getAttributeNS(Template::NS, 'toClone') == 'true') {
58 $tmp = array();
59 $tmp['orig'] = $canditate;
60 $cnode = $canditate->cloneNode(true);
61 $cnode->removeAttribute('toClone');
62 $tmp['clone'] = $cnode;
63 return (object)$tmp;
64 }
65 }
66 }
67
68 function parseSelector($selector, DOMElement $root=NULL) {
69 if(!($root instanceof DOMElement)) {
70 $root = $this->xmlDocument->documentElement;
71 }
72 $obj = array();
73 $pos = strpos($selector, '@');
74 if($pos !== false) {
75 $obj['attribute'] = substr($selector,$pos +1);
76 $selector = substr($selector, 0, $pos);
77 }
78 if($selector[0] == '#') {
79 $obj['xpath'] = "//*[@t:id='" . substr($selector, 1) . "']";
80 $obj['nodes'] = $this->xmlXPath->query($obj['xpath'], $root);
81 }
82 else {
83 $obj['nodeName'] = $selector;
84 $obj['nodes'] = $root->getElementsByTagName($selector);
85 $obj['nodes'] = $this->xmlDocument->getElementsByTagName($selector);
86 }
87 return (object)$obj;
88 }
89
90 function getNodesMatching($selector, DOMElement $root) {
91 $pos = strpos($selector, '/');
92 if($pos !== false) {
93 $currentSelector = substr($selector, 0, $pos);
94 $remainingSelector = substr($selector, $pos+1);
95 }
96 else {
97 $currentSelector = $selector;
98 $remainingSelector = NULL;
99 }
100 if($selector[0] == '#') {
101 $nodes = $this->xmlXPath->query($currentSelector, $root);
102 }
103 else {
104
105 }
106 }
107
108 function parseReplacement($obj) {
109 $retVal = NULL;
110 if(is_string($obj))
111 $retVal = $this->xmlDocument->createTextNode($obj);
112 else if($obj instanceof DOMDocument)
113 $retVal = $obj->documentElement;
114 else if($obj instanceof DOMNode)
115 $retVal = $obj;
116 return $retVal;
117 }
118
119 function setParams($array) {
120 foreach($array as $selector => $obj) {
121 $this->apply($selector, $obj);
122 }
123 }
124
125 function replaceNode(DOMNode $node, DOMNode $content) {
126 $parent = $node->parentNode;
127 $parent->replaceChild($content, $node);
128 }
129
130 function setNodeContent(DOMElement $node, DOMNode $content) {
131 // Suppress existing childs
132 foreach($node->childNodes as $child) {
133 $node->removeChild($child);
134 }
135 // Add the new child
136 $node->appendChild($content);
137 }
138
139 function clean() {
140 // Suppression des noeuds à cloner
141 $nodes = $this->xmlXPath->query("//*[@t:toClone]|//t:*");
142 foreach($nodes as $node) {
143 if($node->parentNode) {
144 $node->parentNode->removeChild($node);
145 }
146 }
147 }
148
149 function __toString() {
150 $this->clean();
151 $this->xmlDocument->saveXML();
152 return $this->xmlDocument->saveXML();
153 }
154 }