org.jpox.metadata
Class MetaDataParser

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byorg.jpox.metadata.MetaDataParser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class MetaDataParser
extends org.xml.sax.helpers.DefaultHandler

Class to parse a MetaData file and return a FileMetaData object. This class is invoked by either of

 MetaDataParser.parseMetaData(url,validate);
 MetaDataParser.parseMetaData(input_str,validate);
 
returning the FileMetaData. This class simply constructs the MetaData representation mirroring what is in the MetaData file. It has no knowledge of the class(es) that it represents, simply the information in the MetaData file. The knowledge of the classes is imposed on the representation at a later stage where necessary.

Operates the parse process using a Stack. MetaData components are added to the stack as they are encountered and created. They are then popped off the stack when the end element is encountered.

Since:
1.1
Version:
$Revision: 1.69 $

Field Summary
protected  java.lang.String latestElement
          Latest element.
protected static Localiser LOCALISER
           
protected  FileMetaData metadata
          The MetaData for the file.
protected  java.util.Stack stack
          Stack of meta-data elements.
 
Constructor Summary
protected MetaDataParser(MetaDataManager mdm, java.lang.String filename)
          Constructor.
 
Method Summary
 void characters(char[] ch, int start, int length)
          Notification handler for the data inside an element.
 void endDocument()
          Handler method called at the end of the XML document.
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
          Handler method called at the end of an element.
 void error(org.xml.sax.SAXParseException e)
          Parser error method.
protected  java.lang.String getAttr(org.xml.sax.Attributes attrs, java.lang.String key)
          Accessor for an attribute out of the attributes for an element.
protected  java.lang.String getAttr(org.xml.sax.Attributes attrs, java.lang.String key, java.lang.String defaultValue)
          Accessor for an attribute out of the attributes for an element.
protected  FileMetaData getMetaData()
          Accessor for the MetaData for this file.
protected  MetaData getStack()
          Accessor for the current MetaData component.
 java.lang.String getString()
          Accessor to the string form of this metadata
protected  ClassMetaData newClassObject(PackageMetaData pmd, org.xml.sax.Attributes attrs)
          Utility to create a new class component.
protected  FieldMetaData newFieldObject(MetaData md, org.xml.sax.Attributes attrs)
          Utility to create a new field component.
protected  PropertyMetaData newPropertyObject(MetaData md, org.xml.sax.Attributes attrs)
          Utility to create a new property component.
static FileMetaData parseMetaData(java.net.URL url, boolean validate, MetaDataManager mdm)
          Method to parse a JDO MetaData file given the URL of the file.
static FileMetaData parseMetaDataStream(java.io.InputStream in, boolean validate, MetaDataManager mdm, java.lang.String filename)
          Method to parse a JDO MetaData file given an InputStream.
protected  MetaData popStack()
          Method to remove the current MetaData component from the Stack.
protected  void pushStack(MetaData md)
          Method to add a MetaData component to the Stack.
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          Method to resolve XML entities.
 void startDocument()
          Handler method called at the start of the XML document.
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs)
          Handler method called at the start of an element.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, fatalError, ignorableWhitespace, notationDecl, processingInstruction, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALISER

protected static Localiser LOCALISER

stack

protected java.util.Stack stack
Stack of meta-data elements. The top of the stack is always the element being process at the moment. The elements are not the XML Element type but are things like ClassMetaData, PackageMetaData etc.


latestElement

protected java.lang.String latestElement
Latest element. This only keeps track of the element just started. Its useful where we have an element with no subelements, where it is set in the startElement() and used in characters() so we can extract the value.


metadata

protected final FileMetaData metadata
The MetaData for the file.

Constructor Detail

MetaDataParser

protected MetaDataParser(MetaDataManager mdm,
                         java.lang.String filename)
Constructor. Protected to prevent instantiation.

Parameters:
mdm - the metadata manager
filename - The name of the file to parse
Method Detail

parseMetaData

public static FileMetaData parseMetaData(java.net.URL url,
                                         boolean validate,
                                         MetaDataManager mdm)
Method to parse a JDO MetaData file given the URL of the file.

Parameters:
url - Url of the file
validate - Whether to validate against the DTD as well
mdm - the metadata manager
Returns:
The MetaData for this file
Throws:
javax.jdo.JDOException - thrown if error occurred

