<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
  <HEAD>
    <TITLE>Arbre XML d'un fichier project</TITLE>
    <!-- Created by: D.K.S., 02-Jun-1999 -->
<!-- Changed by: D.K.S., 14-Jun-1999 -->

<!-- TREE Display of project files
     This code would work for any XML
     Author: Daniel.Schneider@tecfa.unige.ch
             You can freely use and distribute and modify this program
         but please let me know if you come up with something much better 
 -->

  </HEAD>
  <BODY>
    <H1>Arbre XML d'un fichier project <A HREF="http://tecfa.unige.ch/tecfa/teaching/staf18/staf18-overview.html">STAF-18</A> (EVA) déposée</H1>

Ce formulaire s'addresse aux gens qui veulent programmer PHP/XML ou
aux curieux qui veulent regarder la structure de leur fichier XML.


<?php

// Parameters for files, directories and URLs we use

$project_dir "/web/staf/staf-e/staf18/proj/";
$xml_path "http://tecfa2.unige.ch/staf/staf-e/staf18/proj/";
$xml_source_path "http://tecfa2.unige.ch/staf/staf-e/staf18/proj/";
$xml_servlet "http://tecfa2.unige.ch:8080/servlet/XSLatorServlet";
$style_sheet "http://tecfa2.unige.ch/staf/staf-e/staf18/project.xsl";


// other globals (commented, since no need to init)
# $stack = array();
# $astack = array();
# $pointer = 0;
# $apointer = 0;


// ---------------- Utils
// 'Pops' off and returns the last element of $array. By 
// doing so, the size of $array is reduced by 1.
// Adapted from Harding Shay (mekla@geocities.com)

function pop(&$array) {
  if (!
is_array($array)){ return; }
  
$variable $array[count($array)-1];
  unset(
$array[count($array)-1]);
  return 
$variable;
}


function 
push(&$array,$variable) {
  
$array[count($array)] = $variable;
  return 
count($array);
}


// ---------------------------------------------------------------------------
// ------------------------------- XML Parsing ------------------------------


// ------------- printing functions

function attr_printer ($attrs) {
  if (
$attrs) {
    
// echo "ATTRIBUTES: ";
    
while (list ($attr$content) = each ($attrs)){
      echo 
"$attr = <strong>$content</strong>\n";
    }
    echo 
"<br>";
  }
}

function 
stack_printer ($el) {
  echo 
"$el ";
}
function 
show_stack ($indent) {
  global 
$stack$astack;
  global 
$pointer$apointer$current;
  global 
$xml_tree$xml_stack$xml_data$xml_attrs$html_tree

  
echo "$indent\n";
  
array_walk ($stack'stack_printer');  
  
// echo "Current attrs : ";
  // array_walk ($astack[$apointer], 'stack_printer');  
  // echo "<br>\n";
}


// ----------------------- The 3 functions for the PHP XML Parser
// xml_parse fait appel à ces fonctions

// grab current element and push it onto the stack
// push its attributs on the astack

function startElement($parser$name$attrs) {
  global 
$stack$astack;
  global 
$pointer$apointer$current;
  global 
$xml_tree$xml_stack$xml_data$xml_attrs$html_tree;
  
  
$pointer push ($stack$name) -;
  
$apointer push ($astack$attrs) -;
  
// the current element
  
$current $name;
  if (
$html_tree) { echo "\n<li><i>$current</i><ol>\n"; };
  if (
$xml_tree) { echo "\n<li>&lt;$current&gt;<ol>\n"; };
  if (
$xml_stack) { show_stack("\nstack=\n"); };
  if (
$xml_attrs) { attr_printer($attrs); } ;
}

// pop off the element from the stack
// pop off the attributes from the astack

function endElement($parser$name) {
  global 
$stack$astack;
  global 
$pointer$apointer$current;
  global 
$xml_tree$xml_stack$xml_data$xml_attrs$html_tree;

  if (
$html_tree) {echo "\n</ol></li>\n"; };
  if (
$xml_tree) { echo "\n</ol>&lt;/" $stack[$pointer] . "&gt;</li>\n"; };
  if (
$xml_stack) { show_stack("\n<br>stack=\n"); };

  
pop ($stack);
  
pop ($astack);
  
$pointer $pointer 1;
  
$apointer $apointer 1;
  
$current FALSE;

}

