1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.xwiki.internal

File XWikiExtension.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

24
64
5
1
229
151
20
0.31
12.8
5
4

Classes

Class Line # Actions
XWikiExtension 62 64 0% 20 20
0.7849462678.5%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.repository.xwiki.internal;
21   
22    import java.io.IOException;
23    import java.io.StringReader;
24    import java.net.MalformedURLException;
25    import java.net.URI;
26    import java.net.URISyntaxException;
27    import java.net.URL;
28    import java.util.ArrayList;
29    import java.util.Collection;
30    import java.util.List;
31   
32    import org.apache.commons.io.IOUtils;
33    import org.xwiki.extension.AbstractRatingExtension;
34    import org.xwiki.extension.DefaultExtensionScm;
35    import org.xwiki.extension.DefaultExtensionScmConnection;
36    import org.xwiki.extension.Extension;
37    import org.xwiki.extension.ExtensionId;
38    import org.xwiki.extension.ExtensionLicense;
39    import org.xwiki.extension.ExtensionLicenseManager;
40    import org.xwiki.extension.internal.ExtensionFactory;
41    import org.xwiki.extension.internal.ExtensionUtils;
42    import org.xwiki.extension.rating.DefaultExtensionRating;
43    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
44    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionAuthor;
45    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionDependency;
46    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionIssueManagement;
47    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRating;
48    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRepository;
49    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionScm;
50    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionScmConnection;
51    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersion;
52    import org.xwiki.extension.repository.xwiki.model.jaxb.License;
53    import org.xwiki.extension.repository.xwiki.model.jaxb.Namespaces;
54    import org.xwiki.extension.repository.xwiki.model.jaxb.Property;
55   
56    /**
57    * XWiki Repository implementation of {@link org.xwiki.extension.Extension}.
58    *
59    * @version $Id: 75de0a68dc3ed53e0cb57632f461059e11165449 $
60    * @since 4.0M1
61    */
 
