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