Login   Register  
PHP Classes
elePHPant
Icontem

File: Document.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Reiner Entzminger  >  PDOM  >  Document.php  >  Download  
File: Document.php
Role: ???
Content type: text/plain
Description: Parser
Class: PDOM
Author: By
Last change:
Date: 2002-03-01 06:20
Size: 19,490 bytes
 

Contents

Class file image Download
<?
include_once( "Element.php" );
include_once( "Comment.php" );
include_once( "Attribut.php" );

define( "DOCUMENT", "document" );

/*========================================================================================
*  Autor: Reiner Entzminger            Telefon  (0 63 41) 8 43 69
*         Südring 10                   Telefax  (0 63 41) 8 43 69
*         D-76829 Landau               Mobil +49(0)170 7 34 62 72
*
*  http://www.team-entzminger.de       Mail reiner@team-entzminger.de
*=========================================================================================
*  Document Version 1.00.0003          Datum: 28.02.2002
*=========================================================================================
*  Erstellt ein neues XML-Dokument oder liest ein XML-Dokument ein
*=======================================================================================*/

   class Document {
      var $filename           = '';             // Name der XML-Datei

      var $encoding           = '';             // Zeichencodierung
      var $doctype            = array();        // DocType des XML-Dokuments
      var $entity             = array();        // Entity´s

      var $element            = array();        // das Root-Element
      var $comment;                             // Kommentar zur XML-Datei

      var $others             = array();        // sonstiges aus der XML

      /**-----------------------------------------------------------------------
      * Document erstellt ein neues Dokument oder ließt ein Dokument ein
      * @param  Filename (string) - Name der zu lesenden oder erstellenden XML-
      *         Datei
      *-----------------------------------------------------------------------*/
      function Document( $filename ) {
      // $this->pro->Method( "class Document" );

         $this->is_DocType       = false;
         $this->is_Entity        = false;

         $this->element_stack[0] = 'root';
         $this->filename         = $filename;

         if ( file_exists( $filename ) && is_file( $filename ) ) {
            $fHdl                = fopen( $filename, 'r' );
            $this->document      = str_replace( chr(13).chr(10), "", fread( $fHdl, filesize( $filename ) ) );

            fclose( $fHdl );

            $this->Parse();
            $this->setElements();

            $this->Release();
         }  // if ( file_exists( $filename ) && is_file( $filename ) )

      // $this->pro->Returns( "class Document" );
      }  // function Document()


      /**-----------------------------------------------------------------------
      * setFile initiiert das Objekt neu und ließt ein neues Dokument ein.
      * @param  Filename (string) - Name der zu lesenden oder erstellenden XML-
      *         Datei
      *-----------------------------------------------------------------------*/
      function setFile( $filename ) {
      // $this->pro->Method( "Document: setFile()" );

         $this->initial();

         $this->is_DocType       = false;
         $this->is_Entity        = false;

         $this->element_stack[0] = 'root';
         $this->filename         = $filename;

         if ( file_exists( $filename ) && is_file( $filename ) ) {
            $fHdl                = fopen( $filename, 'r' );
            $this->document      = str_replace( chr(13).chr(10), "", fread( $fHdl, filesize( $filename ) ) );

            fclose( $fHdl );

            $this->Parse();
            $this->setElements();

            $this->Release();
         }  // if ( file_exists( $filename ) && is_file( $filename ) )

      // $this->pro->Returns( "Document: setFile()" );
      }  // function setFile()


      /**-----------------------------------------------------------------------
      * Parse initialisiert den XML-Parser und parst das (XML-)Dokument
      *-----------------------------------------------------------------------*/
      function Parse() {
      // $this->pro->Method( "Document: parse()" );

         $parser           = xml_parser_create();

         xml_set_object( $parser, &$this );

         xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
         xml_set_default_handler( $parser, "DefaultHandler" );
         xml_set_element_handler( $parser, "StartElement", "EndElement" );
         xml_set_character_data_handler( $parser, "CharacterHandler" );
      // xml_set_unparsed_entity_decl_handler( $parser, "EntityHandler" );
      // xml_set_processing_instruction_handler( $parser, "InstructionHandler" );

         xml_parse( $parser, $this->document, false );

         xml_parser_free( $parser );

      // $this->pro->Returns( "Document: parse()" );
      }  // function Parse()


      /**-----------------------------------------------------------------------
      * setRootElement instanziiert das Objekt Element und setzt das Root-Element
      *-----------------------------------------------------------------------*/
      function setElements() {
      // $this->pro->Method( "Document: setRootElement()" );

         $keys                = array_keys( $this->element );
         $count               = count( $keys ) - 2;
         $idx                 = array();

         for( $i=$count; $i>=0; $i-- ) {
            for( $j=0; $j< count( $this->element[$keys[$i]] ); $j++ ) {
               for( $e=0; $e< count( $this->element[$keys[$i]][$j] ); $e++ ) {

                  if ( isset( $this->names[$this->element[$keys[$i]][$j][$e]->name] ) ) {
                     if ( !isset( $idx[$this->element[$keys[$i]][$j][$e]->name] ) ) {
                        $idx[$this->element[$keys[$i]][$j][$e]->name]   = 0;
                     }  // if ( !isset( $idx[$this->element[$keys[$i]][$j][$e]->name] ) )

                     for( $y=0; $y< count( $this->element[$this->element[$keys[$i]][$j][$e]->name][$idx[$this->element[$keys[$i]][$j][$e]->name]] ); $y++ ) {
                        $this->element[$keys[$i]][$j][$e]->addElement( $this->element[$this->element[$keys[$i]][$j][$e]->name][$idx[$this->element[$keys[$i]][$j][$e]->name]][$y] );
                     }  // for( $y=0; $y< count( $this->element[$this->element[$keys[$i]][$j][$e]->name][$idx[$this->element[$keys[$i]][$j][$e]->name]] ); $y++ )

                     $idx[$this->element[$keys[$i]][$j][$e]->name]++;
                  }  // if ( isset( $this->names[$this->element[$keys[$i]][$j][$e]->name] ) )

               }  // for( $e=0; $e< count( $this->element[$keys[$i]][$j] ); $e++ )
            }  // for( $j=0; $j< count( $this->element[$keys[$i]] ); $j++ )
         }  // for( $i=$count; $i>=0; $i-- )

         $this->element       = $this->element['root'][0][0];

      // $this->pro->Returns( "Document: setRootElement()" );
      }  // function setRootElement()


      /**-----------------------------------------------------------------------
      * StartElement wertet das Start-Element sowie dessen Parameter aus
      *-----------------------------------------------------------------------*/
      function StartElement( $parser, $element, $attributes ) {
      // $this->pro->Method( "Document: StartElement()" );

         $this->is_endElement    = false;

      // Vater-Element ermitteln und auf den Stack legen
         $parent                 = $this->element_stack[count($this->element_stack) - 1];

         if ( !isset( $this->parent_stack[$parent] ) ) {
            $this->parent_stack[$parent] = 0;
         }  // if ( !isset( $this->parent_stack[$parent] ) )

         $index                  = $this->parent_stack[$parent];

         array_push( $this->element_stack, trim( $element ) );

         if ( ! isset( $this->element[$parent][$index] ) ) {
            $this->element[$parent][$index]     = array();
            $this->names[$parent]               = array();
         }  // if ( ! is_array( $this->element[$parent] ) )

         $speicher               = new Element( $element, $parent, new Attribut( $attributes ) );

         array_push( $this->element[$parent][$index], $speicher );
         array_push( $this->names[$parent], $element );

      // $this->pro->Returns( "Document: StartElement()" );
      }  // function StartElement()


      /**-----------------------------------------------------------------------
      * CharacterHandler ließt den Wert eines Elements
      *-----------------------------------------------------------------------*/
      function CharacterHandler( $parser, $value ) {
      // $this->pro->Method( "Document: CharacterHandler()" );

         if ( ! $this->is_endElement ) {
            $parent                          = $this->element_stack[count($this->element_stack) - 2];
            $index                           = $this->parent_stack[$parent];

            $last                            = count( $this->element[$parent][$index] ) - 1;

            $this->element[$parent][$index][$last]->setValue( trim( $value ) );
         }  // if ( ! $is_endElement )

      // $this->pro->Returns( "Document: CharacterHandler()" );
      }  // function EndElement()


      /**-----------------------------------------------------------------------
      * DefaultHandler
      * @param  Beschreibung
      * @return Beschreibung
      *-----------------------------------------------------------------------*/
      function DefaultHandler( $parser, $data ) {
      // $this->pro->Method( "Document: DefaultHandler()" );

         if ( substr( $data, 0, 5 ) == "<?xml" ) {
            $start               = strpos( $data, 'encoding="' ) + 10;
            $end                 = strpos( $data, '?>' ) - $start;
            $this->encoding      = trim( substr( $data, $start, $end ) );
            $this->encoding      = substr( $this->encoding, 0, strlen( $this->encoding ) - 1 );
         }  // if ( substring( $data, 0, ) == "<?xml" )

         else if ( substr( $data, 0, 4 ) == "<!--" ) {
            $end                 = strpos( $data, ' -->' ) - 5;
            $comment             = substr( $data, 5, $end );

            if ( ! isset( $this->is_endElement ) ) {
               $parent              = 'root';
            }  // if ( ! isset( $this->is_endElement ) )

            else if ( $this->is_endElement ) {
               $parent              = $this->element_stack[count($this->element_stack) - 1];
            }  // if ( $this->is_endElement )

            else {
               $parent              = $this->element_stack[count($this->element_stack) - 2];
            }  // end-of-if

            if ( $parent == 'root' ) {
               if ( empty( $this->comment ) ) {
                  $this->comment       = new Comment();
               }  // if ( empty( $this->comment ) )

               $this->addComment( trim( $comment ) );
            } else {
               $index               = $this->parent_stack[$parent];
               $last                = count( $this->element[$parent][$index] ) - 1;

               $this->element[$parent][$index][$last]->addComment( trim( $comment ) );
            }  // if ( $parent == 'root' )
         }  // if ( substr( $data, 0, 4 ) == "<!--" )

         else if ( $data == '<!DOCTYPE' ) {
            $this->is_DocType    = true;
         }  // if ( $data == '<!DOCTYPE' )

         else if ( $data == '<!ENTITY' ) {
            $this->is_Entity     = true;
         }  // if ( $data == '<!DOCTYPE' )

         else {
            if ( $this->is_DocType ) {
               $data             = trim( $data );

               if ( !empty( $data  ) && ( $data != '>' && $data != ']>' && $data != '[' && $data != ']' ) ) {
                  if ( $this->is_Entity ) {
                     if ( !isset( $this->EntityName ) ) {
                        $this->EntityName                = $data;
                     } else {
                        $this->entity[$this->EntityName] = substr( $data, 1, strlen( $data ) - 2 );

                        unset( $this->EntityName );
                     }  // if ( !isset( $this->Entity_name ) )
                  } else {
                     array_push( $this->doctype, $data );
                  }  // if ( $this->is_Entity )
               }  // if ( ! empty( trim( $data ) ) )

               if ( $data == '>' && $data == ']>' ) {
                  $this->is_DocType    = false;
                  $this->is_Entity     = false;
               }  // if ( $data != '>' && $data != ']>' )
            } else {
               array_push( $this->others, $data );
            }  // if ( $this->is_DocType )
         }  // end-of-if

      // $this->pro->Returns( "Document: DefaultHandler()" );
      }  // function DefaultHandler()


      /**-----------------------------------------------------------------------
      * EndElement wertet das Ende eines Elements aus
      *-----------------------------------------------------------------------*/
      function EndElement( $parser, $element ) {
      // $this->pro->Method( "Document: EndElement()" );

         $this->is_endElement    = true;

         $parent                 = array_pop( $this->element_stack );

         if ( isset( $this->parent_stack[$parent] ) ) {
            $this->parent_stack[$parent]++;
         }  // if ( isset( $this->parent_stack[$parent] ) )

      // $this->pro->Returns( "Document: EndElement()" );
      }  // function EndElement()


      /**-----------------------------------------------------------------------
      * addComment setzt Commentare für dieses Dokument
      * @param  comment - der Kommentar zum Dokument
      *-----------------------------------------------------------------------*/
      function addComment( $comment ) {
      // $this->pro->Method( "Document: addComment()" );

         if ( empty( $this->comment ) ) {
            $this->comment       = new Comment();           // Kommentar instanziieren
         }  // if ( empty( $this->comment ) )

         $this->comment->addComment( $comment );

      // $this->pro->Returns( "Document: addComment()" );
      }  // function addComment()


      /**-----------------------------------------------------------------------
      * getCoding gibt die Zeichencodierung des XML-Documents zurück.
      * @return die Zeichencodierung des Dokuments
      *-----------------------------------------------------------------------*/
      function getCoding() {
      // $this->pro->Method( "Document: getCoding()" );
      // $this->pro->Returns( "Document: getCoding()" );

         return $this->encoding;
      }  // function getCoding()


      /**-----------------------------------------------------------------------
      * getComment gibt eine Instanze des Objects COMMENT des Dokuments zurück
      * @return eine Instanze des Objects COMMENT des Dokuments
      *-----------------------------------------------------------------------*/
      function getComment() {
      // $this->pro->Method( "Document: getComment()" );
      // $this->pro->Returns( "Document: getComment()" );

         return $this->comment;
      }  // function getComment()


      /**-----------------------------------------------------------------------
      * getDocType gibt die Document Type Definition des XML-Documents als
      * indizierten Array zurück.
      * @return die Document Type Definition des Dokuments
      *-----------------------------------------------------------------------*/
      function getDocType() {
      // $this->pro->Method( "Document: getDocType()" );
      // $this->pro->Returns( "Document: getDocType()" );

         return $this->doctype;
      }  // function getDocType()


      /**-----------------------------------------------------------------------
      * getElement gibt das Root-Element des XML-Documents zurück.
      * @return das Root-Element
      *-----------------------------------------------------------------------*/
      function getElement() {
      // $this->pro->Method( "Document: getElement()" );
      // $this->pro->Returns( "Document: getElement()" );

         return $this->element;
      }  // function getElement()


      /**-----------------------------------------------------------------------
      * getEntity gibt die Entities des XML-Documents als indizierten Array
      * zurück.
      * @return die Entities des Dokuments
      *-----------------------------------------------------------------------*/
      function getEntity() {
      // $this->pro->Method( "Document: getEntity()" );
      // $this->pro->Returns( "Document: getEntity()" );

         return $this->entity;
      }  // function getEntity()


      /**-----------------------------------------------------------------------
      * getFilename gibt den Namen des aktuell verarbeiteten Dokuments zurück
      * @return der Dateiname des aktuellen Dokuments
      *-----------------------------------------------------------------------*/
      function getFilename() {
      // $this->pro->Method( "Document: getFilename()" );
      // $this->pro->Returns( "Document: getFilename()" );

         return $this->filename;
      }  // function getFilename()


      /**-----------------------------------------------------------------------
      * getOthers gibt weitere nicht zuordenbare Informationen des Dokuments in
      * einem indizierten Array zurück (nur zur Zeit)
      * @return nicht zuordenbare Informationen des Dokuments
      *-----------------------------------------------------------------------*/
      function getOthers() {
      // $this->pro->Method( "Document: getOthers()" );
      // $this->pro->Returns( "Document: getOthers()" );

         return $this->others;
      }  // function getOthers()


      /**-----------------------------------------------------------------------
      * initial initialisiert das Objekt für die Aufnahme eines neuen Dokuments
      * neu
      *-----------------------------------------------------------------------*/
      function initial() {
      // $this->pro->Method( "Document: initial()" );

         $this->filename      = '';

         $this->encoding      = '';
         $this->doctype       = array();
         $this->entity        = array();

         $this->element       = array();
         $this->comment       = null;

         $this->others        = array();

      // $this->pro->Returns( "Document: initial()" );
      }  // function initial()


      /**-----------------------------------------------------------------------
      * Release initialisiert alle nicht mehr benötigten temporär angelegte
      * Variablen.
      *-----------------------------------------------------------------------*/
      function Release() {
      // $this->pro->Method( "Document: Release()" );

         unset( $this->document );
         unset( $this->is_endElement );
         unset( $this->is_DocType );
         unset( $this->is_Entity );
         unset( $this->parent_stack );
         unset( $this->element_stack );
         unset( $this->names );

      // $this->pro->Returns( "Document: Release()" );
      }  // function Release()

   }  // class Document
?>