2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.updatecenter.common;
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.Date;
30 import java.util.Properties;
32 import static org.sonar.updatecenter.common.FormatUtils.toDate;
34 public final class UpdateCenterDeserializer {
36 private UpdateCenterDeserializer() {
37 // only static methods
40 public static UpdateCenter fromProperties(File file) throws IOException {
41 FileInputStream in = FileUtils.openInputStream(file);
43 Properties props = new Properties();
45 UpdateCenter center = fromProperties(props);
46 center.setDate(new Date(file.lastModified()));
50 IOUtils.closeQuietly(in);
54 public static UpdateCenter fromProperties(Properties p) {
55 UpdateCenter center = new UpdateCenter();
56 center.setDate(FormatUtils.toDate(p.getProperty("date"), true));
57 String[] sonarVersions = getArray(p, "sonar.versions");
58 for (String sonarVersion : sonarVersions) {
59 Release release = new Release(center.getSonar(), sonarVersion);
60 release.setChangelogUrl(get(p, "sonar." + sonarVersion + ".changelogUrl"));
61 release.setDescription(get(p, "sonar." + sonarVersion + ".description"));
62 release.setDownloadUrl(get(p, "sonar." + sonarVersion + ".downloadUrl"));
63 release.setDate(FormatUtils.toDate(get(p, "sonar." + sonarVersion + ".date"), false));
64 center.getSonar().addRelease(release);
67 String[] pluginKeys = getArray(p, "plugins");
68 for (String pluginKey : pluginKeys) {
69 Plugin plugin = new Plugin(pluginKey);
70 center.addPlugin(plugin);
71 plugin.setName(get(p, pluginKey, "name"));
72 plugin.setDescription(get(p, pluginKey, "description"));
73 plugin.setCategory(get(p, pluginKey, "category"));
74 plugin.setHomepageUrl(get(p, pluginKey, "homepageUrl"));
75 plugin.setLicense(get(p, pluginKey, "license"));
76 plugin.setOrganization(get(p, pluginKey, "organization"));
77 plugin.setOrganizationUrl(get(p, pluginKey, "organizationUrl"));
78 plugin.setTermsConditionsUrl(get(p, pluginKey, "termsConditionsUrl"));
79 plugin.setIssueTrackerUrl(get(p, pluginKey, "issueTrackerUrl"));
81 String[] pluginReleases = StringUtils.split(StringUtils.defaultIfEmpty(get(p, pluginKey, "versions"), ""), ",");
82 for (String pluginVersion : pluginReleases) {
83 Release release = new Release(plugin, pluginVersion);
84 plugin.addRelease(release);
85 release.setDownloadUrl(get(p, pluginKey, pluginVersion + ".downloadUrl"));
86 release.setChangelogUrl(get(p, pluginKey, pluginVersion + ".changelogUrl"));
87 release.setDescription(get(p, pluginKey, pluginVersion + ".description"));
88 release.setDate(toDate(get(p, pluginKey, pluginVersion + ".date"), false));
89 String[] requiredSonarVersions = StringUtils.split(StringUtils.defaultIfEmpty(get(p, pluginKey, pluginVersion + ".requiredSonarVersions"), ""), ",");
90 for (String requiredSonarVersion : requiredSonarVersions) {
91 release.addRequiredSonarVersions(Version.create(requiredSonarVersion));
99 private static String get(Properties props, String key) {
100 return StringUtils.defaultIfEmpty(props.getProperty(key), null);
103 private static String[] getArray(Properties props, String key) {
104 return StringUtils.split(StringUtils.defaultIfEmpty(props.getProperty(key), ""), ",");
107 private static String get(Properties p, String pluginKey, String field) {
108 return get(p, pluginKey + "." + field);