Creating a new parser


Parsers transform input data that was send in-line or by reference with a WPS request to data objects that can be used by the WPS processes. Parsers are bound to specific input formats that are assigned to them in the wps_config file, as well as to specific data bindings that are assigned in the Java class itself.

Implement a parser for a specific data binding

In the following, we will show how to implement a parser specific for the GTGMLTimeInstantDataBinding.

All parsers have to extend the AbstractParser class.

public class GTGMLParser extends AbstractParser {

private static Logger LOGGER = LoggerFactory.getLogger(GTGMLParser.class);

public IData parse(InputStream input, String mimeType, String schema) {

return null;

}

} 

The supported data binding is added in the default constructor.

public GTGMLParser() { super(); supportedIDataTypes.add(GTGMLTimeInstantDataBinding.class); } 

Now the parsing functionality is added.

public IData parse(InputStream input, String mimeType, String schema) {

IData result = null;

Configuration configuration = new GMLConfiguration();

boolean shouldSetParserStrict = false;

org.geotools.xml.Parser parser = new org.geotools.xml.Parser( configuration);

parser.setStrict(shouldSetParserStrict);

Object parsedData;

try {

parsedData = parser.parse(input);

if (parsedData instanceof DefaultInstant) { result = new GTGMLTimeInstantDataBinding( (DefaultInstant) parsedData); }

} catch (Exception e) {

LOGGER.error("Parsing of the input failed.", e);

}

return result;

} 

For simplicity reasons we are not checking MIME type or schema here.You can download the complete class here: GTGMLParser.java

To test the parser, a unit test should be written. Test classes can extend AbstractTestCase for convenience.

public class GTGMLParserTest extends AbstractTestCase<GTGMLParser>{

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmmZ");

String timePosition = "2014-01-01T00:00:10.000+01:00";

String timeInstant = "" +"" + timePosition + "" +"";

public void testParser(){

InputStream dataInputStream = new ByteArrayInputStream(timeInstant.getBytes());

IData parsedData = dataHandler.parse(dataInputStream, "text/xml", "http://schemas.opengis.net/gml/3.1.1/base/gml.xsd");

assertTrue(parsedData instanceof GTGMLTimeInstantDataBinding);

GTGMLTimeInstantDataBinding instantDataBinding = (GTGMLTimeInstantDataBinding)parsedData;

String formattedParsedTimePosition = dateFormat.format(instantDataBinding.getPayload().getPosition().getDate());

formattedParsedTimePosition = formattedParsedTimePosition.substring(0, formattedParsedTimePosition.length()-2) + ":00";

assertTrue(timePosition.equals(formattedParsedTimePosition));

}

@Override

protected void initializeDataHandler() {

dataHandler = new GTGMLParser();

}

} 

You can download the complete test class here: GTGMLParserTest.java

Finally, the parser needs to be added to the wps_config.

<Parser name="GTGMLParser" className="org.n52.wps.io.datahandler.parser.GTGMLParser" active="true">

<Format mimetype="text/xml" schema="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd" />

</Parser>

Now a process can make use of the DefaultInstant if the respective input supports the format in the process description and the data binding in the process class.

Extend the parser to return different data bindings based on the GML data type

Based on the GML data type, e.g. GML geometries or time instants, the parser can return different data bindings fitted for the GML types. To do this, additional functionality is added to the parse-method.

if (parsedData instanceof DefaultInstant) {

result = new GTGMLTimeInstantDataBinding( (DefaultInstant) parsedData);

}else if(parsedData instanceof Geometry){

result = new JTSGeometryBinding((Geometry)parsedData);

} 

You can find the code of the JTSGeometryBinding here: JTSGeometryBinding.java

Now the parser will be chosen for different inputs that support the same format in the process description but either the GTGMLTimeInstantDataBinding or the JTSGeometryBinding in the process class.
Topic revision: r2 - 06 May 2014, BenjaminPross
Legal Notice | Privacy Statement


This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Wiki? Send feedback