Skip to main content
summaryrefslogtreecommitdiffstats
blob: 651613dad39d006f0a1a4978b92d644729da3ec9 (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
/**
 * Copyright (c) 2004 - 2011 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.emf.cdo.releng.version.Release.Element.Type;

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

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.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class Release
{
  private IFile file;

  private String tag;

  private boolean integration;

  private String repository;

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

  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);
    }
    finally
    {
      if (contents != null)
      {
        try
        {
          contents.close();
        }
        catch (Exception ex)
        {
          Activator.log(ex);
        }
      }
    }
  }

  public IFile getFile()
  {
    return file;
  }

  public String getTag()
  {
    return tag;
  }

  public boolean isIntegration()
  {
    return integration;
  }

  public String getRepository()
  {
    return repository;
  }

  public Map<String, Element> getElements()
  {
    return Collections.unmodifiableMap(elements);
  }

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

  public static Version normalizeVersion(Version version)
  {
    return new Version(version.getMajor(), version.getMinor(), version.getMicro());
  }

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

    private Version version;

    private Type type;

    public Element(String name, Version version, Type type)
    {
      this.name = name;
      this.version = normalizeVersion(version);
      this.type = type;
    }

    public String getName()
    {
      return name;
    }

    public Version getVersion()
    {
      return version;
    }

    public Type getType()
    {
      return type;
    }

    /**
     * @author Eike Stepper
     */
    public static enum Type
    {
      FEATURE, PLUGIN
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class XMLHandler extends DefaultHandler
  {
    public XMLHandler()
    {
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
      if ("release".equalsIgnoreCase(qName))
      {
        tag = getString(attributes, "tag");
        integration = getBoolean(attributes, "integration");
        repository = getString(attributes, "repository");
      }
      else if ("element".equalsIgnoreCase(qName))
      {
        String name = getString(attributes, "name");
        Version version = new Version(getString(attributes, "version"));
        Type type = getType(attributes, "type");

        Element element = new Element(name, version, type);
        elements.put(name, element);
      }
    }

    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);
    }

    private Type getType(Attributes attributes, String name) throws SAXException
    {
      String type = getString(attributes, name);
      if ("org.eclipse.update.feature".equals(type))
      {
        return Type.FEATURE;
      }

      if ("osgi.bundle".equals(type))
      {
        return Type.PLUGIN;
      }

      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