Project Report: fawkez

Packagesummary org.jcoderz.phoenix.templategen

org.jcoderz.phoenix.templategen.TemplateGenerator

LineHitsNoteSource
1  /*
2   * $Id: TemplateGenerator.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.io.ByteArrayOutputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.zip.ZipEntry;
42  import java.util.zip.ZipOutputStream;
43  
44  import javax.xml.parsers.FactoryConfigurationError;
45  import javax.xml.parsers.ParserConfigurationException;
46  
47  import org.jcoderz.commons.util.IoUtil;
48  import org.xml.sax.SAXException;
49  
50  /**
51   * @author Albrecht Messner
52   */
53  public class TemplateGenerator
54  {
55     private final TemplateZip mTemplateZip;
56  
57 (1)   public TemplateGenerator (String templateZipName)
58           throws IOException,
59                 ParserConfigurationException,
60                 SAXException,
61                 FactoryConfigurationError,
62                 TemplateGeneratorException
630    {
640       mTemplateZip = new TemplateZip(templateZipName);
650       mTemplateZip.readTemplateFile();
660    }
67  
68 (2)   public String getTemplateDescription ()
69     {
700       return mTemplateZip.getDescription().getDescription();
71     }
72  
73 (3)   public List getParameterList ()
74     {
750       return mTemplateZip.getDescription().getParameterList();
76     }
77  
78 (4)   public byte[] parametrizeTemplates (Map parameterMap)
79           throws IOException, TemplateGeneratorException
80     {
810       final ByteArrayOutputStream bos = new ByteArrayOutputStream();
820       final ZipOutputStream archive = new ZipOutputStream(bos);
83  
84        // check values
850       for (final Iterator it = mTemplateZip.getDescription()
860             .getParameterList().iterator(); it.hasNext(); )
87        {
880          final Parameter p = (Parameter) it.next();
890          final String val = (String) parameterMap.get(p.getName());
900          if (val == null)
91           {
920             throw new TemplateGeneratorException("Value for parameter "
93                    + p.getName() + " missing");
94           }
950          p.checkValue(val);
960       }
97  
980       for (final Iterator it = mTemplateZip.getTemplates().iterator();
990           it.hasNext(); )
100        {
1010          final Template t = (Template) it.next();
1020          final String parametrizedTemplate = t.parametrize(parameterMap);
103  
1040          final byte[] entryData = parametrizedTemplate.getBytes();
1050          final ZipEntry entry = new ZipEntry(t.parametrizeTarget(parameterMap));
106  
1070          archive.putNextEntry(entry);
1080          archive.write(entryData, 0, entryData.length);
1090          archive.closeEntry();
1100       }
111  
1120       archive.flush();
1130       archive.close();
1140       return bos.toByteArray();
115     }
116  
117     protected static String getJcoderzHeader (String type)
118        throws TemplateGeneratorException
119     {
1200(5)(6)      if (type.equals("java"))
121        {
1220          final String resource = "build/resources/jcoderz_java_header.txt";
123           try
124           {
1250             final InputStream is
126                    = TemplateGenerator.class.getResourceAsStream(resource);
1270             if (is == null)
128              {
1290(7)               throw new IOException("Could not load resource " + resource);
130              }
1310             final ByteArrayOutputStream bout = new ByteArrayOutputStream();
1320             IoUtil.copy(is, bout);
1330             return new String(bout.toByteArray());
134           }
1350          catch (IOException x)
136           {
1370(8)            throw new TemplateGeneratorException(
138                    "Could not read resource " + resource, x);
139           }
140        }
141        else
142        {
1430          throw new TemplateGeneratorException(
144                 "Don't have jCoderZ header for type " + type);
145        }
146     }
147  }
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  

Findings in this File

c (1) 57 : 4 Missing a Javadoc comment.
c (2) 68 : 4 Missing a Javadoc comment.
c (3) 73 : 4 Missing a Javadoc comment.
c (4) 78 : 4 Missing a Javadoc comment.
i (5) 120 : 0 method org.jcoderz.phoenix.templategen.TemplateGenerator.getJcoderzHeader(String) makes literal string comparisons passing the literal as an argument
c (6) 120 : 11 Position literals first in String comparisons
i (7) 129 : 0 method org.jcoderz.phoenix.templategen.TemplateGenerator.getJcoderzHeader(String) throws exception with static message string
i (8) 137 : 0 method org.jcoderz.phoenix.templategen.TemplateGenerator.getJcoderzHeader(String) throws exception with static message string