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