Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.jts;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlType;
- import com.vividsolutions.jts.geom.Geometry;
- import com.vividsolutions.jts.geom.Point;
- @XmlRootElement(name = "customer")
- @XmlAccessorType(XmlAccessType.NONE)
- public class Customer {
- private Integer id;
- private String customername;
- private Point location;
- @XmlElement
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- @XmlElement
- public String getCustomername() {
- return this.customername;
- }
- public void setCustomername(String customername) {
- this.customername = customername;
- }
- public Point getLocation() {
- return this.location;
- }
- public void setLocation(Point location) {
- this.location=location;
- }
- }
- import com.vividsolutions.jts.geom.Geometry;
- @Provider
- @Produces({ "application/xml", "application/json" })
- @Consumes({ "application/xml", "application/json" })
- public class JTSPointMarshaller extends XmlAdapter<String, Geometry> implements
- MessageBodyReader<Geometry>, MessageBodyWriter<Geometry> {
- @Override
- public long getSize(Geometry point, Class<?> type, Type genericType,
- Annotation annotations[], MediaType mediaType) {
- return -1;
- }
- @Override
- public boolean isWriteable(Class<?> type, Type genericType,
- Annotation annotations[], MediaType mediaType) {
- // Only support the Properties class and inherited classes of
- return Geometry.class.isAssignableFrom(type);
- }
- @Override
- public void writeTo(Geometry point, Class<?> type, Type genericType,
- Annotation annotations[], MediaType mediaType,
- MultivaluedMap<String, Object> httpheaders, OutputStream out)
- throws IOException, WebApplicationException {
- System.out.println("Writeto:: " + point.toString() + "::"
- + type.toString() + "::" + annotations.toString() + ": tttt: "
- + mediaType.toString());
- if(mediaType.equals(MediaType.APPLICATION_XML)){
- JAXBContext context;
- // Marshal
- try {
- context=JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");
- context.createMarshaller().marshal(point, out);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }else{
- JSONStringer stringer;
- MfGeoJSONWriter builder;
- stringer = new JSONStringer();
- builder = new MfGeoJSONWriter(stringer);
- try {
- builder.encodeGeometry(point);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- out.write(stringer.toString().getBytes());
- }
- }
- @Override
- public boolean isReadable(Class<?> type, Type genericType,
- Annotation annotations[], MediaType mediaType) {
- return Geometry.class.isAssignableFrom(type);
- }
- @Override
- public Geometry readFrom(Class<Geometry> type, Type genericType,
- Annotation annotations[], MediaType mediaType,
- MultivaluedMap<String, String> httpheaders, InputStream in)
- throws IOException, WebApplicationException {
- //TODO
- return null;
- }
- @Override
- public String marshal(Geometry point) throws Exception {
- // JAXBContext context;
- // StringWriter sw = new StringWriter();
- // try {
- // System.out.println("tester11s");
- // context=JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");
- // System.out.println("testers");
- // context.createMarshaller().marshal(point,sw);
- // } catch (JAXBException e) {
- // e.printStackTrace();
- // }
- // return sw.toString();
- JSONStringer stringer;
- MfGeoJSONWriter builder;
- stringer = new JSONStringer();
- builder = new MfGeoJSONWriter(stringer);
- try {
- builder.encodeGeometry(point);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return stringer.toString();
- }
- // @Override
- public Geometry unmarshal(String arg0) throws Exception {
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement