📄 wfsggetfeatureresponsehandler.java
字号:
package com.esri.solutions.jitk.common.gazetteer;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
/**
* Class responsible for parsing WFS-G GetFeature responses by extending
* {@link DefaultHandler}.
*/
public class WfsgGetFeatureResponseHandler extends DefaultHandler {
/**
* {@link ArrayList} of {@link GazetteerPlace} objects parsed from
* the WFS-G GetFeature response.
*/
private List<GazetteerPlace> _places = null;
/**
* Value indicating if the current element is the SI_LocationInstance
* element.
*/
private boolean _isSILocationInstance = false;
/**
* Value indicating if the current element is the
* SI_LocationInstance/geographicIdentifier element.
*/
private boolean _isGeographicIdentifier = false;
/**
* Value indicating if the current element is the location element
* of the SI_LocationInstance/geographicIdentifier element.
*
* Referenced element is gml:posList
*/
private boolean _isPosList = false;
/**
* Value indicating if the current element is the location element
* of the SI_LocationInstance/geographicIdentifier element.
*
* Referenced element is gml:pos
*/
private boolean _isPos = false;
/**
* Value indicating if the current element is the bounded by element
* of the WFS-G GetFeature response.
*/
private boolean _isBoundedBy = false;
/**
* {@link GazetteerPlace} object that is created with each
* new SI_LocationInstance element.
*/
private GazetteerPlace _place = null;
/**
* Spatial reference ID. Default is 4326.
*/
private String _srsId = "4326";
/**
* Default no-args constructor.
*/
public WfsgGetFeatureResponseHandler() {
_places = new ArrayList<GazetteerPlace>();
}
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
if (_isSILocationInstance && _isGeographicIdentifier) {
String name = _place.getPlaceName();
// the characters() method is no required to give the entire text
// node all at once. The parser is free to split up the text node
// and call characters() several times. Handle this possibility.
if (name == null) {
name = new String(ch, start, length);
} else {
name += new String(ch, start, length);
}
_place.setPlaceName(name);
} else if (_isSILocationInstance && _isPos) {
String location = _place.getPlaceLocation();
// the characters() method is no required to give the entire text
// node all at once. The parser is free to split up the text node
// and call characters() several times. Handle this possibility.
if (location == null) {
location = new String(ch, start, length);
} else {
location += new String(ch, start, length);
}
_place.setPlaceLocation(location);
} else if (_isSILocationInstance && _isPosList) {
String location = _place.getPlaceLocation();
// the characters() method is no required to give the entire text
// node all at once. The parser is free to split up the text node
// and call characters() several times. Handle this possibility.
if (location == null) {
location = new String(ch, start, length);
} else {
location += new String(ch, start, length);
}
_place.setPlaceLocation(location);
}
}
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
public void endElement(String uri, String localName, String name)
throws SAXException {
if (localName.equalsIgnoreCase("SI_LocationInstance")) {
_isSILocationInstance = false;
_places.add(_place);
} else if (localName.equalsIgnoreCase("geographicIdentifier")) {
_isGeographicIdentifier = false;
} else if (localName.equalsIgnoreCase("pos")) {
_isPos = false;
} else if (localName.equalsIgnoreCase("posList")) {
_isPosList = false;
} else if (localName.equalsIgnoreCase("boundedBy")) {
_isBoundedBy = false;
}
}
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("SI_LocationInstance")) {
_isSILocationInstance = true;
_place = new GazetteerPlace();
} else if (localName.equalsIgnoreCase("geographicIdentifier")) {
_isGeographicIdentifier = true;
} else if (localName.equalsIgnoreCase("pos")) {
_isPos = true;
} else if (localName.equalsIgnoreCase("posList")) {
_isPosList = true;
} else if (localName.equalsIgnoreCase("boundedBy")) {
_isBoundedBy = true;
} else if (localName.equalsIgnoreCase("Envelope") && _isBoundedBy) {
_srsId = attributes.getValue("srsName");
}
}
/**
* Gets the {@link List} of {@link GazetteerPlace}.
*
* @return {@link List} of {@link GazetteerPlace} parsed from the WFS-G GetFeature
* response.
*/
public List<GazetteerPlace> getPlaces() {
return _places;
}
/**
* Gets the Spatial Reference ID of the WFS-G GetFeature
* response results.
*
* @return Spatial Reference of the WFS-G GetFeature response results. If no ID is
* specified 4326 is the default.
*/
public String getSrsId() {
return _srsId;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -