~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/framework/mapDownload/DownloadFileParser.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package games.strategy.engine.framework.mapDownload;
 
2
 
 
3
import games.strategy.util.Version;
 
4
 
 
5
import java.io.InputStream;
 
6
import java.net.MalformedURLException;
 
7
import java.net.URL;
 
8
import java.util.ArrayList;
 
9
import java.util.HashSet;
 
10
import java.util.List;
 
11
import java.util.Locale;
 
12
import java.util.Set;
 
13
 
 
14
import javax.xml.parsers.SAXParser;
 
15
import javax.xml.parsers.SAXParserFactory;
 
16
 
 
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;
 
21
 
 
22
public class DownloadFileParser {
 
23
 
 
24
        
 
25
        public List<DownloadFileDescription> parse(InputStream is, final String hostedUrl) {
 
26
                final List<DownloadFileDescription> rVal = new ArrayList<DownloadFileDescription>();
 
27
                
 
28
                try
 
29
                {
 
30
                        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
 
31
                        parser.parse(new InputSource(is), new DefaultHandler() {
 
32
 
 
33
                                
 
34
                                private StringBuilder content = new StringBuilder();
 
35
                                private String url;
 
36
                                private String description;
 
37
                                private String mapName;
 
38
                                private Version version;
 
39
                                
 
40
                                
 
41
                                @Override
 
42
                                public void characters(char[] ch, int start, int length)
 
43
                                                throws SAXException {
 
44
                                        content.append(ch,start,length);
 
45
                                }
 
46
 
 
47
                                @Override
 
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());
 
59
                                        }
 
60
                                        else if(elementName.equals("game")) {                                           
 
61
                                                rVal.add(new DownloadFileDescription(url, description, mapName, version, hostedUrl));
 
62
                                                //clear optional properties
 
63
                                                version = null;
 
64
                                        } else if(!elementName.equals("games")) {
 
65
                                                throw new IllegalStateException("unexpected tag:" + elementName);
 
66
                                        }
 
67
                                        content = new StringBuilder();
 
68
                                }
 
69
                        });
 
70
                } catch(RuntimeException e) { 
 
71
                        throw e;
 
72
                } catch(SAXParseException e) {
 
73
                        throw new IllegalStateException("Could not parse xml error at line:" + e.getLineNumber() + " column:" + e.getColumnNumber() + " error:" + e.getMessage());      
 
74
                }               
 
75
                catch(Exception e) {
 
76
                        throw new IllegalStateException(e);
 
77
                }
 
78
                
 
79
                validate(rVal);
 
80
                
 
81
                return rVal;
 
82
                
 
83
        }
 
84
 
 
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) {
 
89
                        
 
90
                        if(isEmpty(d.getUrl())) {
 
91
                                throw new IllegalStateException("Missing game url");
 
92
                        }
 
93
                        
 
94
                        //ignore urls
 
95
                        if(!d.isDummyUrl()) {
 
96
                                if(isEmpty(d.getDescription())) {
 
97
                                        throw new IllegalStateException("Missing game description");
 
98
                                }
 
99
                                if(isEmpty(d.getMapName())) {
 
100
                                        throw new IllegalStateException("Missing map name");
 
101
                                }
 
102
                                if(!names.add(d.getMapName())) {
 
103
                                        throw new IllegalStateException("duplicate mapName:" + d.getMapName());
 
104
                                }
 
105
                                
 
106
                                if(!urls.add(d.getUrl())) {
 
107
                                        throw new IllegalStateException("duplicate url:" + d.getUrl());
 
108
                                }
 
109
                                try {
 
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);
 
113
                                        }
 
114
                                } catch (MalformedURLException e) {
 
115
                                        throw new IllegalStateException(e);
 
116
                                }
 
117
                        }
 
118
                }
 
119
        }
 
120
        
 
121
        public boolean isEmpty(String s) { 
 
122
                return s == null || s.trim().length() == 0;
 
123
        }
 
124
        
 
125
}