Project Report: fawkez

Packagesummary org.jcoderz.phoenix.templategen

org.jcoderz.phoenix.templategen.Template

LineHitsNoteSource
1  /*
2   * $Id: Template.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.phoenix.templategen;
34  
35  import java.util.HashSet;
36  import java.util.Iterator;
37  import java.util.Map;
38  import java.util.Set;
39  
40  /**
41   * @author Albrecht Messner
42   */
43  public class Template
44  {
45 (1)   public static final String PARAM_START = "${";
46 (2)   public static final String PARAM_START_ESCAPED = "\\$\\{";
47 (3)   public static final char PARAM_END = '}';
48 (4)   public static final String PARAM_END_ESCAPED = "\\}";
49  
50     private final String mSourceName;
51     private final String mTemplateString;
52     private String mTargetName;
53  
54 (5)   public Template (String sourceName, String templateString)
550    {
560       mSourceName = sourceName;
57        // per default the target name is the same as the source name
580       mTargetName = sourceName;
590       mTemplateString = templateString;
600    }
61  
62 (6)   public String getSourceName ()
63     {
640       return mSourceName;
65     }
66  
67 (7)   public String getTargetName ()
68     {
690       return mTargetName;
70     }
71  
72 (8)   public void setTargetName (String targetName)
73     {
740       mTargetName = targetName;
750    }
76  
77     private Set getAllParameters ()
78     {
790       final Set parameters = new HashSet();
800       findParameters(parameters, mTemplateString);
810       findParameters(parameters, mTargetName);
82  
830       return parameters;
84     }
85  
86 (9)   public Set getParameters ()
87     {
880       final Set parameters = getAllParameters();
89  
900       for (final Iterator it = parameters.iterator(); it.hasNext(); )
91        {
920          final String param = (String) it.next();
930          if (param.startsWith("jcoderz_header"))
94           {
950             it.remove();
96           }
970       }
98  
990       return parameters;
100     }
101  
102 (10)   public String parametrizeTarget (Map map) throws TemplateGeneratorException
103     {
1040       return parametrizeString(mTargetName, map);
105     }
106  
107 (11)   public String parametrize (Map map) throws TemplateGeneratorException
108     {
1090       return parametrizeString(mTemplateString, map);
110     }
111  
112     private String parametrizeString (String s, Map map)
113           throws TemplateGeneratorException
114     {
1150       String parametrizedTemplate = s;
116  
1170       final Set parameters = getAllParameters();
118  
1190       for (final Iterator it = parameters.iterator(); it.hasNext(); )
120        {
1210          final String key = (String) it.next();
1220          String value = (String) map.get(key);
123  
1240          if (key.startsWith("jcoderz_header"))
125           {
1260             final String headerType = key.substring(key.lastIndexOf('_') + 1);
1270             value = TemplateGenerator.getJcoderzHeader(headerType);
128           }
129  
1300          if (value == null)
131           {
1320             throw new IllegalArgumentException(
133                    "No replacement found for parameter " + key);
134           }
135  
1360          final String variable = PARAM_START_ESCAPED + key + PARAM_END_ESCAPED;
137           // System.out.println("Replacing " + key + " with " + value);
138  
1390          value = escapeString(value);
1400          parametrizedTemplate
141              = parametrizedTemplate.replaceAll(variable, value);
1420       }
143        // System.out.println("Parametrized Template:" + parametrizedTemplate);
1440       return parametrizedTemplate;
145     }
146  
147     private String escapeString (String unescaped)
148     {
1490       final StringBuffer result = new StringBuffer();
150  
1510       for (int i = 0; i < unescaped.length(); i++)
152        {
1530          final char c = unescaped.charAt(i);
1540          switch (c)
155           {
156              case '\\':
1570                result.append("\\\\");
1580                break;
159              case '$':
1600                result.append("\\$");
1610                break;
162              default:
1630                result.append(c);
164                 break;
165           }
166        }
167  
1680       return result.toString();
169     }
170  
171     private void findParameters (Set parameters, String s)
172     {
1730       int paramStartPos = 0;
174        while ((paramStartPos = s.indexOf(
1750             PARAM_START, paramStartPos)) != -1)
176        {
1770          paramStartPos += PARAM_START.length();
1780          final int paramEndPos = s.indexOf(PARAM_END, paramStartPos);
1790          if (paramEndPos == -1)
180           {
1810             throw new IllegalArgumentException(
182                    "No matching close tag found for opening tag at "
183                    + paramStartPos);
184           }
1850          final String paramName = s.substring(paramStartPos, paramEndPos);
1860          parameters.add(paramName);
1870          paramStartPos = paramEndPos + 1;
1880       }
1890    }
190  }

Findings in this File

c (1) 45 : 4 Missing a Javadoc comment.
c (2) 46 : 4 Missing a Javadoc comment.
c (3) 47 : 4 Missing a Javadoc comment.
c (4) 48 : 4 Missing a Javadoc comment.
c (5) 54 : 4 Missing a Javadoc comment.
c (6) 62 : 4 Missing a Javadoc comment.
c (7) 67 : 4 Missing a Javadoc comment.
c (8) 72 : 4 Missing a Javadoc comment.
c (9) 86 : 4 Missing a Javadoc comment.
c (10) 102 : 4 Missing a Javadoc comment.
c (11) 107 : 4 Missing a Javadoc comment.