parseMetaDataStream

public static FileMetaData parseMetaDataStream(java.io.InputStream in,
                                               boolean validate,
                                               MetaDataManager mdm,
                                               java.lang.String filename)
Method to parse a JDO MetaData file given an InputStream.

Parameters:
in - input stream
validate - Whether to validate the MetaData
mdm - the metadata manager
filename - Name of the file (if applicable)
Returns:
The MetaData for this file
Throws:
javax.jdo.JDOException - thrown if error occurred

getMetaData

protected FileMetaData getMetaData()
Accessor for the MetaData for this file.

Returns:
The MetaData.

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Handler method called at the start of the XML document.

Throws:
org.xml.sax.SAXException - in parsing errors

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Handler method called at the end of the XML document.

Throws:
org.xml.sax.SAXException - in parsing errors

newClassObject

protected ClassMetaData newClassObject(PackageMetaData pmd,
                                       org.xml.sax.Attributes attrs)
Utility to create a new class component.

Parameters:
pmd - The parent PackageMetaData
attrs - The attributes
Returns:
The ClassMetaData

newFieldObject

protected FieldMetaData newFieldObject(MetaData md,
                                       org.xml.sax.Attributes attrs)
Utility to create a new field component.

Parameters:
md - The parent MetaData
attrs - The attributes
Returns:
The FieldMetaData

newPropertyObject

protected PropertyMetaData newPropertyObject(MetaData md,
                                             org.xml.sax.Attributes attrs)
Utility to create a new property component.

Parameters:
md - The parent MetaData
attrs - The attributes
Returns:
The PropertyMetaData

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Handler method called at the start of an element.

Parameters:
uri - URI of the tag
localName - Local name
qName - Element name
attrs - Attributes for this element
Throws:
org.xml.sax.SAXException - in parsing errors

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Handler method called at the end of an element.

Parameters:
uri - URI of the tag
localName - local name
qName - Name of element just ending
Throws:
org.xml.sax.SAXException - in parsing errors

error

public void error(org.xml.sax.SAXParseException e)
           throws org.xml.sax.SAXException
Parser error method. If any syntactical errors are encountered on validation they will appear here. Just points the user to the line/column of their Meta-Data file for now.

Parameters:
e - Parse Exception
Throws:
org.xml.sax.SAXException
See Also:
ErrorHandler.error(org.xml.sax.SAXParseException)

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Notification handler for the data inside an element.

Parameters:
ch - The characters
start - The start position in the character array.
length - The length of the string.
Throws:
org.xml.sax.SAXException - in parsing errors

getString

public java.lang.String getString()
Accessor to the string form of this metadata

Returns:
the string form of this metadata

getStack

protected MetaData getStack()
Accessor for the current MetaData component.

Returns:
The current MetaData component.

popStack

protected MetaData popStack()
Method to remove the current MetaData component from the Stack.

Returns:
Latest MetaData component.

pushStack

protected void pushStack(MetaData md)
Method to add a MetaData component to the Stack.

Parameters:
md - The component to add.

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
                                      throws org.xml.sax.SAXException
Method to resolve XML entities. Checks for PUBLIC and SYSTEM entities valid for JDO 1.0 and JDO 2.0. Optimises access using local versions of the JDO 1 and 2 DTD's.

Parameters:
publicId - The public id.
systemId - The system id.
Returns:
Input Source for the URI.
Throws:
org.xml.sax.SAXException
See Also:
EntityResolver.resolveEntity(java.lang.String,java.lang.String)

getAttr

protected java.lang.String getAttr(org.xml.sax.Attributes attrs,
                                   java.lang.String key,
                                   java.lang.String defaultValue)
Accessor for an attribute out of the attributes for an element. Allows the specification of a default if no value is provided.

Parameters:
attrs - The attributes
key - Key for the attribute to return
defaultValue - A default to impose if no value.
Returns:
Value for the attribute with the specified key.

getAttr

protected java.lang.String getAttr(org.xml.sax.Attributes attrs,
                                   java.lang.String key)
Accessor for an attribute out of the attributes for an element.

Parameters:
attrs - The attributes
key - Key for the attribute to return
Returns:
Value for the attribute with the specified key.


Copyright © -2007 . All Rights Reserved.