com.objectwave.templateMerge
Class TemplatePreprocessor

java.lang.Object
  |
  +--com.objectwave.templateMerge.TemplatePreprocessor

public class TemplatePreprocessor
extends java.lang.Object

Read a flatfile and convert it's contexts to a MergeTemplate object, possibly containing some MergeTemplates itself. This is useful for defining a file which will contain text & token tags, with a couple of extra tags suported: beginBlock ... [ beginBlock ... endBlock] ... endBlock and include The beginBlock/endBlock tags are to accomodate the "collection" token types. For instance, we may want to iterate all Equipment objects in a given Quote.lease.equipmentSummary, so instead of having to define several different templates/files (preEquipment, iterEquipment, postEquipment), we can simply define a file as: ... pre-equipment text ... beginBlock ... iterEquipment text ... endBlock ... post-equipment text ... Furthermore, we can nest these structures. Suppose each Equipment object had a collection of Parts: ... preEquipment ... beginBlock ... iterEquipmentPreParts ... beginBlock ... iterParts ... endBlock ... iterEquipmentPostParts ... endBlock ... postEquipment ...


Field Summary
static boolean debugOn
           
 
Constructor Summary
TemplatePreprocessor()
           
 
Method Summary
protected  void dout(java.lang.String s)
          Method for debuggin purposes only.
protected  int eatWhitespace(char[] array, int index)
          Return the next non-whitespace character (not a space, tab, newline, for formfeed).
protected  int findEndTag(char[] array, int beginPos, int endPos)
          Find the next endTag, after matching nested pairs of beginTag, endTag.
protected  int findFirstOf(char[] array, java.lang.String findMe, int beginIndex, int endIndex)
          Provide a custom method to find substrings in a char array.
 java.lang.String getBeginTag()
           
 java.lang.String getEndTag()
           
 java.lang.String getImportTag()
           
 InformationToken[] getTokens()
           
 char[] importURLs(char[] array)
          (attempt to) import files indicated by the importTag.
 int lastProcessingFileSize()
           
static void main(java.lang.String[] args)
          A main method to test to the class to some degree.
protected  MergeTemplate processArray(char[] array, int beginPos, int endPos)
          Create a MergeTemplate from the subarray from beginPos to endPos-1.
 MergeTemplate processFile(java.io.File file)
          Creates an inputstream based on the File object.
 MergeTemplate processFile(java.lang.String filename)
          This method will create the File object to be used, given a String which is presumably the filename.
 MergeTemplate processStream(java.io.InputStream is)
          On the presumption (uh-oh) that the stream sizes involved here will be relatively small (less than 1MB), we'll read the entire stream into a StringBuffer to simplify our manipulation of the data.
protected  char[] readStream(java.io.InputStream is)
          Build a character array of all available data in the InputStream.
protected  java.lang.StringBuffer readURL(java.net.URL url)
          Read a full stream from a given URL, until the stream is empty or an exception occurs.
 void setBeginTag(java.lang.String tag)
           
 void setEndTag(java.lang.String tag)
           
 void setImportTag(java.lang.String tag)
           
 void setTokens(InformationToken[] _tokens)
          Create a vector based on the InformationToken values given in tokens.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debugOn

public static boolean debugOn
Constructor Detail

TemplatePreprocessor

public TemplatePreprocessor()
Method Detail

dout

protected void dout(java.lang.String s)
Method for debuggin purposes only. If the public data member "debugOn" is true, then it'll print the passed string w/ a cute li'l prefix.


eatWhitespace

protected int eatWhitespace(char[] array,
                            int index)
Return the next non-whitespace character (not a space, tab, newline, for formfeed).


findEndTag

protected int findEndTag(char[] array,
                         int beginPos,
                         int endPos)
Find the next endTag, after matching nested pairs of beginTag, endTag. If beginPos is at the beginning of a beginTag, that begin tag is ignored for the purposes of nested-pair matching.


findFirstOf

protected int findFirstOf(char[] array,
                          java.lang.String findMe,
                          int beginIndex,
                          int endIndex)
