1 package com.atlassian.plugin.parsers;
2
3 import com.atlassian.fugue.Option;
4 import com.atlassian.plugin.Application;
5 import com.atlassian.plugin.InstallationMode;
6 import com.atlassian.plugin.Plugin;
7 import com.atlassian.plugin.Resources;
8 import com.atlassian.plugin.util.PluginUtils;
9 import com.google.common.base.Function;
10 import com.google.common.base.Predicate;
11 import com.google.common.base.Predicates;
12 import com.google.common.collect.ImmutableList;
13 import org.dom4j.Document;
14 import org.dom4j.Element;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import javax.annotation.Nullable;
19 import java.util.List;
20 import java.util.Set;
21
22 import static com.atlassian.fugue.Iterables.findFirst;
23 import static com.atlassian.plugin.parsers.PluginInformationReader.PLUGIN_INFO;
24 import static com.atlassian.plugin.parsers.XmlDescriptorParserUtils.removeAllNamespaces;
25 import static com.google.common.base.Preconditions.checkNotNull;
26 import static com.google.common.collect.ImmutableSet.copyOf;
27 import static com.google.common.collect.Iterables.filter;
28 import static com.google.common.collect.Iterables.transform;
29
30
31
32
33
34
35 public final class PluginDescriptorReader
36 {
37 private static final Logger log = LoggerFactory.getLogger(PluginDescriptorReader.class);
38
39 static final String RESOURCE = "resource";
40
41 private final Document descriptor;
42 private final Set<Application> applications;
43
44 public PluginDescriptorReader(Document descriptor, Set<Application> applications)
45 {
46 this.descriptor = removeAllNamespaces(checkNotNull(descriptor));
47 this.applications = copyOf(checkNotNull(applications));
48 }
49
50 public Document getDescriptor()
51 {
52 return descriptor;
53 }
54
55 private Element getPluginElement()
56 {
57 return descriptor.getRootElement();
58 }
59
60 public String getPluginKey()
61 {
62 return getPluginElement().attributeValue("key");
63 }
64
65 public String getPluginName()
66 {
67 return getPluginElement().attributeValue("name");
68 }
69
70 public boolean isSystemPlugin()
71 {
72 return Boolean.valueOf(getPluginElement().attributeValue("system"));
73 }
74
75 public Option<String> getI18nPluginNameKey()
76 {
77 return Option.option(getPluginElement().attributeValue("i18n-name-key"));
78 }
79
80 public boolean isEnabledByDefault()
81 {
82 return !"disabled".equalsIgnoreCase(getPluginElement().attributeValue("state"));
83 }
84
85 public Option<Element> getPluginInformation()
86 {
87 return findFirst(elements(getPluginElement()), new ElementWithName(PLUGIN_INFO));
88 }
89
90 public PluginInformationReader getPluginInformationReader()
91 {
92 return new PluginInformationReader(getPluginInformation(), applications, getPluginsVersion());
93 }
94
95 public Iterable<Element> getModules(final InstallationMode installationMode)
96 {
97 return filter(filter(elements(getPluginElement()), Predicates.not(Predicates.or(new ElementWithName(PLUGIN_INFO), new ElementWithName(RESOURCE)))),
98 new Predicate<Element>()
99 {
100 @Override
101 public boolean apply(Element module)
102 {
103 if (!PluginUtils.doesModuleElementApplyToApplication(module, applications, installationMode))
104 {
105 log.debug("Ignoring module descriptor for this application: {}", module.attributeValue("key"));
106 return false;
107 }
108 return true;
109 }
110 });
111 }
112
113 public Iterable<ModuleReader> getModuleReaders(InstallationMode installationMode)
114 {
115 return transform(getModules(installationMode), new Function<Element, ModuleReader>()
116 {
117 @Override
118 public ModuleReader apply(Element module)
119 {
120 return new ModuleReader(module);
121 }
122 });
123 }
124
125 public Resources getResources()
126 {
127 return Resources.fromXml(getPluginElement());
128 }
129
130 public int getPluginsVersion()
131 {
132 String val = getPluginElement().attributeValue("pluginsVersion");
133 if (val == null)
134 {
135 val = getPluginElement().attributeValue("plugins-version");
136 }
137 if (val != null)
138 {
139 try
140 {
141 return Integer.parseInt(val);
142 }
143 catch (final NumberFormatException e)
144 {
145 throw new RuntimeException("Could not parse pluginsVersion: " + e.getMessage(), e);
146 }
147 }
148 else
149 {
150 return Plugin.VERSION_1;
151 }
152 }
153
154 @SuppressWarnings("unchecked")
155 static List<Element> elements(Element e)
156 {
157 return e.elements();
158 }
159
160 @SuppressWarnings("unchecked")
161 static List<Element> elements(Element e, String name)
162 {
163 return e != null ? e.elements(name) : ImmutableList.of();
164 }
165
166 private static final class ElementWithName implements Predicate<Element>
167 {
168 private final String name;
169
170 private ElementWithName(String name)
171 {
172 this.name = checkNotNull(name);
173 }
174
175 @Override
176 public boolean apply(@Nullable Element element)
177 {
178 return element != null && name.equalsIgnoreCase(element.getName());
179 }
180 }
181 }