62    public class XWikiExtension extends AbstractRatingExtension
63    {
 
64  67 toggle public XWikiExtension(XWikiExtensionRepository repository, ExtensionVersion restExtension,
65    ExtensionLicenseManager licenseManager, ExtensionFactory factory)
66    {
67  67 super(repository, new ExtensionId(restExtension.getId(), factory.getVersion(restExtension.getVersion())),
68    restExtension.getType());
69   
70  67 setName(restExtension.getName());
71  67 setSummary(restExtension.getSummary());
72  67 setWebsite(restExtension.getWebsite());
73   
74  67 setDescription(restExtension.getDescription());
75   
76    // Features
77  67 for (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionId feature : restExtension
78    .getExtensionFeatures()) {
79  11 addExtensionFeature(new ExtensionId(feature.getId(),
80  11 feature.getVersion() != null ? factory.getVersion(feature.getVersion()) : getId().getVersion()));
81    }
82   
83    // Rating
84  67 ExtensionRating restRating = restExtension.getRating();
85  67 if (restRating != null) {
86  67 setRating(
87    new DefaultExtensionRating(restRating.getTotalVotes(), restRating.getAverageVote(), getRepository()));
88    }
89   
90    // Authors
91  67 for (ExtensionAuthor restAuthor : restExtension.getAuthors()) {
92  22 URL url;
93  22 try {
94  22 url = new URL(restAuthor.getUrl());
95    } catch (MalformedURLException e) {
96  22 url = null;
97    }
98   
99  22 addAuthor(factory.getExtensionAuthor(restAuthor.getName(), url));
100    }
101   
102    // License
103   
104  67 for (License restLicense : restExtension.getLicenses()) {
105  56 if (restLicense.getName() != null) {
106  11 ExtensionLicense extensionLicense = licenseManager.getLicense(restLicense.getName());
107  11 if (extensionLicense != null) {
108  0 addLicense(extensionLicense);
109    } else {
110  11 List<String> content = null;
111  11 if (restLicense.getContent() != null) {
112  0 try {
113  0 content = IOUtils.readLines(new StringReader(restLicense.getContent()));
114    } catch (IOException e) {
115    // That should never happen
116    }
117    }
118   
119  11 addLicense(new ExtensionLicense(restLicense.getName(), content));
120    }
121    }
122    }
123   
124    // Scm
125   
126  67 ExtensionScm restSCM = restExtension.getScm();
127  67 if (restSCM != null) {
128  56 DefaultExtensionScmConnection connection = toDefaultExtensionScmConnection(restSCM.getConnection());
129  56 DefaultExtensionScmConnection developerConnection =
130    toDefaultExtensionScmConnection(restSCM.getDeveloperConnection());
131   
132  56 setScm(new DefaultExtensionScm(restSCM.getUrl(), connection, developerConnection));
133    }
134   
135    // Issue management
136   
137  67 ExtensionIssueManagement restIssueManagement = restExtension.getIssueManagement();
138  67 if (restIssueManagement != null) {
139  0 setIssueManagement(
140    factory.getExtensionIssueManagement(restIssueManagement.getSystem(), restIssueManagement.getUrl()));
141    }
142   
143    // Category
144  67 setCategory(restExtension.getCategory());
145   
146    // Allowed namespaces
147  67 Namespaces namespaces = restExtension.getAllowedNamespaces();
148  67 if (namespaces != null) {
149  0 setAllowedNamespaces(namespaces.getNamespaces());
150    }
151   
152    // Recommended
153  67 Boolean recommended = restExtension.isRecommended();
154  67 if (recommended != null) {
155  58 setRecommended(restExtension.isRecommended());
156    }
157   
158    // Properties
159  67 for (Property restProperty : restExtension.getProperties()) {
160  0 putProperty(restProperty.getKey(), restProperty.getStringValue());
161    }
162   
163    // Make sure the dependency will be resolved in the extension repository first
164  67 if (repository != null) {
165  67 addRepository(repository.getDescriptor());
166    }
167   
168    // Repositories
169  67 for (ExtensionRepository restRepository : restExtension.getRepositories()) {
170  0 try {
171  0 addRepository(toExtensionRepositoryDescriptor(restRepository, factory));
172    } catch (URISyntaxException e) {
173    // TODO: Log something ?
174    }
175    }
176   
177    // Dependencies
178  67 setDependencies(toXWikiExtensionDependencies(restExtension.getDependencies(), factory));
179   
180    // Managed dependencies
181  67 setManagedDependencies(toXWikiExtensionDependencies(restExtension.getManagedDependencies(), factory));
182   
183    // File
184   
185  67 setFile(new XWikiExtensionFile(repository, getId()));
186   
187    // XWiki Repository often act as a proxy and the source extension might have more information that the XWiki
188    // Repository version supports
189  67 setName(ExtensionUtils.importProperty(this, Extension.FIELD_NAME, getName()));
190  67 setSummary(ExtensionUtils.importProperty(this, Extension.FIELD_SUMMARY, getSummary()));
191  66 setWebsite(ExtensionUtils.importProperty(this, Extension.FIELD_WEBSITE, getWebSite()));
192  67 setAllowedNamespaces(ExtensionUtils.importProperty(this, Extension.FIELD_NAMESPACES, getAllowedNamespaces()));
193    }
194   
 
195  134 toggle private Collection<XWikiExtensionDependency> toXWikiExtensionDependencies(
196    List<ExtensionDependency> restDependencies, ExtensionFactory factory)
197    {
198  134 List<XWikiExtensionDependency> newDependencies = new ArrayList<>(restDependencies.size());
199   
200  134 for (ExtensionDependency dependency : restDependencies) {
201  41 newDependencies.add(new XWikiExtensionDependency(dependency,
202  41 this.repository != null ? this.repository.getDescriptor() : null, factory));
203    }
204   
205  134 return newDependencies;
206    }
207   
 
208  112 toggle protected static DefaultExtensionScmConnection toDefaultExtensionScmConnection(ExtensionScmConnection connection)
209    {
210  112 if (connection != null) {
211  0 return new DefaultExtensionScmConnection(connection.getSystem(), connection.getPath());
212    } else {
213  112 return null;
214    }
215    }
216   
 
217  0 toggle protected static ExtensionRepositoryDescriptor toExtensionRepositoryDescriptor(ExtensionRepository restRepository,
218    ExtensionFactory factory) throws URISyntaxException
219    {
220  0 return factory.getExtensionRepositoryDescriptor(restRepository.getId(), restRepository.getType(),
221    new URI(restRepository.getUri()));
222    }
223   
 
224  181 toggle @Override
225    public XWikiExtensionRepository getRepository()
226    {
227  181 return (XWikiExtensionRepository) super.getRepository();
228    }
229    }