Provide a custom method to find substrings in a char array. This is to save the memory costs of building a String and doing it that way. This method can also expect the tagPrefix substring to preceed any of these strings. The returned index is the index of the first character of "findMe". Use of the custom for(int j...) {...} loop instead of use of findMe.equals(...) is a good optimization. It makes (end-begin-findMe.length())*findMe.length() fewer array comparisons.


getBeginTag

public java.lang.String getBeginTag()

getEndTag

public java.lang.String getEndTag()

getImportTag

public java.lang.String getImportTag()

getTokens

public InformationToken[] getTokens()

importURLs

public char[] importURLs(char[] array)
(attempt to) import files indicated by the importTag. The importTag expects a single argument, either a word or a "-delimited string. Any number of whitespace characters can be present between the importTag and the argument. Using the default importTag value "importURL", a line of the source file may look like this: importURL "c:\program files\common files\vendor.htm" \n The imported file has importURL run on it, too, so inclusions can be recursive. If there's a problem reading the indicated file due to some problem with the argument or whatever, a warning will be issued and nothing will be substituted. That is, the tag and argument will be eaten and be replaced by the empty string "".


lastProcessingFileSize

public int lastProcessingFileSize()

main

public static void main(java.lang.String[] args)
A main method to test to the class to some degree.


processArray

protected MergeTemplate processArray(char[] array,
                                     int beginPos,
                                     int endPos)
                              throws java.io.IOException
Create a MergeTemplate from the subarray from beginPos to endPos-1. The data will presented in the form ... text section #0 ... [ beginTag ... sub-template #1 text ... endTag ] ... text section #1 ... [ beginTag ... sub-template #2 text ... endTag ] ... text section #2 ... [ beginTag ... sub-template #n text ... endTag ] ... text section #n... Each sub-template will have the above format. This method processes this information to yield a structured MergeTemplate. Here's a sample of the template structure, where n = 3 and only sub-template #2 has an additional sub-template: template [ prefix=section#0, suffix = null ] | `----> template [ prefix=sub-template#1 text, suffix = null ] `----> template [ prefix=section#1 text, suffix = null ] `----> template [ prefix=sub-template#2 text, suffix = null ] | | | `----> template [prefix=sub-template#2a text, suffix = null] `----> template [ prefix=section#2 text, suffix = null ] `----> template [ prefix=sub-template#3 text, suffix = null ] `----> template [ prefix=section#3 text, suffix = null ]

java.io.IOException

processFile

public MergeTemplate processFile(java.io.File file)
                          throws java.io.IOException,
                                 java.lang.SecurityException
Creates an inputstream based on the File object. A simple fileObject can be created like so: File file = new File("c:\autoexec.bat");

java.io.IOException
java.lang.SecurityException

processFile

public MergeTemplate processFile(java.lang.String filename)
                          throws java.io.IOException,
                                 java.lang.SecurityException
This method will create the File object to be used, given a String which is presumably the filename.

java.io.IOException
java.lang.SecurityException

processStream

public MergeTemplate processStream(java.io.InputStream is)
                            throws java.io.IOException
On the presumption (uh-oh) that the stream sizes involved here will be relatively small (less than 1MB), we'll read the entire stream into a StringBuffer to simplify our manipulation of the data.

java.io.IOException

readStream

protected char[] readStream(java.io.InputStream is)
                     throws java.io.IOException
Build a character array of all available data in the InputStream. If the InputStream is blocking and the data always gets slurped by someone else between isAvailable() and read(), or if there's a continuous stream of data coming from InputStream, then this method could continue until memory runs out. These scenarios are not worth accomodating since the InputStream is gonna be a file, so there will be few IO issues.

java.io.IOException

readURL

protected java.lang.StringBuffer readURL(java.net.URL url)
                                  throws java.io.IOException
Read a full stream from a given URL, until the stream is empty or an exception occurs.

java.io.IOException

setBeginTag

public void setBeginTag(java.lang.String tag)

setEndTag

public void setEndTag(java.lang.String tag)

setImportTag

public void setImportTag(java.lang.String tag)

setTokens

public void setTokens(InformationToken[] _tokens)
Create a vector based on the InformationToken values given in tokens.