| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 |
| 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) |
| 66 | 0 | | { |
| 67 | 0 | | mTemplateList = new ArrayList(); |
| 68 | 0 | | mFileName = fileName; |
| 69 | 0 | | } |
| 70 | | | |
| 71 | | (2) | public void readTemplateFile () |
| 72 | | | throws IOException, |
| 73 | | | ParserConfigurationException, |
| 74 | | | SAXException, |
| 75 | | | FactoryConfigurationError, TemplateGeneratorException |
| 76 | | | { |
| 77 | 0 | (3) | final ZipInputStream zin = new ZipInputStream( |
| 78 | | | new FileInputStream(mFileName)); |
| 79 | | | ZipEntry entry; |
| 80 | | | |
| 81 | 0 | | while ((entry = zin.getNextEntry()) != null) |
| 82 | | | { |
| 83 | 0 | | if (entry.isDirectory()) |
| 84 | | | { |
| 85 | 0 | | System.err.println("Ignoring directory entry " + entry.getName()); |
| 86 | 0 | | continue; |
| 87 | | | } |
| 88 | | | |
| 89 | 0 | | final File entryFile = new File(entry.getName()); |
| 90 | 0 | | final String baseName = entryFile.getName(); |
| 91 | | | |
| 92 | | | int read; |
| 93 | 0 | | final byte[] buffer = new byte[READ_BUFFER_SIZE]; |
| 94 | 0 | | final ByteArrayOutputStream data = new ByteArrayOutputStream(); |
| 95 | 0 | | while ((read = zin.read(buffer)) != -1) |
| 96 | | | { |
| 97 | 0 | | data.write(buffer, 0, read); |
| 98 | | | } |
| 99 | | | |
| 100 | 0 | | final String dataStr = new String(data.toByteArray()); |
| 101 | | | |
| 102 | 0 | (4) | if (baseName.equals(DESCRIPTION_FILE)) |
| 103 | | | { |
| 104 | 0 | | mDescription = new TemplateDescr(); |
| 105 | 0 | | mDescription.parseDescription(dataStr); |
| 106 | | | } |
| 107 | | | else |
| 108 | | | { |
| 109 | 0 | | final Template t = new Template(baseName, dataStr); |
| 110 | 0 | | mTemplateList.add(t); |
| 111 | | | } |
| 112 | 0 | | } |
| 113 | | | |
| 114 | 0 | | completeTemplates(); |
| 115 | | | |
| 116 | 0 | | checkTemplate(); |
| 117 | 0 | | } |
| 118 | | | |
| 119 | | | private void completeTemplates () throws TemplateGeneratorException |
| 120 | | | { |
| 121 | | | |
| 122 | 0 | | for (final Iterator it = mTemplateList.iterator(); it.hasNext();) |
| 123 | | | { |
| 124 | 0 | | final Template t = (Template) it.next(); |
| 125 | 0 | | final String targetName |
| 126 | | | = (String) mDescription.getFilesMap().get(t.getSourceName()); |
| 127 | 0 | | if (targetName == null) |
| 128 | | | { |
| 129 | 0 | | throw new TemplateGeneratorException( |
| 130 | | | "File description for file '" |
| 131 | | | + t.getSourceName() |
| 132 | | | + "' not found in template description"); |
| 133 | | | } |
| 134 | 0 | | t.setTargetName(targetName); |
| 135 | 0 | | } |
| 136 | 0 | | } |
| 137 | | | |
| 138 | | | private void checkTemplate () throws TemplateGeneratorException |
| 139 | | | { |
| 140 | 0 | | if (mDescription == null) |
| 141 | | | { |
| 142 | 0 | | throw new TemplateGeneratorException( |
| 143 | | | "Description file '" |
| 144 | | | + DESCRIPTION_FILE |
| 145 | | | + "' missing in template zip file " + mFileName); |
| 146 | | | } |
| 147 | | | |
| 148 | 0 | | final Set allParams = new HashSet(); |
| 149 | 0 | | for (final Iterator it = mTemplateList.iterator(); it.hasNext(); ) |
| 150 | | | { |
| 151 | 0 | | final Template t = (Template) it.next(); |
| 152 | 0 | | allParams.addAll(t.getParameters()); |
| 153 | 0 | | } |
| 154 | | | |
| 155 | 0 | | for (final Iterator it = allParams.iterator(); it.hasNext(); ) |
| 156 | | | { |
| 157 | 0 | | final String param = (String) it.next(); |
| 158 | 0 | | if (! mDescription.getParameterMap().containsKey(param)) |
| 159 | | | { |
| 160 | 0 | | throw new TemplateGeneratorException( |
| 161 | | | "Parameter description for parameter '" + param |
| 162 | | | + "' not found in template.xml"); |
| 163 | | | } |
| 164 | 0 | | } |
| 165 | 0 | | } |
| 166 | | | |
| 167 | | (5) | public TemplateDescr getDescription () |
| 168 | | | { |
| 169 | 0 | | return mDescription; |
| 170 | | | } |
| 171 | | | |
| 172 | | (6) | public List getTemplates () |
| 173 | | | { |
| 174 | 0 | | return mTemplateList; |
| 175 | | | } |
| 176 | | | |
| 177 | | (7) | public static void main (String[] args) |
| 178 | | | throws IOException, |
| 179 | | | ParserConfigurationException, |
| 180 | | | SAXException, |
| 181 | | | FactoryConfigurationError, |
| 182 | | | TemplateGeneratorException |
| 183 | | | { |
| 184 | 0 | | final TemplateZip tz = new TemplateZip("D:\\temp\\foo.zip"); |
| 185 | 0 | | tz.readTemplateFile(); |
| 186 | 0 | | System.out.println("Got " + tz.getTemplates().size() + " templates"); |
| 187 | 0 | | } |
| 188 | | | } |