News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1186257534:v:use_geshi-1:v:type-php

Started by Guest, Aug 04, 2007, 07:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Guest

<?php
setup_dtd();

function setup_dtd()
{
   // Setup element attributes.
   $elements = array(
      'package-info' => array(
         'attributes' => array(
            'xmlns' => array(
               'CDATA' => true,
               'default' => 'http://www.simplemachines.org/xml/package-info',
            ),
            'xmlns:smf' => array(
               'CDATA' => true,
               'default' => 'http://www.simplemachines.org/',
            ),
         ),
      ),
      'id' => array('PCDATA' => true,),
      'name' => array('PCDATA' => true,),
      'type' => array('PCDATA' => true,),
      'version' => array('PCDATA' => true,),
      'install' => array(
         'attributes' => array(
            'for' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
         ),
      ),
      'uninstall' => array(),
      'upgrade' => array(),
      'readme' => array(
         'attributes' => array(
            'type' => array(
               'vals' => array('inline', 'file'),
               'default' => 'file',
            ),
            'parsebbc' => array(
               'vals' => array('true', 'false'),
               'default' => 'false',
            ),
         ),
      ),
      'code' => array(
         'PCDATA' => true,
         'attributes' => array(
            'type' => array(
               'vals' => array('inline', 'file'),
               'default' => 'file',
            ),
         ),
      ),
      'modification' => array(
         'PCDATA' => true,
         'attributes' => array(
            'type' => array(
               'vals' => array('inline', 'file'),
               'default' => 'file',
            ),
            'reverse' => array(
               'vals' => array('true', 'false'),
               'default' => 'false',
            ),
            'format' => array(
               'vals' => array('xml', 'boardmod'),
               'default' => 'xml',
            ),
         ),
      ),
      'create-dir' => array(
         'PCDATA' => true,
         'attributes' => array(
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'create-file' => array(
         'PCDATA' => true,
         'attributes' => array(
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'require-dir' => array(
         'PCDATA' => true,
         'attributes' => array(
            'from' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'require-file' => array(
         'PCDATA' => true,
         'attributes' => array(
            'from' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'move-dir' => array(
         'PCDATA' => true,
         'attributes' => array(
            'from' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'move-file' => array(
         'PCDATA' => true,
         'attributes' => array(
            'from' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'destination' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'remove-dir' => array(
         'PCDATA' => true,
         'attributes' => array(
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'remove-file' => array(
         'PCDATA' => true,
         'attributes' => array(
            'name' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
      'redirect' => array(
         'PCDATA' => true,
         'attributes' => array(
            'url' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
            'type' => array(
               'CDATA' => true,
               'IMPLIED' => true,
            ),
            'timeout' => array(
               'CDATA' => true,
               'REQUIRED' => true,
            ),
         ),
      ),
   );

   // These are all the same.
   $elements['uninstall'] = $elements['upgrade'] = $elements['install'];

   // Setup hierarchy.
   $dtd = array (
      'package-info' => array(
         'required' => true,
         'children' => array(
            'id' => $elements['id'],
            'name' => $elements['name'],
            'type' => $elements['type'],
            'version' => $elements['version'],
            'install' => $elements['install'],
            'uninstall' => $elements['uninstall'],
            'upgrade' => $elements['upgrade']
         ),
      ),
   );

   // Save some space.
   $pack_children = &$dtd['package-info']['children'];

   // The following are required to be in <package-info>.
   $pack_children['id']['required'] = $pack_children['name']['required'] = $pack_children['version']['required'] = $pack_children['install']['required'] = true;

   // These are only allowed to have one occurence in <package-info>.
   $pack_children['id']['multiple'] = $pack_children['name']['multiple'] = $pack_children['version']['multiple'] = $pack_children['readme']['multiple'] = false;

   // Install, Uninstall, and Upgrade all have the same children so just define them together.
   $pack_children['install']['children'] = array(
      'readme' => $elements['readme'],
      'code' => $elements['code'],
      'modification' => $elements['modification'],
      'create-dir' => $elements['create-dir'],
      'create-file' => $elements['create-file'],
      'require-dir' => $elements['require-dir'],
      'require-file' => $elements['require-file'],
      'move-dir' => $elements['move-dir'],
      'move-file' => $elements['move-file'],
      'remove-dir' => $elements['remove-dir'],
      'remove-file' => $elements['remove-file']
   );

   // Readme can have only one, but nothing is required in an install.
   $pack_children['install']['children']['readme']['multiple'] = false;

   // Install, uninstall, and upgrade are the same.
   $pack_children['uninstall']['children'] = $pack_children['upgrade']['children'] = $pack_children['install']['children'];

   print_r($dtd);
}
?>

Guest

parse_package('package-info.xml');
function parse_package($xml_loc)
{
   $package = new DOMDocument;
   $package->Load($xml_loc);
   /*if ($package->validate()) {
       echo "This document is valid!\n";
   }*/
   $elements = $package->getElementsByTagName('*');
   foreach ($elements as $element)
      echo $element->tagname;

   print_r($package);
}

//////////////////////////////////////////////////////////////
<?xml version="1.0"?>
<!DOCTYPE package-info SYSTEM "http://www.simplemachines.org/xml/package-info">
<package-info xmlns="http://www.simplemachines.org/xml/package-info" xmlns:smf="http://www.simplemachines.org/">
   <name>Referrals Mod</name>
   <id>karlbenson:referrals</id>
   <type>modification</type>
   <version>1.1</version>

   <install>
      <readme type="file">readme.txt</readme>
      <modification>install.xml</modification>
      <code>install.php</code>
   </install>
   
   <uninstall>
      <modification reverse="true">install.xml</modification>
   </uninstall>
</package-info>