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