9f266051891286c4ca1f00f3993bc994a7c49c17
[atutor.git] / mods / atutor_opencaps / opencaps / conversion_service / include / classes / core_XmlTagTrace.php
1 <?php\r
2 // \r
3 /**\r
4  * This class provides the functionality to trace an xml tag \r
5  */\r
6 \r
7 class XmlTagTrace\r
8 {\r
9         private $tagToTrace; // the xml tag to be collected\r
10         private $tagToTraceLevel; // The level of the xml tag bo be collected/traced\r
11         private $xmlString; // Xml data as string\r
12         \r
13         private $xmlTag; // temporary object to store XML object while reading xml array\r
14         private $xmlTagState; // true or false if tracing tag is done\r
15         private $myXmlCollection; // collection of the traced xml tag\r
16         \r
17         /**\r
18          * Class Constructor: starts tracing a XML tag. It recieves a xml TagName and xml TagLevel\r
19          * @param String $theXmlString XML data as String\r
20          * @param String $theTagToTrace XML Tag to trace \r
21          * @param String $theTagToTraceLevel Level of the XML tag to trace\r
22          */\r
23         function __construct($theXmlString,$theTagToTrace,$theTagToTraceLevel)\r
24         {\r
25         \r
26                 // set the XML data\r
27                 //$this->xmlString = $theXmlString;\r
28                 \r
29                 // set the tag to trace and the starting level\r
30                 $this->tagToTrace = $theTagToTrace;\r
31                 $this->tagToTraceLevel = $theTagToTraceLevel;\r
32                 \r
33                 // create XML tag Collection\r
34                 $this->myXmlCollection = new XmlTagCollection();\r
35                 \r
36                 // start tracing\r
37                 $this->traceXmlTag($theXmlString);              \r
38         } // __construct() end\r
39 \r
40         /**\r
41          * Returns the XML tag Collection\r
42          *\r
43          */\r
44         public function getCollection()\r
45         {\r
46                 //get from here of from XmlTagCollection collection object???\r
47                 return $this->myXmlCollection->getXmlTagCollection();\r
48                 //return $this->myXmlCollection;\r
49                 //echo '<br>From '\r
50                 \r
51                         \r
52         }// end getCollection()\r
53         \r
54         /**\r
55          * Creates an XML parcer and Iterates through array\r
56          *\r
57          * @param unknown_type $theXmlString\r
58          */\r
59         private function traceXmlTag($theXmlString)\r
60         {\r
61                 // create XML parser\r
62                 $p = xml_parser_create();\r
63                 \r
64                 // parse XML data into array\r
65                 xml_parse_into_struct($p, $theXmlString, $xmlVals, $xmlIndex);\r
66                 \r
67                 // free XML parser \r
68                 xml_parser_free($p);\r
69                 \r
70                 // initialize XML object ????\r
71                 //$myXmlTag = new XmlTag()\r
72 \r
73                 \r
74                 // start looping xml array\r
75                 for($i = 0; $i < count($xmlVals); $i++) \r
76                 {\r
77                         // initialize XML data\r
78                         $theTagName='';\r
79                         $theTagType='';\r
80                         $theTagLevel='';\r
81                         $theTagValue='';\r
82                         $theTagAtrib = Array();\r
83                         \r
84                         // verify data before adding\r
85                         if (isset($xmlVals[$i]['tag']))\r
86                         {\r
87                                 $theTagName = $xmlVals[$i]['tag'];\r
88                         }\r
89                         \r
90                         if (isset($xmlVals[$i]['type']))\r
91                         {\r
92                                 $theTagType = $xmlVals[$i]['type'];\r
93                         }\r
94                         \r
95                         if (isset($xmlVals[$i]['level']))\r
96                         {\r
97                                 $theTagLevel = $xmlVals[$i]['level'];\r
98                         }\r
99                         \r
100                         if (isset($xmlVals[$i]['value']))\r
101                         {\r
102                                 $theTagValue = $xmlVals[$i]['value'];\r
103                         }\r
104 \r
105                         if (isset($xmlVals[$i]['attributes']))\r
106                         {\r
107                                 $theTagAtrib = $xmlVals[$i]['attributes'];\r
108                         }                       \r
109                         \r
110                         // set xml data\r
111                         $this->setXmlData($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);\r
112                         \r
113                 \r
114                 } // end for loop xml array\r
115                 \r
116         } // end traceXmlTag()\r
117         \r
118         /**\r
119          * Recieves Xml data from each tag and determines if values should be collected \r
120          * this to trace <P> and <div> tags  \r
121          *\r
122          * @param String $theTagName\r
123          * @param String $theTagType\r
124          * @param String $theTagLevel\r
125          * @param String $theTagValue\r
126          * @param String $theTagAtrib\r
127          */\r
128         private function setXmlData($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib)\r
129         {\r
130                 \r
131                 //echo '<br/>'.$this->tagToTrace;\r
132                 \r
133                 // verify also if the level is lower !!!!\r
134                 \r
135                 //echo '<br/>'.$theTagName;\r
136                 // verify if this data is from the xml tag being traced/collected\r
137                 if ($theTagName == $this->tagToTrace)\r
138                 {\r
139                         \r
140                         //echo '<br/>'.$theTagName.' TAG IS THE SAME';\r
141                         //echo '<br/>Value: '.$theTagValue.'';\r
142                         \r
143                         \r
144                         // if tag is complete. The tag has NO children\r
145                         if ($theTagType=='complete')\r
146                         {\r
147                                 // create a XmlTag object\r
148                                 $this->xmlTag = new XmlTag($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);\r
149                                 \r
150                                 // tracing this tag is done.  \r
151                                 $this->xmlTagState = true;\r
152                                 \r
153                                 $this->xmlTag->setTagState(true);\r
154                                 \r
155                                 //echo '<br/>'.$theTagName.' TAG is complete !!!';\r
156                                 \r
157                                 //$this->xmlTag->toString();\r
158                                 \r
159                                 // add to collection\r
160                                 $this->myXmlCollection->addXmlTagObject($this->xmlTag);\r
161                                 \r
162                                 // reset temp object\r
163                                 $this->xmlTag = null;\r
164 \r
165                         } // end if complete\r
166 \r
167                         // if tag is open. tag do have children\r
168                         else if ($theTagType=='open')\r
169                         {\r
170                                 // create a new XmlTag object\r
171                                 $this->xmlTag = new XmlTag($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);\r
172                                 \r
173                                 // tracing this tag is NOT done.  \r
174                                 $this->xmlTagState = false;\r
175                                 \r
176                                 //echo '<br/>'.$theTagName.' TAG is Open !!!';\r
177                                 \r
178                         } // end if\r
179 \r
180                         // if tag cdata. Getting data of the traced Tag, after any children data\r
181                         else if ($theTagType=='cdata')\r
182                         {\r
183                                 // add value to traced tag \r
184                                 // OJO !! This skips any child value \r
185                                 $this->xmlTag->addToTagValue($theTagValue);\r
186                                 \r
187                                 // tracing this tag is NOT done.  \r
188                                 $this->xmlTagState = false;\r
189                                 //echo '<br/>'.$theTagName.' TAG is cdata!!!';\r
190                                 \r
191                                 \r
192                         } // end if\r
193 \r
194                         // if tag close. Getting data of the traced Tag is done\r
195                         else if ($theTagType=='close')\r
196                         {\r
197                                 // add value of traced tag \r
198                                 $this->xmlTag->addToTagValue($theTagValue);\r
199                                 \r
200                                 // tracing this tag is NOT done.  \r
201                                 $this->xmlTagState = true;\r
202                                 \r
203                                 $this->xmlTag->setTagState(true);\r
204                                 //echo '<br/>'.$theTagName.' TAG is close!!!';\r
205                                 \r
206                                 //add to collection\r
207                                 $this->myXmlCollection->addXmlTagObject($this->xmlTag);\r
208                                 \r
209                                 // reset temp object\r
210                                 $this->xmlTag = null;\r
211                                 \r
212                         } // end if\r
213                         \r
214                         \r
215                 }// end if tracing xml tag\r
216 \r
217                 /*\r
218                  * if the tag is a children. the tagToTraceLevel is lower than the current TagLevel \r
219                  * this for all tags like <br/>, <b>, <i>, <u>, or even for <span> inside our traced tag\r
220                  */\r
221                 if ($this->tagToTraceLevel<$theTagLevel && (isset($this->xmlTag)))\r
222                 {\r
223                         // adding children data to tag as value \r
224                         $this->xmlTag->addChildTagAsValue($theTagName,$theTagType,$theTagValue);\r
225                         \r
226                 } // end if\r
227                 \r
228                 \r
229         } // end setXmlData()\r
230         \r
231         /**\r
232          * Print all values of the XML tag as a String\r
233          */     \r
234         public function toString()\r
235         {\r
236                 $this->myXmlCollection->toString();\r
237 \r
238                 \r
239         }// toString() end \r
240         \r
241 } // end class XmlTagTrace\r
242 ?>