1 7 package org.jboss.test.xml.book; 8 9 import java.text.SimpleDateFormat ; 10 import java.text.ParseException ; 11 import java.util.TimeZone ; 12 import java.util.Calendar ; 13 import org.jboss.xb.binding.ObjectModelProvider; 14 import org.jboss.xb.binding.MarshallingContext; 15 16 23 public class BookObjectProvider 24 implements ObjectModelProvider 25 { 26 public Object getRoot(Object o, MarshallingContext ctx, String namespaceURI, String localName) 27 { 28 return o; 29 } 30 31 public Object getChildren(Book book, String namespaceUri, String localName) 32 { 33 Object children = null; 34 if(localName.equals("book")) 35 { 36 children = book; 37 } 38 else if(localName.equals("character")) 39 { 40 children = book.getCharacters(); 41 } 42 return children; 43 } 44 45 public Object getAttributeValue(Book book, String namespaceUri, String localName) 46 { 47 Object value; 48 if("isbn".equals(localName)) 49 { 50 value = book.getIsbn(); 51 } 52 else 53 { 54 value = null; 55 } 56 return value; 57 } 58 59 public Object getElementValue(Book book, String namespaceUri, String localName) 60 { 61 Object value; 62 if("title".equals(localName)) 63 { 64 value = book.getTitle(); 65 } 66 else if("author".equals(localName)) 67 { 68 value = book.getAuthor(); 69 } 70 else 71 { 72 value = null; 73 } 74 return value; 75 } 76 77 public Object getElementValue(BookCharacter character, String namespaceUri, String localName) 78 { 79 Object value = null; 80 if("name".equals(localName)) 81 { 82 value = character.getName(); 83 } 84 else if("friend-of".equals(localName)) 85 { 86 value = character.getFriendOf(); 87 } 88 else if("since".equals(localName)) 89 { 90 String since = character.getSince(); 91 if(since == null) 92 { 93 value = null; 94 } 95 else 96 { 97 SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd"); 98 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 99 java.util.Date date = null; 100 try 101 { 102 date = dateFormat.parse(since); 103 } 104 catch(ParseException e) 105 { 106 throw new IllegalStateException ("Failed to parse date " + since + ": " + e.getMessage()); 107 } 108 109 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); 110 cal.setTime(date); 111 value = cal; 112 } 113 } 114 else if("qualification".equals(localName)) 115 { 116 value = character.getQualification(); 117 } 118 return value; 119 } 120 } 121 | Popular Tags |