function 
characterData($parser$data) {
  global 
$stack$astack;
  global 
$pointer$apointer$current;
  global 
$xml_tree$xml_stack$xml_data$xml_attrs$html_tree;

  if (
$xml_stack) { show_stack("<br>DATA current=" $current " stack="); };

  if (
$xml_data) {
    if (
trim($data) != "") {
      if (!
$xml_tree) { echo "<br>"; } ;
      echo 
"<strong>\"$data\"</strong>\n";
    }
  }
}


  
// -------------------- XML Parser 


function parse_xml($file) {

  
// create the XML Parser, a new one for each filep
  
$xml_parser xml_parser_create("ISO-8859-1");
  
xml_parser_set_option($xml_parserXML_OPTION_CASE_FOLDINGfalse);
  
xml_set_element_handler($xml_parser,  "startElement",  "endElement");
  
xml_set_character_data_handler($xml_parser,  "characterData");
  
  
// echo "<p>DEBUG: file = $file";
  
if (!($fp fopen($file,  "r"))) {
    die( 
"could not open XML input");
  }
  else {
    
// init per-file global vars
    
$wp_count 0;
  }
  
  while (
$data fread($fp50000)) {
    if (!
xml_parse($xml_parser$datafeof($fp))) {
      die(
sprintf"XML error: %s at line %d",
           
xml_error_string(xml_get_error_code($xml_parser)),
           
xml_get_current_line_number($xml_parser)));
    }
  }
  
// close file and free parser
  
fclose($fp);
  
xml_parser_free($xml_parser);
 
}

// ------------------------------ File ops



function make_file_option($file) {
  echo 
"<option value = \"" $file "\"> Fichier " $file;
}

function 
make_file_menu ($files) {
  
// echo "DEBUG: files = $files <p>\n";
  
echo "<select name=\"projectfile\">";
  
array_walk ($files'make_file_option');
  echo 
"</select>";
}




// ----------------------------------------------------------------------
// ----------------------- Main routine

// -------------------------- Get the list of files

$handle=opendir($project_dir);
// read the files into an array
$files = array();
while (
$file readdir($handle)) {
  
$pieces explode("."$file);
  if (
$pieces[1] == "xml") {
    
$files[] = $file;
  }
}
closedir($handle);

// sort them in alphabetical order
sort($files);
reset($files);


// ------------------------------ Make a HTML form

?>

<FORM METHOD="Get" ACTION= <? echo "\""$PHP_SELF "\""?> >
  <? make_file_menu($files); ?>

  <INPUT TYPE="submit" VALUE="Submit" name="process2"> <br>

  <input type="radio" name="display" value="xml_tree" checked> XML TREE
  <input type="radio" name="display" value="html_tree"> HTML TREE
  <input type="radio" name="display" value="no_tree"> No tree
 <br>

  <input type="checkbox" name="xml_stack"> XML Stack TREE (long)
  <input type="checkbox" name="xml_data" checked> Data (element contents)
  <input type="checkbox" name="xml_attrs" checked> Attributes

</FORM>
<hr>
 
<?php
  
echo  "[ <a href = \""$PHP_SELF ."\">Restart Over</a> ] ";
  echo  
"[ <a href = \""$PHP_SELF ."s\">Source Code</a> ] <hr>";



// ------------------------------  Here we deal with form requests

if ($projectfile
  
// to call like ?projectfile=file.xml
{
  
//  ----- PARSE XML

  // --- fix options

  
if     ($display == "xml_tree") { $xml_tree "on";}
  elseif (
$display == "html_tree") { $html_tree "on";}

  
// XML stack needs xml_tree
  //if ($xml_stack) $xml_tree = "on";


  // An URL to contents

  
$xml_url $xml_path $projectfile;
  
$url $xml_servlet "?xml=" $xml_url "&xsl=" $style_sheet;
  echo 
"<p>Tree display for <A HREF=\"$url\">$projectfile</A>: \n";

  
// ----- DOIT 
  // calls xml_parse for the file, which in turn calls
  // - startElement when it finds an open tag
  // - endElement when it finds a close tag
  // - characterData when it finds CDATA (things inside a tag including blanks)

  
echo "<ul>";
  
parse_xml($project_dir.$projectfile);
  echo 
"</ul>";
}


?>



<hr>
<A HREF="http://tecfa.unige.ch/tecfa/teaching/staf18/pm-eva.html">Gestion de projets</A> | 
<A HREF="http://tecfa.unige.ch/tecfa/teaching/staf18/staf18-overview.html">Retour à STAF-18</A>
<hr>
    <ADDRESS>
      <A NAME="Signature"
     HREF="http://tecfa.unige.ch/tecfa-people/schneider.html">D.K.S.</A>
    </ADDRESS>
  </BODY>
</HTML>