OWLStanza.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 = [];
  16. // An array of OWLStanza objects.
  17. private $children = [];
  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 {
  93. if ($this->owl->nodeType == XMLReader::ELEMENT) {
  94. $child = new OWLStanza($this->owl);
  95. $this->children[] = $child;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Get the Value
  103. *
  104. * @ return The text value of a XML element.
  105. */
  106. public function getValue() {
  107. return $this->value;
  108. }
  109. /**
  110. * Gets the children array.
  111. *
  112. * @return An array of OWLStanza objects containing the children elements.
  113. */
  114. public function getChildren() {
  115. return $this->children;
  116. }
  117. /**
  118. * Sets the children array.
  119. *
  120. * @param $children
  121. * An array of OWLStanza objects containing the children elements.
  122. *
  123. * @return FALSE if the array was not set, TRUE otherwise.
  124. */
  125. public function setChildren($children) {
  126. // Makes sure the incoming argument is an array.
  127. if (!is_array($children)) {
  128. return FALSE;
  129. }
  130. // Make sure that all of the array elements are OWLStanza objects.
  131. foreach ($children as $child) {
  132. if (get_class($child) != 'OWLStanza') {
  133. return FALSE;
  134. }
  135. }
  136. // All is good, set the children.
  137. $this->children = $children;
  138. }
  139. /**
  140. * Gets the child($tag_name) array.
  141. *
  142. * @return
  143. * An OWLStanza object whos tag name matches the tag name provided.
  144. *
  145. *
  146. */
  147. public function getChild($tag_name) {
  148. foreach ($this->children as $child) {
  149. if ($child->getTagName() == $tag_name) {
  150. return $child;
  151. }
  152. }
  153. return NULL;
  154. }
  155. /**
  156. * Gets the tag name.
  157. *
  158. * @return An XML element tag name (e.g. ‘owl:ObjectProperty’,
  159. * 'owl:AnnotationProperty’, 'owl:Ontology').
  160. */
  161. public function getTagName() {
  162. return $this->tag_name;
  163. }
  164. /**
  165. * Sets the tag name.
  166. *
  167. * @param $tag_name The
  168. * XML element tag name
  169. *
  170. * @return value element tag name.
  171. */
  172. public function setTagName($tag_name) {
  173. $this->tag_name = $tag_name;
  174. return;
  175. }
  176. /**
  177. * Gets the attributes array.
  178. *
  179. * @return An array containing a key/value associative array
  180. * of all of the attributes between the XML elements tag name.
  181. */
  182. public function getAttributes() {
  183. return $this->attributes;
  184. }
  185. /**
  186. * Sets the attributes array.
  187. *
  188. * @param $attributes An
  189. * array containing a key/value associative array of all of the
  190. * attributes.
  191. *
  192. * @return FALSE if the array was not set, TRUE otherwise.
  193. */
  194. public function setAttributes($attributes) {
  195. // Makes sure the incoming argument is an array.
  196. if (!is_array($attributes)) {
  197. return FALSE;
  198. }
  199. // Make sure that all of the array are key/values.
  200. foreach ($attributes as $attribute_name) {
  201. if (get_class($attribute_name) != 'OWLStanza') {
  202. return FALSE;
  203. }
  204. }
  205. // All is good, set the attributes.
  206. $this->attributes = $attributes;
  207. }
  208. /**
  209. *
  210. * @param
  211. * $attribute_name
  212. *
  213. * @return An|NULL
  214. */
  215. public function getAttribute($attribute_name) {
  216. foreach ($this->attributes as $aname => $value) {
  217. if ($aname == $attribute_name) {
  218. return $value;
  219. }
  220. }
  221. return NULL;
  222. }
  223. /**
  224. * Checks if the OWL XML file has been completely parsed.
  225. *
  226. * @return
  227. * TRUE if parsing is completed, FALSE otherwise.
  228. */
  229. public function isFinished() {
  230. return $this->is_finished;
  231. }
  232. /**
  233. * Reconstructs the XML for the stanza.
  234. *
  235. * @return
  236. * A string containing XML for this stanza.
  237. */
  238. public function getXML() {
  239. // Start the element with the tag name.
  240. $xml = '<' . $this->tag_name;
  241. // Iterate through the attributes and add them to our XML string.
  242. if (count($this->attributes) > 0) {
  243. foreach ($this->attributes as $aname => $value) {
  244. $xml .= " " . $aname . '="' . $value . '"';
  245. }
  246. }
  247. // If this stanza has a value this implies there are no children,
  248. // so close the element start, add the value and add the closing element
  249. // tag.
  250. if ($this->value) {
  251. $xml .= '>' . $this->value . '</' . $this->tag_name . ">\n";
  252. return $xml;
  253. }
  254. // If we're here, we do not have a value. This is therefore an empty
  255. // element, or it has children. If we have no children then we
  256. // have an empty element and we can close it out and return.
  257. if (count($this->children) == 0) {
  258. $xml .= " />\n";
  259. return $xml;
  260. }
  261. // Add in the children's XML recursively.
  262. $childs = '';
  263. foreach ($this->children as $child) {
  264. // We want to add two character indentation to all lines returned by
  265. // the child XML.
  266. $childs .= preg_replace("/^/", " ", $child->getXML());
  267. }
  268. $xml .= ">\n";
  269. $xml .= $childs;
  270. $xml .= "</" . $this->tag_name . ">\n";
  271. return $xml;
  272. }
  273. }