Skip to content

Commit

Permalink
- not throwing exceptions on parsing as errors
Browse files Browse the repository at this point in the history
- preventing duplicate description retrieval
  • Loading branch information
Idrinth committed May 28, 2021
1 parent f15bd89 commit 28e48eb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.idrinth</groupId>
<artifactId>WARAddonClient</artifactId>
<version>1.12.2</version>
<version>1.13.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.idrinth.waraddonclient.model.InvalidArgumentException;
import de.idrinth.waraddonclient.service.logger.BaseLogger;
import de.idrinth.waraddonclient.service.Request;
import de.idrinth.waraddonclient.service.SilencingErrorHandler;
import de.idrinth.waraddonclient.service.XmlParser;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class ActualAddon implements de.idrinth.waraddonclient.model.addon.Addon
private final Config config;

private String defaultDescription = "";

private boolean loadedDescription = false;

public ActualAddon(javax.json.JsonObject addon, Request client, BaseLogger logger, XmlParser parser, Config config) throws InvalidArgumentException {
if (addon == null) {
Expand Down Expand Up @@ -101,7 +104,7 @@ private File find(String name) {

}
}
return new File(config.getAddonFolder() + name);
return new File(config.getAddonFolder() + name);
}

public java.util.ArrayList<String> getTags() {
Expand Down Expand Up @@ -186,9 +189,10 @@ public String getDescription(String language) {
}

public Runnable loadDescription(JEditorPane field, String language) {
if (!descriptions.isEmpty()) {
if (!descriptions.isEmpty() || loadedDescription) {
return () -> {};
}
loadedDescription = true;
return () -> {
try {
JsonObject addon = client.getAddon(slug);
Expand All @@ -197,6 +201,7 @@ public Runnable loadDescription(JEditorPane field, String language) {
descriptions.put("fr", getStringFromObject("description_fr", addon));
field.setText(getDescription(language));
} catch (IOException ex) {
loadedDescription = false;
logger.error("Failed loading addon-data from server.");
}
};
Expand Down Expand Up @@ -359,7 +364,7 @@ private boolean processDirectory() {
if (!fileEntry.isDirectory()
&& org.apache.commons.io.FilenameUtils.getExtension(fileEntry.getName()).equalsIgnoreCase("mod")) {
try {
Document doc = parser.parse(fileEntry);
Document doc = parser.parse(fileEntry, new SilencingErrorHandler(logger));
NodeList list = doc.getElementsByTagName("UiMod");
installed = list.item(0).getAttributes().getNamedItem("version").getTextContent();
NodeList description = doc.getElementsByTagName("Description");
Expand All @@ -368,7 +373,7 @@ private boolean processDirectory() {
}
return true;
} catch (FactoryConfigurationError | SAXException | IOException exception) {
logger.error(exception);
logger.warn(exception);
}
}
}
Expand All @@ -386,7 +391,7 @@ private boolean getDownloadVersion() {
installed = list.item(0).getTextContent().replace("(sys)", "");
return true;
} catch (FactoryConfigurationError | SAXException | IOException exception) {
logger.error(exception);
logger.warn(exception);
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.idrinth.waraddonclient.service.Config;
import de.idrinth.waraddonclient.service.logger.BaseLogger;
import de.idrinth.waraddonclient.service.Request;
import de.idrinth.waraddonclient.service.SilencingErrorHandler;
import de.idrinth.waraddonclient.service.XmlParser;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -54,7 +55,7 @@ public UnknownAddon(File folder, Request client, BaseLogger logger, XmlParser pa
for (java.io.File fileEntry : folder.listFiles()) {
if (!fileEntry.isDirectory() && FilenameUtils.getExtension(fileEntry.getName()).equalsIgnoreCase("mod")) {
try {
Document doc = parser.parse(fileEntry);
Document doc = parser.parse(fileEntry, new SilencingErrorHandler(logger));
NodeList list = doc.getElementsByTagName("UiMod");
installed = list.item(0).getAttributes().getNamedItem("version").getTextContent();
name = list.item(0).getAttributes().getNamedItem("name").getTextContent();
Expand All @@ -81,12 +82,6 @@ public ArrayList<String> getTags() {
return list;
}

/**
* does this addon have the given tag?
*
* @param tag
* @return boolean
*/
public boolean hasTag(String tag) {
return "Not Tagged".equals(tag) || "Auto-Discovered".equals(tag);
}
Expand Down Expand Up @@ -115,23 +110,12 @@ public Object[] getTableRow() {
return row;
}

/**
* return a languages description if avaible, otherwise a default
*
* @param language
* @return String
*/
public String getDescription(String language) {
return "<p><strong>There is currently no Description for " + name + ".</strong></p>"
+ "<p>You can help by adding the addon and one at <a href=\"http://tools.idrinth.de/addons/\">http://tools.idrinth.de/addons/</a>.</p>"
+ "<p>"+defaultDescription+"</p>";
}

/**
* get a list of descriptions
*
* @return java.util.HashMap
*/
public HashMap<String, String> getDescriptions() {
return new HashMap<>();
}
Expand Down Expand Up @@ -232,11 +216,11 @@ public void install() throws IOException {

@Override
public int getEndorsements() {
return 0;
return -1;
}

@Override
public int getDownloads() {
return 0;
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.idrinth.waraddonclient.service;

import de.idrinth.waraddonclient.service.logger.BaseLogger;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class SilencingErrorHandler implements ErrorHandler {
private final BaseLogger logger;

public SilencingErrorHandler(BaseLogger logger) {
this.logger = logger;
}

@Override
public void warning(SAXParseException exception) throws SAXException {
logger.info(exception);
}

@Override
public void error(SAXParseException exception) throws SAXException {
logger.warn(exception);
}

@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw new SAXException(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

public class XmlParser {
Expand All @@ -17,10 +18,16 @@ public XmlParser() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
factory.setAttribute(XMLConstants.FEATURE_SECURE_PROCESSING, true);
builder = factory.newDocumentBuilder();
}

public Document parse(File input) throws SAXException, IOException {
return builder.parse(input);
}

public Document parse(File input, ErrorHandler handler) throws SAXException, IOException {
builder.setErrorHandler(handler);
return builder.parse(input);
}
}

0 comments on commit 28e48eb

Please sign in to comment.