Project Report: fawkez

Packagesummary org.jcoderz.phoenix.templategen

org.jcoderz.phoenix.templategen.TemplateZip

LineHitsNoteSource
1  /*
2   * $Id: TemplateZip.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.File;
37  import java.io.FileInputStream;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Set;
44  import java.util.zip.ZipEntry;
45  import java.util.zip.ZipInputStream;
46  
47  import javax.xml.parsers.FactoryConfigurationError;
48  import javax.xml.parsers.ParserConfigurationException;
49  
50  import org.xml.sax.SAXException;
51  
52  /**
53   * @author Albrecht Messner
54   */
55  public class TemplateZip
56  {
57     private static final String DESCRIPTION_FILE = "template.xml";
58     private static final int READ_BUFFER_SIZE = 1024;
59  
60     private final List mTemplateList;
61     private TemplateDescr mDescription;
62  
63     private final String mFileName;
64  
65 (1)   public TemplateZip (String fileName)
660    {
670       mTemplateList = new ArrayList();
680       mFileName = fileName;
690    }
70  
71 (2)   public void readTemplateFile ()
72           throws IOException,
73              ParserConfigurationException,
74              SAXException,
75              FactoryConfigurationError, TemplateGeneratorException
76     {
770(3)      final ZipInputStream zin = new ZipInputStream(
78             new FileInputStream(mFileName));
79        ZipEntry entry;
80  
810       while ((entry = zin.getNextEntry()) != null)
82        {
830          if (entry.isDirectory())
84           {
850             System.err.println("Ignoring directory entry " + entry.getName());
860             continue;
87           }
88  
890          final File entryFile = new File(entry.getName());
900          final String baseName = entryFile.getName();
91  
92           int read;
930          final byte[] buffer = new byte[READ_BUFFER_SIZE];
940          final ByteArrayOutputStream data = new ByteArrayOutputStream();
950          while ((read = zin.read(buffer)) != -1)
96           {
970             data.write(buffer, 0, read);
98           }
99  
1000          final String dataStr = new String(data.toByteArray());
101  
1020(4)         if (baseName.equals(DESCRIPTION_FILE))
103           {
1040             mDescription = new TemplateDescr();
1050             mDescription.parseDescription(dataStr);
106           }
107           else
108           {
1090             final Template t = new Template(baseName, dataStr);
1100             mTemplateList.add(t);
111           }
1120       }
113  
1140       completeTemplates();
115  
1160       checkTemplate();
1170    }
118  
119     private void completeTemplates () throws TemplateGeneratorException
120     {
121        // set the target for all files
1220       for (final Iterator it = mTemplateList.iterator(); it.hasNext();)
123        {
1240          final Template t = (Template) it.next();
1250          final String targetName
126                 = (String) mDescription.getFilesMap().get(t.getSourceName());
1270          if (targetName == null)
128           {
1290             throw new TemplateGeneratorException(
130                    "File description for file '"
131                    + t.getSourceName()
132                    + "' not found in template description");
133           }
1340          t.setTargetName(targetName);
1350       }
1360    }
137  
138     private void checkTemplate () throws TemplateGeneratorException
139     {
1400       if (mDescription == null)
141        {
1420          throw new TemplateGeneratorException(
143                 "Description file '"
144                 + DESCRIPTION_FILE
145                 + "' missing in template zip file " + mFileName);
146        }
147  
1480       final Set allParams = new HashSet();
1490       for (final Iterator it = mTemplateList.iterator(); it.hasNext(); )
150        {
1510          final Template t = (Template) it.next();
1520          allParams.addAll(t.getParameters());
1530       }
154  
1550       for (final Iterator it = allParams.iterator(); it.hasNext(); )
156        {
1570          final String param = (String) it.next();
1580          if (! mDescription.getParameterMap().containsKey(param))
159           {
1600             throw new TemplateGeneratorException(
161                    "Parameter description for parameter '" + param
162                    + "' not found in template.xml");
163           }
1640       }
1650    }
166  
167 (5)   public TemplateDescr getDescription ()
168     {
1690       return mDescription;
170     }
171  
172 (6)   public List getTemplates ()
173     {
1740       return mTemplateList;
175     }
176  
177 (7)   public static void main (String[] args)
178           throws IOException,
179              ParserConfigurationException,
180              SAXException,
181              FactoryConfigurationError,
182              TemplateGeneratorException
183     {
1840       final TemplateZip tz = new TemplateZip("D:\\temp\\foo.zip");
1850       tz.readTemplateFile();
1860       System.out.println("Got " + tz.getTemplates().size() + " templates");
1870    }
188  }

Findings in this File

i (8) TemplateZip.mDescription not initialized in constructor
f (9) System.out.print is used main class
f (10) System.out.print is used main class
c (1) 65 : 4 Missing a Javadoc comment.
c (2) 71 : 4 Missing a Javadoc comment.
w (3) 77 : 0 org.jcoderz.phoenix.templategen.TemplateZip.readTemplateFile() may fail to close stream
i (4) 102 : 0 method org.jcoderz.phoenix.templategen.TemplateZip.readTemplateFile() makes literal string comparisons passing the literal as an argument
c (5) 167 : 4 Missing a Javadoc comment.
c (6) 172 : 4 Missing a Javadoc comment.
c (7) 177 : 4 Missing a Javadoc comment.