001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xml.book;
023:
024: import org.jboss.xb.binding.GenericObjectModelFactory;
025: import org.jboss.xb.binding.UnmarshallingContext;
026: import org.xml.sax.Attributes;
027:
028: /**
029: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
030: * @version <tt>$Revision: 57211 $</tt>
031: */
032: public class BookGenericObjectModelFactory implements
033: GenericObjectModelFactory {
034: public Object newRoot(Object root, UnmarshallingContext navigator,
035: String namespaceURI, String localName, Attributes attrs) {
036: final Book book;
037: if (root == null) {
038: root = book = new Book();
039: } else {
040: book = (Book) root;
041: }
042:
043: if (attrs.getLength() > 0) {
044: for (int i = 0; i < attrs.getLength(); ++i) {
045: if (attrs.getLocalName(i).equals("isbn")) {
046: book.setIsbn(attrs.getValue(i));
047: }
048: }
049: }
050:
051: return root;
052: }
053:
054: public Object newChild(Object parent,
055: UnmarshallingContext navigator, String namespaceURI,
056: String localName, Attributes attrs) {
057: Object child = null;
058: if (parent instanceof Book) {
059: if ("character".equals(localName)) {
060: child = new BookCharacter();
061: }
062: }
063: return child;
064: }
065:
066: public void addChild(Object parent, Object child,
067: UnmarshallingContext navigator, String namespaceURI,
068: String localName) {
069: if (parent instanceof Book) {
070: final Book book = (Book) parent;
071: if (child instanceof BookCharacter) {
072: book.addCharacter((BookCharacter) child);
073: }
074: }
075: }
076:
077: public void setValue(Object o, UnmarshallingContext navigator,
078: String namespaceURI, String localName, String value) {
079: if (o instanceof Book) {
080: final Book book = (Book) o;
081: if ("title".equals(localName)) {
082: book.setTitle(value);
083: } else if ("author".equals(localName)) {
084: book.setAuthor(value);
085: }
086: } else if (o instanceof BookCharacter) {
087: BookCharacter character = (BookCharacter) o;
088: if ("name".equals(localName)) {
089: character.setName(value);
090: } else if ("friend-of".equals(localName)) {
091: character.setFriendOf(value);
092: } else if ("since".equals(localName)) {
093: character.setSince(value);
094: } else if ("qualification".equals(localName)) {
095: character.setQualification(value);
096: }
097: }
098: }
099:
100: public Object completeRoot(Object root,
101: UnmarshallingContext navigator, String namespaceURI,
102: String localName) {
103: return root;
104: }
105: }
|