]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/XmlFileLoader.java
4170dc9b747b9e4380d1832b156393b68a238743
[GpsPrune.git] / tim / prune / load / xml / XmlFileLoader.java
1 package tim.prune.load.xml;
2
3 import java.io.File;
4 import javax.xml.parsers.SAXParser;
5 import javax.xml.parsers.SAXParserFactory;
6
7 import org.xml.sax.Attributes;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.DefaultHandler;
10 import tim.prune.App;
11 import tim.prune.I18nManager;
12 import tim.prune.data.Altitude;
13 import tim.prune.data.SourceInfo;
14
15 /**
16  * Class for handling loading of Xml files, and passing the
17  * loaded data back to the App object
18  */
19 public class XmlFileLoader extends DefaultHandler implements Runnable
20 {
21         private File _file = null;
22         private App _app = null;
23         private XmlHandler _handler = null;
24         private String _unknownType = null;
25
26
27         /**
28          * Constructor
29          * @param inApp Application object to inform of track load
30          */
31         public XmlFileLoader(App inApp)
32         {
33                 _app = inApp;
34         }
35
36         /**
37          * Reset the handler to ensure data cleared
38          */
39         public void reset()
40         {
41                 _handler = null;
42                 _unknownType = null;
43         }
44
45         /**
46          * Open the selected file
47          * @param inFile File to open
48          */
49         public void openFile(File inFile)
50         {
51                 _file = inFile;
52                 reset();
53                 // start new thread in case xml parsing is time-consuming
54                 new Thread(this).start();
55         }
56
57
58         /**
59          * Run method, to parse the file
60          * @see java.lang.Runnable#run()
61          */
62         public void run()
63         {
64                 try
65                 {
66                         // Construct a SAXParser and use this as a default handler
67                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
68                         saxParser.parse(_file, this);
69
70                         // Check whether handler was properly instantiated
71                         if (_handler == null)
72                         {
73                                 // Wasn't either kml or gpx
74                                 _app.showErrorMessageNoLookup("error.load.dialogtitle",
75                                         I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
76                         }
77                         else
78                         {
79                                 // Pass information back to app
80                                 SourceInfo sourceInfo = new SourceInfo(_file,
81                                         (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
82                                 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
83                                         Altitude.Format.METRES, sourceInfo, _handler.getTrackNameList());
84                         }
85                 }
86                 catch (Exception e)
87                 {
88                         // Show error dialog
89                         _app.showErrorMessageNoLookup("error.load.dialogtitle",
90                                 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
91                 }
92         }
93
94
95         /**
96          * Receive a tag
97          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
98          */
99         public void startElement(String uri, String localName, String qName,
100                 Attributes attributes) throws SAXException
101         {
102                 // Check for "kml" or "gpx" tags
103                 if (_handler == null)
104                 {
105                         if (qName.equals("kml")) {_handler = new KmlHandler();}
106                         else if (qName.equals("gpx")) {_handler = new GpxHandler();}
107                         else if (_unknownType == null && !qName.equals(""))
108                         {
109                                 _unknownType = qName;
110                         }
111                 }
112                 else
113                 {
114                         // Handler instantiated so pass tags on to it
115                         _handler.startElement(uri, localName, qName, attributes);
116                 }
117                 super.startElement(uri, localName, qName, attributes);
118         }
119
120
121         /**
122          * Receive characters, either between or inside tags
123          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
124          */
125         public void characters(char[] ch, int start, int length)
126                 throws SAXException
127         {
128                 if (_handler != null)
129                 {
130                         // Handler instantiated so pass tags on to it
131                         _handler.characters(ch, start, length);
132                 }
133                 super.characters(ch, start, length);
134         }
135
136
137         /**
138          * Receive end of element
139          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
140          */
141         public void endElement(String uri, String localName, String qName)
142                 throws SAXException
143         {
144                 if (_handler != null)
145                 {
146                         // Handler instantiated so pass tags on to it
147                         _handler.endElement(uri, localName, qName);
148                 }
149                 super.endElement(uri, localName, qName);
150         }
151
152         /**
153          * @return The Xml handler used for the parsing
154          */
155         public XmlHandler getHandler()
156         {
157                 return _handler;
158         }
159 }