RedTeaHacker

JAXB Money

Mar 19th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. import java.io.File;
  2. import java.math.BigDecimal;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import javax.xml.bind.JAXBContext;
  6. import javax.xml.bind.JAXBException;
  7. import javax.xml.bind.Marshaller;
  8. import javax.xml.bind.Unmarshaller;
  9. import javax.xml.bind.annotation.XmlElement;
  10. import javax.xml.bind.annotation.XmlRootElement;
  11. import javax.xml.bind.annotation.adapters.XmlAdapter;
  12. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  13.  
  14. /**
  15.  * Parses the
  16.  * <a href="http://www.w3schools.com/xml/xml_examples.asp">Plant Catalog XML</a>
  17.  * <br /><br />
  18.  *
  19.  * Shows one method of parsing money, if you're not able to break the currencies
  20.  * and values out into separate XML elements.
  21.  *
  22.  * @author RedTeaHacker
  23.  */
  24. @XmlRootElement(name="CATALOG")
  25. public class PlantXml {
  26.   public static void main(String[] args) throws JAXBException {
  27.     if(args.length < 1) {
  28.       System.out.println("Usage:  java PlantXml <input_file> (<output_file>)");
  29.       return;
  30.     }
  31.  
  32.     // Read
  33.     JAXBContext context = JAXBContext.newInstance(PlantXml.class);
  34.     Unmarshaller unmarsh = context.createUnmarshaller();
  35.     PlantXml plantXml = (PlantXml)unmarsh.unmarshal(new File(args[0]));
  36.  
  37.     // Output
  38.     for(Plant plant: plantXml.plants) {
  39.       System.out.printf("%20s |%25s |%7s |%13s |%6s |%7s\n"
  40.         ,plant.common,plant.botanical,plant.zone,plant.light,plant.price,plant.availability);
  41.     }
  42.  
  43.     if(args.length > 1) {
  44.       // New
  45.       plantXml = new PlantXml();
  46.       plantXml.plants = new LinkedList<Plant>();
  47.       plantXml.plants.add(new Plant("North Tower Flower","Lobelia cardinalis","99","Sunny",new Money("$",new BigDecimal("0.05")),"000001"));
  48.       plantXml.plants.add(new Plant("Beethovenfoil","Potentilla","747","Shade",new Money("$",new BigDecimal("1000")),"011440"));
  49.  
  50.       // Write
  51.       Marshaller marsh = context.createMarshaller();
  52.       marsh.setProperty("jaxb.formatted.output",Boolean.TRUE);
  53.       marsh.marshal(plantXml,new File(args[1]));
  54.     }
  55.   }
  56.  
  57.   @XmlElement(name="PLANT")
  58.   public List<Plant> plants;
  59.  
  60.   public static class Plant {
  61.     @XmlElement(name="COMMON")
  62.     public String common;
  63.  
  64.     @XmlElement(name="BOTANICAL")
  65.     public String botanical;
  66.  
  67.     @XmlElement(name="ZONE")
  68.     public String zone;
  69.  
  70.     @XmlElement(name="LIGHT")
  71.     public String light;
  72.  
  73.     @XmlElement(name="PRICE")
  74.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  75.     public Money price;
  76.  
  77.     @XmlElement(name="AVAILABILITY")
  78.     public String availability;
  79.  
  80.     public Plant() {
  81.     }
  82.  
  83.     // Not necessary for JAXB, just helpful
  84.     public Plant(String common,String botanical,String zone,String light,Money price,String availability) {
  85.       this.common = common;
  86.       this.botanical = botanical;
  87.       this.zone = zone;
  88.       this.light = light;
  89.       this.price = price;
  90.       this.availability = availability;
  91.     }
  92.   }
  93.  
  94.   public static class Money {
  95.     public String currency = "$"; // Should probably use the Currency class
  96.     public BigDecimal value = BigDecimal.ZERO;
  97.  
  98.     public Money() {
  99.     }
  100.  
  101.     // Not necessary for JAXB, just helpful
  102.     public Money(String currency,BigDecimal value) {
  103.       this.currency = currency;
  104.       this.value = value;
  105.     }
  106.  
  107.     public String toString() {
  108.       return currency + value.toPlainString();
  109.     }
  110.   }
  111.  
  112.   // You could combine this with the Money class
  113.   public static class MoneyAdapter extends XmlAdapter<String,Money> {
  114.     public String marshal(Money m) {
  115.       return (m != null) ? m.toString() : "";
  116.     }
  117.  
  118.     public Money unmarshal(String s) {
  119.       Money m = new Money();
  120.  
  121.       // Currency
  122.       StringBuilder b = new StringBuilder();
  123.       int i = 0;
  124.       for(; i < s.length(); ++i) {
  125.         char c = s.charAt(i);
  126.         if(Character.isWhitespace(c)) {
  127.           continue; // ignore
  128.         }
  129.         if(Character.isDigit(c) || c == '-' || c == '.') {
  130.           break; // done
  131.         }
  132.         b.append(c);
  133.       }
  134.       m.currency = b.toString();
  135.  
  136.       // Value
  137.       b = new StringBuilder();
  138.       for(; i < s.length(); ++i) {
  139.         char c = s.charAt(i);
  140.         if(!Character.isWhitespace(c)) {
  141.           b.append(c);
  142.         }
  143.       }
  144.       try {
  145.         m.value = new BigDecimal(b.toString());
  146.       }
  147.       catch(Exception ex) {
  148.         m.value = BigDecimal.ZERO;
  149.       }
  150.  
  151.       return m;
  152.     }
  153.   }
  154. }
Add Comment
Please, Sign In to add comment