Back to index

d-push  2.0
Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | Private Attributes
WBXMLDecoder Class Reference
Inheritance diagram for WBXMLDecoder:
Inheritance graph
[legend]
Collaboration diagram for WBXMLDecoder:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 WBXMLDecoder ($input)
 WBXML Decode Constructor.
 getElement ()
 Returns either start, content or end, and auto-concatenates successive content.
 peek ()
 Get a peek at the next element.
 getElementStartTag ($tag)
 Get the element of a StartTag.
 getElementEndTag ()
 Get the element of a EndTag.
 getElementContent ()
 Get the content of an element.
 ungetElement ($element)
 'Ungets' an element writing it into a buffer to be 'get' again
 GetPlainInputStream ()
 Returns the plain input stream.
 IsWBXML ()
 Returns if the input is WBXML.

Public Attributes

const VERSION = 0x03

Protected Attributes

 $dtd
 The WBXML DTDs.

Private Member Functions

 getToken ()
 
Private WBXMLDecoder stuff
 logToken ($el)
 Log the a token to ZLog.
 _getToken ()
 Returns either a start tag, content or end tag.
 getAttributes ()
 Gets attributes.
 splitAttribute ($attr)
 Splits an attribute.
 getTermStr ()
 Reads from the stream until getting a string terminator.
 getOpaque ($len)
 Reads $len from the input stream.
 getByte ()
 Reads one byte from the input stream.
 getMBUInt ()
 Reads string length from the input stream.
 getStringTable ()
 Reads string table from the input stream.
 getMapping ($cp, $id)
 Returns the mapping for a specified codepage and id.
 readVersion ()
 Reads one byte from the input stream.

Private Attributes

 $in
 $version
 $publicid
 $publicstringid
 $charsetid
 $stringtable
 $tagcp = 0
 $attrcp = 0
 $ungetbuffer
 $logStack = array()
 $inputBuffer = ""
 $isWBXML = true

Detailed Description

Definition at line 45 of file wbxmldecoder.php.


Member Function Documentation

WBXMLDecoder::_getToken ( ) [private]

Returns either a start tag, content or end tag.

private

Returns:

Definition at line 308 of file wbxmldecoder.php.

                                 {
        // Get the data from the input stream
        $element = array();

        while(1) {
            $byte = $this->getByte();

            if(!isset($byte))
                break;

            switch($byte) {
                case WBXML_SWITCH_PAGE:
                    $this->tagcp = $this->getByte();
                    continue;

                case WBXML_END:
                    $element[EN_TYPE] = EN_TYPE_ENDTAG;
                    return $element;

                case WBXML_ENTITY:
                    $entity = $this->getMBUInt();
                    $element[EN_TYPE] = EN_TYPE_CONTENT;
                    $element[EN_CONTENT] = $this->entityToCharset($entity);
                    return $element;

                case WBXML_STR_I:
                    $element[EN_TYPE] = EN_TYPE_CONTENT;
                    $element[EN_CONTENT] = $this->getTermStr();
                    return $element;

                case WBXML_LITERAL:
                    $element[EN_TYPE] = EN_TYPE_STARTTAG;
                    $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt());
                    $element[EN_FLAGS] = 0;
                    return $element;

                case WBXML_EXT_I_0:
                case WBXML_EXT_I_1:
                case WBXML_EXT_I_2:
                    $this->getTermStr();
                    // Ignore extensions
                    continue;

                case WBXML_PI:
                    // Ignore PI
                    $this->getAttributes();
                    continue;

                case WBXML_LITERAL_C:
                    $element[EN_TYPE] = EN_TYPE_STARTTAG;
                    $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt());
                    $element[EN_FLAGS] = EN_FLAGS_CONTENT;
                    return $element;

                case WBXML_EXT_T_0:
                case WBXML_EXT_T_1:
                case WBXML_EXT_T_2:
                    $this->getMBUInt();
                    // Ingore extensions;
                    continue;

                case WBXML_STR_T:
                    $element[EN_TYPE] = EN_TYPE_CONTENT;
                    $element[EN_CONTENT] = $this->getStringTableEntry($this->getMBUInt());
                    return $element;

                case WBXML_LITERAL_A:
                    $element[EN_TYPE] = EN_TYPE_STARTTAG;
                    $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt());
                    $element[EN_ATTRIBUTES] = $this->getAttributes();
                    $element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES;
                    return $element;
                case WBXML_EXT_0:
                case WBXML_EXT_1:
                case WBXML_EXT_2:
                    continue;

                case WBXML_OPAQUE:
                    $length = $this->getMBUInt();
                    $element[EN_TYPE] = EN_TYPE_CONTENT;
                    $element[EN_CONTENT] = $this->getOpaque($length);
                    return $element;

                case WBXML_LITERAL_AC:
                    $element[EN_TYPE] = EN_TYPE_STARTTAG;
                    $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt());
                    $element[EN_ATTRIBUTES] = $this->getAttributes();
                    $element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES | EN_FLAGS_CONTENT;
                    return $element;

                default:
                    $element[EN_TYPE] = EN_TYPE_STARTTAG;
                    $element[EN_TAG] = $this->getMapping($this->tagcp, $byte & 0x3f);
                    $element[EN_FLAGS] = ($byte & 0x80 ? EN_FLAGS_ATTRIBUTES : 0) | ($byte & 0x40 ? EN_FLAGS_CONTENT : 0);
                    if($byte & 0x80)
                        $element[EN_ATTRIBUTES] = $this->getAttributes();
                    return $element;
            }
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Gets attributes.

