File: XmlFileExtractor.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (146 lines) | stat: -rw-r--r-- 3,055 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package tim.prune.function.filesleuth.extract;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.TimeZone;

import tim.prune.function.filesleuth.data.LocationFilter;
import tim.prune.function.filesleuth.data.TrackContents;


/**
 * Responsible for extracting text from gpx/kml files
 * by receiving the contents from a separate XmlParser
 */
public class XmlFileExtractor implements ContentExtractor, ContentReceiver
{
	private final File _file;
	private final InputStream _stream;
	private TrackContents _contents = null;
	private LocationFilter _locationFilter = null;
	private final Object _mutex = new Object();
	private boolean _filterMatched = false;
	private boolean _finishedParsing = false;


	public XmlFileExtractor(File inFile)
	{
		_file = inFile;
		_stream = null;
	}

	public XmlFileExtractor(InputStream inStream)
	{
		_file = null;
		_stream = inStream;
	}

	@Override
	public TrackContents getContents(TimeZone inTimezone)
	{
		_contents = new TrackContents(inTimezone);
		_locationFilter = null;
		readFile();
		return _contents;
	}

	/** Read the file and block until it has been completely read */
	private void readFile()
	{
		synchronized(_mutex)
		{
			_finishedParsing = false;
			XmlParser parser = new XmlParser(this);
			if (_file != null)
			{
				try (FileInputStream inStream = new FileInputStream(_file))
				{
					if (!parser.parseXmlStream(inStream)) {
						_finishedParsing = true;
					}
				}
				catch (IOException ioe) {
					_finishedParsing = true;
				}
			}
			else if (_stream == null
				|| !parser.parseXmlStream(_stream))
			{
				_finishedParsing = true;
			}
			while (!_finishedParsing)
			{
				// This is inside a synchronized block but the wait yields the lock
				// to allow the parsing thread to set the finished flag
				try {
					_mutex.wait();
				} catch (InterruptedException e) {}
			}
		}
	}

	@Override
	public void endDocument()
	{
		synchronized(_mutex)
		{
			_finishedParsing = true;
			_mutex.notifyAll();
		}
	}

	@Override
	public void addString(String inValue)
	{
		if (_contents != null) {
			_contents.addString(inValue);
		}
	}

	@Override
	public void setName(String inName)
	{
		if (_contents != null) {
			_contents.setName(inName);
		}
	}

	@Override
	public void setDescription(String inDesc)
	{
		if (_contents != null) {
			_contents.setDescription(inDesc);
		}
	}

	@Override
	public void addDateString(String inDate)
	{
		if (_contents != null) {
			_contents.addDateString(inDate);
		}
	}

	@Override
	public void addCoordinates(double inLatitude, double inLongitude)
	{
		if (_contents != null) {
			_contents.addCoordinates(inLatitude, inLongitude);
		}
		else if (_locationFilter != null && !_filterMatched) {
			_filterMatched = _locationFilter.doesLocationMatch(inLatitude, inLongitude);
		}
	}

	@Override
	public boolean matchesFilter(LocationFilter inFilter)
	{
		_locationFilter = inFilter;
		_contents = null;
		_filterMatched = false;
		readFile();
		return _filterMatched;
	}
}