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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
package tim.prune.load.xml;
import java.util.ArrayList;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import tim.prune.data.ExtensionInfo;
import tim.prune.data.Field;
import tim.prune.data.FieldGpx;
import tim.prune.data.FieldXml;
import tim.prune.data.FileType;
/**
* Class for handling specifics of parsing Gpx files
*/
public class GpxHandler extends XmlHandler
{
private boolean _insidePoint = false;
private boolean _insideWaypoint = false;
private boolean _insideExtensions = false;
private boolean _startSegment = true;
private int _trackNum = -1;
private final GpxTag _fileTitle = new GpxTag(), _fileDescription = new GpxTag();
private final GpxTag _pointName = new GpxTag(), _trackName = new GpxTag();
private final GpxTag _elevation = new GpxTag(), _time = new GpxTag();
private final GpxTag _type = new GpxTag(), _description = new GpxTag();
private final GpxTag _link = new GpxTag(), _comment = new GpxTag();
private final GpxTag _sym = new GpxTag();
private GpxTag _currentTag = null;
private final ExtensionInfo _extensionInfo = new ExtensionInfo();
private final ArrayList<String[]> _pointList = new ArrayList<>();
private final ArrayList<String> _linkList = new ArrayList<>();
private Stack<String> _extensionTags = null;
private FieldGpx _gpxField = null;
/** Constructor, setting up the fields */
public GpxHandler()
{
final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT,
Field.WAYPT_TYPE, Field.DESCRIPTION, Field.COMMENT, Field.SYMBOL};
for (Field field : fields) {
addField(field);
}
}
/**
* Receive the start of a tag
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
// Read the parameters for waypoints and track points
String tag = qName.toLowerCase();
_gpxField = null;
if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
{
_insidePoint = true;
_insideWaypoint = tag.equals("wpt");
resetCurrentValues();
addCurrentValue(Field.LATITUDE, getAttribute(attributes, "lat"));
addCurrentValue(Field.LONGITUDE, getAttribute(attributes, "lon"));
_elevation.setValue(null);
_pointName.setValue(null);
_time.setValue(null);
_type.setValue(null);
_link.setValue(null);
_description.setValue(null);
_comment.setValue(null);
_sym.setValue(null);
}
else if (tag.equals("ele")) {
_currentTag = _elevation;
}
else if (tag.equals("name"))
{
if (_insidePoint) {
_currentTag = _pointName;
}
else if (_trackNum < 0) {
_currentTag = _fileTitle;
}
else {
_currentTag = _trackName;
}
}
else if (tag.equals("time")) {
_currentTag = _time;
}
else if (tag.equals("type")) {
_currentTag = _type;
}
else if (tag.equals("description") || tag.equals("desc"))
{
if (_insidePoint) {
_currentTag = _description;
}
else {
_currentTag = _fileDescription;
}
}
else if (tag.equals("cmt")) {
_currentTag = _comment;
}
else if (tag.equals("sym")) {
_currentTag = _sym;
}
else if (tag.equals("link")) {
_link.setValue(attributes.getValue("href"));
}
else if (tag.equals("trkseg")) {
_startSegment = true;
}
else if (tag.equals("trk"))
{
_trackNum++;
_trackName.setValue(null);
}
else if (tag.equals("extensions") && _insidePoint)
{
_insideExtensions = true;
_currentTag = new GpxTag();
_extensionTags = new Stack<>();
}
else if (_insideExtensions)
{
_extensionTags.add(qName);
_currentTag.clear();
}
else if (tag.equals("gpx"))
{
setFileType(FileType.GPX);
processGpxAttributes(attributes);
}
else
{
// Maybe it's a recognised gpx field like hdop
_gpxField = FieldGpx.getField(tag);
_currentTag = new GpxTag();
}
super.startElement(uri, localName, qName, attributes);
}
/** Process the attributes from the main gpx tag including extensions */
private void processGpxAttributes(Attributes attributes)
{
// System.out.println("Start gpx element: " + qName);
final int numAttributes = attributes.getLength();
for (int i=0; i<numAttributes; i++)
{
String attributeName = attributes.getQName(i).toLowerCase();
String attrValue = attributes.getValue(i);
// System.out.println(" Attribute '" + attributeName + "' - '" + attributes.getValue(i) + "'");
if (attributeName.equals("version")) {
setFileVersion(attributes.getValue(i));
}
else if (attributeName.contentEquals("xmlns")) {
_extensionInfo.setNamespace(attrValue);
}
else if (attributeName.equals("xmlns:xsi")) {
_extensionInfo.setXsi(attrValue);
}
else if (attributeName.equals("xsi:schemalocation"))
{
String[] schemas = attrValue.split(" ");
for (int s=0; s<schemas.length; s++) {
_extensionInfo.addXsiAttribute(schemas[s]);
}
}
else if (attributeName.startsWith("xmlns:"))
{
String prefix = attributeName.substring(6);
_extensionInfo.addNamespace(prefix, attrValue);
}
}
}
/**
* Process end tag
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
public void endElement(String uri, String localName, String qName)
throws SAXException
{
String tag = qName.toLowerCase();
if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
{
processPoint();
_insidePoint = false;
}
else if (tag.equals("extensions")) {
_insideExtensions = false;
}
else if (_insideExtensions)
{
String value = _currentTag.getValue();
_extensionTags.pop();
if (!value.isEmpty())
{
FieldXml field = new FieldXml(FileType.GPX, tag, _extensionTags);
if (!hasField(field)) {
addField(field);
}
addCurrentValue(field, value);
}
_currentTag.clear();
}
else if (_gpxField != null)
{
String value = _currentTag.getValue();
if (!value.isEmpty())
{
if (!hasField(_gpxField)) {
addField(_gpxField);
}
addCurrentValue(_gpxField, value);
}
_currentTag.clear();
_gpxField = null;
}
else if (_insidePoint && _currentTag != null && getFileVersion().equals("1.0"))
{
String value = _currentTag.getValue();
String tagNamespace = getNamespace(tag);
if (tagNamespace != null && !value.isEmpty())
{
String id = this.getExtensionInfo().getNamespaceName(tagNamespace);
if (id != null)
{
FieldXml field = new FieldXml(FileType.GPX, tag, id);
if (!hasField(field)) {
addField(field);
}
addCurrentValue(field, value);
}
}
_currentTag = null;
}
else {
_currentTag = null;
}
super.endElement(uri, localName, qName);
}
private static String getNamespace(String inTagName)
{
int firstColonPos = inTagName.indexOf(':');
if (firstColonPos > 0) {
return inTagName.substring(0, firstColonPos);
}
return null;
}
/**
* Process character text (inside tags or between them)
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
*/
public void characters(char[] ch, int start, int length)
throws SAXException
{
String value = new String(ch, start, length);
if (_currentTag != null) {
_currentTag.setValue(checkCharacters(_currentTag.getValue(), value));
}
super.characters(ch, start, length);
}
/**
* Check to concatenate partially-received values, if necessary
* @param inVariable variable containing characters received until now
* @param inValue new value received
* @return concatenation
*/
private static String checkCharacters(String inVariable, String inValue)
{
if (inVariable == null) {
return inValue;
}
return inVariable + inValue;
}
/**
* Process a point, either a waypoint or track point
*/
private void processPoint()
{
// Values go into a String array matching the order in getFieldArray()
addCurrentValue(Field.ALTITUDE, _elevation.getValue());
if (_insideWaypoint) {
addCurrentValue(Field.WAYPT_NAME, _pointName.getValue());
}
addCurrentValue(Field.TIMESTAMP, _time.getValue());
if (_startSegment && !_insideWaypoint)
{
addCurrentValue(Field.NEW_SEGMENT, "1");
_startSegment = false;
}
addCurrentValue(Field.WAYPT_TYPE, _type.getValue());
addCurrentValue(Field.DESCRIPTION, _description.getValue());
addCurrentValue(Field.COMMENT, _comment.getValue());
addCurrentValue(Field.SYMBOL, _sym.getValue());
_pointList.add(getCurrentValues());
_linkList.add(_link.getValue());
}
/**
* Return the parsed information as a 2d array
* @see tim.prune.load.xml.XmlHandler#getDataArray()
*/
public String[][] getDataArray()
{
int numPoints = _pointList.size();
// construct data array
String[][] result = new String[numPoints][];
for (int i=0; i<numPoints; i++) {
result[i] = _pointList.get(i);
}
return result;
}
/**
* @return array of links, or null if none
*/
public String[] getLinkArray()
{
int numPoints = _linkList.size();
boolean hasLink = false;
String[] result = new String[numPoints];
for (int i=0; i<numPoints; i++)
{
result[i] = _linkList.get(i);
if (result[i] != null) {hasLink = true;}
}
if (!hasLink) {
result = null;
}
return result;
}
/**
* @return file title
*/
public String getFileTitle() {
return _fileTitle.getValue();
}
/**
* @return file description
*/
public String getFileDescription() {
return _fileDescription.getValue();
}
@Override
public ExtensionInfo getExtensionInfo() {
return _extensionInfo;
}
}
|