Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8839aa3298a0ba3cbe4027ddb32186238c180ad5 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 2004 - 2012 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.releng.version;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;

import org.osgi.framework.Version;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Eike Stepper
 */
public class Release implements ElementResolver
{
  public static final String RELEASE_TAG = "release";

  public static final String FEATURE_TAG = "feature";

  public static final String PLUGIN_TAG = "plugin";

  public static final String TAG_ATTRIBUTE = "tag";

  public static final String INTEGRATION_ATTRIBUTE = "integration";

  public static final String NAME_ATTRIBUTE = "name";

  public static final String VERSION_ATTRIBUTE = "version";

  private static final String INDENT = "\t";

  private IFile file;

  private boolean integration;

  private String tag;

  private Map<Element, Element> elements = new HashMap<Element, Element>();

  public Release(IFile file)
  {
    this.file = file;
    integration = true;
    initTag();
  }

  Release(SAXParser parser, IFile file) throws CoreException, IOException, SAXException
  {
    this.file = file;

    XMLHandler handler = new XMLHandler();
    InputStream contents = null;

    try
    {
      contents = file.getContents();
      parser.parse(contents, handler);
      initTag();
    }
    finally
    {
      if (contents != null)
      {
        try
        {
          contents.close();
        }
        catch (Exception ex)
        {
          Activator.log(ex);
        }
      }
    }
  }

  private void initTag()
  {
    if (tag == null)
    {
      tag = UUID.randomUUID().toString();
    }
  }

  public IFile getFile()
  {
    return file;
  }

  public String getTag()
  {
    return tag;
  }

  public boolean isIntegration()
  {
    return integration;
  }

  public Map<Element, Element> getElements()
  {
    return elements;
  }

  public Element resolveElement(Element key)
  {
    return elements.get(key);
  }

  public int getSize()
  {
    return elements.size();
  }

  public void write() throws IOException, CoreException
  {
    StringBuilder builder = new StringBuilder();
    writeRelease(builder);

    String xml = builder.toString();
    ByteArrayInputStream contents = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    if (file.exists())
    {
      file.setContents(contents, true, true, new NullProgressMonitor());
    }
    else
    {
      file.create(contents, true, new NullProgressMonitor());
    }
  }

  private void writeRelease(StringBuilder builder)
  {
    builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    builder.append("<" + RELEASE_TAG + " " + INTEGRATION_ATTRIBUTE + "=\"+integration+\" " + TAG_ATTRIBUTE + "=\""
        + tag + "\">\n");

    List<Element> list = new ArrayList<Element>(elements.keySet());
    Collections.sort(list, new Comparator<Element>()
    {
      public int compare(Element o1, Element o2)
      {
        int result = o1.getType().compareTo(o2.getType());
        if (result == 0)
        {
          result = o1.getName().compareTo(o2.getName());
        }

        if (result == 0)
        {
          result = o1.getVersion().compareTo(o2.getVersion());
        }

        return result;
      }
    });

    for (Element element : list)
    {
      writeElement(builder, element, INDENT);
    }

    builder.append("</" + RELEASE_TAG + ">\n");
  }

  private void writeElement(StringBuilder builder, Element element, String indent)
  {
    String name = element.getName();
    Version version = element.getVersion();

    builder.append(indent + "<" + element.getTag() + " " + NAME_ATTRIBUTE + "=\"" + name + "\" " + VERSION_ATTRIBUTE
        + "=\"" + version + "\"");

    List<Element> content = element.getChildren();
    if (content.isEmpty())
    {
      builder.append("/");
      writeElementEnd(builder, element);
    }
    else
    {
      writeElementEnd(builder, element);

      for (Element child : content)
      {
        writeElement(builder, child, indent + INDENT);
      }

      builder.append(indent + "</" + element.getTag() + ">\n");
    }
  }

  private void writeElementEnd(StringBuilder builder, Element element)
  {
    builder.append(">");
    if (element.getVersion().equals(Version.emptyVersion))
    {
      builder.append(" <!-- UNRESOLVED -->");
    }

    builder.append("\n");
  }

  /**
   * @author Eike Stepper
   */
  public class XMLHandler extends DefaultHandler
  {
    private Element parent;

    private int level;

    public XMLHandler()
    {
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
      if (RELEASE_TAG.equalsIgnoreCase(qName))
      {
        tag = getString(attributes, TAG_ATTRIBUTE);
        integration = getBoolean(attributes, INTEGRATION_ATTRIBUTE);
      }
      else if (FEATURE_TAG.equalsIgnoreCase(qName))
      {
        Element element = createElement(Element.Type.FEATURE, attributes);
        if (++level == 1)
        {
          elements.put(element, element);
          parent = element;
        }
        else
        {
          parent.getChildren().add(element);
        }
      }
      else if (PLUGIN_TAG.equalsIgnoreCase(qName))
      {
        Element element = createElement(Element.Type.PLUGIN, attributes);
        if (++level == 1)
        {
          elements.put(element, element);
          parent = element;
        }
        else
        {
          parent.getChildren().add(element);
        }
      }
    }

    private Element createElement(Element.Type type, Attributes attributes) throws SAXException
    {
      String name = getString(attributes, NAME_ATTRIBUTE);
      Version version = new Version(getString(attributes, VERSION_ATTRIBUTE));

      return new Element(type, name, version);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
      if (FEATURE_TAG.equalsIgnoreCase(qName) || PLUGIN_TAG.equalsIgnoreCase(qName))
      {
        --level;
      }
    }

    private String getString(Attributes attributes, String name) throws SAXException
    {
      String value = attributes.getValue(name);
      if (value != null)
      {
        return value;
      }

      throw new SAXException("Illegal value for " + name);
    }

    private boolean getBoolean(Attributes attributes, String name) throws SAXException
    {
      String value = attributes.getValue(name);
      if ("false".equalsIgnoreCase(value))
      {
        return false;
      }

      if ("true".equalsIgnoreCase(value))
      {
        return true;
      }

      throw new SAXException("Illegal value for " + name);
    }

    @Override
    public void error(SAXParseException exception) throws SAXException
    {
      addMarker(exception, IMarker.SEVERITY_ERROR);
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException
    {
      addMarker(exception, IMarker.SEVERITY_ERROR);
    }

    @Override
    public void warning(SAXParseException exception) throws SAXException
    {
      addMarker(exception, IMarker.SEVERITY_WARNING);
    }

    private void addMarker(SAXParseException e, int severity)
    {
      try
      {
        Markers.addMarker(file, e.getMessage(), severity, e.getLineNumber());
      }
      catch (Exception ex)
      {
        Activator.log(ex);
      }
    }
  }
}

Back to the top