Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 99df9a10d9f35f334580d81f245cf4d9146a7c8a (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2018 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.migrator.tasks;

import org.apache.tools.ant.BuildException;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class ExpandTemplateTask extends CDOTask
{
  private final List<TemplateProperty> templateProperties = new ArrayList<TemplateProperty>();

  private File template;

  private File target;

  private String placeholderPrefix = "%%";

  private String placeholderSuffix = "%%";

  public TemplateProperty createTemplateProperty()
  {
    TemplateProperty templateProperty = new TemplateProperty();
    templateProperties.add(templateProperty);
    return templateProperty;
  }

  public void setTemplate(File template)
  {
    this.template = template;
  }

  public void setPlaceholderPrefix(String placeholderPrefix)
  {
    this.placeholderPrefix = placeholderPrefix;
  }

  public void setPlaceholderSuffix(String placeholderSuffix)
  {
    this.placeholderSuffix = placeholderSuffix;
  }

  public void setTarget(File target)
  {
    this.target = target;
  }

  @Override
  protected void checkAttributes() throws BuildException
  {
    assertTrue("'template' must be specified.", template != null);
    assertTrue("'template' must be point to an existing file.", template.isFile());
    assertTrue("'target' must be specified.", target != null);
    assertTrue("'placeholderPrefix' must be specified.", placeholderPrefix != null && placeholderPrefix.length() != 0);
    assertTrue("'placeholderSuffix' must be specified.", placeholderSuffix != null && placeholderSuffix.length() != 0);

    for (TemplateProperty templateProperty : templateProperties)
    {
      String name = templateProperty.getName();
      assertTrue("'name' of property must be specified.", name != null && name.length() != 0);
    }
  }

  @Override
  protected void doExecute() throws Exception
  {
    verbose("Expanding template " + template + " to target " + target);

    String content = readTextFile(template);
    Map<String, String> properties = getProperties();

    String result = generate(content, properties);
    writeTextFile(target, result);
  }

  protected String generate(String content, Map<String, String> properties) throws Exception
  {
    StringBuilder result = new StringBuilder(content);

    for (Map.Entry<String, String> entry : properties.entrySet())
    {
      String placeholder = formatPlaceholder(entry.getKey());
      String value = entry.getValue();
      if (value == null)
      {
        value = "";
      }

      int placeholderLength = placeholder.length();
      int valueLength = value.length();
      int start = 0;

      for (;;)
      {
        int pos = result.indexOf(placeholder, start);
        if (pos == -1)
        {
          break;
        }

        verbose("Expanding template property '" + entry.getKey() + "' at position " + pos);
        result.replace(pos, pos + placeholderLength, value);
        start = pos + valueLength;
      }
    }

    return result.toString();
  }

  protected String formatPlaceholder(String name)
  {
    return placeholderPrefix + name + placeholderSuffix;
  }

  protected Map<String, String> getProperties()
  {
    Map<String, String> result = new LinkedHashMap<String, String>();

    for (TemplateProperty templateProperty : templateProperties)
    {
      result.put(templateProperty.getName(), templateProperty.getValue());
    }

    return result;
  }

  /**
   * @author Eike Stepper
   */
  public static final class TemplateProperty
  {
    private String name;

    private String value;

    public TemplateProperty()
    {
    }

    public String getName()
    {
      return name;
    }

    public void setName(String name)
    {
      this.name = name;
    }

    public String getValue()
    {
      return value;
    }

    public void setValue(String value)
    {
      this.value = value;
    }

    @Override
    public String toString()
    {
      StringBuilder builder = new StringBuilder();
      builder.append(name);
      builder.append(" = ");
      builder.append(value);
      return builder.toString();
    }
  }
}

Back to the top