root/trunk/src/java/org/jcoderz/phoenix/templategen/TemplateZip.java

Revision 1011, 5.9 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
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 */
33package org.jcoderz.phoenix.templategen;
34
35import java.io.ByteArrayOutputStream;
36import java.io.File;
37import java.io.FileInputStream;
38import java.io.IOException;
39import java.util.ArrayList;
40import java.util.HashSet;
41import java.util.Iterator;
42import java.util.List;
43import java.util.Set;
44import java.util.zip.ZipEntry;
45import java.util.zip.ZipInputStream;
46
47import javax.xml.parsers.FactoryConfigurationError;
48import javax.xml.parsers.ParserConfigurationException;
49
50import org.xml.sax.SAXException;
51
52/**
53 * @author Albrecht Messner
54 */
55public 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   public TemplateZip (String fileName)
66   {
67      mTemplateList = new ArrayList();
68      mFileName = fileName;
69   }
70
71   public void readTemplateFile ()
72         throws IOException,
73            ParserConfigurationException,
74            SAXException,
75            FactoryConfigurationError, TemplateGeneratorException
76   {
77      final ZipInputStream zin = new ZipInputStream(
78           new FileInputStream(mFileName));
79      ZipEntry entry;
80
81      while ((entry = zin.getNextEntry()) != null)
82      {
83         if (entry.isDirectory())
84         {
85            System.err.println("Ignoring directory entry " + entry.getName());
86            continue;
87         }
88
89         final File entryFile = new File(entry.getName());
90         final String baseName = entryFile.getName();
91
92         int read;
93         final byte[] buffer = new byte[READ_BUFFER_SIZE];
94         final ByteArrayOutputStream data = new ByteArrayOutputStream();
95         while ((read = zin.read(buffer)) != -1)
96         {
97            data.write(buffer, 0, read);
98         }
99
100         final String dataStr = new String(data.toByteArray());
101
102         if (baseName.equals(DESCRIPTION_FILE))
103         {
104            mDescription = new TemplateDescr();
105            mDescription.parseDescription(dataStr);
106         }
107         else
108         {
109            final Template t = new Template(baseName, dataStr);
110            mTemplateList.add(t);
111         }
112      }
113
114      completeTemplates();
115
116      checkTemplate();
117   }
118
119   private void completeTemplates () throws TemplateGeneratorException
120   {
121      // set the target for all files
122      for (final Iterator it = mTemplateList.iterator(); it.hasNext();)
123      {
124         final Template t = (Template) it.next();
125         final String targetName
126               = (String) mDescription.getFilesMap().get(t.getSourceName());
127         if (targetName == null)
128         {
129            throw new TemplateGeneratorException(
130                  "File description for file '"
131                  + t.getSourceName()
132                  + "' not found in template description");
133         }
134         t.setTargetName(targetName);
135      }
136   }
137
138   private void checkTemplate () throws TemplateGeneratorException
139   {
140      if (mDescription == null)
141      {
142         throw new TemplateGeneratorException(
143               "Description file '"
144               + DESCRIPTION_FILE
145               + "' missing in template zip file " + mFileName);
146      }
147
148      final Set allParams = new HashSet();
149      for (final Iterator it = mTemplateList.iterator(); it.hasNext(); )
150      {
151         final Template t = (Template) it.next();
152         allParams.addAll(t.getParameters());
153      }
154
155      for (final Iterator it = allParams.iterator(); it.hasNext(); )
156      {
157         final String param = (String) it.next();
158         if (! mDescription.getParameterMap().containsKey(param))
159         {
160            throw new TemplateGeneratorException(
161                  "Parameter description for parameter '" + param
162                  + "' not found in template.xml");
163         }
164      }
165   }
166
167   public TemplateDescr getDescription ()
168   {
169      return mDescription;
170   }
171
172   public List getTemplates ()
173   {
174      return mTemplateList;
175   }
176
177   public static void main (String[] args)
178         throws IOException,
179            ParserConfigurationException,
180            SAXException,
181            FactoryConfigurationError,
182            TemplateGeneratorException
183   {
184      final TemplateZip tz = new TemplateZip("D:\\temp\\foo.zip");
185      tz.readTemplateFile();
186      System.out.println("Got " + tz.getTemplates().size() + " templates");
187   }
188}
Note: See TracBrowser for help on using the browser.