Console_CommandLine
[ class tree: Console_CommandLine ] [ index: Console_CommandLine ] [ all elements ]

Source for file XmlParser.php

Documentation is available at XmlParser.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Console_CommandLine package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Console
  15.  * @package   Console_CommandLine
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2007 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id: XmlParser.php,v 1.9 2008/12/22 15:08:11 izi Exp $
  20.  * @link      http://pear.php.net/package/Console_CommandLine
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Required file
  27.  */
  28. require_once 'Console/CommandLine.php';
  29.  
  30. /**
  31.  * Parser for command line xml definitions.
  32.  *
  33.  * @category  Console
  34.  * @package   Console_CommandLine
  35.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  36.  * @copyright 2007 David JEAN LOUIS
  37.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  38.  * @version   Release: @package_version@
  39.  * @link      http://pear.php.net/package/Console_CommandLine
  40.  * @since     Class available since release 0.1.0
  41.  */
  42. {
  43.     // parse() {{{
  44.  
  45.     /**
  46.      * Parses the given xml definition file and returns a
  47.      * Console_CommandLine instance constructed with the xml data.
  48.      *
  49.      * @param string $xmlfile The xml file to parse
  50.      *
  51.      * @return Console_CommandLine A parser instance
  52.      */
  53.     public static function parse($xmlfile
  54.     {
  55.         if (!is_readable($xmlfile)) {
  56.             Console_CommandLine::triggerError('invalid_xml_file',
  57.                 E_USER_ERRORarray('{$file}' => $xmlfile));
  58.         }
  59.         $doc = new DomDocument();
  60.         $doc->load($xmlfile);
  61.         self::validate($doc);
  62.         $nodes $doc->getElementsByTagName('command');
  63.         $root  $nodes->item(0);
  64.         return self::_parseCommandNode($roottrue);
  65.     }
  66.  
  67.     // }}}
  68.     // parseString() {{{
  69.  
  70.     /**
  71.      * Parses the given xml definition string and returns a
  72.      * Console_CommandLine instance constructed with the xml data.
  73.      *
  74.      * @param string $xmlstr The xml string to parse
  75.      *
  76.      * @return Console_CommandLine A parser instance
  77.      */
  78.     public static function parseString($xmlstr
  79.     {
  80.         $doc = new DomDocument();
  81.         $doc->loadXml($xmlstr);
  82.         self::validate($doc);
  83.         $nodes $doc->getElementsByTagName('command');
  84.         $root  $nodes->item(0);
  85.         return self::_parseCommandNode($roottrue);
  86.     }
  87.  
  88.     // }}}
  89.     // validate() {{{
  90.  
  91.     /**
  92.      * Validates the xml definition using Relax NG.
  93.      *
  94.      * @param DomDocument $doc The document to validate
  95.      *
  96.      * @return boolean Whether the xml data is valid or not.
  97.      * @throws Console_CommandLine_Exception
  98.      * @todo use exceptions
  99.      */
  100.     public static function validate($doc
  101.     {
  102.         if (is_dir('@data_dir@' . DIRECTORY_SEPARATOR . 'Console_CommandLine')) {
  103.             $rngfile '@data_dir@' . DIRECTORY_SEPARATOR
  104.                 . 'Console_CommandLine' . DIRECTORY_SEPARATOR . 'data' 
  105.                 . DIRECTORY_SEPARATOR . 'xmlschema.rng';
  106.         else {
  107.             $rngfile dirname(__FILE__. DIRECTORY_SEPARATOR . '..' 
  108.                 . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR 
  109.                 . 'xmlschema.rng';
  110.         }
  111.         if (!is_readable($rngfile)) {
  112.             Console_CommandLine::triggerError('invalid_xml_file',
  113.                 E_USER_ERRORarray('{$file}' => $rngfile));
  114.         }
  115.         return $doc->relaxNGValidate($rngfile);
  116.     }
  117.  
  118.     // }}}
  119.     // _parseCommandNode() {{{
  120.  
  121.     /**
  122.      * Parses the root command node or a command node and returns the
  123.      * constructed Console_CommandLine or Console_CommandLine_Command instance.
  124.      *
  125.      * @param DomDocumentNode $node       The node to parse
  126.      * @param bool            $isRootNode Whether it is a root node or not
  127.      *
  128.      * @return mixed Console_CommandLine or Console_CommandLine_Command
  129.      */
  130.     private static function _parseCommandNode($node$isRootNode = false
  131.     {
  132.         if ($isRootNode
  133.             $obj = new Console_CommandLine();
  134.         else {
  135.             include_once 'Console/CommandLine/Command.php';
  136.             $obj = new Console_CommandLine_Command();
  137.         }
  138.         foreach ($node->childNodes as $cNode{
  139.             $cNodeName $cNode->nodeName;
  140.             switch ($cNodeName{
  141.             case 'name':
  142.             case 'description':
  143.             case 'version':
  144.                 $obj->$cNodeName trim($cNode->nodeValue);
  145.                 break;
  146.             case 'add_help_option':
  147.             case 'add_version_option':
  148.             case 'force_posix':
  149.                 $obj->$cNodeName = self::_bool(trim($cNode->nodeValue));
  150.                 break;
  151.             case 'option':
  152.                 $obj->addOption(self::_parseOptionNode($cNode));
  153.                 break;
  154.             case 'argument':
  155.                 $obj->addArgument(self::_parseArgumentNode($cNode));
  156.                 break;
  157.             case 'command':
  158.                 $obj->addCommand(self::_parseCommandNode($cNode));
  159.                 break;
  160.             default:
  161.                 break;
  162.             }
  163.         }
  164.         return $obj;
  165.     }
  166.  
  167.     // }}}
  168.     // _parseOptionNode() {{{
  169.  
  170.     /**
  171.      * Parses an option node and returns the constructed
  172.      * Console_CommandLine_Option instance.
  173.      *
  174.      * @param DomDocumentNode $node The node to parse
  175.      *
  176.      * @return Console_CommandLine_Option The built option
  177.      */
  178.     private static function _parseOptionNode($node
  179.     {
  180.         include_once 'Console/CommandLine/Option.php';
  181.         $obj = new Console_CommandLine_Option($node->getAttribute('name'));
  182.         foreach ($node->childNodes as $cNode{
  183.             $cNodeName $cNode->nodeName;
  184.             if ($cNodeName == 'choices'{
  185.                 foreach ($cNode->childNodes as $subChildNode{
  186.                     if ($subChildNode->nodeName == 'choice'{
  187.                         $obj->choices[trim($subChildNode->nodeValue);
  188.                     }
  189.                 }
  190.             elseif (property_exists($obj$cNodeName)) {
  191.                 $obj->$cNodeName trim($cNode->nodeValue);
  192.             }
  193.         }
  194.         if ($obj->action == 'Password'{
  195.             $obj->argument_optional = true;
  196.         }
  197.         return $obj;
  198.     }
  199.  
  200.     // }}}
  201.     // _parseArgumentNode() {{{
  202.  
  203.     /**
  204.      * Parses an argument node and returns the constructed
  205.      * Console_CommandLine_Argument instance.
  206.      *
  207.      * @param DomDocumentNode $node The node to parse
  208.      *
  209.      * @return Console_CommandLine_Argument The built argument
  210.      */
  211.     private static function _parseArgumentNode($node
  212.     {
  213.         include_once 'Console/CommandLine/Argument.php';
  214.         $obj = new Console_CommandLine_Argument($node->getAttribute('name'));
  215.         foreach ($node->childNodes as $cNode{
  216.             $cNodeName $cNode->nodeName;
  217.             switch ($cNodeName{
  218.             case 'description':
  219.             case 'help_name':
  220.             case 'default':
  221.                 $obj->$cNodeName trim($cNode->nodeValue);
  222.                 break;
  223.             case 'multiple':
  224.                 $obj->multiple = self::_bool(trim($cNode->nodeValue));
  225.                 break;
  226.             default:
  227.                 break;
  228.             }
  229.         }
  230.         return $obj;
  231.     }
  232.  
  233.     // }}}
  234.     // _bool() {{{
  235.  
  236.     /**
  237.      * Returns a boolean according to true/false possible strings.
  238.      * 
  239.      * @param string $str The string to process
  240.      *
  241.      * @return boolean 
  242.      */
  243.     private static function _bool($str)
  244.     {
  245.         return in_array((string)$strarray('true''1''on''yes'));
  246.     }
  247.  
  248.     // }}}
  249. }

Documentation generated on Mon, 11 Mar 2019 15:28:02 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.