1. Project Clover database Mon Aug 5 2019 23:32:56 UTC
  2. Package org.xwiki.extension.repository.xwiki.internal

File XWikiExtension.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
72% of files have more coverage

Code metrics

24
60
5
1
220
143
19
0.32
12
5
3.8

Classes

Class Line # Actions
XWikiExtension 60 60 0% 19 89
0.00%
 

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.URI;
25    import java.net.URISyntaxException;
26    import java.util.ArrayList;
27    import java.util.Collection;
28    import java.util.List;
29   
30    import org.apache.commons.io.IOUtils;
31    import org.xwiki.extension.AbstractRatingExtension;
32    import org.xwiki.extension.DefaultExtensionScm;
33    import org.xwiki.extension.DefaultExtensionScmConnection;
34    import org.xwiki.extension.Extension;
35    import org.xwiki.extension.ExtensionId;
36    import org.xwiki.extension.ExtensionLicense;
37    import org.xwiki.extension.ExtensionLicenseManager;
38    import org.xwiki.extension.internal.ExtensionFactory;
39    import org.xwiki.extension.internal.ExtensionUtils;
40    import org.xwiki.extension.rating.DefaultExtensionRating;
41    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
42    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionAuthor;
43    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionDependency;
44    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionIssueManagement;
45    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRating;
46    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRepository;
47    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionScm;
48    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionScmConnection;
49    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersion;
50    import org.xwiki.extension.repository.xwiki.model.jaxb.License;
51    import org.xwiki.extension.repository.xwiki.model.jaxb.Namespaces;
52    import org.xwiki.extension.repository.xwiki.model.jaxb.Property;
53   
54    /**
55    * XWiki Repository implementation of {@link org.xwiki.extension.Extension}.
56    *
57    * @version $Id: 3610b232676d47a81b55b925b81ddb976c32475d $
58    * @since 4.0M1
59    */
 
