new PDF reader

This commit is contained in:
Roland Gruber 2017-06-25 14:23:28 +02:00
parent 77e592d1dc
commit e4eed3c55d
3 changed files with 247 additions and 127 deletions

View File

@ -71,128 +71,6 @@ function getPDFStructures($typeId, $profile = null) {
return $return;
}
/**
* This function is used to get the PDF structure from XML file.
*
* @param string $typeId the account type
* @param string $name structure name
*
* @return array PDF structure
*/
function loadPDFStructureOld($typeId, $name='default') {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return null;
}
$parser = new xmlParser();
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
$xml = $parser->parse($file);
$border = array();
$structure = array();
$complete_page_definitions = array('filename' => 'printLogo.jpg', 'headline' => 'LDAP Account Manager');
if (!empty($xml)) {
$border['start'] = $xml[1]['PDF'][0];
$page_definitions = $xml[0][$xml[1]['PDF'][0]]['attributes'];
foreach($page_definitions as $key => $value) {
$complete_page_definitions[strtolower($key)] = $value;
unset($page_definitions[$key]);
}
$border['end'] = $xml[1]['PDF'][1];
$structure = array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
}
return array('structure' => $structure, 'page_definitions' => $complete_page_definitions);
}
/**
* This function is used to get the PDF structure from XML file.
*
* @param string $typeId the account type
* @param string $name structure name
*
* @return array PDF structure
*/
function loadPDFStructure($typeId, $name='default') {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return null;
}
$parser = new xmlParser();
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
$xml = $parser->parse($file);
$border = array();
$structure = array();
$complete_page_definitions = array('filename' => 'printLogo.jpg', 'headline' => 'LDAP Account Manager');
if (!empty($xml)) {
$border['start'] = $xml[1]['PDF'][0];
$page_definitions = $xml[0][$xml[1]['PDF'][0]]['attributes'];
foreach($page_definitions as $key => $value) {
$complete_page_definitions[strtolower($key)] = $value;
unset($page_definitions[$key]);
}
$border['end'] = $xml[1]['PDF'][1];
$structure = array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
}
return array('structure' => $structure, 'page_definitions' => $complete_page_definitions);
}
/**
* Reads a PDF structure file.
*
* @param string $file file name
* @return PDFStructure structure
*/
function _readPDFFile($file) {
$xml = new \XMLReader();
$xml->open($file);
$structure = new PDFStructure();
// open <pdf>
$xml->read();
if (!$xml->name == 'pdf') {
throw new \LAMException(_('Unable to read PDF structure.'));
}
$structure->setLogo($xml->getAttribute('filename'));
$structure->setTitle($xml->getAttribute('headline'));
$foldingMarks = $xml->getAttribute('foldingmarks');
if ($foldingMarks === 'yes') {
$structure->setFoldingMarks(true);
}
$sections = array();
while ($xml->read()) {
if ($xml->name === '#text') {
continue;
}
elseif (($xml->name === 'pdf') && ($xml->nodeType == \XMLReader::END_ELEMENT)) {
continue;
}
elseif ($xml->name === 'text') {
$xml->read();
$sections[] = new PDFTextSection($xml->value);
$xml->read();
if (!$xml->name === 'text') {
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
elseif ($xml->name === 'section') {
$section = new PDFEntrySection();
while ($xml->read()) {
if (($xml->name === 'section') && ($xml->nodeType == \XMLReader::END_ELEMENT)) {
break;
}
if (!$xml->name === 'entry') {
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
$sections[] = $section;
}
else {
echo '#' . $xml->name . '#';
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
$xml->close();
print_r($sections);
$structure->setSections($sections);
return $structure;
}
/**
* Saves PDF structure to XML file in format: <name>.<typeId>.xml
*
@ -472,6 +350,123 @@ function installPDFTemplates() {
}
}
/**
* Reads a PDF structure.
*
* @author Roland Gruber
*/
class PDFStructureReader {
/**
* Reads a PDF structure.
*
* @param string $typeId type id
* @param string $name structure name
* @return PDFStructure structure
*/
public function read($typeId, $name) {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return null;
}
$file = $this->getFileName($typeId, $name);
return $this->readPDFFile($file);
}
/**
* Returns the file name for the given structure.
*
* @param string $typeId type id
* @param string $name structure name
* @return string file name
*/
protected function getFileName($typeId, $name) {
return dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
}
/**
* Reads a PDF structure file.
*
* @param string $file file name
* @return PDFStructure structure
*/
private function readPDFFile($file) {
$xml = new \XMLReader();
$xml->open($file);
$structure = new PDFStructure();
// open <pdf>
$xml->read();
if (!$xml->name == 'pdf') {
logNewMessage(LOG_ERR, 'Unknown tag name: ' . $xml->name);
throw new \LAMException(_('Unable to read PDF structure.'));
}
$structure->setLogo($xml->getAttribute('filename'));
$structure->setTitle($xml->getAttribute('headline'));
$foldingMarks = $xml->getAttribute('foldingmarks');
if ($foldingMarks === 'yes') {
$structure->setFoldingMarks(true);
}
$sections = array();
while ($xml->read()) {
if ($xml->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE) {
continue;
}
elseif (($xml->name === 'pdf') && ($xml->nodeType == \XMLReader::END_ELEMENT)) {
continue;
}
elseif ($xml->name === 'text') {
$xml->read();
$sections[] = new PDFTextSection($xml->value);
$xml->read();
if (!$xml->name === 'text') {
logNewMessage(LOG_ERR, 'Unexpected tag name: ' . $xml->name);
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
elseif ($xml->name === 'section') {
$sections[] = $this->readSection($xml);
}
else {
logNewMessage(LOG_ERR, 'Unexpected tag name: ' . $xml->name);
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
$xml->close();
$structure->setSections($sections);
return $structure;
}
/**
* Reads a single section from XML.
*
* @param \XMLReader $xml reader
*/
private function readSection(&$xml) {
$section = new PDFEntrySection($xml->getAttribute('name'));
$entries = array();
while ($xml->read()) {
if (($xml->name === 'section') && ($xml->nodeType == \XMLReader::END_ELEMENT)) {
break;
}
elseif ($xml->nodeType === \XMLReader::END_ELEMENT) {
continue;
}
elseif ($xml->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE) {
continue;
}
elseif ($xml->name === 'entry') {
$entries[] = new PDFSectionEntry($xml->getAttribute('name'));
}
elseif (!$xml->name === 'entry') {
logNewMessage(LOG_ERR, 'Unexpected tag name: ' . $xml->name);
throw new \LAMException(_('Unable to read PDF structure.'));
}
}
$section->setEntries($entries);
return $section;
}
}
/**
* PDF structure
*
@ -597,6 +592,92 @@ class PDFTextSection {
*/
class PDFEntrySection {
private $title;
private $entries;
/**
* Constructor
*
* @param string $title title
*/
public function __construct($title) {
$this->title = $title;
}
/**
* Returns if the title is an attribute value.
*
* @return boolean is attribute
*/
public function isAttributeTitle() {
return boolval(preg_match('/^_([a-zA-Z0-9_-])+$/', $this->title));
}
/**
* Returns the PDF key name.
*
* @return string PDF key name
*/
public function getPdfKey() {
return substr($this->title, 1);
}
/**
* Returns the text title.
*
* @return string title
*/
public function getTitle() {
return $this->title;
}
/**
* Returns the entries.
*
* @return PDFSectionEntry[] entries
*/
public function getEntries() {
return $this->entries;
}
/**
* Sets the entries.
*
* @param PDFSectionEntry[] $entries entries
*/
public function setEntries($entries) {
$this->entries = $entries;
}
}
/**
* Single PDF entry.
*
* @author Roland Gruber
*/
class PDFSectionEntry {
private $key;
/**
* Constructor
*
* @param string $key key
*/
public function __construct($key) {
$this->key = $key;
}
/**
* Returns the PDF key.
*
* @return string $key key
*/
public function getKey() {
return $this->key;
}
}
?>

View File

@ -1,4 +1,8 @@
<?php
use LAM\PDF\PDFTextSection;
use LAM\PDF\PDFEntrySection;
use LAM\PDF\PDFStructureReader;
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2017 Roland Gruber
@ -29,17 +33,50 @@ include_once 'lam/lib/pdfstruct.inc';
*/
class ReadStructureTest extends PHPUnit_Framework_TestCase {
const TEST_FILE = 'lam/tests/resources/pdf/test.xml';
/**
* Reads the sample structure.
*/
public function testRead() {
$structure = \LAM\PDF\_readPDFFile(ReadStructureTest::TEST_FILE);
$reader = $this->getMockBuilder('\LAM\PDF\PDFStructureReader')
->setMethods(array('getFileName'))
->getMock();
$reader->method('getFileName')->willReturn($this->getTestFileName('test.xml'));
$structure = $reader->read('type', 'name');
$this->assertEquals('printLogo.jpg', $structure->getLogo());
$this->assertEquals('User information', $structure->getTitle());
$this->assertEquals(true, $structure->getFoldingMarks());
$this->assertEquals(3, sizeof($structure->getSections()));
$sections = $structure->getSections();
$this->assertEquals(3, sizeof($sections));
// check first section
$this->assertInstanceOf(PDFEntrySection::class, $sections[0]);
$this->assertFalse($sections[0]->isAttributeTitle());
$this->assertEquals('Personal user information', $sections[0]->getTitle());
$entries = $sections[0]->getEntries();
$this->assertEquals(3, sizeof($entries));
$this->assertEquals('inetOrgPerson_givenName', $entries[0]->getKey());
$this->assertEquals('inetOrgPerson_sn', $entries[1]->getKey());
$this->assertEquals('inetOrgPerson_street', $entries[2]->getKey());
// check text section
$this->assertInstanceOf(PDFTextSection::class, $sections[1]);
$this->assertEquals('test text', $sections[1]->getText());
// check third section
$this->assertInstanceOf(PDFEntrySection::class, $sections[2]);
$this->assertTrue($sections[2]->isAttributeTitle());
$this->assertEquals('posixAccount_uid', $sections[2]->getPdfKey());
$entries = $sections[2]->getEntries();
$this->assertEquals(2, sizeof($entries));
$this->assertEquals('posixAccount_homeDirectory', $entries[0]->getKey());
$this->assertEquals('posixAccount_loginShell', $entries[1]->getKey());
}
/**
* Returns the full path to the given file name.
*
* @param string $file file name
*/
private function getTestFileName($file) {
return dirname(dirname(__FILE__)) . '/resources/pdf/' . $file;
}
}

View File

@ -1,7 +1,9 @@
<pdf filename="printLogo.jpg" headline="User information" type="user" foldingmarks="yes">
<section name="Personal user information">
<entry name="inetOrgPerson_givenName" />
<entry name="inetOrgPerson_sn" />
<entry name="inetOrgPerson_sn">
</entry>
<entry name="inetOrgPerson_street" />
</section>
<text>test text</text>
<section name="_posixAccount_uid">