123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- class OWLStanza {
-
- private $owl = NULL;
-
- private $parse_children = TRUE;
-
-
- private $tag_name = '';
-
- private $attributes = array ();
-
- private $children = array ();
-
- private $value;
-
- private $is_finished = FALSE;
-
- public function __construct($owl, $parse_children = TRUE) {
- $this->owl = $owl;
- $this->parse_children = $parse_children;
- $this->parse();
- }
-
- private function owl_read() {
- $retval = $this->owl->read();
- if ($this->owl->nodeType == XMLReader::END_ELEMENT and $this->owl->name == 'rdf:RDF') {
- $this->is_finished = TRUE;
- }
- return $retval;
- }
-
- private function parse() {
-
- while ($this->owl->nodeType != XMLReader::ELEMENT) {
-
- if ($this->is_finished) {
- return;
- }
- $this->owl_read();
- }
- $this->tag_name = $this->owl->name;
- $has_value = $this->owl->hasValue;
- $is_empty = $this->owl->isEmptyElement;
-
- $num_attrs = $this->owl->attributeCount;
- if ($num_attrs > 0) {
- $this->owl->moveToFirstAttribute();
- for ($i = 0; $i < $num_attrs; $i++) {
- $this->attributes[$this->owl->name] = $this->owl->value;
- $this->owl->moveToNextAttribute();
- }
- }
-
- if ($is_empty) {
- return;
- }
-
- if ($this->owl->hasValue) {
- $this->owl_read();
- $this->value = $this->owl->value;
- }
-
- if ($this->parse_children == TRUE) {
- while ($this->owl_read()) {
- if ($this->owl->nodeType == XMLReader::END_ELEMENT and $this->owl->name == $this->tag_name) {
- return;
- }
- else if ($this->owl->nodeType == XMLReader::ELEMENT) {
- $child = new OWLStanza($this->owl);
- $this->children[] = $child;
- }
- }
- }
- }
-
- public function getValue() {
- return $this->value;
- }
-
- public function getChildren() {
- return $this->children;
- }
-
- public function setChildren($children) {
-
- if (! is_array ( $children )) {
- return FALSE;
- }
-
- foreach ( $children as $child ) {
- if (get_class ( $child ) != 'OWLStanza') {
- return FALSE;
- }
- }
-
- $this->children = $children;
- }
-
- public function getChild($tag_name) {
- foreach ($this->children as $child) {
- if ($child->getTagName() == $tag_name) {
- return $child;
- }
- }
- return NULL;
- }
-
- public function getTagName() {
- return $this->tag_name;
- }
-
- public function setTagName($tag_name) {
- $this->tag_name = $tag_name;
- return;
- }
-
- public function getAttributes() {
- return $this->attributes;
- }
-
- public function setAttributes($attributes) {
-
- if (! is_array ( $attributes )) {
- return FALSE;
- }
-
- foreach ( $attributes as $attribute_name ) {
- if (get_class ($attribute_name ) != 'OWLStanza') {
- return FALSE;
- }
- }
-
- $this->attributes = $attributes;
- }
-
- public function getAttribute($attribute_name) {
- foreach ($this->attributes as $aname => $value ) {
- if ($aname == $attribute_name) {
- return $value;
- }
- }
- return NULL;
- }
-
- public function isFinished() {
- return $this->is_finished;
- }
-
- public function getXML() {
-
- $xml = '<' . $this->tag_name;
-
- if (count($this->attributes) > 0) {
- foreach ($this->attributes as $aname => $value) {
- $xml .= " " . $aname . '="' . $value . '"';
- }
- }
-
-
-
- if ($this->value) {
- $xml .= '>' . $this->value . '</' . $this->tag_name . ">\n";
- return $xml;
- }
-
-
-
- if (count($this->children) == 0) {
- $xml .= " />\n";
- return $xml;
- }
-
- $childs = '';
- foreach ($this->children as $child) {
-
-
- $childs .= preg_replace("/^/"," ", $child->getXML());
- }
- $xml .= ">\n";
- $xml .= $childs;
- $xml .= "</" . $this->tag_name . ">\n";
- return $xml;
- }
- }
|