ModUpdater/src/main/java/com/thebrokenrail/modupdater/strategy/MavenStrategy.java

119 lines
4.1 KiB
Java
Raw Normal View History

2020-06-24 02:04:22 +00:00
package com.thebrokenrail.modupdater.strategy;
import com.thebrokenrail.modupdater.ModUpdater;
2020-06-25 20:56:09 +00:00
import com.thebrokenrail.modupdater.api.ConfigObject;
import com.thebrokenrail.modupdater.api.UpdateStrategy;
2020-06-28 17:43:17 +00:00
import com.thebrokenrail.modupdater.data.ModUpdate;
2020-06-24 02:04:22 +00:00
import com.thebrokenrail.modupdater.util.Util;
import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.util.version.VersionParsingException;
2020-06-29 15:01:40 +00:00
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
2020-06-24 02:04:22 +00:00
2020-06-25 20:56:09 +00:00
import javax.annotation.Nullable;
2020-06-29 15:01:40 +00:00
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
2020-06-25 02:05:10 +00:00
import java.io.ByteArrayInputStream;
2020-06-25 01:54:28 +00:00
import java.io.IOException;
2020-06-25 13:16:27 +00:00
import java.io.InputStream;
2020-06-24 02:04:22 +00:00
2020-06-25 20:56:09 +00:00
public class MavenStrategy implements UpdateStrategy {
2020-06-29 15:01:40 +00:00
private final DocumentBuilder builder;
public MavenStrategy() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
2020-06-27 17:56:22 +00:00
2020-06-24 02:04:22 +00:00
@Override
2020-06-25 20:56:09 +00:00
@Nullable
2020-08-05 17:06:45 +00:00
public ModUpdate run(ConfigObject obj, String oldVersion, String name, String id) {
2020-06-24 02:04:22 +00:00
String repository;
String group;
String artifact;
try {
repository = obj.getString("repository");
group = obj.getString("group");
artifact = obj.getString("artifact");
} catch (ConfigObject.MissingValueException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.getMessage());
2020-06-24 02:04:22 +00:00
return null;
}
2020-06-24 18:21:26 +00:00
String mavenRoot = String.format("%s/%s/%s", repository, group.replaceAll("\\.", "/"), artifact);
2020-06-24 02:04:22 +00:00
2020-06-25 01:54:28 +00:00
String data;
try {
data = Util.urlToString(mavenRoot + "/maven-metadata.xml");
} catch (IOException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.toString());
2020-06-25 01:54:28 +00:00
return null;
}
2020-06-24 02:04:22 +00:00
Document doc;
2020-06-25 13:16:27 +00:00
try (InputStream source = new ByteArrayInputStream(data.getBytes())) {
2020-06-29 15:01:40 +00:00
doc = builder.parse(source);
} catch (IOException | SAXException e) {
2020-06-27 17:56:22 +00:00
ModUpdater.logWarn(name, e.toString());
2020-06-25 13:16:27 +00:00
return null;
2020-06-24 02:04:22 +00:00
}
2020-06-29 15:01:40 +00:00
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList versions;
try {
versions = (NodeList) xPath.compile("/metadata/versioning/versions/*").evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
ModUpdater.logWarn(name, e.toString());
return null;
}
2020-06-24 02:04:22 +00:00
2020-07-25 18:10:06 +00:00
boolean strict = isStrict(obj);
2020-06-24 02:04:22 +00:00
String newestVersion = null;
2020-06-29 15:01:40 +00:00
for (int i = 0; i < versions.getLength(); i++) {
Node node = versions.item(i);
String version = node.getTextContent();
2020-08-05 17:06:45 +00:00
if (Util.isVersionCompatible(id, version, strict)) {
2020-06-24 02:04:22 +00:00
if (newestVersion != null) {
try {
if (SemanticVersion.parse(version).compareTo(SemanticVersion.parse(newestVersion)) > 0) {
newestVersion = version;
}
} catch (VersionParsingException ignored) {
}
} else {
newestVersion = version;
}
}
}
if (newestVersion != null) {
try {
if (SemanticVersion.parse(newestVersion).compareTo(SemanticVersion.parse(oldVersion)) > 0) {
2020-06-25 13:16:27 +00:00
return new ModUpdate(oldVersion, newestVersion, String.format("%s/%s/%s-%s%s", mavenRoot, newestVersion, artifact, newestVersion, Util.JAR_EXTENSION), name);
2020-06-24 02:04:22 +00:00
} else {
return null;
}
} catch (VersionParsingException e) {
return null;
}
} else {
return null;
}
}
}