highsource / ogc-schemas

XML<->Java and XML<->JS for OGC XSDs.
BSD 2-Clause "Simplified" License
82 stars 49 forks source link

MarshalException "anonymous type" for a Filter 2.0.0 QueryType AbstractProjectionClause (WFS 2.0 GetFeature request) #203

Closed emskopp closed 5 years ago

emskopp commented 5 years ago

I encounter the following MarshalException

com.sun.istack.SAXException2: Instance of "javax.xml.bind.JAXBElement" is substituting "java.lang.Object", but "javax.xml.bind.JAXBElement" is bound to an anonymous type.]

when adding PropertyName to a net.opengis.wfs.v_2_0.QueryType using withAbstractProjectionClause

Testcase is: ` package sample;

import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List;

import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.namespace.QName;

import net.opengis.filter.v_2_0.BinarySpatialOpType; import net.opengis.filter.v_2_0.FilterType; import net.opengis.gml.v_3_2_1.DirectPositionType; import net.opengis.gml.v_3_2_1.PointType; import net.opengis.wfs.v_2_0.GetFeatureType; import net.opengis.wfs.v_2_0.ObjectFactory; import net.opengis.wfs.v_2_0.PropertyName; import net.opengis.wfs.v_2_0.QueryType;

public class GetFeatureTest {

public GetFeatureTest() {
}

public static void main(String[] args) throws Exception {

    boolean addProperties = true;
    Double[] coords = { 10. , 50. };
    JAXBContext xmlContext = JAXBContext.newInstance("net.opengis.wfs.v_2_0:net.opengis.filter.v_2_0:net.opengis.gml.v_3_2_1");

    Marshaller ma = xmlContext.createMarshaller();
    ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ma.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

    ObjectFactory wfsFactory = new ObjectFactory();

    GetFeatureType featureType = wfsFactory.createGetFeatureType();
    featureType.withVersion("2.0.0").withOutputFormat("text/xml; subtype=gml/3.2.1");

    List<JAXBElement<?>> queries = new ArrayList<JAXBElement<?>>();

    QueryType qt = new QueryType();
    qt.setTypeNames(Arrays.asList("testType"));

    List<String> propertyValues = Arrays.asList("p1", "p2");
    List<String> pNames = new ArrayList<>();
    pNames.addAll(propertyValues);
    if (!addProperties) pNames.clear();
    for (String p : pNames) {
        qt.withAbstractProjectionClause(wfsFactory.createPropertyName(new PropertyName().withValue(new QName(p))));
    }

    qt.withAbstractSelectionClause(getIntersectsFilter(coords));
    queries.add(wfsFactory.createQuery(qt));

    featureType.setAbstractQueryExpression(queries);
    StringWriter getFeatureXML = new StringWriter();
    ma.marshal(wfsFactory.createGetFeature(featureType), getFeatureXML);

    System.out.println(getFeatureXML);

}

private static JAXBElement<FilterType> getIntersectsFilter(Double[] coords) {
    FilterType ft = new FilterType();
    BinarySpatialOpType interSect = new BinarySpatialOpType();
    DirectPositionType pos = new DirectPositionType();
    pos.setValue(Arrays.asList(coords));

    PointType point = new PointType();
    point.setSrsName("EPSG:4326");
    point.setPos(pos);

    net.opengis.gml.v_3_2_1.ObjectFactory gmlFac = new net.opengis.gml.v_3_2_1.ObjectFactory();
    interSect.withExpressionOrAny(gmlFac.createPoint(point));
    net.opengis.filter.v_2_0.ObjectFactory filterFac = new net.opengis.filter.v_2_0.ObjectFactory();
    JAXBElement<BinarySpatialOpType> je = filterFac.createIntersects(interSect);

    ft.setSpatialOps(je);

    return filterFac.createFilter(ft);
}

} ` As far as I can tell this needs a schema adjustment, but I am not really sure how to do this.

highsource commented 5 years ago

Reproduced.