1 7 package org.jboss.test.xml.book; 8 9 import org.xml.sax.Attributes ; 10 import org.jboss.xb.binding.UnmarshallingContext; 11 import org.jboss.xb.binding.ObjectModelFactory; 12 13 20 public class BookObjectFactory 21 implements ObjectModelFactory 22 { 23 25 public Object completeRoot(Object root, UnmarshallingContext ctx, 26 String uri, String name) 27 { 28 return root; 29 } 30 31 34 public Object newRoot(Object root, 35 UnmarshallingContext navigator, 36 String namespaceURI, 37 String localName, 38 Attributes attrs) 39 { 40 final Book book; 41 if(root == null) 42 { 43 root = book = new Book(); 44 } 45 else 46 { 47 book = (Book) root; 48 } 49 50 if(attrs.getLength() > 0) 51 { 52 for(int i = 0; i < attrs.getLength(); ++i) 53 { 54 if(attrs.getLocalName(i).equals("isbn")) 55 { 56 book.setIsbn(attrs.getValue(i)); 57 } 58 } 59 } 60 61 return root; 62 } 63 64 66 69 public void setValue(Book book, 70 UnmarshallingContext navigator, 71 String namespaceURI, 72 String localName, 73 String value) 74 { 75 if("title".equals(localName)) 76 { 77 book.setTitle(value); 78 } 79 else if("author".equals(localName)) 80 { 81 book.setAuthor(value); 82 } 83 } 84 85 88 public Object newChild(Book book, 89 UnmarshallingContext navigator, 90 String namespaceURI, 91 String localName, 92 Attributes attrs) 93 { 94 Object child = null; 95 if("character".equals(localName)) 96 { 97 child = new BookCharacter(); 98 } 99 return child; 100 } 101 102 105 public void setValue(BookCharacter character, 106 UnmarshallingContext navigator, 107 String namespaceURI, 108 String localName, 109 String value) 110 { 111 if("name".equals(localName)) 112 { 113 character.setName(value); 114 } 115 else if("friend-of".equals(localName)) 116 { 117 character.setFriendOf(value); 118 } 119 else if("since".equals(localName)) 120 { 121 if(value.endsWith("Z")) 122 { 123 value = value.substring(0, value.length() - 1); 124 } 125 character.setSince(value); 126 } 127 else if("qualification".equals(localName)) 128 { 129 character.setQualification(value); 130 } 131 } 132 133 136 public void addChild(Book book, 137 BookCharacter character, 138 UnmarshallingContext navigator, 139 String namespaceURI, 140 String localName) 141 { 142 book.addCharacter(character); 143 } 144 } 145 | Popular Tags |