60    public class XWikiExtension extends AbstractRatingExtension
61    {
 
62  0 toggle public XWikiExtension(XWikiExtensionRepository repository, ExtensionVersion restExtension,
63    ExtensionLicenseManager licenseManager, ExtensionFactory factory)
64    {
65  0 super(repository, new ExtensionId(restExtension.getId(), factory.getVersion(restExtension.getVersion())),
66    restExtension.getType());
67   
68  0 setName(restExtension.getName());
69  0 setSummary(restExtension.getSummary());
70  0 setWebsite(restExtension.getWebsite());
71   
72  0 setDescription(restExtension.getDescription());
73   
74    // Features
75  0 for (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionId feature : restExtension
76    .getExtensionFeatures()) {
77  0 addExtensionFeature(new ExtensionId(feature.getId(),
78  0 feature.getVersion() != null ? factory.getVersion(feature.getVersion()) : getId().getVersion()));
79    }
80   
81    // Rating
82  0 ExtensionRating restRating = restExtension.getRating();
83  0 if (restRating != null) {
84  0 setRating(
85    new DefaultExtensionRating(restRating.getTotalVotes(), restRating.getAverageVote(), getRepository()));
86    }
87   
88    // Authors
89  0 for (ExtensionAuthor restAuthor : restExtension.getAuthors()) {
90  0 addAuthor(factory.getExtensionAuthor(restAuthor.getName(), restAuthor.getUrl()));
91    }
92   
93    // License
94   
95  0 for (License restLicense : restExtension.getLicenses()) {
96  0 if (restLicense.getName() != null) {
97  0 ExtensionLicense extensionLicense = licenseManager.getLicense(restLicense.getName());
98  0 if (extensionLicense != null) {
99  0 addLicense(extensionLicense);
100    } else {
101  0 List<String> content = null;
102  0 if (restLicense.getContent() != null) {
103  0 try {
104  0 content = IOUtils.readLines(new StringReader(restLicense.getContent()));
105    } catch (IOException e) {
106    // That should never happen
107    }
108    }
109   
110  0 addLicense(new ExtensionLicense(restLicense.getName(), content));
111    }
112    }
113    }
114   
115    // Scm
116   
117  0 ExtensionScm restSCM = restExtension.getScm();
118  0 if (restSCM != null) {
119  0 DefaultExtensionScmConnection connection = toDefaultExtensionScmConnection(restSCM.getConnection());
120  0 DefaultExtensionScmConnection developerConnection =
121    toDefaultExtensionScmConnection(restSCM.getDeveloperConnection());
122   
123  0 setScm(new DefaultExtensionScm(restSCM.getUrl(), connection, developerConnection));
124    }
125   
126    // Issue management
127   
128  0 ExtensionIssueManagement restIssueManagement = restExtension.getIssueManagement();
129  0 if (restIssueManagement != null) {
130  0 setIssueManagement(
131    factory.getExtensionIssueManagement(restIssueManagement.getSystem(), restIssueManagement.getUrl()));
132    }
133   
134    // Category
135  0 setCategory(restExtension.getCategory());
136   
137    // Allowed namespaces
138  0 Namespaces namespaces = restExtension.getAllowedNamespaces();
139  0 if (namespaces != null) {
140  0 setAllowedNamespaces(namespaces.getNamespaces());
141    }
142   
143    // Recommended
144  0 Boolean recommended = restExtension.isRecommended();
145  0 if (recommended != null) {
146  0 setRecommended(restExtension.isRecommended());
147    }
148   
149    // Properties
150  0 for (Property restProperty : restExtension.getProperties()) {
151  0 putProperty(restProperty.getKey(), restProperty.getStringValue());
152    }
153   
154    // Make sure the dependency will be resolved in the extension repository first
155  0 if (repository != null) {
156  0 addRepository(repository.getDescriptor());
157    }
158   
159    // Repositories
160  0 for (ExtensionRepository restRepository : restExtension.getRepositories()) {
161  0 try {
162  0 addRepository(toExtensionRepositoryDescriptor(restRepository, factory));
163    } catch (URISyntaxException e) {
164    // TODO: Log something ?
165    }
166    }
167   
168    // Dependencies
169  0 setDependencies(toXWikiExtensionDependencies(restExtension.getDependencies(), factory));
170   
171    // Managed dependencies
172  0 setManagedDependencies(toXWikiExtensionDependencies(restExtension.getManagedDependencies(), factory));
173   
174    // File
175   
176  0 setFile(new XWikiExtensionFile(repository, getId()));
177   
178    // XWiki Repository often act as a proxy and the source extension might have more information that the XWiki
179    // Repository version supports
180  0 setName(ExtensionUtils.importProperty(this, Extension.FIELD_NAME, getName()));
181  0 setSummary(ExtensionUtils.importProperty(this, Extension.FIELD_SUMMARY, getSummary()));
182  0 setWebsite(ExtensionUtils.importProperty(this, Extension.FIELD_WEBSITE, getWebSite()));
183  0 setAllowedNamespaces(ExtensionUtils.importProperty(this, Extension.FIELD_NAMESPACES, getAllowedNamespaces()));
184    }
185   
 
186  0 toggle private Collection<XWikiExtensionDependency> toXWikiExtensionDependencies(
187    List<ExtensionDependency> restDependencies, ExtensionFactory factory)
188    {
189  0 List<XWikiExtensionDependency> newDependencies = new ArrayList<>(restDependencies.size());
190   
191  0 for (ExtensionDependency dependency : restDependencies) {
192  0 newDependencies.add(new XWikiExtensionDependency(dependency,
193  0 this.repository != null ? this.repository.getDescriptor() : null, factory));
194    }
195   
196  0 return newDependencies;
197    }
198   
 
199  0 toggle protected static DefaultExtensionScmConnection toDefaultExtensionScmConnection(ExtensionScmConnection connection)
200    {
201  0 if (connection != null) {
202  0 return new DefaultExtensionScmConnection(connection.getSystem(), connection.getPath());
203    } else {
204  0 return null;
205    }
206    }
207   
 
208  0 toggle protected static ExtensionRepositoryDescriptor toExtensionRepositoryDescriptor(ExtensionRepository restRepository,
209    ExtensionFactory factory) throws URISyntaxException
210    {
211  0 return factory.getExtensionRepositoryDescriptor(restRepository.getId(), restRepository.getType(),
212    new URI(restRepository.getUri()));
213    }
214   
 
215  0 toggle @Override
216    public XWikiExtensionRepository getRepository()
217    {
218  0 return (XWikiExtensionRepository) super.getRepository();
219    }
220    }