Advertisement
Guest User

JTS class unmarshalling

a guest
Jun 28th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package com.test.jts;
  2.  
  3.  
  4. import javax.xml.bind.annotation.XmlAccessType;
  5. import javax.xml.bind.annotation.XmlAccessorType;
  6. import javax.xml.bind.annotation.XmlElement;
  7. import javax.xml.bind.annotation.XmlRootElement;
  8. import javax.xml.bind.annotation.XmlType;
  9. import com.vividsolutions.jts.geom.Geometry;
  10. import com.vividsolutions.jts.geom.Point;
  11.  
  12. @XmlRootElement(name = "customer")
  13. @XmlAccessorType(XmlAccessType.NONE)
  14. public class Customer {
  15.  
  16.     private Integer id;
  17.     private String customername;
  18.     private Point location;
  19.    
  20.    
  21.         @XmlElement
  22.     public Integer getId() {
  23.         return this.id;
  24.     }
  25.  
  26.     public void setId(Integer id) {
  27.         this.id = id;
  28.     }
  29.     @XmlElement
  30.     public String getCustomername() {
  31.         return this.customername;
  32.     }
  33.  
  34.     public void setCustomername(String customername) {
  35.         this.customername = customername;
  36.     }
  37.  
  38.     public Point getLocation() {
  39.             return this.location;
  40.         }
  41.     public void setLocation(Point location) {
  42.             this.location=location;
  43.         }
  44.  
  45. }
  46. import com.vividsolutions.jts.geom.Geometry;
  47.  
  48.  
  49. @Provider
  50. @Produces({ "application/xml", "application/json" })
  51. @Consumes({ "application/xml", "application/json" })
  52. public class JTSPointMarshaller extends XmlAdapter<String, Geometry> implements
  53.         MessageBodyReader<Geometry>, MessageBodyWriter<Geometry> {
  54.  
  55.     @Override
  56.     public long getSize(Geometry point, Class<?> type, Type genericType,
  57.             Annotation annotations[], MediaType mediaType) {
  58.  
  59.         return -1;
  60.     }
  61.  
  62.     @Override
  63.     public boolean isWriteable(Class<?> type, Type genericType,
  64.             Annotation annotations[], MediaType mediaType) {
  65.         // Only support the Properties class and inherited classes of
  66.         return Geometry.class.isAssignableFrom(type);
  67.     }
  68.  
  69.     @Override
  70.     public void writeTo(Geometry point, Class<?> type, Type genericType,
  71.             Annotation annotations[], MediaType mediaType,
  72.             MultivaluedMap<String, Object> httpheaders, OutputStream out)
  73.             throws IOException, WebApplicationException {
  74.         System.out.println("Writeto:: " + point.toString() + "::"
  75.                 + type.toString() + "::" + annotations.toString() + ": tttt: "
  76.                 + mediaType.toString());
  77.          if(mediaType.equals(MediaType.APPLICATION_XML)){
  78.             JAXBContext context;
  79.             // Marshal
  80.             try {
  81.                  context=JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");
  82.                  context.createMarshaller().marshal(point, out);
  83.             } catch (JAXBException e) {
  84.                 e.printStackTrace();
  85.             }
  86.         }else{  
  87.             JSONStringer stringer;
  88.             MfGeoJSONWriter builder;
  89.             stringer = new JSONStringer();
  90.             builder = new MfGeoJSONWriter(stringer);
  91.             try {
  92.                 builder.encodeGeometry(point);
  93.             } catch (JSONException e) {
  94.                 e.printStackTrace();
  95.             }
  96.             out.write(stringer.toString().getBytes());
  97.         }
  98.  
  99.     }
  100.  
  101.     @Override
  102.     public boolean isReadable(Class<?> type, Type genericType,
  103.             Annotation annotations[], MediaType mediaType) {
  104.         return Geometry.class.isAssignableFrom(type);
  105.     }
  106.  
  107.     @Override
  108.     public Geometry readFrom(Class<Geometry> type, Type genericType,
  109.             Annotation annotations[], MediaType mediaType,
  110.             MultivaluedMap<String, String> httpheaders, InputStream in)
  111.             throws IOException, WebApplicationException {
  112.         //TODO
  113.  
  114.         return null;
  115.     }
  116.  
  117.     @Override
  118.     public String marshal(Geometry point) throws Exception {
  119.         // JAXBContext context;
  120.         // StringWriter sw = new StringWriter();
  121.         // try {
  122.         // System.out.println("tester11s");
  123.         // context=JAXBContext.newInstance("org.jvnet.ogc.gml.v_3_1_1.jts");
  124.         // System.out.println("testers");
  125.         // context.createMarshaller().marshal(point,sw);
  126.         // } catch (JAXBException e) {
  127.         // e.printStackTrace();
  128.         // }
  129.         // return sw.toString();
  130.        
  131.         JSONStringer stringer;
  132.         MfGeoJSONWriter builder;
  133.         stringer = new JSONStringer();
  134.         builder = new MfGeoJSONWriter(stringer);
  135.         try {
  136.              builder.encodeGeometry(point);
  137.         } catch (JSONException e) {
  138.  
  139.             e.printStackTrace();
  140.         }
  141.         return stringer.toString();
  142.  
  143.     }
  144.  
  145.     // @Override
  146.     public Geometry unmarshal(String arg0) throws Exception {
  147.         // TODO Auto-generated method stub
  148.         return null;
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement