net.sf.fikin.xml
Class TemplateEngine

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by net.sf.fikin.xml.sax.SAXSerializer
          extended by net.sf.fikin.xml.TemplateEngine
All Implemented Interfaces:
SAXHandler, SAXHandlerAndResolver, org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler

public class TemplateEngine
extends SAXSerializer

Simple template engine.

It can read input XML document and for each tag from predefined namespace it can execute a method of a POJO lookup object.

      public class MyPOJO {
          public String empty() { return ""; }
          public String m1() { return "Hello world!"; }
          public String m2() { return "Simple TE"; }
      }
      ...
      Input XML file:
          <te:empty xmlns:te="http://sf.net/fikin-ant-tasks/xml/sax-template-engine">
          This is "<te:m1/>" example, generated by <te:m2/> <today/>
          </te:empty>
      ...
      
      TemplateEngine te = new TemplateEngine();
      
      te.transform( input, new MyPOJO(), outputStream );
      ...
 

TODO:

  • loops and other condition statements
  • passing method parameters
  • created on Aug 12, 2006

    Since:
    Version:
    $Revision: 1.2 $
    Author:
    fiykov

    Field Summary
    static java.lang.String NAMESPACE
              tags namespace of this template engine
     
    Fields inherited from class net.sf.fikin.xml.sax.SAXSerializer
    doIndent, elementDepth, inCDATA, indent, isCanonical, isPreviousStartElement, isXML11, locator, namespaces, output
     
    Constructor Summary
    TemplateEngine()
              instantiate new template engine with default xml reader
    TemplateEngine(org.xml.sax.XMLReader reader)
              instantiate new template engine with given xml reader
     
    Method Summary
    protected  java.lang.Object[] convertAtttibutes(java.lang.reflect.Method method, org.xml.sax.Attributes attributes)
              convert xml sax Attributes into an array of values sorts them first in alphabetical order
     java.lang.String empty()
              lookup built-in method
     void endDocument()
              flush the output writer and reset internal state
     void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
              print clising tag is this is not a tag from NAMESPACE namespace
     java.lang.String forAll(java.util.Iterator iterator)
               
     java.lang.Object getLookupObject()
              get lookup object
     java.util.Hashtable getMethods()
              get indexed methods of the lookup object
     org.xml.sax.XMLReader getReader()
              get xml reader object
    protected  void indexMethods(java.lang.Object lookupObject)
              index lookup object's methods which the negine can work with public not void
    protected  java.lang.String invokeMethod(java.lang.String methodName, org.xml.sax.Attributes attributes)
              invoke given method and using given sax-tag attributes as parameters
    static org.xml.sax.InputSource newInputSource(java.io.File in)
              instaitate new input source
    static org.xml.sax.InputSource newInputSource(java.io.InputStream in)
              instaitate new input source
    static org.xml.sax.InputSource newInputSource(java.io.Reader in)
              instaitate new input source
    static org.xml.sax.InputSource newInputSource(java.lang.String file, java.lang.Class callingCls)
              instantiate new input source
    static org.xml.sax.InputSource newInputSource(java.lang.String fqnFile, java.lang.ClassLoader cl)
              instantiate new input source
    static org.xml.sax.InputSource newInputSource(java.lang.String file, java.lang.Package pkg)
              instantiate new input source
    static org.xml.sax.InputSource newInputSource(java.net.URL in)
              instaitate new input source
    static org.xml.sax.InputSource newInputSourceFromCode(java.lang.String code)
              instaitate new input source
    static org.xml.sax.InputSource newInputSourceFromFile(java.lang.String file)
              instaitate new input source
    static org.xml.sax.InputSource newInputSourceFromSystemId(java.lang.String systemId)
              instaitate new input source
    static java.io.CharArrayWriter newOutput()
              instantiate new char array writer
    static java.io.BufferedWriter newOutput(java.io.File out)
              instantiate new file writer
    static java.io.CharArrayWriter newOutput(int initialSize)
              instantiate new char array writer
    static java.io.BufferedWriter newOutputFromFile(java.lang.String file)
              instantiate new file writer
    static org.xml.sax.XMLReader newXMLReader()
              instantiate new defualt xml reader
    static org.xml.sax.XMLReader newXMLReader(java.lang.String readerClassName)
              instantiate new xml reader with given class name
    protected  void reset()
              reset internal state after transformation
    protected  void setLookupObject(java.lang.Object lookupObject)
              build methods index of the lookup object
    protected  void setXMLReader(org.xml.sax.XMLReader r)
              initialize xml reader with this object receiving the parsing notifications
     void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
              process tag if this is a tag from NAMESPACE namespace it executes the corrsponding to the tag name lookupObject method and prints out the method invocation result. is this is any other tag it copes it as occured in the input stream
     void transform(org.xml.sax.InputSource input)
              same as transform(InputSource, Object, BufferedWriter) but using this as lookup object and a dummy output stream
     void transform(org.xml.sax.InputSource input, java.io.BufferedWriter output)
              same as transform(InputSource, Object, BufferedWriter) but using this as lookup object
     void transform(org.xml.sax.InputSource input, java.lang.Object lookupObject)
              same as transform(InputSource, Object, BufferedWriter) but using a dummy output stream
     void transform(org.xml.sax.InputSource input, java.lang.Object lookupObject, java.io.BufferedWriter output)
              process input document and for each tag in NAMESPACE namespace perform method invocation of the corresponding to the tag's name method of lookupObject and print the result.
     
    Methods inherited from class net.sf.fikin.xml.sax.SAXSerializer
    characters, characters, comment, comment, endCDATA, endDTD, endEntity, error, fatalError, getDocumentLocator, getDoIndent, getIndent, getIsXML11, getIsXMLCanonical, getNamespaces, getOutput, getXMLVersion, ignorableWhitespace, ignorableWhitespace, newLocationException, newLocationException, newLocationException, newLocationException, newLocationException, newLocationException, newLocationException, newLocationException, normalize, normalize, normalize, parse, parse, parse, printError, printIndent, processingInstruction, setDocumentLocator, setDoIndent, setIndent, setIsXML11, setIsXMLCanonical, setOutput, sortAttributes, startCDATA, startDocument, startDTD, startEntity, startPrefixMapping, warning, writeClosingTag
     
    Methods inherited from class org.xml.sax.helpers.DefaultHandler
    endPrefixMapping, notationDecl, resolveEntity, skippedEntity, unparsedEntityDecl
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     
    Methods inherited from interface org.xml.sax.EntityResolver
    resolveEntity
     
    Methods inherited from interface org.xml.sax.ContentHandler
    endPrefixMapping, skippedEntity
     
    Methods inherited from interface org.xml.sax.DTDHandler
    notationDecl, unparsedEntityDecl
     

    Field Detail

    NAMESPACE

    public static final java.lang.String NAMESPACE
    tags namespace of this template engine

    See Also:
    Constant Field Values
    Constructor Detail

    TemplateEngine

    public TemplateEngine()
                   throws org.xml.sax.SAXException
    instantiate new template engine with default xml reader

    Throws:
    org.xml.sax.SAXException

    TemplateEngine

    public TemplateEngine(org.xml.sax.XMLReader reader)
                   throws org.xml.sax.SAXException
    instantiate new template engine with given xml reader

    Parameters:
    reader -
    Throws:
    org.xml.sax.SAXException
    Method Detail

    setXMLReader

    protected void setXMLReader(org.xml.sax.XMLReader r)
    initialize xml reader with this object receiving the parsing notifications


    getReader

    public org.xml.sax.XMLReader getReader()
    get xml reader object


    getMethods

    public java.util.Hashtable getMethods()
    get indexed methods of the lookup object


    setLookupObject

    protected void setLookupObject(java.lang.Object lookupObject)
    build methods index of the lookup object

    Parameters:
    lookupObject -
    Throws:
    java.lang.VerifyError - in case there are dublicated method names and total parameters counts

    getLookupObject

    public java.lang.Object getLookupObject()
    get lookup object


    transform

    public void transform(org.xml.sax.InputSource input,
                          java.lang.Object lookupObject,
                          java.io.BufferedWriter output)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
    process input document and for each tag in NAMESPACE namespace perform method invocation of the corresponding to the tag's name method of lookupObject and print the result. all other data is coped as it is received via sax events.

    Parameters:
    input - is the input xml document to be processed
    lookupObject - is plain old java object having public methods and returning strings or other toString() capable objects. These methods will be executed each time a tag with same name is encounted in the input document.
    output - is the output stream
    Throws:
    org.xml.sax.SAXException
    java.io.IOException

    transform

    public void transform(org.xml.sax.InputSource input,
                          java.io.BufferedWriter output)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
    same as transform(InputSource, Object, BufferedWriter) but using this as lookup object

    Parameters:
    input - is the input xml document to be processed
    output - is the output stream
    Throws:
    org.xml.sax.SAXException
    java.io.IOException

    transform

    public void transform(org.xml.sax.InputSource input)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
    same as transform(InputSource, Object, BufferedWriter) but using this as lookup object and a dummy output stream

    Parameters:
    input - is the input xml document to be processed
    Throws:
    org.xml.sax.SAXException
    java.io.IOException

    transform

    public void transform(org.xml.sax.InputSource input,
                          java.lang.Object lookupObject)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
    same as transform(InputSource, Object, BufferedWriter) but using a dummy output stream

    Parameters:
    input - is the input xml document to be processed
    Throws:
    org.xml.sax.SAXException
    java.io.IOException

    indexMethods

    protected void indexMethods(java.lang.Object lookupObject)
    index lookup object's methods which the negine can work with
  • public
  • not void
  • Parameters:
    lookupObject -
    Throws:
    java.lang.VerifyError - in case there are methods with equal names and total number of arguments

    invokeMethod

    protected java.lang.String invokeMethod(java.lang.String methodName,
                                            org.xml.sax.Attributes attributes)
                                     throws org.xml.sax.SAXException
    invoke given method and using given sax-tag attributes as parameters

    Parameters:
    methodName -
    attributes -
    Returns:
    result of the method invocation where empty string in case of method's null result invocation
    Throws:
    org.xml.sax.SAXException

    convertAtttibutes

    protected java.lang.Object[] convertAtttibutes(java.lang.reflect.Method method,
                                                   org.xml.sax.Attributes attributes)
    convert xml sax Attributes into an array of values sorts them first in alphabetical order

    Parameters:
    method -
    attributes -
    Returns:
    array of values

    reset

    protected void reset()
    reset internal state after transformation


    empty

    public java.lang.String empty()
    lookup built-in method

    Returns:
    return empty string

    forAll

    public java.lang.String forAll(java.util.Iterator iterator)

    newXMLReader

    public static org.xml.sax.XMLReader newXMLReader()
                                              throws org.xml.sax.SAXException
    instantiate new defualt xml reader

    Returns:
    xml reader
    Throws:
    org.xml.sax.SAXException

    newXMLReader

    public static org.xml.sax.XMLReader newXMLReader(java.lang.String readerClassName)
                                              throws org.xml.sax.SAXException
    instantiate new xml reader with given class name

    Parameters:
    readerClassName - if fqn of the class
    Returns:
    xml reader
    Throws:
    org.xml.sax.SAXException

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.io.InputStream in)
    instaitate new input source

    Parameters:
    in - is the input stream
    Returns:
    new input source

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.io.Reader in)
    instaitate new input source

    Parameters:
    in - is the input reader
    Returns:
    new input source

    newInputSourceFromSystemId

    public static org.xml.sax.InputSource newInputSourceFromSystemId(java.lang.String systemId)
    instaitate new input source

    Parameters:
    systemId -
    Returns:
    new input source

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.io.File in)
                                                  throws java.io.FileNotFoundException
    instaitate new input source

    Parameters:
    in - is the file to be used as input
    Returns:
    new input source
    Throws:
    java.io.FileNotFoundException

    newInputSourceFromFile

    public static org.xml.sax.InputSource newInputSourceFromFile(java.lang.String file)
                                                          throws java.io.FileNotFoundException
    instaitate new input source

    Parameters:
    file - name to be used as input
    Returns:
    new input source
    Throws:
    java.io.FileNotFoundException

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.net.URL in)
                                                  throws java.io.IOException
    instaitate new input source

    Parameters:
    in - is the url which stream is to be used
    Returns:
    new input source
    Throws:
    java.io.IOException

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.lang.String fqnFile,
                                                         java.lang.ClassLoader cl)
    instantiate new input source

    Parameters:
    fqnFile - is the fully qualified name of the classloader resource which input stream is to be used as input
    cl - is classloader to be used to resolve the resource. if null it will use Thread context classloader.
    Returns:
    new input source

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.lang.String file,
                                                         java.lang.Package pkg)
    instantiate new input source

    Parameters:
    file - is file name (local) of the classloader reasource, which input stream is to be used as input source
    pkg - is the java package where this resource is located
    Returns:
    new input source

    newInputSource

    public static org.xml.sax.InputSource newInputSource(java.lang.String file,
                                                         java.lang.Class callingCls)
    instantiate new input source

    Parameters:
    file - is file name (local) of the classloader reasource, which input stream is to be used as input source
    callingCls - is the java class under which package this resource is located
    Returns:
    new input source

    newInputSourceFromCode

    public static org.xml.sax.InputSource newInputSourceFromCode(java.lang.String code)
    instaitate new input source

    Parameters:
    code - is the actual input code
    Returns:
    new input source

    newOutput

    public static java.io.CharArrayWriter newOutput()
    instantiate new char array writer

    Returns:
    new char array writer

    newOutput

    public static java.io.CharArrayWriter newOutput(int initialSize)
    instantiate new char array writer

    Parameters:
    initialSize - with initial capacity
    Returns:
    new char array writer

    newOutput

    public static java.io.BufferedWriter newOutput(java.io.File out)
                                            throws java.io.IOException
    instantiate new file writer

    Parameters:
    out - is the output file to be used
    Returns:
    new buffered file writer
    Throws:
    java.io.IOException

    newOutputFromFile

    public static java.io.BufferedWriter newOutputFromFile(java.lang.String file)
                                                    throws java.io.IOException
    instantiate new file writer

    Parameters:
    file - is the output file name to be used
    Returns:
    new buffered file writer
    Throws:
    java.io.IOException

    endDocument

    public void endDocument()
                     throws org.xml.sax.SAXException
    flush the output writer and reset internal state

    Specified by:
    endDocument in interface org.xml.sax.ContentHandler
    Overrides:
    endDocument in class SAXSerializer
    Throws:
    org.xml.sax.SAXException

    startElement

    public void startElement(java.lang.String uri,
                             java.lang.String localName,
                             java.lang.String qName,
                             org.xml.sax.Attributes attributes)
                      throws org.xml.sax.SAXException
    process tag
  • if this is a tag from NAMESPACE namespace it executes the corrsponding to the tag name lookupObject method and prints out the method invocation result.
  • is this is any other tag it copes it as occured in the input stream
  • Specified by:
    startElement in interface org.xml.sax.ContentHandler
    Overrides:
    startElement in class SAXSerializer
    Throws:
    org.xml.sax.SAXException

    endElement

    public void endElement(java.lang.String uri,
                           java.lang.String localName,
                           java.lang.String qName)
                    throws org.xml.sax.SAXException
    print clising tag is this is not a tag from NAMESPACE namespace

    Specified by:
    endElement in interface org.xml.sax.ContentHandler
    Overrides:
    endElement in class SAXSerializer
    Throws:
    org.xml.sax.SAXException


    Copyright © 2003-2007 Nikolay Fiykov. All Rights Reserved.