1
package games.strategy.engine.framework.mapDownload;
3
import games.strategy.util.Version;
5
import java.io.InputStream;
6
import java.net.MalformedURLException;
8
import java.util.ArrayList;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Locale;
14
import javax.xml.parsers.SAXParser;
15
import javax.xml.parsers.SAXParserFactory;
17
import org.xml.sax.InputSource;
18
import org.xml.sax.SAXException;
19
import org.xml.sax.SAXParseException;
20
import org.xml.sax.helpers.DefaultHandler;
22
public class DownloadFileParser {
25
public List<DownloadFileDescription> parse(InputStream is, final String hostedUrl) {
26
final List<DownloadFileDescription> rVal = new ArrayList<DownloadFileDescription>();
30
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
31
parser.parse(new InputSource(is), new DefaultHandler() {
34
private StringBuilder content = new StringBuilder();
36
private String description;
37
private String mapName;
38
private Version version;
42
public void characters(char[] ch, int start, int length)
44
content.append(ch,start,length);
48
public void endElement(String uri, String localName,
49
String qName) throws SAXException {
50
String elementName = qName;
51
if(elementName.equals("description")) {
52
description = content.toString().trim();
53
} else if(elementName.equals("url")) {
54
url = content.toString().trim();
55
} else if(elementName.equals("mapName")) {
56
mapName = content.toString().trim();
57
} else if(elementName.equals("version")) {
58
this.version = new Version(content.toString().trim());
60
else if(elementName.equals("game")) {
61
rVal.add(new DownloadFileDescription(url, description, mapName, version, hostedUrl));
62
//clear optional properties
64
} else if(!elementName.equals("games")) {
65
throw new IllegalStateException("unexpected tag:" + elementName);
67
content = new StringBuilder();
70
} catch(RuntimeException e) {
72
} catch(SAXParseException e) {
73
throw new IllegalStateException("Could not parse xml error at line:" + e.getLineNumber() + " column:" + e.getColumnNumber() + " error:" + e.getMessage());
76
throw new IllegalStateException(e);
85
private void validate(final List<DownloadFileDescription> downloads) {
86
Set<String> urls = new HashSet<String>();
87
Set<String> names = new HashSet<String>();
88
for(DownloadFileDescription d : downloads) {
90
if(isEmpty(d.getUrl())) {
91
throw new IllegalStateException("Missing game url");
96
if(isEmpty(d.getDescription())) {
97
throw new IllegalStateException("Missing game description");
99
if(isEmpty(d.getMapName())) {
100
throw new IllegalStateException("Missing map name");
102
if(!names.add(d.getMapName())) {
103
throw new IllegalStateException("duplicate mapName:" + d.getMapName());
106
if(!urls.add(d.getUrl())) {
107
throw new IllegalStateException("duplicate url:" + d.getUrl());
110
URL url = new URL(d.getUrl());
111
if(!url.getProtocol().toLowerCase(Locale.ENGLISH).startsWith("http")) {
112
throw new IllegalStateException("Url must start with http:" + url);
114
} catch (MalformedURLException e) {
115
throw new IllegalStateException(e);
121
public boolean isEmpty(String s) {
122
return s == null || s.trim().length() == 0;