com.catcode.odf
Class ODFMetaFileAnalyzer

Object
  extended byODFMetaFileAnalyzer

public class ODFMetaFileAnalyzer
extends Object

Analyze an OpenDocument meta file and create an OpenDocumentMetadata object. This code depends upon there being no duplicate element names between the Dublin Core and OpenDocument meta namespaces. If there are, this code will break. This code also depends upon there being no nested elements in the <office:meta> element.

Version:
0.2, 2005-11-09
Author:
J. David Eisenberg

Field Summary
protected  String dcNamespace
           
protected  String metaNamespace
           
protected  String officeNamespace
           
 
Constructor Summary
ODFMetaFileAnalyzer()
           
 
Method Summary
static OpenDocumentMetadata analyzeFile(File inputFile)
          Analyzes an Open Document meta file, presumed to be in .zip format.
 OpenDocumentMetadata analyzeMetadata(InputStream metaStream)
          Analyze the metadata in an InputStream.
 OpenDocumentMetadata analyzeMetaData(InputStream metaStream)
          Analyze the metadata in an InputStream.
 OpenDocumentMetadata analyzeZip(File inputFile)
          Analyze the metadata in a File which is a .zip file.
 OpenDocumentMetadata analyzeZip(InputStream inputStream)
          Analyze the metadata in a InputStream which is a .zip file.
protected  String extractNamespace(String namespaceAttrName)
          Extract a namespace from a namespace attribute.
protected  void findNamespaces(Element rootElement)
          Finds the namespace prefixes associated with OpenDocument, Dublin Core, and OpenDocument meta elements.
protected  String getBaseName(Node node)
          Get the "local" name in a non-namespace-aware parser.
protected  String makeSetMethodName(String elementName)
          Create a set method name corresponding to a meta-element.
protected  void processElement(Element element, OpenDocumentMetadata metadataResult)
          Put the content of this element into the metadata object.
protected  void processStatistic(Element element, OpenDocumentMetadata metadataResult)
          Put the content of the statistic element's attributes into the metadata object.
protected  void processUserDefined(Element element, OpenDocumentMetadata metadataResult)
          Put the content of this element into the user-defined section of the metadata object.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

officeNamespace

protected String officeNamespace

dcNamespace

protected String dcNamespace

metaNamespace

protected String metaNamespace
Constructor Detail

ODFMetaFileAnalyzer

public ODFMetaFileAnalyzer()
Method Detail

analyzeMetadata

public OpenDocumentMetadata analyzeMetadata(InputStream metaStream)
Analyze the metadata in an InputStream.

Algorithm:

  1. Parse the input stream into a Document
  2. From the root element, determine the namespace prefixes for that correspond to office:, meta:, and dc:.
  3. For each child element of the <office:meta> element, process it with the processElement() method, except for <meta:document-statistic>, which is handled with the processStatistic(), and <meta:user-defined>, which is handled with the processUserDefined() method.

Parameters:
metaStream - an InputStream that contains OpenDocument meta-information.
Returns:
an OpenDocumentMetadata structure that represents the file's meta information.

analyzeMetaData

public OpenDocumentMetadata analyzeMetaData(InputStream metaStream)
Analyze the metadata in an InputStream. This is a wrapper for the correctly-capitalized analyzeMetadata() method.


analyzeZip

public OpenDocumentMetadata analyzeZip(InputStream inputStream)
Analyze the metadata in a InputStream which is a .zip file. Written by Antti Jokipii to allow streaming of the .zip file

Parameters:
inputStream - a InputStream that contains .zip with OpenDocument meta-information.
Returns:
an OpenDocumentMetadata structure that represents the file's meta information.

analyzeZip

public OpenDocumentMetadata analyzeZip(File inputFile)
Analyze the metadata in a File which is a .zip file.

Parameters:
inputFile - a File that contains OpenDocument meta-information.
Returns:
an OpenDocumentMetadata structure that represents the file's meta information.

processElement

protected void processElement(Element element,
                              OpenDocumentMetadata metadataResult)
Put the content of this element into the metadata object.

The algorithm depends on reflection; element names must correspond to fields in OpenDocumentMetadata.

  1. Convert the element name to a "set" method name.
  2. Get the element content (first child, which should be text).
  3. Invoke the set method with the content as its parameter

Parameters:
element - the <meta:...> or <dc:...> element.
metadataResult - the metadata object to modify.

processStatistic

protected void processStatistic(Element element,
                                OpenDocumentMetadata metadataResult)
Put the content of the statistic element's attributes into the metadata object.

The algorithm depends on reflection; attribute names must correspond to fields in OpenDocumentMetadata.

  1. Convert each attribute name to a "set" method name.
  2. Get the attribute value.
  3. Invoke the set method with the content as its parameter

Parameters:
element - the <meta:document-statistic> element.
metadataResult - the metadata object to be set.

processUserDefined

protected void processUserDefined(Element element,
                                  OpenDocumentMetadata metadataResult)
Put the content of this element into the user-defined section of the metadata object.

This method presumes that the content of the element is its first child, which is a text node.

Parameters:
element - the <meta:user-defined> element containing the information.
metadataResult - the metadata object to modify.

analyzeFile

public static OpenDocumentMetadata analyzeFile(File inputFile)
Analyzes an Open Document meta file, presumed to be in .zip format. This is a wrapper for the non-static method.

Parameters:
inputFile - the File to analyze.
Returns:
an OpenDocumentMetadata object.

findNamespaces

protected void findNamespaces(Element rootElement)
Finds the namespace prefixes associated with OpenDocument, Dublin Core, and OpenDocument meta elements.

This function presumes that all the namespaces are in the root element. If they aren't, this breaks.

Parameters:
rootElement - the root element of the document.

extractNamespace

protected String extractNamespace(String namespaceAttrName)
Extract a namespace from a namespace attribute.

Parameters:
namespaceAttrName - an attribute name in the form xmlns:aaaa.
Returns:
the namespace, including the colon separator.

getBaseName

protected String getBaseName(Node node)
Get the "local" name in a non-namespace-aware parser.

Parameters:
node - the Node whose local name we want.
Returns:
the portion of the name after the :.

makeSetMethodName

protected String makeSetMethodName(String elementName)
Create a set method name corresponding to a meta-element. Takes the given name and changes all -letter sequences to (capitalized) Letter, prepended by set. Thus, an element name of initial-creator converts to the method name setInitialCreator.

Parameters:
elementName - the name of the element to munge.
Returns:
the name of a set method