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