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