diff options
author | Fabio Bas <ctrlaltca@gmail.com> | 2015-01-20 21:52:37 +0100 |
---|---|---|
committer | Fabio Bas <ctrlaltca@gmail.com> | 2015-01-20 21:52:37 +0100 |
commit | f6c4b70070a8e4378a37f750d53920e44bcc5857 (patch) | |
tree | 4427abbe8e786ccceddb3d43123a831c448d08ad /framework/Xml | |
parent | 001e69daef223679ad2331e61e78f45aec590f0a (diff) |
One class per file: framework/Xml
Diffstat (limited to 'framework/Xml')
-rw-r--r-- | framework/Xml/TXmlDocument.php | 316 | ||||
-rw-r--r-- | framework/Xml/TXmlElement.php | 253 | ||||
-rw-r--r-- | framework/Xml/TXmlElementList.php | 81 |
3 files changed, 334 insertions, 316 deletions
diff --git a/framework/Xml/TXmlDocument.php b/framework/Xml/TXmlDocument.php index 69017660..6cce0640 100644 --- a/framework/Xml/TXmlDocument.php +++ b/framework/Xml/TXmlDocument.php @@ -10,249 +10,6 @@ */ /** - * TXmlElement class. - * - * TXmlElement represents an XML element node. - * You can obtain its tag-name, attributes, text between the opening and closing - * tags via the TagName, Attributes, and Value properties, respectively. - * You can also retrieve its parent and child elements by Parent and Elements - * properties, respectively. - * - * TBD: xpath - * - * @author Qiang Xue <qiang.xue@gmail.com> - * @package System.Xml - * @since 3.0 - */ -class TXmlElement extends TComponent -{ - /** - * @var TXmlElement parent of this element - */ - private $_parent=null; - /** - * @var string tag-name of this element - */ - private $_tagName='unknown'; - /** - * @var string text enclosed between opening and closing tags of this element - */ - private $_value=''; - /** - * @var TXmlElementList list of child elements of this element - */ - private $_elements=null; - /** - * @var TMap attributes of this element - */ - private $_attributes=null; - - /** - * Constructor. - * @param string tag-name for this element - */ - public function __construct($tagName) - { - $this->setTagName($tagName); - } - - /** - * @return TXmlElement parent element of this element - */ - public function getParent() - { - return $this->_parent; - } - - /** - * @param TXmlElement parent element of this element - */ - public function setParent($parent) - { - $this->_parent=$parent; - } - - /** - * @return string tag-name of this element - */ - public function getTagName() - { - return $this->_tagName; - } - - /** - * @param string tag-name of this element - */ - public function setTagName($tagName) - { - $this->_tagName=$tagName; - } - - /** - * @return string text enclosed between opening and closing tag of this element - */ - public function getValue() - { - return $this->_value; - } - - /** - * @param string text enclosed between opening and closing tag of this element - */ - public function setValue($value) - { - $this->_value=TPropertyValue::ensureString($value); - } - - /** - * @return boolean true if this element has child elements - */ - public function getHasElement() - { - return $this->_elements!==null && $this->_elements->getCount()>0; - } - - /** - * @return boolean true if this element has attributes - */ - public function getHasAttribute() - { - return $this->_attributes!==null && $this->_attributes->getCount()>0; - } - - /** - * @return string the attribute specified by the name, null if no such attribute - */ - public function getAttribute($name) - { - if($this->_attributes!==null) - return $this->_attributes->itemAt($name); - else - return null; - } - - /** - * @param string attribute name - * @param string attribute value - */ - public function setAttribute($name,$value) - { - $this->getAttributes()->add($name,TPropertyValue::ensureString($value)); - } - - /** - * @return TXmlElementList list of child elements - */ - public function getElements() - { - if(!$this->_elements) - $this->_elements=new TXmlElementList($this); - return $this->_elements; - } - - /** - * @return TMap list of attributes - */ - public function getAttributes() - { - if(!$this->_attributes) - $this->_attributes=new TMap; - return $this->_attributes; - } - - /** - * @return TXmlElement the first child element that has the specified tag-name, null if not found - */ - public function getElementByTagName($tagName) - { - if($this->_elements) - { - foreach($this->_elements as $element) - if($element->_tagName===$tagName) - return $element; - } - return null; - } - - /** - * @return TList list of all child elements that have the specified tag-name - */ - public function getElementsByTagName($tagName) - { - $list=new TList; - if($this->_elements) - { - foreach($this->_elements as $element) - if($element->_tagName===$tagName) - $list->add($element); - } - return $list; - } - - /** - * @return string string representation of this element - */ - public function toString($indent=0) - { - $attr=''; - if($this->_attributes!==null) - { - foreach($this->_attributes as $name=>$value) - { - $value=$this->xmlEncode($value); - $attr.=" $name=\"$value\""; - } - } - $prefix=str_repeat(' ',$indent*4); - if($this->getHasElement()) - { - $str=$prefix."<{$this->_tagName}$attr>\n"; - foreach($this->getElements() as $element) - $str.=$element->toString($indent+1)."\n"; - $str.=$prefix."</{$this->_tagName}>"; - return $str; - } - else if(($value=$this->getValue())!=='') - { - $value=$this->xmlEncode($value); - return $prefix."<{$this->_tagName}$attr>$value</{$this->_tagName}>"; - } - else - return $prefix."<{$this->_tagName}$attr />"; - } - - /** - * Magic-method override. Called whenever this element is used as a string. - * <code> - * $element = new TXmlElement('tag'); - * echo $element; - * </code> - * or - * <code> - * $element = new TXmlElement('tag'); - * $xml = (string)$element; - * </code> - * @return string string representation of this element - */ - public function __toString() - { - return $this->toString(); - } - - private function xmlEncode($str) - { - return strtr($str,array( - '>'=>'>', - '<'=>'<', - '&'=>'&', - '"'=>'"', - "\r"=>'
', - "\t"=>'	', - "\n"=>'
')); - } -} - -/** * TXmlDocument class. * * TXmlDocument represents a DOM representation of an XML file. @@ -490,76 +247,3 @@ class TXmlDocument extends TXmlElement return $element; } } - - -/** - * TXmlElementList class. - * - * TXmlElementList represents a collection of {@link TXmlElement}. - * You may manipulate the collection with the operations defined in {@link TList}. - * - * @author Qiang Xue <qiang.xue@gmail.com> - * @package System.Xml - * @since 3.0 - */ -class TXmlElementList extends TList -{ - /** - * @var TXmlElement owner of this list - */ - private $_o; - - /** - * Constructor. - * @param TXmlElement owner of this list - */ - public function __construct(TXmlElement $owner) - { - $this->_o=$owner; - } - - /** - * @return TXmlElement owner of this list - */ - protected function getOwner() - { - return $this->_o; - } - - /** - * Inserts an item at the specified position. - * This overrides the parent implementation by performing additional - * operations for each newly added TXmlElement object. - * @param integer the specified position. - * @param mixed new item - * @throws TInvalidDataTypeException if the item to be inserted is not a TXmlElement object. - */ - public function insertAt($index,$item) - { - if($item instanceof TXmlElement) - { - parent::insertAt($index,$item); - if($item->getParent()!==null) - $item->getParent()->getElements()->remove($item); - $item->setParent($this->_o); - } - else - throw new TInvalidDataTypeException('xmlelementlist_xmlelement_required'); - } - - /** - * Removes an item at the specified position. - * This overrides the parent implementation by performing additional - * cleanup work when removing a TXmlElement object. - * @param integer the index of the item to be removed. - * @return mixed the removed item. - */ - public function removeAt($index) - { - $item=parent::removeAt($index); - if($item instanceof TXmlElement) - $item->setParent(null); - return $item; - } -} - diff --git a/framework/Xml/TXmlElement.php b/framework/Xml/TXmlElement.php new file mode 100644 index 00000000..2df8308c --- /dev/null +++ b/framework/Xml/TXmlElement.php @@ -0,0 +1,253 @@ +<?php +/** + * TXmlElement, TXmlDocument, TXmlElementList class file + * + * @author Qiang Xue <qiang.xue@gmail.com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Xml + */ + +/** + * TXmlElement class. + * + * TXmlElement represents an XML element node. + * You can obtain its tag-name, attributes, text between the opening and closing + * tags via the TagName, Attributes, and Value properties, respectively. + * You can also retrieve its parent and child elements by Parent and Elements + * properties, respectively. + * + * TBD: xpath + * + * @author Qiang Xue <qiang.xue@gmail.com> + * @package System.Xml + * @since 3.0 + */ +class TXmlElement extends TComponent +{ + /** + * @var TXmlElement parent of this element + */ + private $_parent=null; + /** + * @var string tag-name of this element + */ + private $_tagName='unknown'; + /** + * @var string text enclosed between opening and closing tags of this element + */ + private $_value=''; + /** + * @var TXmlElementList list of child elements of this element + */ + private $_elements=null; + /** + * @var TMap attributes of this element + */ + private $_attributes=null; + + /** + * Constructor. + * @param string tag-name for this element + */ + public function __construct($tagName) + { + $this->setTagName($tagName); + } + + /** + * @return TXmlElement parent element of this element + */ + public function getParent() + { + return $this->_parent; + } + + /** + * @param TXmlElement parent element of this element + */ + public function setParent($parent) + { + $this->_parent=$parent; + } + + /** + * @return string tag-name of this element + */ + public function getTagName() + { + return $this->_tagName; + } + + /** + * @param string tag-name of this element + */ + public function setTagName($tagName) + { + $this->_tagName=$tagName; + } + + /** + * @return string text enclosed between opening and closing tag of this element + */ + public function getValue() + { + return $this->_value; + } + + /** + * @param string text enclosed between opening and closing tag of this element + */ + public function setValue($value) + { + $this->_value=TPropertyValue::ensureString($value); + } + + /** + * @return boolean true if this element has child elements + */ + public function getHasElement() + { + return $this->_elements!==null && $this->_elements->getCount()>0; + } + + /** + * @return boolean true if this element has attributes + */ + public function getHasAttribute() + { + return $this->_attributes!==null && $this->_attributes->getCount()>0; + } + + /** + * @return string the attribute specified by the name, null if no such attribute + */ + public function getAttribute($name) + { + if($this->_attributes!==null) + return $this->_attributes->itemAt($name); + else + return null; + } + + /** + * @param string attribute name + * @param string attribute value + */ + public function setAttribute($name,$value) + { + $this->getAttributes()->add($name,TPropertyValue::ensureString($value)); + } + + /** + * @return TXmlElementList list of child elements + */ + public function getElements() + { + if(!$this->_elements) + $this->_elements=new TXmlElementList($this); + return $this->_elements; + } + + /** + * @return TMap list of attributes + */ + public function getAttributes() + { + if(!$this->_attributes) + $this->_attributes=new TMap; + return $this->_attributes; + } + + /** + * @return TXmlElement the first child element that has the specified tag-name, null if not found + */ + public function getElementByTagName($tagName) + { + if($this->_elements) + { + foreach($this->_elements as $element) + if($element->_tagName===$tagName) + return $element; + } + return null; + } + + /** + * @return TList list of all child elements that have the specified tag-name + */ + public function getElementsByTagName($tagName) + { + $list=new TList; + if($this->_elements) + { + foreach($this->_elements as $element) + if($element->_tagName===$tagName) + $list->add($element); + } + return $list; + } + + /** + * @return string string representation of this element + */ + public function toString($indent=0) + { + $attr=''; + if($this->_attributes!==null) + { + foreach($this->_attributes as $name=>$value) + { + $value=$this->xmlEncode($value); + $attr.=" $name=\"$value\""; + } + } + $prefix=str_repeat(' ',$indent*4); + if($this->getHasElement()) + { + $str=$prefix."<{$this->_tagName}$attr>\n"; + foreach($this->getElements() as $element) + $str.=$element->toString($indent+1)."\n"; + $str.=$prefix."</{$this->_tagName}>"; + return $str; + } + else if(($value=$this->getValue())!=='') + { + $value=$this->xmlEncode($value); + return $prefix."<{$this->_tagName}$attr>$value</{$this->_tagName}>"; + } + else + return $prefix."<{$this->_tagName}$attr />"; + } + + /** + * Magic-method override. Called whenever this element is used as a string. + * <code> + * $element = new TXmlElement('tag'); + * echo $element; + * </code> + * or + * <code> + * $element = new TXmlElement('tag'); + * $xml = (string)$element; + * </code> + * @return string string representation of this element + */ + public function __toString() + { + return $this->toString(); + } + + private function xmlEncode($str) + { + return strtr($str,array( + '>'=>'>', + '<'=>'<', + '&'=>'&', + '"'=>'"', + "\r"=>'
', + "\t"=>'	', + "\n"=>'
')); + } +}
\ No newline at end of file diff --git a/framework/Xml/TXmlElementList.php b/framework/Xml/TXmlElementList.php new file mode 100644 index 00000000..301f9d1e --- /dev/null +++ b/framework/Xml/TXmlElementList.php @@ -0,0 +1,81 @@ +<?php +/** + * TXmlElement, TXmlDocument, TXmlElementList class file + * + * @author Qiang Xue <qiang.xue@gmail.com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Xml + */ + +/** + * TXmlElementList class. + * + * TXmlElementList represents a collection of {@link TXmlElement}. + * You may manipulate the collection with the operations defined in {@link TList}. + * + * @author Qiang Xue <qiang.xue@gmail.com> + * @package System.Xml + * @since 3.0 + */ +class TXmlElementList extends TList +{ + /** + * @var TXmlElement owner of this list + */ + private $_o; + + /** + * Constructor. + * @param TXmlElement owner of this list + */ + public function __construct(TXmlElement $owner) + { + $this->_o=$owner; + } + + /** + * @return TXmlElement owner of this list + */ + protected function getOwner() + { + return $this->_o; + } + + /** + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added TXmlElement object. + * @param integer the specified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TXmlElement object. + */ + public function insertAt($index,$item) + { + if($item instanceof TXmlElement) + { + parent::insertAt($index,$item); + if($item->getParent()!==null) + $item->getParent()->getElements()->remove($item); + $item->setParent($this->_o); + } + else + throw new TInvalidDataTypeException('xmlelementlist_xmlelement_required'); + } + + /** + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a TXmlElement object. + * @param integer the index of the item to be removed. + * @return mixed the removed item. + */ + public function removeAt($index) + { + $item=parent::removeAt($index); + if($item instanceof TXmlElement) + $item->setParent(null); + return $item; + } +}
\ No newline at end of file |