feat: 使用SimpleJson解析仓库数据

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-08-11 20:39:37 +08:00
parent 5c59bb1813
commit 31a2277672
4 changed files with 92 additions and 28 deletions

View File

@ -12,7 +12,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.CommonData.UpdatePlugin; import cn.citycraft.CommonData.UpdatePlugin;
import cn.citycraft.PluginHelper.kit.PluginKit; import cn.citycraft.PluginHelper.kit.PluginKit;
import cn.citycraft.PluginHelper.utils.VersionChecker;
import pw.yumc.Yum.api.YumAPI; import pw.yumc.Yum.api.YumAPI;
import pw.yumc.Yum.commands.FileCommand; import pw.yumc.Yum.commands.FileCommand;
import pw.yumc.Yum.commands.MonitorCommand; import pw.yumc.Yum.commands.MonitorCommand;
@ -27,6 +26,7 @@ import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.MonitorManager; import pw.yumc.Yum.managers.MonitorManager;
import pw.yumc.Yum.managers.NetworkManager; import pw.yumc.Yum.managers.NetworkManager;
import pw.yumc.Yum.runnables.MainThreadCheckTask; import pw.yumc.Yum.runnables.MainThreadCheckTask;
import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.update.SubscribeTask; import pw.yumc.YumCore.update.SubscribeTask;
/** /**
@ -63,7 +63,7 @@ public class Yum extends JavaPlugin {
initListeners(); initListeners();
initRunnable(); initRunnable();
MonitorManager.init(); MonitorManager.init();
new VersionChecker(this); new Statistics();
new SubscribeTask(); new SubscribeTask();
YumAPI.updateRepo(Bukkit.getConsoleSender()); YumAPI.updateRepo(Bukkit.getConsoleSender());
YumAPI.updateCheck(Bukkit.getConsoleSender()); YumAPI.updateCheck(Bukkit.getConsoleSender());

View File

@ -11,8 +11,9 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import cn.citycraft.PluginHelper.jsonresult.JsonHandle;
import cn.citycraft.PluginHelper.kit.HttpKit; import cn.citycraft.PluginHelper.kit.HttpKit;
import cn.citycraft.PluginHelper.kit.StrKit; import cn.citycraft.PluginHelper.kit.StrKit;
import pw.yumc.Yum.models.PluginInfo; import pw.yumc.Yum.models.PluginInfo;
@ -154,15 +155,7 @@ public class RepositoryManager {
} }
public PackageInfo jsonToPackage(final String json) { public PackageInfo jsonToPackage(final String json) {
try { return new PackageInfo((JSONObject) JSONValue.parse(json));
return JsonHandle.fromJson(json, PackageInfo.class);
} catch (final Exception e) {
return null;
}
}
public Repositories jsonToRepositories(final String json) {
return JsonHandle.fromJson(json, Repositories.class);
} }
public void updatePackage(final CommandSender sender, final PackageInfo pkg) { public void updatePackage(final CommandSender sender, final PackageInfo pkg) {

View File

@ -7,7 +7,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import cn.citycraft.PluginHelper.jsonresult.JsonHandle; import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import cn.citycraft.PluginHelper.kit.PluginKit; import cn.citycraft.PluginHelper.kit.PluginKit;
import cn.citycraft.PluginHelper.utils.IOUtil; import cn.citycraft.PluginHelper.utils.IOUtil;
import pw.yumc.Yum.models.RepoSerialization.Repositories; import pw.yumc.Yum.models.RepoSerialization.Repositories;
@ -16,10 +18,6 @@ public class RepoCache implements Serializable {
Map<String, PluginInfo> plugins = new HashMap<String, PluginInfo>(); Map<String, PluginInfo> plugins = new HashMap<String, PluginInfo>();
Map<String, Repositories> repos = new HashMap<String, Repositories>(); Map<String, Repositories> repos = new HashMap<String, Repositories>();
public static RepoCache fromJson(final String json) {
return JsonHandle.fromJson(json, RepoCache.class);
}
public void addPlugins(final String name, final PluginInfo info) { public void addPlugins(final String name, final PluginInfo info) {
plugins.put(name, info); plugins.put(name, info);
} }
@ -54,7 +52,7 @@ public class RepoCache implements Serializable {
PluginKit.sc("§c源地址获取数据为空 §b" + repo); PluginKit.sc("§c源地址获取数据为空 §b" + repo);
return null; return null;
} }
final Repositories reposes = JsonHandle.fromJson(json, Repositories.class); final Repositories reposes = new Repositories((JSONObject) JSONValue.parse(json));
if (reposes == null || reposes.repos.isEmpty()) { if (reposes == null || reposes.repos.isEmpty()) {
PluginKit.sc("§c源地址解析Json为空 §b" + repo); PluginKit.sc("§c源地址解析Json为空 §b" + repo);
return null; return null;
@ -73,9 +71,4 @@ public class RepoCache implements Serializable {
repos.remove(repo); repos.remove(repo);
return true; return true;
} }
@Override
public String toString() {
return JsonHandle.toJson(this);
}
} }

View File

@ -4,9 +4,14 @@
package pw.yumc.Yum.models; package pw.yumc.Yum.models;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
/** /**
* 源仓库序列化类 * 源仓库序列化类
* *
@ -14,15 +19,41 @@ import java.util.List;
* @since 2015年8月31日下午7:41:53 * @since 2015年8月31日下午7:41:53
*/ */
public class RepoSerialization { public class RepoSerialization {
public class PackageInfo implements Serializable { @SuppressWarnings("unchecked")
public static <E> List<E> parse(final String json, final Class<?> clazz) {
if (json == null || "null".equals(json) || json.isEmpty()) {
return null;
}
final List<E> temp = new ArrayList<>();
final JSONArray ja = (JSONArray) JSONValue.parse(json);
for (int i = 0; i < ja.size(); i++) {
try {
temp.add((E) clazz.getConstructor(JSONObject.class).newInstance((JSONObject) ja.get(i)));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
return temp;
}
public static class PackageInfo implements Serializable {
public String name; public String name;
public List<Plugin> plugins = new ArrayList<>(); public List<Plugin> plugins = new ArrayList<>();
public String pom; public String pom;
public String url; public String url;
public URLType type; public URLType type;
public PackageInfo(final JSONObject obj) {
name = String.valueOf(obj.get("name"));
plugins = Plugin.parseList(String.valueOf(obj.get("plugins")));
pom = String.valueOf(obj.get("pom"));
url = String.valueOf(obj.get("url"));
final Object tt = obj.get("type");
type = tt == null ? null : URLType.valueOf(tt.toString());
}
} }
public class Plugin implements Serializable { public static class Plugin implements Serializable {
public String artifactId; public String artifactId;
public String branch; public String branch;
public String description; public String description;
@ -33,28 +64,75 @@ public class RepoSerialization {
public List<TagInfo> tags; public List<TagInfo> tags;
public String version; public String version;
public URLType type; public URLType type;
public Plugin(final JSONObject obj) {
artifactId = String.valueOf(obj.get("artifactId"));
branch = String.valueOf(obj.get("branch"));
description = String.valueOf(obj.get("description"));
groupId = String.valueOf(obj.get("groupId"));
name = String.valueOf(obj.get("name"));
url = String.valueOf(obj.get("url"));
pom = String.valueOf(obj.get("pom"));
tags = TagInfo.parseList(String.valueOf(obj.get("tags")));
version = String.valueOf(obj.get("version"));
final Object tt = obj.get("type");
type = tt == null ? null : URLType.valueOf(tt.toString());
}
public static List<Plugin> parseList(final String json) {
return parse(json, Plugin.class);
}
} }
public class Repositories implements Serializable { public static class Repositories implements Serializable {
public String name; public String name;
public List<Repository> repos; public List<Repository> repos;
public Repositories(final JSONObject obj) {
name = String.valueOf(obj.get("name"));
repos = Repository.parseList(String.valueOf(obj.get("repos")));
}
} }
public class Repository implements Serializable { public static class Repository implements Serializable {
public String id; public String id;
public URLType type; public URLType type;
public String url; public String url;
public Repository(final JSONObject obj) {
id = String.valueOf(obj.get("id"));
final Object tt = obj.get("type");
type = tt == null ? null : URLType.valueOf(tt.toString());
url = String.valueOf(obj.get("url"));
}
public static List<Repository> parseList(final String json) {
return parse(json, Repository.class);
}
} }
public class TagInfo implements Serializable { public static class TagInfo implements Serializable {
public String tag; public String tag;
public String version; public String version;
public URLType type; public URLType type;
public String url; public String url;
public TagInfo(final JSONObject obj) {
tag = String.valueOf(obj.get("tag"));
version = String.valueOf(obj.get("version"));
final Object tt = obj.get("type");
type = tt == null ? null : URLType.valueOf(tt.toString());
url = String.valueOf(obj.get("url"));
}
public static List<TagInfo> parseList(final String json) {
return parse(json, TagInfo.class);
}
} }
public enum URLType { public enum URLType {
Maven, Maven,
maven,
DirectUrl; DirectUrl;
} }
} }