| 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.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 |
| 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 |
| 63 | 0 | | { |
| 64 | 0 | | mTemplateZip = new TemplateZip(templateZipName); |
| 65 | 0 | | mTemplateZip.readTemplateFile(); |
| 66 | 0 | | } |
| 67 | | | |
| 68 | | (2) | public String getTemplateDescription () |
| 69 | | | { |
| 70 | 0 | | return mTemplateZip.getDescription().getDescription(); |
| 71 | | | } |
| 72 | | | |
| 73 | | (3) | public List getParameterList () |
| 74 | | | { |
| 75 | 0 | | return mTemplateZip.getDescription().getParameterList(); |
| 76 | | | } |
| 77 | | | |
| 78 | | (4) | public byte[] parametrizeTemplates (Map parameterMap) |
| 79 | | | throws IOException, TemplateGeneratorException |
| 80 | | | { |
| 81 | 0 | | final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 82 | 0 | | final ZipOutputStream archive = new ZipOutputStream(bos); |
| 83 | | | |
| 84 | | | |
| 85 | 0 | | for (final Iterator it = mTemplateZip.getDescription() |
| 86 | 0 | | .getParameterList().iterator(); it.hasNext(); ) |
| 87 | | | { |
| 88 | 0 | | final Parameter p = (Parameter) it.next(); |
| 89 | 0 | | final String val = (String) parameterMap.get(p.getName()); |
| 90 | 0 | | if (val == null) |
| 91 | | | { |
| 92 | 0 | | throw new TemplateGeneratorException("Value for parameter " |
| 93 | | | + p.getName() + " missing"); |
| 94 | | | } |
| 95 | 0 | | p.checkValue(val); |
| 96 | 0 | | } |
| 97 | | | |
| 98 | 0 | | for (final Iterator it = mTemplateZip.getTemplates().iterator(); |
| 99 | 0 | | it.hasNext(); ) |
| 100 | | | { |
| 101 | 0 | | final Template t = (Template) it.next(); |
| 102 | 0 | | final String parametrizedTemplate = t.parametrize(parameterMap); |
| 103 | | | |
| 104 | 0 | | final byte[] entryData = parametrizedTemplate.getBytes(); |
| 105 | 0 | | final ZipEntry entry = new ZipEntry(t.parametrizeTarget(parameterMap)); |
| 106 | | | |
| 107 | 0 | | archive.putNextEntry(entry); |
| 108 | 0 | | archive.write(entryData, 0, entryData.length); |
| 109 | 0 | | archive.closeEntry(); |
| 110 | 0 | | } |
| 111 | | | |
| 112 | 0 | | archive.flush(); |
| 113 | 0 | | archive.close(); |
| 114 | 0 | | return bos.toByteArray(); |
| 115 | | | } |
| 116 | | | |
| 117 | | | protected static String getJcoderzHeader (String type) |
| 118 | | | throws TemplateGeneratorException |
| 119 | | | { |
| 120 | 0 | (5)(6) | if (type.equals("java")) |
| 121 | | | { |
| 122 | 0 | | final String resource = "build/resources/jcoderz_java_header.txt"; |
| 123 | | | try |
| 124 | | | { |
| 125 | 0 | | final InputStream is |
| 126 | | | = TemplateGenerator.class.getResourceAsStream(resource); |
| 127 | 0 | | if (is == null) |
| 128 | | | { |
| 129 | 0 | (7) | throw new IOException("Could not load resource " + resource); |
| 130 | | | } |
| 131 | 0 | | final ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 132 | 0 | | IoUtil.copy(is, bout); |
| 133 | 0 | | return new String(bout.toByteArray()); |
| 134 | | | } |
| 135 | 0 | | catch (IOException x) |
| 136 | | | { |
| 137 | 0 | (8) | throw new TemplateGeneratorException( |
| 138 | | | "Could not read resource " + resource, x); |
| 139 | | | } |
| 140 | | | } |
| 141 | | | else |
| 142 | | | { |
| 143 | 0 | | 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 | | | |