ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLPurifier_StringHashParser Class Reference

Parses string hash files. More...

+ Collaboration diagram for HTMLPurifier_StringHashParser:

Public Member Functions

 parseFile ($file)
 Parses a file that contains a single string-hash.
 parseMultiFile ($file)
 Parses a file that contains multiple string-hashes delimited by '-—'.

Data Fields

 $default = 'ID'

Protected Member Functions

 parseHandle ($fh)
 Internal parser that acepts a file handle.

Detailed Description

Parses string hash files.

File format is as such:

DefaultKeyValue
KEY: Value
KEY2: Value2
--MULTILINE-KEY--
Multiline
value.

Which would output something similar to:

 array(
     'ID' => 'DefaultKeyValue',
     'KEY' => 'Value',
     'KEY2' => 'Value2',
     'MULTILINE-KEY' => "Multiline\nvalue.\n",
 )

We use this as an easy to use file-format for configuration schema files, but the class itself is usage agnostic.

You can use -— to forcibly terminate parsing of a single string-hash; this marker is used in multi string-hashes to delimit boundaries.

Definition at line 28 of file StringHashParser.php.

Member Function Documentation

HTMLPurifier_StringHashParser::parseFile (   $file)

Parses a file that contains a single string-hash.

Definition at line 36 of file StringHashParser.php.

References $file, $ret, and parseHandle().

{
if (!file_exists($file)) return false;
$fh = fopen($file, 'r');
if (!$fh) return false;
$ret = $this->parseHandle($fh);
fclose($fh);
return $ret;
}

+ Here is the call graph for this function:

HTMLPurifier_StringHashParser::parseHandle (   $fh)
protected

Internal parser that acepts a file handle.

Note
While it's possible to simulate in-memory parsing by using custom stream wrappers, if such a use-case arises we should factor out the file handle into its own class.
Parameters
$fhFile handle with pointer at start of valid string-hash block.

Definition at line 68 of file StringHashParser.php.

References $default, $ret, and elseif().

Referenced by parseFile(), and parseMultiFile().

{
$state = false;
$single = false;
$ret = array();
do {
$line = fgets($fh);
if ($line === false) break;
$line = rtrim($line, "\n\r");
if (!$state && $line === '') continue;
if ($line === '----') break;
if (strncmp('--#', $line, 3) === 0) {
// Comment
continue;
} elseif (strncmp('--', $line, 2) === 0) {
// Multiline declaration
$state = trim($line, '- ');
if (!isset($ret[$state])) $ret[$state] = '';
continue;
} elseif (!$state) {
$single = true;
if (strpos($line, ':') !== false) {
// Single-line declaration
list($state, $line) = explode(':', $line, 2);
$line = trim($line);
} else {
// Use default declaration
$state = $this->default;
}
}
if ($single) {
$ret[$state] = $line;
$single = false;
$state = false;
} else {
$ret[$state] .= "$line\n";
}
} while (!feof($fh));
return $ret;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

HTMLPurifier_StringHashParser::parseMultiFile (   $file)

Parses a file that contains multiple string-hashes delimited by '-—'.

Definition at line 48 of file StringHashParser.php.

References $file, $ret, and parseHandle().

{
if (!file_exists($file)) return false;
$ret = array();
$fh = fopen($file, 'r');
if (!$fh) return false;
while (!feof($fh)) {
$ret[] = $this->parseHandle($fh);
}
fclose($fh);
return $ret;
}

+ Here is the call graph for this function:

Field Documentation

HTMLPurifier_StringHashParser::$default = 'ID'

Definition at line 31 of file StringHashParser.php.

Referenced by parseHandle().


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