private

Returns:

Definition at line 415 of file wbxmldecoder.php.

                                     {
        $attributes = array();
        $attr = "";

        while(1) {
            $byte = $this->getByte();

            if(count($byte) == 0)
                break;

            switch($byte) {
                case WBXML_SWITCH_PAGE:
                    $this->attrcp = $this->getByte();
                    break;

                case WBXML_END:
                    if($attr != "")
                        $attributes += $this->splitAttribute($attr);

                    return $attributes;

                case WBXML_ENTITY:
                    $entity = $this->getMBUInt();
                    $attr .= $this->entityToCharset($entity);
                    return $element;

                case WBXML_STR_I:
                    $attr .= $this->getTermStr();
                    return $element;

                case WBXML_LITERAL:
                    if($attr != "")
                        $attributes += $this->splitAttribute($attr);

                    $attr = $this->getStringTableEntry($this->getMBUInt());
                    return $element;

                case WBXML_EXT_I_0:
                case WBXML_EXT_I_1:
                case WBXML_EXT_I_2:
                    $this->getTermStr();
                    continue;

                case WBXML_PI:
                case WBXML_LITERAL_C:
                    // Invalid
                    return false;

                case WBXML_EXT_T_0:
                case WBXML_EXT_T_1:
                case WBXML_EXT_T_2:
                    $this->getMBUInt();
                    continue;

                case WBXML_STR_T:
                    $attr .= $this->getStringTableEntry($this->getMBUInt());
                    return $element;

                case WBXML_LITERAL_A:
                    return false;

                case WBXML_EXT_0:
                case WBXML_EXT_1:
                case WBXML_EXT_2:
                    continue;

                case WBXML_OPAQUE:
                    $length = $this->getMBUInt();
                    $attr .= $this->getOpaque($length);
                    return $element;

                case WBXML_LITERAL_AC:
                    return false;

                default:
                    if($byte < 128) {
                        if($attr != "") {
                            $attributes += $this->splitAttribute($attr);
                            $attr = "";
                        }
                    }
                    $attr .= $this->getMapping($this->attrcp, $byte);
                    break;
            }
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXMLDecoder::getByte ( ) [private]

Reads one byte from the input stream.

private

Returns:
int

Definition at line 577 of file wbxmldecoder.php.

                               {
        $ch = fread($this->in, 1);
        if(strlen($ch) > 0)
            return ord($ch);
        else
            return;
    }

Here is the caller graph for this function:

Returns either start, content or end, and auto-concatenates successive content.

public

Returns:
element/value

Definition at line 100 of file wbxmldecoder.php.

                                 {
        $element = $this->getToken();

        switch($element[EN_TYPE]) {
            case EN_TYPE_STARTTAG:
                return $element;
            case EN_TYPE_ENDTAG:
                return $element;
            case EN_TYPE_CONTENT:
                while(1) {
                    $next = $this->getToken();
                    if($next == false)
                        return false;
                    else if($next[EN_TYPE] == EN_CONTENT) {
                        $element[EN_CONTENT] .= $next[EN_CONTENT];
                    } else {
                        $this->ungetElement($next);
                        break;
                    }
                }
                return $element;
        }

        return false;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Get the content of an element.

public

Returns:
string/boolean returns false if not available

Definition at line 190 of file wbxmldecoder.php.

                                        {
        $element = $this->getToken();

        if($element[EN_TYPE] == EN_TYPE_CONTENT) {
            return $element[EN_CONTENT];
        }
        else {
            ZLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementContent(): unmatched WBXML content: '%s' type '%s' flags '%s'", ((isset($element[EN_TAG]))?$element[EN_TAG]:""), ((isset($element[EN_TYPE]))?$element[EN_TYPE]:""), ((isset($element[EN_FLAGS]))?$element[EN_FLAGS]:"")));
            $this->ungetElement($element);
        }

        return false;
    }

Here is the call graph for this function:

Get the element of a EndTag.

public

Returns:
element/boolean returns false if not available

Definition at line 165 of file wbxmldecoder.php.

                                       {
        $element = $this->getToken();

        if($element[EN_TYPE] == EN_TYPE_ENDTAG)
            return $element;
        else {
            ZLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementEndTag(): unmatched WBXML tag: '%s' type '%s' flags '%s'", ((isset($element[EN_TAG]))?$element[EN_TAG]:""), ((isset($element[EN_TYPE]))?$element[EN_TYPE]:""), ((isset($element[EN_FLAGS]))?$element[EN_FLAGS]:"")));

            $bt = debug_backtrace();
            ZLog::Write(LOGLEVEL_ERROR, sprintf("WBXMLDecoder->getElementEndTag(): could not read end tag in '%s'. Please enable the LOGLEVEL_WBXML and send the log to the Z-Push dev team.", $bt[0]["file"] . ":" . $bt[0]["line"]));

            // log the remaining wbxml content
            $this->ungetElement($element);
            while($el = $this->getElement());
        }

        return false;
    }

Here is the call graph for this function:

Get the element of a StartTag.

Parameters:
$tagpublic
Returns:
element/boolean returns false if not available

Definition at line 146 of file wbxmldecoder.php.

                                             {
        $element = $this->getToken();

        if($element[EN_TYPE] == EN_TYPE_STARTTAG && $element[EN_TAG] == $tag)
            return $element;
        else {
            ZLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementStartTag(): unmatched WBXML tag: '%s' matching '%s' type '%s' flags '%s'", $tag, ((isset($element[EN_TAG]))?$element[EN_TAG]:""), ((isset($element[EN_TYPE]))?$element[EN_TYPE]:""), ((isset($element[EN_FLAGS]))?$element[EN_FLAGS]:"")));
            $this->ungetElement($element);
        }

        return false;
    }

Here is the call graph for this function:

WBXMLDecoder::getMapping ( cp,
id 
) [private]

Returns the mapping for a specified codepage and id.

Parameters:
$cpcodepage
$idpublic
Returns:
string

Definition at line 633 of file wbxmldecoder.php.

                                          {
        if(!isset($this->dtd["codes"][$cp]) || !isset($this->dtd["codes"][$cp][$id]))
            return false;
        else {
            if(isset($this->dtd["namespaces"][$cp])) {
                return $this->dtd["namespaces"][$cp] . ":" . $this->dtd["codes"][$cp][$id];
            } else
                return $this->dtd["codes"][$cp][$id];
        }
    }

Here is the caller graph for this function:

WBXMLDecoder::getMBUInt ( ) [private]

Reads string length from the input stream.

private

Returns:

Definition at line 591 of file wbxmldecoder.php.

                                 {
        $uint = 0;

        while(1) {
          $byte = $this->getByte();

          $uint |= $byte & 0x7f;

          if($byte & 0x80)
              $uint = $uint << 7;
          else
              break;
        }

        return $uint;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXMLDecoder::getOpaque ( len) [private]

Reads $len from the input stream.

Parameters:
int$lenprivate
Returns:
string

Definition at line 551 of file wbxmldecoder.php.

                                     {
        // TODO check if it's possible to do it other way
        // fread stops reading because the following condition is true (from php.net):
        // if the stream is read buffered and it does not represent a plain file,
        // at most one read of up to a number of bytes equal to the chunk size
        // (usually 8192) is made; depending on the previously buffered data,
        // the size of the returned data may be larger than the chunk size.

        // using only return fread it will return only a part of stream if chunk is smaller
        // than $len. Read from stream in a loop until the $len is reached.
        $d = "";
        $l = 0;
        while (1) {
            $l = (($len - strlen($d)) > 8192) ? 8192 : ($len - strlen($d));
            if ($l > 0) $d .= fread($this->in, $l);
            if (strlen($d) >= $len) break;
        }
        return $d;
    }

Here is the caller graph for this function:

Returns the plain input stream.

public

Returns:
string

Definition at line 225 of file wbxmldecoder.php.

                                          {
        $plain = $this->inputBuffer;
        while($data = fread($this->in, 4096))
            $plain .= $data;

        return $plain;
    }

Reads string table from the input stream.

private

Returns:
int

Definition at line 614 of file wbxmldecoder.php.

                                      {
        $stringtable = "";

        $length = $this->getMBUInt();
        if($length > 0)
            $stringtable = fread($this->in, $length);

        return $stringtable;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXMLDecoder::getTermStr ( ) [private]

Reads from the stream until getting a string terminator.

private

Returns:
string

Definition at line 529 of file wbxmldecoder.php.

                                  {
        $str = "";
        while(1) {
            $in = $this->getByte();

            if($in == 0)
                break;
            else
                $str .= chr($in);
        }

        return $str;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXMLDecoder::getToken ( ) [private]


Private WBXMLDecoder stuff

Returns the next token

private

Returns:
token

Definition at line 255 of file wbxmldecoder.php.

                                {
        // See if there's something in the ungetBuffer
        if($this->ungetbuffer) {
            $element = $this->ungetbuffer;
            $this->ungetbuffer = false;
            return $element;
        }

        $el = $this->_getToken();
        $this->logToken($el);

        return $el;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Returns if the input is WBXML.

public

Returns:
boolean

Definition at line 239 of file wbxmldecoder.php.

                              {
        return $this->isWBXML;
    }
WBXMLDecoder::logToken ( el) [private]

Log the a token to ZLog.

Parameters:
string$eltoken

private

Returns:

Definition at line 277 of file wbxmldecoder.php.

                                   {
        if(!WBXML_DEBUG)
            return;

        $spaces = str_repeat(" ", count($this->logStack));

        switch($el[EN_TYPE]) {
            case EN_TYPE_STARTTAG:
                if($el[EN_FLAGS] & EN_FLAGS_CONTENT) {
                    ZLog::Write(LOGLEVEL_WBXML,"I " . $spaces . " <". $el[EN_TAG] . ">");
                    array_push($this->logStack, $el[EN_TAG]);
                } else
                    ZLog::Write(LOGLEVEL_WBXML,"I " . $spaces . " <" . $el[EN_TAG] . "/>");

                break;
            case EN_TYPE_ENDTAG:
                $tag = array_pop($this->logStack);
                ZLog::Write(LOGLEVEL_WBXML,"I " . $spaces . "</" . $tag . ">");
                break;
            case EN_TYPE_CONTENT:
                ZLog::Write(LOGLEVEL_WBXML,"I " . $spaces . " " . $el[EN_CONTENT]);
                break;
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Get a peek at the next element.

public

Returns:
element

Definition at line 132 of file wbxmldecoder.php.

                           {
        $element = $this->getElement();
        $this->ungetElement($element);
        return $element;
    }

Here is the call graph for this function:

Reads one byte from the input stream.

private

Returns:
void

Definition at line 650 of file wbxmldecoder.php.

                                   {
        $ch = $this->getByte();

        if($ch != NULL) {
            $this->inputBuffer .= chr($ch);
            $this->version = $ch;
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXMLDecoder::splitAttribute ( attr) [private]

Splits an attribute.

Parameters:
string$attrattribute to be splitted

private

Returns:
array

Definition at line 510 of file wbxmldecoder.php.

                                           {
        $attributes = array();

        $pos = strpos($attr,chr(61)); // equals sign

        if($pos)
            $attributes[substr($attr, 0, $pos)] = substr($attr, $pos+1);
        else
            $attributes[$attr] = null;

        return $attributes;
    }

Here is the caller graph for this function:

'Ungets' an element writing it into a buffer to be 'get' again

Parameters:
element$elementthe element to get ungetten

public

Returns:

Definition at line 212 of file wbxmldecoder.php.

                                           {
        if($this->ungetbuffer)
            ZLog::Write(LOGLEVEL_ERROR,sprintf("WBXMLDecoder->ungetElement(): WBXML double unget on tag: '%s' type '%s' flags '%s'", ((isset($element[EN_TAG]))?$element[EN_TAG]:""), ((isset($element[EN_TYPE]))?$element[EN_TYPE]:""), ((isset($element[EN_FLAGS]))?$element[EN_FLAGS]:"")));

        $this->ungetbuffer = $element;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

WBXML Decode Constructor.

Parameters:
stream$inputthe incoming data stream

public

Definition at line 73 of file wbxmldecoder.php.

                                         {
        // make sure WBXML_DEBUG is defined. It should be at this point
        if (!defined('WBXML_DEBUG')) define('WBXML_DEBUG', false);

        $this->in = $input;

        $this->readVersion();
        if (isset($this->version) && $this->version != self::VERSION) {
            $this->isWBXML = false;
            return;
        }

        $this->publicid = $this->getMBUInt();
        if($this->publicid == 0) {
            $this->publicstringid = $this->getMBUInt();
        }

        $this->charsetid = $this->getMBUInt();
        $this->stringtable = $this->getStringTable();
    }

Here is the call graph for this function:


Member Data Documentation

WBXMLDecoder::$attrcp = 0 [private]

Definition at line 55 of file wbxmldecoder.php.

Definition at line 51 of file wbxmldecoder.php.

WBXMLDefs::$dtd [protected, inherited]

The WBXML DTDs.

Definition at line 83 of file wbxmldefs.php.

WBXMLDecoder::$in [private]

Definition at line 46 of file wbxmldecoder.php.

Definition at line 61 of file wbxmldecoder.php.

WBXMLDecoder::$isWBXML = true [private]

Definition at line 62 of file wbxmldecoder.php.

WBXMLDecoder::$logStack = array() [private]

Definition at line 59 of file wbxmldecoder.php.

Definition at line 49 of file wbxmldecoder.php.

Definition at line 50 of file wbxmldecoder.php.

Definition at line 52 of file wbxmldecoder.php.

WBXMLDecoder::$tagcp = 0 [private]

Definition at line 54 of file wbxmldecoder.php.

Definition at line 57 of file wbxmldecoder.php.

Definition at line 48 of file wbxmldecoder.php.

const WBXMLDecoder::VERSION = 0x03

Definition at line 64 of file wbxmldecoder.php.


The documentation for this class was generated from the following file: