diff --git a/MARC.php b/MARC.php index c6e2ea1..e294f4a 100644 --- a/MARC.php +++ b/MARC.php @@ -121,6 +121,13 @@ class File_MARC * @var int */ protected $type; + + /** + * XMLWriter for writing collections + * + * @var XMLWriter + */ + protected $xmlwriter; // }}} // {{{ Constructor: function __construct() @@ -145,6 +152,9 @@ class File_MARC */ function __construct($source, $type = self::SOURCE_FILE) { + + $this->xmlwriter = new XMLWriter(); + $this->xmlwriter->openMemory(); switch ($type) { case self::SOURCE_FILE: @@ -245,7 +255,7 @@ function next() */ private function _decode($text) { - $marc = new File_MARC_Record(); + $marc = new File_MARC_Record($this); $matches = array(); if (!preg_match("/^(\d{5})/", $text, $matches)) { @@ -367,6 +377,59 @@ private function _decode($text) } // }}} + // {{{ toXMLHeader() + /** + * Initializes the MARCXML output of a record or collection of records + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return bool true if successful + */ + function toXMLHeader() + { + $this->xmlwriter->startDocument('1.0', 'UTF-8'); + $this->xmlwriter->startElement("collection"); + $this->xmlwriter->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim"); + return true; + } + // }}} + + // {{{ getXMLWriter() + /** + * Returns the XMLWriter object + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return XMLWriter XMLWriter instance + */ + function getXMLWriter() + { + return $this->xmlwriter; + } + // }}} + + // {{{ toXMLFooter() + /** + * Returns the MARCXML collection footer + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return string representation of MARC record in MARCXML format + */ + function toXMLFooter() + { + $this->xmlwriter->endElement(); // end collection + $this->xmlwriter->endDocument(); + return $this->xmlwriter->outputMemory(); + } + // }}} + } // }}} diff --git a/MARC/Record.php b/MARC/Record.php index 6d06310..10240fa 100644 --- a/MARC/Record.php +++ b/MARC/Record.php @@ -73,6 +73,22 @@ class File_MARC_Record * @var array */ protected $warnings; + + /** + * XMLWriter for writing collections + * + * @var XMLWriter + */ + protected $marcxml; + + /** + * MARC instance for access to the XML header/footer methods + * We need this so that we can properly wrap a collection of MARC records. + * + * @var File_MARC + */ + protected $marc; + // }}} // {{{ Constructor: function __construct() @@ -83,10 +99,12 @@ class File_MARC_Record * * @return true */ - function __construct() + function __construct($marc) { $this->fields = new File_MARC_List(); $this->leader = str_repeat(' ', 24); + $this->marc = $marc; + $this->marcxml = $marc->getXMLWriter(); } // }}} @@ -479,7 +497,6 @@ function __toString() return $formatted; } // }}} - // {{{ toXML() /** * Return the MARC record in MARCXML format @@ -488,28 +505,21 @@ function __toString() * attempts to adhere to the MARCXML standard documented at * http://www.loc.gov/standards/marcxml/ * - * @param string $encoding output encoding for the MARCXML record - * @param bool $indent pretty-print the MARCXML record - * @param bool $collection wrap the element in a element + * @param string $encoding output encoding for the MARCXML record + * @param bool $indent pretty-print the MARCXML record + * @param bool $single wrap the element in a element * * @return string representation of MARC record in MARCXML format * * @todo Fix encoding input / output issues (PHP 6.0 required?) */ - function toXML($encoding = "UTF-8", $indent = true, $collection = true) + function toXML($encoding = "UTF-8", $indent = true, $single = true) { - $marcxml = new XMLWriter(); - $marcxml->openMemory(); - $marcxml->setIndent($indent); - if ($collection) { - $marcxml->startDocument("1.0", $encoding); - $marcxml->startElement("collection"); - $marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim"); - } - $marcxml->startElement("record"); - if (!$collection) { - $marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim"); + $this->marcxml->setIndent($indent); + if ($single) { + $this->marc->toXMLHeader(); } + $this->marcxml->startElement("record"); // MARCXML schema has some strict requirements // We'll set reasonable defaults to avoid invalid MARCXML @@ -527,43 +537,39 @@ function toXML($encoding = "UTF-8", $indent = true, $collection = true) $xmlLeader[6] = "a"; } - $marcxml->writeElement("leader", $xmlLeader); + $this->marcxml->writeElement("leader", $xmlLeader); foreach ($this->fields as $field) { if (!$field->isEmpty()) { switch(get_class($field)) { case "File_MARC_Control_Field": - $marcxml->startElement("controlfield"); - $marcxml->writeAttribute("tag", $field->getTag()); - $marcxml->text($field->getData()); - $marcxml->endElement(); // end control field + $this->marcxml->startElement("controlfield"); + $this->marcxml->writeAttribute("tag", $field->getTag()); + $this->marcxml->text($field->getData()); + $this->marcxml->endElement(); // end control field break; case "File_MARC_Data_Field": - $marcxml->startElement("datafield"); - $marcxml->writeAttribute("tag", $field->getTag()); - $marcxml->writeAttribute("ind1", $field->getIndicator(1)); - $marcxml->writeAttribute("ind2", $field->getIndicator(2)); + $this->marcxml->startElement("datafield"); + $this->marcxml->writeAttribute("tag", $field->getTag()); + $this->marcxml->writeAttribute("ind1", $field->getIndicator(1)); + $this->marcxml->writeAttribute("ind2", $field->getIndicator(2)); foreach ($field->getSubfields() as $subfield) { - $marcxml->startElement("subfield"); - $marcxml->writeAttribute("code", $subfield->getCode()); - $marcxml->text($subfield->getData()); - $marcxml->endElement(); // end subfield + $this->marcxml->startElement("subfield"); + $this->marcxml->writeAttribute("code", $subfield->getCode()); + $this->marcxml->text($subfield->getData()); + $this->marcxml->endElement(); // end subfield } - $marcxml->endElement(); // end data field + $this->marcxml->endElement(); // end data field break; } } } - $marcxml->endElement(); // end record - if ($collection) { - $marcxml->endElement(); // end collection - $marcxml->endDocument(); + $this->marcxml->endElement(); // end record + if ($single) { + return $this->marc->toXMLFooter(); } - - return $marcxml->outputMemory(); - } // }}} diff --git a/MARCXML.php b/MARCXML.php index d900998..b8d3364 100755 --- a/MARCXML.php +++ b/MARCXML.php @@ -97,6 +97,13 @@ class File_MARCXML * @var int */ protected $counter; + + /** + * XMLWriter for writing collections + * + * @var XMLWriter + */ + protected $xmlwriter; // }}} // {{{ Constructor: function __construct() @@ -123,6 +130,9 @@ function __construct($source, $type = self::SOURCE_FILE) { $this->counter = 0; + $this->xmlwriter = new XMLWriter(); + $this->xmlwriter->openMemory(); + switch ($type) { case self::SOURCE_FILE: @@ -198,7 +208,7 @@ function next() */ private function _decode($text) { - $marc = new File_MARC_Record(); + $marc = new File_MARC_Record($this); // Store leader $marc->setLeader($text->leader); @@ -228,6 +238,59 @@ private function _decode($text) } // }}} + // {{{ toXMLHeader() + /** + * Initializes the MARCXML output of a record or collection of records + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return bool true if successful + */ + function toXMLHeader() + { + $this->xmlwriter->startDocument(); + $this->xmlwriter->startElement("collection"); + $this->xmlwriter->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim"); + return true; + } + // }}} + + // {{{ getXMLWriter() + /** + * Returns the XMLWriter object + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return XMLWriter XMLWriter instance + */ + function getXMLWriter() + { + return $this->xmlwriter; + } + // }}} + + // {{{ toXMLFooter() + /** + * Returns the MARCXML collection footer + * + * This method produces an XML representation of a MARC record that + * attempts to adhere to the MARCXML standard documented at + * http://www.loc.gov/standards/marcxml/ + * + * @return string representation of MARC record in MARCXML format + */ + function toXMLFooter() + { + $this->xmlwriter->endElement(); // end collection + $this->xmlwriter->endDocument(); + return $this->xmlwriter->outputMemory(); + } + // }}} + } // }}}