package org.jboss.test.xml.book;
import org.xml.sax.Attributes;
import org.jboss.xml.binding.ContentNavigator;
import org.jboss.xml.binding.ObjectModelFactory;
public class BookObjectFactory
implements ObjectModelFactory
{
public Object newRoot(Object root,
ContentNavigator navigator,
String namespaceURI,
String localName,
Attributes attrs)
{
final Book book;
if(root == null)
{
root = book = new Book();
}
else
{
book = (Book) root;
}
if(attrs.getLength() > 0)
{
for(int i = 0; i < attrs.getLength(); ++i)
{
if(attrs.getLocalName(i).equals("isbn"))
{
book.setIsbn(attrs.getValue(i));
}
}
}
return root;
}
public void setValue(Book book,
ContentNavigator navigator,
String namespaceURI,
String localName,
String value)
{
if("title".equals(localName))
{
book.setTitle(value);
}
else if("author".equals(localName))
{
book.setAuthor(value);
}
}
public Object newChild(Book book,
ContentNavigator navigator,
String namespaceURI,
String localName,
Attributes attrs)
{
Object child = null;
if("character".equals(localName))
{
child = new BookCharacter();
}
return child;
}
public void setValue(BookCharacter character,
ContentNavigator navigator,
String namespaceURI,
String localName,
String value)
{
if("name".equals(localName))
{
character.setName(value);
}
else if("friend-of".equals(localName))
{
character.setFriendOf(value);
}
else if("since".equals(localName))
{
character.setSince(value);
}
else if("qualification".equals(localName))
{
character.setQualification(value);
}
}
public void addChild(Book book,
BookCharacter character,
ContentNavigator navigator,
String namespaceURI,
String localName)
{
book.addCharacter(character);
}
}