NAME
OpenOffice::OODoc::File - I/O operations with OpenDocument files
DESCRIPTION
The explicit use of this module is generally required only in programs which need to do some raw data import or export operations in OpenDocument files. This module can be used as a special wrapper of Archve::Zip.
However, a look at the constructor and the save() methods is recommended, in order to get a better knowledge of the file interface which is used by the document-oriented modules (i.e. OODoc::XPath and its derivatives).
If the program is only concerned with a single XML element from the file, it is unnecessary to create an OODoc::File explicitly. Just build an OODoc::XPath object with a filename as parameter. The XPath will create a "private" File itself and use it to access the data invisibly.
Please note that OODoc::File is able to handle all standard zip archives and not just OpenOffice.org files. Filenames do not have to have a ".sx?" extension. It allows access by other modules, if required, to XML/OpenOffice data in compressed archives which are not necessarily in OpenOffice.org format.
Methods
Constructor : OpenOffice::OODoc::File->new(filename|handle)
Short Forms (equivalent):odfFile(), odfContainer(), or odfPackage()Returns an instance of OODoc::File if the argument corresponds to avalid and accessible zip archive. The argument may be either afile path/name or an open (in binmode) and seekable IO::File.The file or file handle used here is used in order to initialize theODF Connector for subsequent processing through the other submodulesof the OpenOffice::OODoc API. It becomes the default target file ifthe data are updated and the changes committed using save().If the OODoc::File object is loaded for update, it's stronglyrecommended to avoid the use of a single IO::File object to read andwrite. However, if the source is a file name, the same file name maybe safely used as the source and the target (of course and as usual,any sensitive document must be backed up before processing).If the "create" option is set (see below), a file path/name may beprovided, but a file handle must be avoided.See the explanations about the save() method below.Examples:my $container1 = odfContainer("doc1.odt");my $fh = new IO::File "< doc2.odt";binmode $fh;my $container2 = odfContainer($fh);An optional argument can be passed in hash format (key => value),after the filename. Like this:work_dir => "path"which designates the path to the XML working files also generatedduring a save for this object (each OpenOffice::OODoc::File objectcan have its own working directory); without this option, theworking directory is set according to the content of the classvariable $OpenOffice::OODoc::File::WORKING_DIRECTORY.Note: no content checking is carried out. The archive can be openedwhether or not it is an OpenOffice.org document.It's possible to create an OpenOffice::OODoc::File object withoutproviding an existing OpenOffice.org file. To do so, there is aspecial option:create => "class"where "class" is the document class according to the OpenOffice.orgterminology, so it is one of the following values: "text",spreadsheet", "presentation", "drawing". These values are the sameas the legal parameters of contentClass() in OpenOffice::OODoc::XPath.If "create" is provided, the first argument must not be a file handle,knowing that the target (if the data is later saved) can't be anopen file, and that there is by definition no source file. Note that,for a creation, the value of the file argument is ignored so it may beundef or an empty string; however a valid path/name may be provided inorder to provide a default target for a subsequent save().For a very advanced use, it's possible to combine "create" with anadditional optiontemplate_path => "path"to generate the new file from special, user-provided ODF templatesinstead of those included in the installation. If this option isnot provided, the general template path (possibly changed withthe templatePath() function) is used. The template path mustindicate a directory which contains a set of ODF files whosenames are "template.???", where "???" represents the conventionalODF and OOo suffixes, knowing that the supported suffixes arepresently odt, ods, odp, odg, sxw, sxc, sxi, sxd. Of course, someapplications don't need all the templates; for example, the"template.sx?" files should be omitted if the user don't want touse the legacy OpenOffice.org 1.0 format for new documents.When the "create" option is used, it's possible to provide an"opendocument" option in order to override the installation-leveldefault file format for new documents. If this option is set to"1", "on" or "true", the new document will comply to the OASISOpenDocument format; if it's set to "0", "off" or "false", the newdocument will be created according to the OpenOffice.org 1.0 format.The "opendocument" option is ignored without the "create" one (thistool is not a format converter for existing documents). Remember thatthe default format is set to OpenDocument in the CPAN distributionand the OpenOffice.org 1.0 format is deprecated.The returned object of new(), if successful, is a valid File object,whose methods listed below become available.If unsuccessful (generally due to non-existent file or invalid ziparchive or even a corrupt zip archive), the constructor returns anull value (undef), and an error message is sent to the standardoutput.
extract(<member>)
Returns the decompressed content of the requested member, ifcontained in the archive and corresponds to an XML element of thecurrently active OpenOffice.org file instance. The <member>parameter must therefore correspond to one of the members of thefile (see Introduction). If the application uses any of the words"content", "meta", "styles" or "settings", in upper or lower case,the .xml extension is automatically added but any other names areaccepted without change if they are indeed existing members of thearchive.The following statements are equivalent:my $content = $archive->extract('META');my $content = $archive->extract('meta.xml');my $content = $archive->extract('meta);After the above calls, the variable $content contains the XMLdocument which represents the metadata of an OpenOffice.org file.This content can be used, for example, to instance a Meta object.Note: in most "normal" cases, this method does not have to be calledexplicitly as it is called silently by each occurrence of XPath(therefore by Text and Meta which are derivatives of it), but onlyif XPath is constructed referencing an OODoc::File object as aparameter (see OODoc::XPath). An extract call is only useful whenexporting the XML or handling it outside of OODoc::XPath.On error (e.g. unknown archive member), a null value is returned andan error message is produced.
link(<XPath_object>)
Connects a File object to an XPath object given as an argument. Thisconnection has two output products:- immediately calls the extract method using the corresponding"specialist" component of the XPath object (metadata ifOODoc::Meta, content if OODoc::Text, etc).- stores the link for later updates to all OpenOffice.org filemembers which may have been modified by XPath objects (in case asave is called, see below).Note: This method is used by OODoc::XPath to connect as "clients" toOODoc::File objects. It does not have to be called directly byhighest-level programs which only use OODoc::XPath objects.
raw_delete(member)
Orders the deletion of any OpenOffice.org file member.Example:$archive->raw_delete("Pictures/100000AEFGH.jpg");deletes the physical content of an image loaded in the file.It is entirely up to the application to ensure that such a deletiondoes not compromise the integrity of the file as no dependencychecking is carried out here. In the above example, the deleteoperation could be particularly justified if the "image" memberwhich referenced this content had been (or was going to be)otherwise removed, or if it had been replaced by an externalreference.This method can be used to remove any XML or non-XML member. It canbe combined with raw_import() in order to effect a raw replacementof content without interpretation. Caution: this method should notbe used for an XML member (content, style, meta, etc.) which iscurrently "active" (i.e. linked to an active OODoc::XPath instance),unless the member has been loaded as "read only" (search in theOpenOffice::OODoc::XPath for the "read_only" option).Note: calls to this method only prepare the deletion, which isactually carried out by the save() method if it occurs before theend of the program. If save() is called with a filename which isdifferent from the source filename, the source file remainsunchanged and the deleted member is simply not transferred to thetarget file.
raw_export(member [, destination])
Decompresses and exports the physical content of a given member (XMLor non-XML) of an archive. If the second argument is used, it passesthe destination filename (perhaps with access path). If not, thefile is exported using its internal archive name. Examples:$archive->raw_export("styles.xml");exports the "styles.xml" member into a file of the same name in thecurrent directory.$archive->raw_export("styles.xml", "/tmp/my_style.xml");exports the same XML member to a given path.raw_export executes immediately (and is not deferred likeraw_import).If successful, the returned value is the filename of the exportedfile.
raw_import(member [, source])
Creates or replaces the indicated member by importing an externalsource file. If the second argument is omitted, the source file istaken to have the same access path as the internal member.Example:$arch1->raw_export("styles.xml", "/tmp/styles.xml");$arch2->raw_import("styles.xml", "/tmp/styles.xml");or, in more compact form:$arch2->raw_import("styles.xml",$arch1->raw_export("styles.xml"));The above sequence requests the import of the member "styles.xml"from an archive called $arch1 into $arch2 (a direct means of usingthe styles and page layout of one document as a template foranother).The imported files can be any type and have any content. This "raw"method treats an OpenOffice.org file as any other zip archive. Itnotably allows the import of non-XML members (images, sounds,programs, etc) which the application deals with (and which can beignored by the office application).Caution: the import is only completed when a save() method is calledby the importing object. It can only succeed if the source file isavailable at that very moment. A raw_import method can be calledbefore the imported file is available (no check of availability ismade). An error will be caused if the file is absent at the time ofthe save() call. If several raw_import statements are run against thesame filename, there will actually be a corresponding number ofcopies of the file in its final state which are imported at themoment of the save() call, even if it had perhaps been modified in themeantime (probably not a very useful outcome).
save([filename|handle])
Commits all the changes made in the members of the archive and makesthe resulting content persistent.In addition, the external, non-XML resources (i.e. image files) thathave previously been targeted by import methods, if any, arephysically imported when save() is called (and not before). If theseresources are not available at this time, they are ignored and awarning is issued for each missing file.Example:$archive->save("target.odt");The target may be a file name (with optional path) or an IO::Fileobject (that must be open, in binmode and writable).Without argument, save(), attempts to write the on the file or handlethat was specified as the source file at the creation time, if any.However, if the OODoc::File was initialized with a IO::File, the use ofthe same IO::File for writing the output is presently not recommended;in some situations, bi-directional I/Os against an application-providedopen IO::File may result in unpredictable results with the currentimplementation. Of course, if the OODoc::File object has beeninitialized with the "create" option and with an undef value or anempty string as file name, save() requires an explicit target (a validfile/path or an open and writable IO::File).Please note that OODoc::File does not check the content, and the save()method can be used to force through any data which may produce afile not compliant with the ODF packaging specification.The filename argument is optional. If it is omitted, the source filepreviously supplied by the constructor call (if any) is updated.Without a filename argument, save() if the source document was gotthrough a IO::Handle.Even though the life of an OODoc::File object does not necessarilyend with a save, it is recommended that you avoid repeatedalternation between save and extract (the object's behaviour in thissituation has not been tested). Normally it is preferable to call asave once and for all at the end of a series of updates.Only a call to OODoc::File's save() method saves content, metadate andpresentation changes made by other OODoc components to theOpenOffice.org file, including raw imports of external data(raw_import). However, the XML members currently associated with"read only" OODoc::XPath objects are not changed in the file.No file is created or modified before this method iscalled, with the exception of external files created by raw_export.Nevertheless File's save can be called automatically and silently byan OODoc::XPath object but only where it has been called as aparameter explicitly for this purpose (see the chapter onOODoc::XPath).All XPath objects which are "connected" to a File object by linkmust be present at the time of the save call. If one of theseobjects has meanwhile been deleted, the consequences areunpredictable and, in any case, any document updates it could havemade are lost.
templatePath([path])
Class function (not to be used as a method).Can be replaced by odfTemplatePath(), see OODoc man page.Accessor to get/set the path for a user-defined set of ODF templates,to be used in case of new document creation. This path is empty bydefault. Without an explicit template path, the default XML templatesprovided with the OpenOffice::OODoc distribution are automaticallyselected.The template path must designate a directory containing 4 regularODF files, each one corresponding to a supported ODF document class,i.e. "template.odt", "template.ods", "template.odp", "template.odg".However, the user don't need to provide templates that will not beused.Note that it's always possible to override the default template pathwhen a new document is created, thanks to the 'template_path' option.
Properties
No class variables are exported.The class variable $OpenOffice::OODoc::File::WORKING_DIRECTORYindicates the directory to be used for temporary files (used but thesave() method) when no object-specific path is provided through the'work_dir' option. By default, the working directory is the currentdirectory ('.').The $OpenOffice::OODoc::File::TEMPLATE_PATH variable, empty bydefault, can contain an alternative path for document generationtemplate files; it can be set with the templatePath() function.The $OpenOffice::OODoc::File::$DEFAULT_OFFICE_FORMAT variable,whose default is 2, controls the default format for newly createdfiles (when the format is not explicitly selected by the application).Allowed values are "1" for OpenOffice.org 1.0 and "2" forOASIS OpenDocument. In a regular installation, this variable isautomatically set according to the <File-DEFAULT_OFFICE_FORMAT>element of the config.xml file (see INSTALL).Instance hash variables are:'linked' => list of connected OODoc::XPath instances'members' => list of file member (*.xml and others)'raw_members' => list of import files'temporary_files' => created temporary files.Where $f is a given instance of OODoc::File, the table@{$f->{'linked'}}is a list of OODoc::XPath objects which were connected to $f by thelink method and@{$f->{'members'}}is a list of members found in the archive when new is called.These variables can be read at any time even though they werenormally designed to be used internally by OODoc::File. Unless youare just finding out exactly what they do, it is dangerous to modifythem. Applications do not normally need to access them.
AUTHOR/COPYRIGHT
Developer/Maintainer: Jean-Marie Gouarne <http://jean.marie.gouarne.online.fr>
Contact: [email protected]
Copyright 2004-2008 by Genicorp, S.A. <http://www.genicorp.com>
Initial English version of the reference manual by Graeme A. Hunter ([email protected]).
License: GNU Lesser General Public License v2.1