| 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 | */ |
|---|
| 33 | package org.jcoderz.phoenix.templategen; |
|---|
| 34 | |
|---|
| 35 | import java.util.HashSet; |
|---|
| 36 | import java.util.Iterator; |
|---|
| 37 | import java.util.Map; |
|---|
| 38 | import java.util.Set; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * @author Albrecht Messner |
|---|
| 42 | */ |
|---|
| 43 | public class Template |
|---|
| 44 | { |
|---|
| 45 | public static final String PARAM_START = "${"; |
|---|
| 46 | public static final String PARAM_START_ESCAPED = "\\$\\{"; |
|---|
| 47 | public static final char PARAM_END = '}'; |
|---|
| 48 | public static final String PARAM_END_ESCAPED = "\\}"; |
|---|
| 49 | |
|---|
| 50 | private final String mSourceName; |
|---|
| 51 | private final String mTemplateString; |
|---|
| 52 | private String mTargetName; |
|---|
| 53 | |
|---|
| 54 | public Template (String sourceName, String templateString) |
|---|
| 55 | { |
|---|
| 56 | mSourceName = sourceName; |
|---|
| 57 | // per default the target name is the same as the source name |
|---|
| 58 | mTargetName = sourceName; |
|---|
| 59 | mTemplateString = templateString; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public String getSourceName () |
|---|
| 63 | { |
|---|
| 64 | return mSourceName; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public String getTargetName () |
|---|
| 68 | { |
|---|
| 69 | return mTargetName; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public void setTargetName (String targetName) |
|---|
| 73 | { |
|---|
| 74 | mTargetName = targetName; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | private Set getAllParameters () |
|---|
| 78 | { |
|---|
| 79 | final Set parameters = new HashSet(); |
|---|
| 80 | findParameters(parameters, mTemplateString); |
|---|
| 81 | findParameters(parameters, mTargetName); |
|---|
| 82 | |
|---|
| 83 | return parameters; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public Set getParameters () |
|---|
| 87 | { |
|---|
| 88 | final Set parameters = getAllParameters(); |
|---|
| 89 | |
|---|
| 90 | for (final Iterator it = parameters.iterator(); it.hasNext(); ) |
|---|
| 91 | { |
|---|
| 92 | final String param = (String) it.next(); |
|---|
| 93 | if (param.startsWith("jcoderz_header")) |
|---|
| 94 | { |
|---|
| 95 | it.remove(); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return parameters; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | public String parametrizeTarget (Map map) throws TemplateGeneratorException |
|---|
| 103 | { |
|---|
| 104 | return parametrizeString(mTargetName, map); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public String parametrize (Map map) throws TemplateGeneratorException |
|---|
| 108 | { |
|---|
| 109 | return parametrizeString(mTemplateString, map); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | private String parametrizeString (String s, Map map) |
|---|
| 113 | throws TemplateGeneratorException |
|---|
| 114 | { |
|---|
| 115 | String parametrizedTemplate = s; |
|---|
| 116 | |
|---|
| 117 | final Set parameters = getAllParameters(); |
|---|
| 118 | |
|---|
| 119 | for (final Iterator it = parameters.iterator(); it.hasNext(); ) |
|---|
| 120 | { |
|---|
| 121 | final String key = (String) it.next(); |
|---|
| 122 | String value = (String) map.get(key); |
|---|
| 123 | |
|---|
| 124 | if (key.startsWith("jcoderz_header")) |
|---|
| 125 | { |
|---|
| 126 | final String headerType = key.substring(key.lastIndexOf('_') + 1); |
|---|
| 127 | value = TemplateGenerator.getJcoderzHeader(headerType); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | if (value == null) |
|---|
| 131 | { |
|---|
| 132 | throw new IllegalArgumentException( |
|---|
| 133 | "No replacement found for parameter " + key); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | final String variable = PARAM_START_ESCAPED + key + PARAM_END_ESCAPED; |
|---|
| 137 | // System.out.println("Replacing " + key + " with " + value); |
|---|
| 138 | |
|---|
| 139 | value = escapeString(value); |
|---|
| 140 | parametrizedTemplate |
|---|
| 141 | = parametrizedTemplate.replaceAll(variable, value); |
|---|
| 142 | } |
|---|
| 143 | // System.out.println("Parametrized Template:" + parametrizedTemplate); |
|---|
| 144 | return parametrizedTemplate; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | private String escapeString (String unescaped) |
|---|
| 148 | { |
|---|
| 149 | final StringBuffer result = new StringBuffer(); |
|---|
| 150 | |
|---|
| 151 | for (int i = 0; i < unescaped.length(); i++) |
|---|
| 152 | { |
|---|
| 153 | final char c = unescaped.charAt(i); |
|---|
| 154 | switch (c) |
|---|
| 155 | { |
|---|
| 156 | case '\\': |
|---|
| 157 | result.append("\\\\"); |
|---|
| 158 | break; |
|---|
| 159 | case '$': |
|---|
| 160 | result.append("\\$"); |
|---|
| 161 | break; |
|---|
| 162 | default: |
|---|
| 163 | result.append(c); |
|---|
| 164 | break; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | return result.toString(); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | private void findParameters (Set parameters, String s) |
|---|
| 172 | { |
|---|
| 173 | int paramStartPos = 0; |
|---|
| 174 | while ((paramStartPos = s.indexOf( |
|---|
| 175 | PARAM_START, paramStartPos)) != -1) |
|---|
| 176 | { |
|---|
| 177 | paramStartPos += PARAM_START.length(); |
|---|
| 178 | final int paramEndPos = s.indexOf(PARAM_END, paramStartPos); |
|---|
| 179 | if (paramEndPos == -1) |
|---|
| 180 | { |
|---|
| 181 | throw new IllegalArgumentException( |
|---|
| 182 | "No matching close tag found for opening tag at " |
|---|
| 183 | + paramStartPos); |
|---|
| 184 | } |
|---|
| 185 | final String paramName = s.substring(paramStartPos, paramEndPos); |
|---|
| 186 | parameters.add(paramName); |
|---|
| 187 | paramStartPos = paramEndPos + 1; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|