Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1be9204bd924e4890155303bf75314e3fef5b00 (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
/***************************************************************************
 * Copyright (c) 2004-2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * 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.net4j.util.eclipse;


import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.StringHelper;
import org.eclipse.net4j.util.eclipse.Element.Factory;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ExtensionParser
{
  protected Map<Pattern, Factory> elementDataFactoryRegistry = new HashMap<Pattern, Factory>();

  protected List<String> contextStack = new ArrayList<String>();

  private static final Logger logger = Logger.getLogger(ExtensionParser.class.getName());

  public void parse(String extPointId)
  {
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint point = registry.getExtensionPoint(extPointId);

    if (point == null)
    {
      String msg = "Extension point '" + extPointId + "' does not exist.";
      logger.error(msg);
      return;
    }

    parse(point);
  }

  public void parse(IExtensionPoint point)
  {
    IExtension[] extensions = point.getExtensions();
    for (int i = 0; extensions != null && i < extensions.length; i++)
    {
      try
      {
        parse(extensions[i]);
      }
      catch (Throwable t)
      {
        String msg = "Error in plugin " + extensions[i].getNamespaceIdentifier()
            + " while reading extension to " + point.getUniqueIdentifier();
        logger.error(msg, t);
      }
    }
  }

  public Element[] parse(IExtension extension)
  {
    if (logger.isDebugEnabled())
      logger.debug("Parsing plugin " + extension.getNamespaceIdentifier() + " (extension-point = "
          + extension.getExtensionPointUniqueIdentifier() + ")");

    IConfigurationElement[] elems = extension.getConfigurationElements();
    return parse(elems);
  }

  public Element[] parse(IConfigurationElement[] elems)
  {
    Element[] result = new Element[elems.length];

    for (int i = 0; i < elems.length; i++)
    {
      IConfigurationElement elem = elems[i];
      result[i] = parse(elem);
    }

    if (logger.isDebugEnabled())
      logger.debug("All extension data: " + StringHelper.implode(result, ", "));

    return result;
  }

  public Element parse(IConfigurationElement elem)
  {
    pushContext(elem.getName());
    String context = getContextString();

    Element.Factory factory = findFactory(context);
    if (factory == null)
      throw new ExtensionConfigException("No element factory for context " + context);

    Element elementData = factory.createElementData();
    if (elementData == null)
      throw new ExtensionConfigException("No element created for context " + context);
    elementData.internalSetConfigurationElement(elem);

    String[] attributeNames = elem.getAttributeNames();
    for (int i = 0; i < attributeNames.length; i++)
    {
      String attributeName = attributeNames[i];
      String attributeValue = elem.getAttribute(attributeName);

      if (logger.isDebugEnabled())
        logger.debug("Dispatching " + context + "." + attributeName + " = " + attributeValue);

      elementData.dispatchAttributeValue(attributeName, attributeValue);
    }

    // Recurse
    for (int i = 0; i < elem.getChildren().length; i++)
    {
      IConfigurationElement child = elem.getChildren()[i];
      Element childData = parse(child);
      elementData.dispatchChild(child.getName(), childData);
    }

    popContext();
    validate(elementData);
    return elementData;
  }

  public void addFactory(String contextPattern, Factory factory)
  {
    Pattern pattern = Pattern.compile(contextPattern);
    elementDataFactoryRegistry.put(pattern, factory);
  }

  public Factory findFactory(String context)
  {
    for (Map.Entry<Pattern, Factory> entry : elementDataFactoryRegistry.entrySet())
    {
      Pattern pattern = entry.getKey();
      Matcher matcher = pattern.matcher(context);
      if (matcher.matches())
      {
        return entry.getValue();
      }
    }

    return null;
  }

  protected void pushContext(String elementName)
  {
    contextStack.add(elementName);
  }

  protected String popContext()
  {
    if (contextStack.isEmpty()) throw new ImplementationError("contextStack is empty");
    return contextStack.remove(contextStack.size() - 1);
  }

  protected String getContextString()
  {
    return StringHelper.implode(contextStack, "/");
  }

  protected void validate(Element elementData)
  {
    elementData.validate(this);
  }
}

Back to the top