User: vharcq  
  Date: 02/04/04 14:56:03

  Modified:    core/src/xdoclet/template TemplateEngine.java
  Log:
  Replace "..." by ' ...' and use StringBuffer for performance
  
  Revision  Changes    Path
  1.24      +594 -499  xdoclet/core/src/xdoclet/template/TemplateEngine.java
  
  Index: TemplateEngine.java
  ===================================================================
  RCS file: /cvsroot/xdoclet/xdoclet/core/src/xdoclet/template/TemplateEngine.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -w -r1.23 -r1.24
  --- TemplateEngine.java       4 Apr 2002 01:03:10 -0000       1.23
  +++ TemplateEngine.java       4 Apr 2002 22:56:03 -0000       1.24
  @@ -35,10 +35,35 @@
    * @author Ara Abrahamian ([EMAIL PROTECTED])
    * @author Dmitri Colebatch ([EMAIL PROTECTED])
    * @created July 14, 2001
  - * @version $Revision: 1.23 $
  + * @version   $Revision: 1.24 $
    * @see #generate(java.lang.String)
    */
  -public class TemplateEngine {
  +public class TemplateEngine
  +{
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     public final static String TAG_MAPPINGS_FILE = "/tagmappings.properties";
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     protected static String XDOCLET_PREFIX;
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     protected static String XDOCLET_HEAD;
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     protected static String XDOCLET_TAIL;
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     protected static int XDOCLET_HEAD_LEN;
  +     /**
  +      * @todo-javadoc   Describe the field
  +      */
  +     protected static int XDOCLET_TAIL_LEN;
   
        /**
         * The PrintWriter used for outputing the generated stuff. {@link
  @@ -68,31 +93,18 @@
         * The map of tag mappings. String => TemplateTagHandler
         */
        private Map tagMappings = new HashMap();
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     public final static String TAG_MAPPINGS_FILE = "/tagmappings.properties";
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     protected static String XDOCLET_PREFIX;
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     protected static String XDOCLET_HEAD;
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     protected static String XDOCLET_TAIL;
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     protected static int XDOCLET_HEAD_LEN;
  -     /**
  -      * @todo-javadoc Describe the field
  -      */
  -     protected static int XDOCLET_TAIL_LEN;
   
  +     static
  +     {
  +             XDOCLET_PREFIX = "XD";
  +
  +             // migration path from "XDoclet:" to "XDt"
  +
  +             XDOCLET_HEAD = "<" + XDOCLET_PREFIX;
  +             XDOCLET_TAIL = "</" + XDOCLET_PREFIX;
  +             XDOCLET_HEAD_LEN = XDOCLET_HEAD.length();
  +             XDOCLET_TAIL_LEN = XDOCLET_TAIL.length();
  +     }
   
        /**
         * Initialize the Template Engine. Reads the XDoclet properties file, and loads
  @@ -100,163 +112,164 @@
         *
         * @exception TemplateException Description of Exception
         */
  -     public TemplateEngine() throws TemplateException {
  +     public TemplateEngine() throws TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, "TemplateEngine");
   
  -             try {
  +             try
  +             {
                        Properties tag_handler_mappings = new Properties();
   
                        
tag_handler_mappings.load(getClass().getResourceAsStream(TAG_MAPPINGS_FILE));
   
                        Iterator tag_handler_mapping_names = 
tag_handler_mappings.keySet().iterator();
   
  -                     while (tag_handler_mapping_names.hasNext()) {
  +                     while( tag_handler_mapping_names.hasNext() )
  +                     {
                                String prefix = 
(String)tag_handler_mapping_names.next();
                                String className = 
(String)tag_handler_mappings.get(prefix);
   
  -                             try {
  +                             try
  +                             {
                                        TemplateTagHandler tag_handler = 
(TemplateTagHandler)Class.forName(className).newInstance();
   
                                        tag_handler.setEngine(this);
  -                                     if (this instanceof TemplateParser) {
  +                                     if( this instanceof TemplateParser )
  +                                     {
                                                
tag_handler.setParser((TemplateParser)this);
                                        }
                                        //tag_handler.setContext( context );
   
                                        tagMappings.put(prefix, tag_handler);
  -                             } catch (NoClassDefFoundError e) {
  +                             }
  +                             catch( NoClassDefFoundError e )
  +                             {
                                        
cat.warn(Translator.getString("template_noclassdeffounderror", new String[]{className, 
e.getMessage()}));
  -                             } catch (ClassNotFoundException e) {
  +                             }
  +                             catch( ClassNotFoundException e )
  +                             {
                                        
cat.warn(Translator.getString("template_classnotfoundexception", new 
String[]{className, e.getMessage()}));
  -                             } catch (InstantiationException e) {
  +                             }
  +                             catch( InstantiationException e )
  +                             {
                                        
cat.warn(Translator.getString("template_instantiationexception", new 
String[]{className, e.getMessage()}));
  -                             } catch (IllegalAccessException e) {
  +                             }
  +                             catch( IllegalAccessException e )
  +                             {
                                        
cat.warn(Translator.getString("template_illegalaccessexception", new 
String[]{className, e.getMessage()}));
                                }
                        }
  -             } catch (IOException ioe) {
  +             }
  +             catch( IOException ioe )
  +             {
                        throw new 
TemplateException(Translator.getString("template_ioexception", new 
String[]{ioe.getMessage()}));
                }
        }
   
  -
        /**
  -      * Sets the Writer attribute of the TemplateEngine object
  -      *
  -      * @param out The new Writer value
  -      */
  -     public void setWriter(PrettyPrintWriter out) {
  -             this.out = out;
  -     }
  -
  -
  -     /**
  -      * Sets the CurrentLineNum attribute of the TemplateEngine object
  +      * Skips whitespaces, starting from index i till the first non-whitespace
  +      * character or end of template and returns the new index.
         *
  -      * @param currentLineNum The new CurrentLineNum value
  +      * @param template  Description of Parameter
  +      * @param i         Description of Parameter
  +      * @return          Description of the Returned Value
         */
  -     public void setCurrentLineNum(int currentLineNum) {
  -             this.currentLineNum = currentLineNum;
  +     public static int skipWhitespace( String template, int i )
  +     {
  +             while( i < template.length() && Character.isWhitespace( 
template.charAt( i ) ) )
  +             {
  +                     i++;
        }
   
  -
  -     /**
  -      * A config parameter settable from Ant build file. It sets the current
  -      * template file to templateURL, so thereafter the new template file is used.
  -      *
  -      * @param templateURL The new TemplateFile value
  -      * @see #getTemplateURL()
  -      */
  -     public void setTemplateURL(URL templateURL) {
  -             this.templateURL = templateURL;
  +             return i;
        }
   
  -
        /**
  -      * Sets the Output attribute of the TemplateEngine object
  +      * Loops over the template content till reaching till_index index and returns
  +      * the number of lines it has encountered.
         *
  -      * @param output The new Output value
  +      * @param template    Description of Parameter
  +      * @param till_index  Description of Parameter
  +      * @return            The LineNumber value
         */
  -     public void setOutput(File output) {
  -             this.output = output;
  -     }
  -
  +     protected static int getLineNumber( String template, int till_index )
  +     {
  +             int NL_LEN = PrettyPrintWriter.LINE_SEPARATOR.length();
  +             int index = 0;
  +             int line_num = 0;
   
  -     /**
  -      * Sets the TagHandlerFor attribute of the TemplateEngine object
  -      *
  -      * @param prefix The new TagHandlerFor value
  -      * @param tag_handler The new TagHandlerFor value
  -      * @exception TemplateException Describe the exception
  -      * @todo-javadoc Write javadocs for exception
  -      */
  -     public void setTagHandlerFor(String prefix, TemplateTagHandler tag_handler) 
throws TemplateException {
  -             Category cat = Log.getCategory(TemplateEngine.class, 
"setTagHandlerFor");
  +             do
  +             {
  +                     index = template.indexOf( PrettyPrintWriter.LINE_SEPARATOR, 
index );
   
  -             if (cat.isDebugEnabled()) {
  -                     cat.debug("prefix=" + prefix);
  -                     cat.debug("tag_handler=" + tag_handler);
  +                     if( index != -1 )
  +                     {
  +                             line_num++;
  +                             index += NL_LEN;
                }
  -
  -             tag_handler.setEngine(this);
  -             if (this instanceof TemplateParser) {
  -                     tag_handler.setParser((TemplateParser)this);
  +                     else
  +                     {
  +                             break;
                }
  +             }while ( index < till_index );
   
  -             tagMappings.put(prefix, tag_handler);
  +             return line_num;
        }
   
  -
        /**
         * Returns current template URL.
         *
         * @return The TemplateURL value
         * @see #setTemplateURL(java.net.URL)
         */
  -     public URL getTemplateURL() {
  +     public URL getTemplateURL()
  +     {
                return templateURL;
        }
   
  -
        /**
         * Gets the Output attribute of the TemplateEngine object
         *
         * @return The Output value
         */
  -     public File getOutput() {
  +     public File getOutput()
  +     {
                return output;
        }
   
  -
        /**
         * Gets the CurrentLineNum attribute of the TemplateEngine object
         *
         * @return The CurrentLineNum value
         */
  -     public int getCurrentLineNum() {
  +     public int getCurrentLineNum()
  +     {
                return currentLineNum;
        }
   
  -
        /**
         * Get the tag handler for the prefix.
         *
         * @param prefix The prefix that the tag handler is mapped to
  -      * @return The TemplateTagHandler for the specified prefix. ALways non-null.
  +      * @return                    The TemplateTagHandler for the specified prefix.
  +      *      ALways non-null.
         * @throws TemplateException If there is no tag handler class for the prefix
         *      specified.
         */
  -     public TemplateTagHandler getTagHandlerFor(String prefix) throws 
TemplateException {
  +     public TemplateTagHandler getTagHandlerFor( String prefix ) throws 
TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, 
"getTagHandlerFor");
   
                TemplateTagHandler tag_handler = 
(TemplateTagHandler)tagMappings.get(prefix);
   
  -             if (cat.isDebugEnabled()) {
  +             if( cat.isDebugEnabled() )
  +             {
                        cat.debug("prefix=" + prefix);
                        cat.debug("tag_handler=" + tag_handler);
                }
   
  -             if (tag_handler == null) {
  +             if( tag_handler == null )
  +             {
                        String msg = Translator.getString("template_no_taghandler", 
new String[]{prefix});
   
                        cat.error(msg);
  @@ -266,6 +279,74 @@
                return tag_handler;
        }
   
  +     /**
  +      * Sets the Writer attribute of the TemplateEngine object
  +      *
  +      * @param out  The new Writer value
  +      */
  +     public void setWriter( PrettyPrintWriter out )
  +     {
  +             this.out = out;
  +     }
  +
  +     /**
  +      * Sets the CurrentLineNum attribute of the TemplateEngine object
  +      *
  +      * @param currentLineNum  The new CurrentLineNum value
  +      */
  +     public void setCurrentLineNum( int currentLineNum )
  +     {
  +             this.currentLineNum = currentLineNum;
  +     }
  +
  +     /**
  +      * A config parameter settable from Ant build file. It sets the current
  +      * template file to templateURL, so thereafter the new template file is used.
  +      *
  +      * @param templateURL  The new TemplateFile value
  +      * @see                #getTemplateURL()
  +      */
  +     public void setTemplateURL( URL templateURL )
  +     {
  +             this.templateURL = templateURL;
  +     }
  +
  +     /**
  +      * Sets the Output attribute of the TemplateEngine object
  +      *
  +      * @param output  The new Output value
  +      */
  +     public void setOutput( File output )
  +     {
  +             this.output = output;
  +     }
  +
  +     /**
  +      * Sets the TagHandlerFor attribute of the TemplateEngine object
  +      *
  +      * @param prefix                 The new TagHandlerFor value
  +      * @param tag_handler            The new TagHandlerFor value
  +      * @exception TemplateException  Describe the exception
  +      * @todo-javadoc                 Write javadocs for exception
  +      */
  +     public void setTagHandlerFor( String prefix, TemplateTagHandler tag_handler ) 
throws TemplateException
  +     {
  +             Category cat = Log.getCategory( TemplateEngine.class, 
"setTagHandlerFor" );
  +
  +             if( cat.isDebugEnabled() )
  +             {
  +                     cat.debug( "prefix=" + prefix );
  +                     cat.debug( "tag_handler=" + tag_handler );
  +             }
  +
  +             tag_handler.setEngine( this );
  +             if( this instanceof TemplateParser )
  +             {
  +                     tag_handler.setParser( ( TemplateParser ) this );
  +             }
  +
  +             tagMappings.put( prefix, tag_handler );
  +     }
   
        /**
         * The main template parsing/processing/running logic. It searches for
  @@ -288,23 +369,27 @@
         * @exception TemplateException Description of Exception
         * @see #outputOf(java.lang.String)
         */
  -     public void generate(final String template) throws TemplateException {
  +     public void generate( final String template ) throws TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, "generate");
                int index = 0;
                int prev_index = 0;
                int i = 0;
   
  -             while (true) {
  +             while( true )
  +             {
                        // Look for the next tag that we haven't yet handled.
                        index = template.indexOf(XDOCLET_HEAD, prev_index);
   
  -                     if (index == -1) {
  +                     if( index == -1 )
  +                     {
                                // we didn't find a tag.  print the rest of the 
template and finish
   
                                out.print(template.substring(prev_index));
                                break;
                        }
  -                     else {
  +                     else
  +                     {
                                // we found a tag.  print the template up to the tag 
start, and then handle the tag
   
                                out.print(template.substring(prev_index, index));
  @@ -315,7 +400,6 @@
                out.flush();
        }
   
  -
        /**
         * Calls generate() of the specified template content but instead of outputing
         * it to the generated file, it returns the generated content. It's useful for
  @@ -330,7 +414,8 @@
         * @exception TemplateException Description of Exception
         * @see #generate(java.lang.String)
         */
  -     public String outputOf(String template) throws TemplateException {
  +     public String outputOf( String template ) throws TemplateException
  +     {
                PrettyPrintWriter oldOut = out;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
   
  @@ -342,7 +427,6 @@
                return new String(bout.toByteArray());
        }
   
  -
        /**
         * Describe what the method does
         *
  @@ -350,44 +434,51 @@
         * @todo-javadoc Write javadocs for method
         * @todo-javadoc Write javadocs for method parameter
         */
  -     public void print(String output) {
  -             if (out != null) {
  +     public void print( String output )
  +     {
  +             if( out != null )
  +             {
                        out.print(output);
                }
        }
   
  -
        /**
         * A utility method used for generating the dest_file based on template_file
         * template file.
         *
         * @exception TemplateException Description of Exception
         */
  -     public void start() throws TemplateException {
  +     public void start() throws TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, "start");
   
                output.getParentFile().mkdirs();
   
                String content = FileManager.getURLContent(getTemplateURL());
   
  -             if (content != null) {
  +             if( content != null )
  +             {
                        cat.debug("content.length()=" + content.length());
   
  -                     try {
  +                     try
  +                     {
                                PrettyPrintWriter out = new PrettyPrintWriter(new 
BufferedWriter(new FileWriter(output)));
   
                                setWriter(out);
                                setCurrentLineNum(0);
                                generate(content);
                                out.close();
  -                     } catch (IOException ex) {
  +                     }
  +                     catch( IOException ex )
  +                     {
                                String msg = 
Translator.getString("template_error_writing_output", new String[]{output.toString()});
   
                                cat.error(msg, ex);
                                throw new TemplateException(ex, msg);
                        }
                }
  -             else {
  +             else
  +             {
                        String msg = Translator.getString("template_not_found", new 
String[]{getTemplateURL().toString()});
   
                        cat.error(msg);
  @@ -395,7 +486,6 @@
                }
        }
   
  -
        /**
         * Handle the tag that starts at <code>index</code> in the 
<code>template</code>
         * provided.
  @@ -405,7 +495,8 @@
         * @return The index where the tag finished.
         * @exception TemplateException Description of Exception
         */
  -     protected int handleTag(int index, final String template) throws 
TemplateException {
  +     protected int handleTag( int index, final String template ) throws 
TemplateException
  +     {
                // the point in the template where we are.  starts at the end of the 
tag head.
                int i = index + XDOCLET_HEAD_LEN;
   
  @@ -429,25 +520,26 @@
                // extract the attributes from the tag, and determine if it is a block 
or content tag
                i = extractAttributes(tagContext, template, i, attributes);
   
  -             if (tagContext.isBlock()) {
  +             if( tagContext.isBlock() )
  +             {
                        i = handleBlockTag(i, template, cmd.toString(), attributes);
                }
  -             else {
  +             else
  +             {
                        invokeContentMethod(cmd.toString(), attributes, template, i);
                }
   
                return i;
        }
   
  -
        /**
         * Invokes content tag implementation method named cmd. It first tries with
         * parameters params1, if not successful tries param2. This is used for cases
         * where it's not obvious whether the tag implementation method expects a
         * Properties object or no parameter at all (the tag takes no attributes).
         *
  -      * @param cmd The command to be executed. Everything after the 
<code>&lt;XDoclet:</code>
  -      *      in the template.
  +      * @param cmd                    The command to be executed. Everything after
  +      *      the <code>&lt;XDoclet:</code> in the template.
         * @param params1 Description of Parameter
         * @param params2 Description of Parameter
         * @param template Description of Parameter
  @@ -457,7 +549,8 @@
         * @see 
#invokeBlockMethod(java.lang.String,java.lang.String,java.util.Properties,java.lang.String,int)
         * @see 
#invokeContentMethod(java.lang.String,java.util.Properties,java.lang.String,int)
         */
  -     protected Object invokeMethod(String cmd, Object[] params1, Object[] params2, 
String template, int i) throws TemplateException {
  +     protected Object invokeMethod( String cmd, Object[] params1, Object[] params2, 
String template, int i ) throws TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, "invokeMethod");
   
                /*
  @@ -473,54 +566,74 @@
                TemplateTagHandler cmdImplProvider = 
getTagHandlerFor(prefix.substring(1));
                String className = cmdImplProvider.getClass().getName();
   
  -             try {
  +             try
  +             {
                        Class[] param_types = new Class[params1.length];
   
  -                     for (int j = 0; j < params1.length; j++) {
  +                     for( int j = 0; j < params1.length; j++ )
  +                     {
                                param_types[j] = params1[j].getClass();
                        }
   
                        Method m = cmdImplProvider.getClass().getMethod(methodName, 
param_types);
   
                        return invoke(m, cmdImplProvider, params1);
  -             } catch (InvocationTargetException e) {
  -                     if (e.getTargetException() instanceof TemplateException) {
  +             }
  +             catch( InvocationTargetException e )
  +             {
  +                     if( e.getTargetException() instanceof TemplateException )
  +                     {
                                throw (TemplateException)e.getTargetException();
                        }
  -                     else {
  +                     else
  +                     {
                                cat.error("Invoking method failed: " + className + "." 
+ methodName + ", line=" + getLineNumber(template, i) + " of template file: " + 
getTemplateURL(), e);
                                throw new 
TemplateException(Translator.getString("template_invoke_method_failed",
                                                new String[]{className, methodName, 
Integer.toString(getLineNumber(template, i)), getTemplateURL().toString(), 
e.getMessage()}));
                        }
  -             } catch (IllegalAccessException e) {
  +             }
  +             catch( IllegalAccessException e )
  +             {
                        cat.error("Invoking method failed: " + className + "." + 
methodName + ", line=" + getLineNumber(template, i) + " of template file: " + 
getTemplateURL(), e);
                        throw new 
TemplateException(Translator.getString("template_invoke_method_failed",
                                        new String[]{className, methodName, 
Integer.toString(getLineNumber(template, i)), getTemplateURL().toString(), 
e.getMessage()}));
  -             } catch (NoSuchMethodException e) {
  +             }
  +             catch( NoSuchMethodException e )
  +             {
                        Class[] param_types = new Class[params2.length];
   
  -                     try {
  -                             for (int j = 0; j < params2.length; j++) {
  +                     try
  +                     {
  +                             for( int j = 0; j < params2.length; j++ )
  +                             {
                                        param_types[j] = params2[j].getClass();
                                }
   
                                Method m = 
cmdImplProvider.getClass().getMethod(methodName, param_types);
   
                                return invoke(m, cmdImplProvider, params2);
  -                     } catch (NoSuchMethodException nsme) {
  +                     }
  +                     catch( NoSuchMethodException nsme )
  +                     {
                                cat.error("Could not find method " + className + "." + 
methodName + " in class " + cmdImplProvider.getClass().getName());
                                throw new 
TemplateException(Translator.getString("template_no_such_method",
                                                new String[]{methodName, 
cmdImplProvider.getClass().getName(), nsme.getMessage()}));
  -                     } catch (InvocationTargetException e2) {
  -                             if (e2.getTargetException() instanceof 
TemplateException) {
  +                     }
  +                     catch( InvocationTargetException e2 )
  +                     {
  +                             if( e2.getTargetException() instanceof 
TemplateException )
  +                             {
                                        throw 
(TemplateException)e2.getTargetException();
                                }
  -                             else {
  +                             else
  +                             {
                                        cat.error("Invoking method failed: " + 
className + "." + methodName + ", line=" + getLineNumber(template, i) + " of template 
file: " + getTemplateURL(), e2);
                                        throw new 
TemplateException(Translator.getString("template_invoke_method_failed",
                                                        new String[]{className, 
methodName, Integer.toString(getLineNumber(template, i)), getTemplateURL().toString(), 
e2.getMessage()}));
                                }
  -                     } catch (IllegalAccessException e2) {
  +                     }
  +                     catch( IllegalAccessException e2 )
  +                     {
                                cat.error("Invoking method failed: " + className + "." 
+ methodName + ", line=" + getLineNumber(template, i) + " of template file: " + 
getTemplateURL(), e2);
                                throw new 
TemplateException(Translator.getString("template_invoke_method_failed",
                                                new String[]{className, methodName, 
Integer.toString(getLineNumber(template, i)), getTemplateURL().toString(), 
e2.getMessage()}));
  @@ -528,7 +641,6 @@
                }
        }
   
  -
        /**
         * Invokes content tag implementation method named cmd with the specified set
         * of attributes. If attributes Properties object is not empty it tries to find
  @@ -542,29 +654,32 @@
         * @exception TemplateException Description of Exception
         * @see 
#invokeMethod(java.lang.String,java.lang.Object[],java.lang.Object[],java.lang.String,int)
         */
  -     protected void invokeContentMethod(String cmd, Properties attributes, String 
template, int i) throws TemplateException {
  +     protected void invokeContentMethod( String cmd, Properties attributes, String 
template, int i ) throws TemplateException
  +     {
                Object[] params1 = null;
                Object[] params2 = null;
   
                //probable conditions
   
  -             if (attributes.size() > 0) {
  +             if( attributes.size() > 0 )
  +             {
                        params1 = new Object[]{attributes};
                        params2 = new Object[]{};
                }
  -             else {
  +             else
  +             {
                        params1 = new Object[]{};
                        params2 = new Object[]{attributes};
                }
   
                String result = (String)invokeMethod(cmd, params1, params2, template, 
i);
   
  -             if (result != null && out != null) {
  +             if( result != null && out != null )
  +             {
                        out.print(result);
                }
        }
   
  -
        /**
         * Describe what the method does
         *
  @@ -585,11 +700,11 @@
         * @todo-javadoc Write javadocs for exception
         */
        protected Object invoke(Method m, Object cmdImplProvider, Object[] params1)
  -                      throws InvocationTargetException, IllegalAccessException, 
TemplateException {
  +              throws InvocationTargetException, IllegalAccessException, 
TemplateException
  +     {
                return m.invoke(cmdImplProvider, params1);
        }
   
  -
        /**
         * Extract the attributes from the tag and return the index that the tag
         *
  @@ -600,15 +715,16 @@
         * @return Description of the Returned Value
         * @exception TemplateException Description of Exception
         */
  -     private int extractAttributes(TagContext tagContext, final String template, 
int i, Properties attributes) throws TemplateException {
  -             while (tagContext.hasMoreAttributes()) {
  +     private int extractAttributes( TagContext tagContext, final String template, 
int i, Properties attributes ) throws TemplateException
  +     {
  +             while( tagContext.hasMoreAttributes() )
  +             {
                        i = extractNextAttribute(template, i, tagContext, attributes);
                }
   
                return i;
        }
   
  -
        /**
         * Extract the name of the tag starting at index <code>i</code> from the
         * specified <code>template</code> .
  @@ -618,8 +734,10 @@
         * @param cmd The StringBuffer to put the tag name in.
         * @return The index that the tag name finished at in the template.
         */
  -     private int extractTagName(final String template, int index, StringBuffer cmd) 
{
  -             while ((!Character.isWhitespace(template.charAt(index))) && 
template.charAt(index) != '>' && template.charAt(index) != '/') {
  +     private int extractTagName( final String template, int index, StringBuffer cmd 
)
  +     {
  +             while( ( !Character.isWhitespace( template.charAt( index ) ) ) && 
template.charAt( index ) != '>' && template.charAt( index ) != '/' )
  +             {
                        cmd.append(template.charAt(index));
                        index++;
                }
  @@ -627,60 +745,68 @@
                return index;
        }
   
  -
        /**
         * Extract the next attribute from the <code>template</code> provided, starting
         * at the <code>index</code> specified.
         *
         * @param template The template to extract the attribute from.
         * @param index The index to start looking for the attribute.
  -      * @param tagContext The TagContext - is the tag block/content, are there any
  -      *      more attributes?
  -      * @param attributes The attributes collection to add the next attribute to.
  +      * @param tagContext             The TagContext - is the tag block/content, are
  +      *      there any more attributes?
  +      * @param attributes             The attributes collection to add the next
  +      *      attribute to.
         * @return The index that the next attribute finished at.
         * @exception TemplateException Description of Exception
         */
  -     private int extractNextAttribute(final String template, int index, TagContext 
tagContext, Properties attributes) throws TemplateException {
  +     private int extractNextAttribute( final String template, int index, TagContext 
tagContext, Properties attributes ) throws TemplateException
  +     {
                Category cat = Log.getCategory(TemplateEngine.class, 
"extractNextAttribute");
   
  -             String attr_name = "";
  -             String attr_value = "";
  +             StringBuffer attr_name = new StringBuffer();
  +             StringBuffer attr_value = new StringBuffer();
                char quote_char = '"';
   
                //read attribute name
  -             while (template.charAt(index) != '=' && 
(!Character.isWhitespace(template.charAt(index)))) {
  -                     attr_name += template.charAt(index);
  +             while( template.charAt( index ) != '=' && ( !Character.isWhitespace( 
template.charAt( index ) ) ) )
  +             {
  +                     attr_name.append( template.charAt( index ) );
                        index++;
                }
   
                index = skipWhitespace(template, index);
   
                //skip = sign
  -             if (template.charAt(index) == '=') {
  +             if( template.charAt( index ) == '=' )
  +             {
                        index++;
                }
  -             else {
  +             else
  +             {
                        throw new 
TemplateException(Translator.getString("template_equals_expected", new 
String[]{Integer.toString(getLineNumber(template, index)), 
getTemplateURL().toString()}));
                }
   
                index = skipWhitespace(template, index);
   
                //skip " sign
  -             if (template.charAt(index) == '"') {
  +             if( template.charAt( index ) == '"' )
  +             {
                        index++;
                        quote_char = '"';
                }
  -             else if (template.charAt(index) == '\'') {
  +             else if( template.charAt( index ) == '\'' )
  +             {
                        index++;
                        quote_char = '\'';
                }
  -             else {
  +             else
  +             {
                        throw new 
TemplateException(Translator.getString("template_quote_expected", new 
String[]{Integer.toString(getLineNumber(template, index)), 
getTemplateURL().toString()}));
                }
   
                //read attribute value
  -             while (template.charAt(index) != quote_char) {
  -                     attr_value += template.charAt(index);
  +             while( template.charAt( index ) != quote_char )
  +             {
  +                     attr_value.append( template.charAt( index ) );
                        index++;
                }
   
  @@ -688,40 +814,45 @@
                index++;
                tagContext.setHasMoreAttributes(true);
   
  -             if (attr_value.indexOf(XDOCLET_HEAD) != -1) {
  -                     attr_value = outputOf(attr_value);
  +             if( attr_value.toString().indexOf( XDOCLET_HEAD ) != -1 )
  +             {
  +                     attr_value = new StringBuffer( outputOf( attr_value.toString() 
) );
                }
   
                index = skipWhitespace(template, index);
   
  -             if (template.charAt(index) == '>') {
  +             if( template.charAt( index ) == '>' )
  +             {
                        index++;
                        tagContext.setBlock(true);
                        tagContext.setHasMoreAttributes(false);
   
                        //no more attributes
                }
  -             else if (template.charAt(index) == '/') {
  +             else if( template.charAt( index ) == '/' )
  +             {
                        index++;
   
  -                     if (template.charAt(index) == '>') {
  +                     if( template.charAt( index ) == '>' )
  +                     {
                                index++;
                                tagContext.setBlock(false);
                                tagContext.setHasMoreAttributes(false);
   
                                //no more attributes
                        }
  -                     else {
  +                     else
  +                     {
                                throw new 
TemplateException(Translator.getString("template_gt_expected", new 
String[]{Integer.toString(getLineNumber(template, index)), 
getTemplateURL().toString()}));
                        }
                }
   
  +             if( cat.isDebugEnabled() )
                cat.debug("Attribute " + attr_name + "=" + attr_value);
  -             attributes.setProperty(attr_name, attr_value);
  +             attributes.setProperty( attr_name.toString(), attr_value.toString() );
                return index;
        }
   
  -
        /**
         * Do an initial parse of the tag at the <code>index</code> specified in the
         * <code>template</code> provided. Look to see if the tag has attributes,
  @@ -733,66 +864,78 @@
         * @return Description of the Returned Value
         * @exception TemplateException Description of Exception
         */
  -     private int doInitialTagParse(final String template, int index, TagContext 
tagContext) throws TemplateException {
  -             while (true) {
  -                     if (template.charAt(index) == '>') {
  +     private int doInitialTagParse( final String template, int index, TagContext 
tagContext ) throws TemplateException
  +     {
  +             while( true )
  +             {
  +                     if( template.charAt( index ) == '>' )
  +                     {
                                index++;
                                tagContext.setHasMoreAttributes(false);
                                tagContext.setBlock(true);
                                return index;
                        }
  -                     else if (template.charAt(index) == '/') {
  +                     else if( template.charAt( index ) == '/' )
  +                     {
                                index++;
   
  -                             if (template.charAt(index) == '>') {
  +                             if( template.charAt( index ) == '>' )
  +                             {
                                        index++;
                                        tagContext.setHasMoreAttributes(false);
                                        tagContext.setBlock(false);
                                        return index;
                                }
  -                             else {
  +                             else
  +                             {
                                        throw new 
TemplateException(Translator.getString("template_gt_expected", new 
String[]{Integer.toString(getLineNumber(template, index)), 
getTemplateURL().toString()}));
                                }
                        }
  -                     else if (Character.isWhitespace(template.charAt(index))) {
  +                     else if( Character.isWhitespace( template.charAt( index ) ) )
  +                     {
                                index = skipWhitespace(template, index);
                                continue;
                        }
  -                     else {
  +                     else
  +                     {
                                tagContext.setHasMoreAttributes(true);
                                return index;
                        }
                }
        }
   
  -
        /**
         * Handle the block tag starting at the <code>index</code> specified in the
         * <code>template</code> provided.
         *
         * @param index The start of the block tag's contents.
         * @param template The template the block tag is contained in.
  -      * @param cmd The name of the block tag, without XDOCLET_HEAD
  +      * @param cmd                    The name of the block tag, without
  +      *      XDOCLET_HEAD
         * @param attributes The attributes in the tag.
         * @return The index that the block tag finishes at.
         * @exception TemplateException Description of Exception
         */
  -     private int handleBlockTag(int index, String template, String cmd, Properties 
attributes) throws TemplateException {
  +     private int handleBlockTag( int index, String template, String cmd, Properties 
attributes ) throws TemplateException
  +     {
                int open_nested_elem_count = 1;
                String new_body = null;
                int body_start_index = index;
                int body_end_index = -1;
   
  -             while (index < template.length()) {
  +             while( index < template.length() )
  +             {
                        int from_index = index;
   
  -                     body_end_index = template.indexOf(XDOCLET_TAIL + cmd, index);
  +                     body_end_index = template.indexOf( new StringBuffer( 
XDOCLET_TAIL ).append( cmd ).toString(), index );
   
  -                     if (body_end_index == -1) {
  +                     if( body_end_index == -1 )
  +                     {
                                throw new 
TemplateException(Translator.getString("template_close_tag_missing",
  -                                             new String[]{XDOCLET_TAIL + cmd + ">", 
Integer.toString(getLineNumber(template, index)), getTemplateURL().toString()}));
  +                                     new String[]{new StringBuffer( XDOCLET_TAIL 
).append( cmd ).append( '>' ).toString(), Integer.toString( getLineNumber( template, 
index ) ), getTemplateURL().toString()} ) );
                        }
  -                     else {
  +                     else
  +                     {
                                open_nested_elem_count--;
                        }
   
  @@ -805,25 +948,31 @@
                        // trailing >
                        index++;
   
  +                     StringBuffer xdocletPrefixPlusCmd = new StringBuffer( 
XDOCLET_PREFIX ).append( cmd );
  +
                        // XXX: I cant remember why I did this like that (using prefix 
instead of head), it could
                        // probably be looked at, but there are other things to do 
atm, and it works.  dim 13-Oct-01
  -                     int nested_start_index = template.indexOf(XDOCLET_PREFIX + 
cmd, from_index);
  +                     int nested_start_index = template.indexOf( 
xdocletPrefixPlusCmd.toString(), from_index );
   
                        // has nested elements with the same name, need to loop here 
for multiple nests.
  -                     while (nested_start_index != -1 && nested_start_index < 
body_end_index) {
  +                     while( nested_start_index != -1 && nested_start_index < 
body_end_index )
  +                     {
                                // <XDoclet:blabla ...>
  -                             if (template.charAt(nested_start_index - 1) == '<') {
  +                             if( template.charAt( nested_start_index - 1 ) == '<' )
  +                             {
                                        open_nested_elem_count++;
                                }
  -                             else {
  +                             else
  +                             {
                                        throw new 
TemplateException(Translator.getString("template_corresponding_tag_missing",
  -                                                     new String[]{XDOCLET_TAIL + 
cmd + ">", Integer.toString(getLineNumber(template, index)), 
getTemplateURL().toString()}));
  +                                             new String[]{new StringBuffer( 
XDOCLET_TAIL ).append( cmd ).append( '>' ).toString(), Integer.toString( 
getLineNumber( template, index ) ), getTemplateURL().toString()} ) );
                                }
   
  -                             nested_start_index = template.indexOf(XDOCLET_PREFIX + 
cmd, nested_start_index + 1);
  +                             nested_start_index = template.indexOf( 
xdocletPrefixPlusCmd.toString(), nested_start_index + 1 );
                        }
   
  -                     if (open_nested_elem_count == 0) {
  +                     if( open_nested_elem_count == 0 )
  +                     {
                                break;
                        }
                }
  @@ -834,7 +983,8 @@
                int local_body_line_num = getLineNumber(template, body_start_index);
   
                currentLineNum += local_body_line_num;
  -             if (prev_line_num > 0) {
  +             if( prev_line_num > 0 )
  +             {
                        currentLineNum--;
                }
   
  @@ -844,7 +994,6 @@
                return index;
        }
   
  -
        /**
         * Invokes block tag implementation method named cmd with the specified body
         * block and set of attributes.
  @@ -857,17 +1006,20 @@
         * @exception TemplateException Description of Exception
         * @see 
#invokeMethod(java.lang.String,java.lang.Object[],java.lang.Object[],java.lang.String,int)
         */
  -     private void invokeBlockMethod(String cmd, String block, Properties 
attributes, String template, int i) throws TemplateException {
  +     private void invokeBlockMethod( String cmd, String block, Properties 
attributes, String template, int i ) throws TemplateException
  +     {
                Object[] params1 = null;
                Object[] params2 = null;
   
                //probable conditions
   
  -             if (attributes.size() > 0) {
  +             if( attributes.size() > 0 )
  +             {
                        params1 = new Object[]{block, attributes};
                        params2 = new Object[]{block};
                }
  -             else {
  +             else
  +             {
                        params1 = new Object[]{block};
                        params2 = new Object[]{block, attributes};
                }
  @@ -875,53 +1027,6 @@
                invokeMethod(cmd, params1, params2, template, i);
        }
   
  -
  -     /**
  -      * Skips whitespaces, starting from index i till the first non-whitespace
  -      * character or end of template and returns the new index.
  -      *
  -      * @param template Description of Parameter
  -      * @param i Description of Parameter
  -      * @return Description of the Returned Value
  -      */
  -     public static int skipWhitespace(String template, int i) {
  -             while (i < template.length() && 
Character.isWhitespace(template.charAt(i))) {
  -                     i++;
  -             }
  -
  -             return i;
  -     }
  -
  -
  -     /**
  -      * Loops over the template content till reaching till_index index and returns
  -      * the number of lines it has encountered.
  -      *
  -      * @param template Description of Parameter
  -      * @param till_index Description of Parameter
  -      * @return The LineNumber value
  -      */
  -     protected static int getLineNumber(String template, int till_index) {
  -             int NL_LEN = PrettyPrintWriter.LINE_SEPARATOR.length();
  -             int index = 0;
  -             int line_num = 0;
  -
  -             do {
  -                     index = template.indexOf(PrettyPrintWriter.LINE_SEPARATOR, 
index);
  -
  -                     if (index != -1) {
  -                             line_num++;
  -                             index += NL_LEN;
  -                     }
  -                     else {
  -                             break;
  -                     }
  -             } while (index < till_index);
  -
  -             return line_num;
  -     }
  -
  -
        /**
         * A wrapper around the hasMoreAttributes and isBlock status used in {@link
         * TemplateEngine#generate}.
  @@ -929,7 +1034,8 @@
         * @author Aslak Helles�y
         * @created October 14, 2001
         */
  -     private static class TagContext {
  +     private static class TagContext
  +     {
                /**
                 * @todo-javadoc Describe the field
                 */
  @@ -939,37 +1045,36 @@
                 */
                private boolean isBlock = false;
   
  +             /**
  +              * Gets the Block attribute of the TagContext object
  +              *
  +              * @return   The Block value
  +              */
  +             public boolean isBlock()
  +             {
  +                     return isBlock;
  +             }
   
                /**
                 * Sets the HasMoreAttributes attribute of the TagContext object
                 *
                 * @param attributes The new HasMoreAttributes value
                 */
  -             public void setHasMoreAttributes(boolean attributes) {
  +             public void setHasMoreAttributes( boolean attributes )
  +             {
                        this.hasMoreAttributes = attributes;
                }
   
  -
                /**
                 * Sets the Block attribute of the TagContext object
                 *
                 * @param block The new Block value
                 */
  -             public void setBlock(boolean block) {
  +             public void setBlock( boolean block )
  +             {
                        isBlock = block;
                }
   
  -
  -             /**
  -              * Gets the Block attribute of the TagContext object
  -              *
  -              * @return The Block value
  -              */
  -             public boolean isBlock() {
  -                     return isBlock;
  -             }
  -
  -
                /**
                 * Describe what the method does
                 *
  @@ -977,20 +1082,10 @@
                 * @todo-javadoc Write javadocs for method
                 * @todo-javadoc Write javadocs for return value
                 */
  -             public boolean hasMoreAttributes() {
  +             public boolean hasMoreAttributes()
  +             {
                        return hasMoreAttributes;
                }
  -     }
  -
  -     static {
  -             XDOCLET_PREFIX = "XD";
  -
  -             // migration path from "XDoclet:" to "XDt"
  -
  -             XDOCLET_HEAD = "<" + XDOCLET_PREFIX;
  -             XDOCLET_TAIL = "</" + XDOCLET_PREFIX;
  -             XDOCLET_HEAD_LEN = XDOCLET_HEAD.length();
  -             XDOCLET_TAIL_LEN = XDOCLET_TAIL.length();
        }
   
   }
  
  
  

_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to