Commit cdf9a41d220525faa6d8cf3bbfff2d5e50248430
1 parent
422a48b4
PT: 865156 - Amended Functionality on AtomPub Server Code - Implemented XML2Arra…
…y parser for service modules.
Showing
5 changed files
with
146 additions
and
17 deletions
webservice/atompub/index.php
| ... | ... | @@ -68,6 +68,7 @@ define('KT_ATOM_LIB_FOLDER','../classes/atompub/'); |
| 68 | 68 | * Includes |
| 69 | 69 | */ |
| 70 | 70 | include_once('../../ktapi/ktapi.inc.php'); |
| 71 | +include_once(KT_ATOM_LIB_FOLDER.'XMLns2array.inc.php'); | |
| 71 | 72 | include_once(KT_ATOM_LIB_FOLDER.'KT_atom_server.inc.php'); |
| 72 | 73 | include_once('demodms/KT_atom_service_helper.inc.php'); //Containing helper bridge functions to KtAPI |
| 73 | 74 | include_once(KT_ATOM_LIB_FOLDER.'KT_atom_baseDoc.inc.php'); //Containing the parent class allowing easy XML manipulation |
| ... | ... | @@ -91,6 +92,8 @@ $APP=new KT_atom_server(); |
| 91 | 92 | * ServiceClass :This is the class name of the class to be instantiated when this service is accessed |
| 92 | 93 | * Title :This is the title given to the service/collection in the servicedocument |
| 93 | 94 | */ |
| 95 | +$APP->addWorkspaceTag('dms','atom:title','Standard DMS'); | |
| 96 | + | |
| 94 | 97 | $APP->registerService('DMS','fulltree','KT_atom_service_fulltree','Full Document Tree'); |
| 95 | 98 | $APP->registerService('DMS','folder','KT_atom_service_folder','Folder Detail'); |
| 96 | 99 | $APP->registerService('DMS','document','KT_atom_service_document','Document Detail'); |
| ... | ... | @@ -102,5 +105,6 @@ $APP->execute(); |
| 102 | 105 | |
| 103 | 106 | //Render the resulting feed response |
| 104 | 107 | $APP->render(); |
| 108 | +//print_r($APP); | |
| 105 | 109 | |
| 106 | 110 | ?> |
| 107 | 111 | \ No newline at end of file | ... | ... |
webservice/classes/atompub/KT_atom_server.inc.php
| 1 | 1 | <?php |
| 2 | 2 | class KT_atom_server{ |
| 3 | 3 | protected $services=array(); |
| 4 | + protected $workspaceDetail=array(); | |
| 4 | 5 | protected $errors=array(); |
| 5 | 6 | protected $output=''; |
| 6 | 7 | protected $queryArray=array(); |
| ... | ... | @@ -12,6 +13,10 @@ class KT_atom_server{ |
| 12 | 13 | public function __construct(){ |
| 13 | 14 | } |
| 14 | 15 | |
| 16 | + /** | |
| 17 | + * Run the server switchboard - find the correct service class to instantiate, execute & render that class with the passed parameteres | |
| 18 | + * | |
| 19 | + */ | |
| 15 | 20 | public function execute(){ |
| 16 | 21 | $reqMethod=trim(strtoupper($_SERVER['REQUEST_METHOD'])); |
| 17 | 22 | $queryArray=split('/',trim($_SERVER['QUERY_STRING'],'/')); |
| ... | ... | @@ -43,10 +48,12 @@ class KT_atom_server{ |
| 43 | 48 | $serviceObject->setStatus(KT_atom_service::STATUS_NOT_FOUND); |
| 44 | 49 | $this->output=$serviceObject->render(); |
| 45 | 50 | } |
| 51 | + $this->serviceObject=$serviceObject; | |
| 46 | 52 | } |
| 47 | 53 | |
| 48 | - public function registerService($workspace=NULL,$serviceName=NULL,$serviceClass=NULL,$title=NULL){ | |
| 49 | - $workspace=strtolower(trim($workspace)); | |
| 54 | + | |
| 55 | + public function registerService($workspaceCode=NULL,$serviceName=NULL,$serviceClass=NULL,$title=NULL){ | |
| 56 | + $workspaceCode=strtolower(trim($workspaceCode)); | |
| 50 | 57 | $serviceName=strtolower(trim($serviceName)); |
| 51 | 58 | |
| 52 | 59 | $serviceRecord=array( |
| ... | ... | @@ -55,7 +62,12 @@ class KT_atom_server{ |
| 55 | 62 | 'title' =>$title |
| 56 | 63 | ); |
| 57 | 64 | |
| 58 | - $this->services[$workspace][$serviceName]=$serviceRecord; | |
| 65 | + $this->services[$workspaceCode][$serviceName]=$serviceRecord; | |
| 66 | + } | |
| 67 | + | |
| 68 | + public function addWorkspaceTag($workspaceCode=NULL,$TagName=NULL,$tagValue=NULL){ | |
| 69 | + if(!isset($this->workspaceDetail[$workspaceCode]))$this->workspaceDetail[$workspaceCode]=array(); | |
| 70 | + $this->workspaceDetail[$workspaceCode][$TagName]=$tagValue; | |
| 59 | 71 | } |
| 60 | 72 | |
| 61 | 73 | public function getRegisteredService($workspace,$serviceName=NULL){ |
| ... | ... | @@ -69,7 +81,18 @@ class KT_atom_server{ |
| 69 | 81 | |
| 70 | 82 | foreach($this->services as $workspace=>$collection){ |
| 71 | 83 | //Creating the Default Workspace for use with standard atomPub Clients |
| 72 | - $ws=$service->newWorkspace($workspace); | |
| 84 | + $ws=$service->newWorkspace(); | |
| 85 | + | |
| 86 | + $hadDetail=false; | |
| 87 | + if(isset($this->workspaceDetail[$workspace]))if(is_array($this->workspaceDetail[$workspace])){ | |
| 88 | + foreach ($this->workspaceDetail[$workspace] as $wsTag=>$wsValue){ | |
| 89 | + $ws->appendChild($service->newElement($wsTag,$wsValue)); | |
| 90 | + $hadDetail=true; | |
| 91 | + } | |
| 92 | + } | |
| 93 | + if(!$hadDetail){ | |
| 94 | + $ws->appendChild($service->newElement('atom:title',$workspace)); | |
| 95 | + } | |
| 73 | 96 | |
| 74 | 97 | foreach($collection as $serviceName=>$serviceInstance){ |
| 75 | 98 | $col=$service->newCollection(KT_APP_BASE_URI.$workspace.'/'.$serviceName.'/',$serviceInstance['title'],$ws); | ... | ... |
webservice/classes/atompub/KT_atom_service.inc.php
| ... | ... | @@ -60,7 +60,11 @@ class KT_atom_service{ |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | protected function xml2array($xml){ |
| 63 | - $array=json_decode(json_encode(@simplexml_load_string($xml)),true); //TODO - XML2ARRAY Translation | |
| 63 | + if(class_exists('')){ | |
| 64 | + $array=XMLns2array::parse($xml); | |
| 65 | + }else{ | |
| 66 | + $array=json_decode(json_encode(@simplexml_load_string($xml)),true); | |
| 67 | + } | |
| 64 | 68 | return $array; |
| 65 | 69 | } |
| 66 | 70 | ... | ... |
webservice/classes/atompub/KT_atom_serviceDoc.inc.php
| ... | ... | @@ -32,7 +32,7 @@ |
| 32 | 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices |
| 33 | 33 | * must display the words "Powered by KnowledgeTree" and retain the original |
| 34 | 34 | * copyright notice. |
| 35 | - * Contributor( s): | |
| 35 | + * Contributor( s): | |
| 36 | 36 | * Mark Holtzhausen <mark@knowledgetree.com> |
| 37 | 37 | * |
| 38 | 38 | */ |
| ... | ... | @@ -45,17 +45,17 @@ include_once('KT_atom_baseDoc.inc.php'); |
| 45 | 45 | |
| 46 | 46 | |
| 47 | 47 | class KT_atom_serviceDoc extends KT_atom_baseDoc { |
| 48 | - | |
| 48 | + | |
| 49 | 49 | protected $baseURI=NULL; |
| 50 | 50 | protected $service=NULL; |
| 51 | 51 | |
| 52 | - | |
| 52 | + | |
| 53 | 53 | public function __construct($baseURI=NULL){ |
| 54 | 54 | parent::__construct(); |
| 55 | 55 | $this->constructServiceDocumentHeaders(); |
| 56 | 56 | $this->baseURI=$baseURI; |
| 57 | 57 | } |
| 58 | - | |
| 58 | + | |
| 59 | 59 | protected function constructServiceDocumentHeaders(){ |
| 60 | 60 | $service=$this->newElement('service'); |
| 61 | 61 | $service->appendChild($this->newAttr('xmlns','http://www.w3.org/2007/app')); |
| ... | ... | @@ -63,14 +63,14 @@ class KT_atom_serviceDoc extends KT_atom_baseDoc { |
| 63 | 63 | $this->service=&$service; |
| 64 | 64 | $this->DOM->appendChild($this->service); |
| 65 | 65 | } |
| 66 | - | |
| 66 | + | |
| 67 | 67 | public function &newWorkspace($title=NULL){ |
| 68 | 68 | $ws=$this->newElement('workspace'); |
| 69 | - $ws->appendChild($this->newElement('atom:title',$title)); | |
| 69 | + if($title)$ws->appendChild($this->newElement('atom:title',$title)); | |
| 70 | 70 | $this->service->appendChild($ws); |
| 71 | - return $ws; | |
| 71 | + return $ws; | |
| 72 | 72 | } |
| 73 | - | |
| 73 | + | |
| 74 | 74 | public function &newCollection($url=NULL,$title=NULL,&$ws=NULL){ |
| 75 | 75 | $collection=$this->newElement('collection'); |
| 76 | 76 | $collection->appendChild($this->newAttr('href',$url)); |
| ... | ... | @@ -78,7 +78,7 @@ class KT_atom_serviceDoc extends KT_atom_baseDoc { |
| 78 | 78 | if(isset($ws))$ws->appendChild($collection); |
| 79 | 79 | return $collection; |
| 80 | 80 | } |
| 81 | - | |
| 81 | + | |
| 82 | 82 | public function &newAccept($docType=NULL,&$collection=NULL){ |
| 83 | 83 | if($docType){ |
| 84 | 84 | $accept=$this->newElement('accept',$docType); |
| ... | ... | @@ -88,12 +88,12 @@ class KT_atom_serviceDoc extends KT_atom_baseDoc { |
| 88 | 88 | if($collection)$collection->appendChild($accept); |
| 89 | 89 | return $accept; |
| 90 | 90 | } |
| 91 | - | |
| 92 | - | |
| 91 | + | |
| 92 | + | |
| 93 | 93 | public function getAPPdoc(){ |
| 94 | 94 | return $this->formatXmlString(trim($this->DOM->saveXML())); |
| 95 | 95 | } |
| 96 | - | |
| 96 | + | |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | ... | ... |
webservice/classes/atompub/XMLns2array.inc.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +class XMLns2array{ | |
| 4 | + | |
| 5 | + private static $namespaces=null; | |
| 6 | + | |
| 7 | + /** | |
| 8 | + * Parse an XML document into an array. Supports namespaces. Uses SimpleXML | |
| 9 | + * | |
| 10 | + * @param String/SimpleXMLElement $xml | |
| 11 | + * @return Array | |
| 12 | + */ | |
| 13 | + public static function parse($xml){ | |
| 14 | + $xml=(get_class($xml)=='SimpleXMLElement')?$xml:@simplexml_load_string($xml); | |
| 15 | + if(get_class($xml)!='SimpleXMLElement') die('The string passed is not a valid XML string or an SimpleXMLElement'); | |
| 16 | + self::$namespaces=$xml->getNamespaces(true); | |
| 17 | + return self::parsetag($xml); | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Parse a single tag recursively calling self to parse children. | |
| 22 | + * | |
| 23 | + * @param SimpleXMLElement $xml The SimpleXMLElement to convert to an array | |
| 24 | + * @return Array | |
| 25 | + */ | |
| 26 | + private static function parsetag($xml){ | |
| 27 | + $array=array(); | |
| 28 | + $array['@attributes']=self::getAttributes($xml); | |
| 29 | + $array['@children']=array(); | |
| 30 | + if(self::hasChildren($xml)){ | |
| 31 | + $children=self::getChildren($xml); | |
| 32 | + $tmpChildren=array(); | |
| 33 | + foreach($children as $fullChildName=>$childCollection){ | |
| 34 | + $childName=split(':',$fullChildName); | |
| 35 | + $fullChildName=trim($fullChildName,' :'); | |
| 36 | + foreach($childCollection as $child){ | |
| 37 | + $childParsed=self::parsetag($child); | |
| 38 | + if(!isset($tmpChildren[$fullChildName]))$tmpChildren[$fullChildName]=array(); | |
| 39 | + $tmpChildren[$fullChildName][]=$childParsed; | |
| 40 | + } | |
| 41 | + $array['@children']=array_merge($array['@children'],$tmpChildren); | |
| 42 | + } | |
| 43 | + }else{ | |
| 44 | + $array['@value']=(string)$xml; | |
| 45 | + } | |
| 46 | + return $array; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Tests whether a node has children | |
| 51 | + * | |
| 52 | + * @param SimpleXMLElement $xml The node to test | |
| 53 | + * @return Array | |
| 54 | + */ | |
| 55 | + private static function hasChildren($xml){ | |
| 56 | + return count(self::getChildren($xml))>0; | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Gets the attributes of a node | |
| 61 | + * | |
| 62 | + * @param SimpleXMLElement $xml The node to process | |
| 63 | + * @return Array | |
| 64 | + */ | |
| 65 | + private static function getAttributes($xml){ | |
| 66 | + $attr=array(); | |
| 67 | + foreach(self::$namespaces as $namespace=>$uri){ | |
| 68 | + $nsAttrs=(array)$xml->attributes($uri); | |
| 69 | + $nsAttrs=isset($nsAttrs['@attributes'])?$nsAttrs['@attributes']:array(); | |
| 70 | + foreach($nsAttrs as $nsAttr=>$nsAttrVal){ | |
| 71 | + $attr[$namespace.':'.$nsAttr]=$nsAttrVal; | |
| 72 | + } | |
| 73 | + } | |
| 74 | + return $attr; | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Returns the children of a node | |
| 79 | + * | |
| 80 | + * @param SimpleXMLElement $xml The node to process | |
| 81 | + * @return unknown | |
| 82 | + */ | |
| 83 | + private static function getChildren($xml){ | |
| 84 | + $children=array(); | |
| 85 | + foreach(self::$namespaces as $namespace=>$uri){ | |
| 86 | + $nsChildren=$xml->children($uri); | |
| 87 | + foreach($nsChildren as $nsChild){ | |
| 88 | + $childRealName=$namespace.':'.$nsChild->getName(); | |
| 89 | + if(!isset($children[$childRealName]))$children[$childRealName]=array(); | |
| 90 | + if(!is_array($children[$childRealName]))$children[$childRealName]=array(); | |
| 91 | + $children[$childRealName][]=$nsChild; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + return $children; | |
| 95 | + } | |
| 96 | + | |
| 97 | +} | |
| 98 | +?> | |
| 0 | 99 | \ No newline at end of file | ... | ... |