OWLStanza.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * @file
  4. * OWL redesign using Classes (Objects)
  5. */
  6. class OWLStanza {
  7. // The XMLReader object.
  8. private $owl = NULL;
  9. // A boolean (TRUE or FALSE) indicating if children should be parsed.
  10. private $parse_children = TRUE;
  11. // The XML element tag name (e.g. ‘owl:ObjectProperty’,
  12. // ‘owl:AnnotationProperty’, 'owl:Ontology').
  13. private $tag_name = '';
  14. // A key/value associative array of all of the attributes.
  15. private $attributes = array ();
  16. // An array of OWLStanza objects.
  17. private $children = array ();
  18. // The text value of a XML element.
  19. private $value;
  20. // Indicates if there is no more to read from the OWL XML file.
  21. private $is_finished = FALSE;
  22. /**
  23. * Implements the constructor.
  24. *
  25. * @param $parse_children
  26. * A boolean (TRUE or FALSE) indicating if children should be parsed.
  27. * If FALSE only attributes will be parse.
  28. */
  29. public function __construct($owl, $parse_children = TRUE) {
  30. $this->owl = $owl;
  31. $this->parse_children = $parse_children;
  32. $this->parse();
  33. }
  34. /**
  35. * A wrapper for XMLReader::read() read function.
  36. *
  37. * This function calls read() on the $owl object and checks to see
  38. * if we are at the end of the XML file. If so, it sets the $is_finished
  39. * member variable to TRUE.
  40. *
  41. * @return
  42. * The value of XMLReader::read().
  43. */
  44. private function owl_read() {
  45. $retval = $this->owl->read();
  46. if ($this->owl->nodeType == XMLReader::END_ELEMENT and $this->owl->name == 'rdf:RDF') {
  47. $this->is_finished = TRUE;
  48. }
  49. return $retval;
  50. }
  51. /**
  52. *
  53. * @param private
  54. * This function is to parse each of the OWL Stanzas of the ro.owl file.
  55. */
  56. private function parse() {
  57. // Make sure we are at the beginning of an element.
  58. while ($this->owl->nodeType != XMLReader::ELEMENT) {
  59. // In the event we've hit the end of the file, then return.
  60. if ($this->is_finished) {
  61. return;
  62. }
  63. $this->owl_read();
  64. }
  65. $this->tag_name = $this->owl->name;
  66. $has_value = $this->owl->hasValue;
  67. $is_empty = $this->owl->isEmptyElement;
  68. // Get the attributes.
  69. $num_attrs = $this->owl->attributeCount;
  70. if ($num_attrs > 0) {
  71. $this->owl->moveToFirstAttribute();
  72. for ($i = 0; $i < $num_attrs; $i++) {
  73. $this->attributes[$this->owl->name] = $this->owl->value;
  74. $this->owl->moveToNextAttribute();
  75. }
  76. }
  77. // If this element is empty then just return.
  78. if ($is_empty) {
  79. return;
  80. }
  81. // Determine if the element has a value. If so, then set the value for the class.
  82. if ($this->owl->hasValue) {
  83. $this->owl_read();
  84. $this->value = $this->owl->value;
  85. }
  86. // Get the children that should be parsed within the Stanza.
  87. if ($this->parse_children == TRUE) {
  88. while ($this->owl_read()) {
  89. if ($this->owl->nodeType == XMLReader::END_ELEMENT and $this->owl->name == $this->tag_name) {
  90. return;
  91. }
  92. else if ($this->owl->nodeType == XMLReader::ELEMENT) {
  93. $child = new OWLStanza($this->owl);
  94. $this->children[] = $child;
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * Get the Value
  101. *
  102. * @ return The text value of a XML element.
  103. */
  104. public function getValue() {
  105. return $this->value;
  106. }
  107. /**
  108. * Gets the children array.
  109. *
  110. * @return An array of OWLStanza objects containing the children elements.
  111. */
  112. public function getChildren() {
  113. return $this->children;
  114. }
  115. /**
  116. * Sets the children array.
  117. *
  118. * @param $children
  119. * An array of OWLStanza objects containing the children elements.
  120. * @return FALSE if the array was not set, TRUE otherwise.
  121. */
  122. public function setChildren($children) {
  123. // Makes sure the incoming argument is an array.
  124. if (! is_array ( $children )) {
  125. return FALSE;
  126. }
  127. // Make sure that all of the array elements are OWLStanza objects.
  128. foreach ( $children as $child ) {
  129. if (get_class ( $child ) != 'OWLStanza') {
  130. return FALSE;
  131. }
  132. }
  133. // All is good, set the children.
  134. $this->children = $children;
  135. }
  136. /**
  137. * Gets the child($tag_name) array.
  138. *
  139. * @return
  140. * An OWLStanza object whos tag name matches the tag name provided.
  141. *
  142. *
  143. */
  144. public function getChild($tag_name) {
  145. foreach ($this->children as $child) {
  146. if ($child->getTagName() == $tag_name) {
  147. return $child;
  148. }
  149. }
  150. return NULL;
  151. }
  152. /**
  153. * Gets the tag name.
  154. *
  155. * @return An XML element tag name (e.g. ‘owl:ObjectProperty’,
  156. * 'owl:AnnotationProperty’, 'owl:Ontology').
  157. */
  158. public function getTagName() {
  159. return $this->tag_name;
  160. }
  161. /**
  162. * Sets the tag name.
  163. *
  164. * @param $tag_name The
  165. * XML element tag name
  166. * @return value element tag name.
  167. */
  168. public function setTagName($tag_name) {
  169. $this->tag_name = $tag_name;
  170. return;
  171. }
  172. /**
  173. * Gets the attributes array.
  174. *
  175. * @return An array containing a key/value associative array
  176. * of all of the attributes between the XML elements tag name.
  177. */
  178. public function getAttributes() {
  179. return $this->attributes;
  180. }
  181. /**
  182. * Sets the attributes array.
  183. *
  184. * @param $attributes An
  185. * array containing a key/value associative array of all of the attributes.
  186. * @return FALSE if the array was not set, TRUE otherwise.
  187. */
  188. public function setAttributes($attributes) {
  189. // Makes sure the incoming argument is an array.
  190. if (! is_array ( $attributes )) {
  191. return FALSE;
  192. }
  193. // Make sure that all of the array are key/values.
  194. foreach ( $attributes as $attribute_name ) {
  195. if (get_class ($attribute_name ) != 'OWLStanza') {
  196. return FALSE;
  197. }
  198. }
  199. // All is good, set the attributes.
  200. $this->attributes = $attributes;
  201. }
  202. /**
  203. *
  204. * @param
  205. * $attribute_name
  206. * @return An|NULL
  207. */
  208. public function getAttribute($attribute_name) {
  209. foreach ($this->attributes as $aname => $value ) {
  210. if ($aname == $attribute_name) {
  211. return $value;
  212. }
  213. }
  214. return NULL;
  215. }
  216. /**
  217. * Checks if the OWL XML file has been completely parsed.
  218. *
  219. * @return
  220. * TRUE if parsing is completed, FALSE otherwise.
  221. */
  222. public function isFinished() {
  223. return $this->is_finished;
  224. }
  225. /**
  226. * Reconstructs the XML for the stanza.
  227. *
  228. * @return
  229. * A string containing XML for this stanza.
  230. */
  231. public function getXML() {
  232. // Start the element with the tag name.
  233. $xml = '<' . $this->tag_name;
  234. // Iterate through the attributes and add them to our XML string.
  235. if (count($this->attributes) > 0) {
  236. foreach ($this->attributes as $aname => $value) {
  237. $xml .= " " . $aname . '="' . $value . '"';
  238. }
  239. }
  240. // If this stanza has a value this implies there are no children,
  241. // so close the element start, add the value and add the closing element
  242. // tag.
  243. if ($this->value) {
  244. $xml .= '>' . $this->value . '</' . $this->tag_name . ">\n";
  245. return $xml;
  246. }
  247. // If we're here, we do not have a value. This is therefore an empty
  248. // element, or it has children. If we have no children then we
  249. // have an empty element and we can close it out and return.
  250. if (count($this->children) == 0) {
  251. $xml .= " />\n";
  252. return $xml;
  253. }
  254. // Add in the children's XML recursively.
  255. $childs = '';
  256. foreach ($this->children as $child) {
  257. // We want to add two character indentation to all lines returned by
  258. // the child XML.
  259. $childs .= preg_replace("/^/"," ", $child->getXML());
  260. }
  261. $xml .= ">\n";
  262. $xml .= $childs;
  263. $xml .= "</" . $this->tag_name . ">\n";
  264. return $xml;
  265. }
  266. }