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.xml.sax.Attributes;
025: import org.jboss.xb.binding.ObjectModelFactory;
026: import org.jboss.xb.binding.UnmarshallingContext;
027:
028: /**
029: * org.jboss.xb.binding.ObjectModelFactory implementation that accepts data chuncks from unmarshaller
030: * and assembles them into an instance Book.
031: *
032: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
033: * @version <tt>$Revision: 57211 $</tt>
034: */
035: public class BookObjectFactory implements ObjectModelFactory {
036: // ObjectModelFactory implementation
037:
038: /**
039: * Return the root.
040: */
041: public Object newRoot(Object root, UnmarshallingContext navigator,
042: String namespaceURI, String localName, Attributes attrs) {
043: final Book book;
044: if (root == null) {
045: root = book = new Book();
046: } else {
047: book = (Book) root;
048: }
049:
050: if (attrs.getLength() > 0) {
051: for (int i = 0; i < attrs.getLength(); ++i) {
052: if (attrs.getLocalName(i).equals("isbn")) {
053: book.setIsbn(attrs.getValue(i));
054: }
055: }
056: }
057:
058: return root;
059: }
060:
061: public Object completeRoot(Object root, UnmarshallingContext ctx,
062: String uri, String name) {
063: return root;
064: }
065:
066: // Methods discovered by introspection
067:
068: /**
069: * Called when a child element with simple content is read for book.
070: */
071: public void setValue(Book book, UnmarshallingContext navigator,
072: String namespaceURI, String localName, String value) {
073: if ("title".equals(localName)) {
074: book.setTitle(value);
075: } else if ("author".equals(localName)) {
076: book.setAuthor(value);
077: }
078: }
079:
080: /**
081: * Called when parsing of a new element started.
082: */
083: public Object newChild(Book book, UnmarshallingContext navigator,
084: String namespaceURI, String localName, Attributes attrs) {
085: Object child = null;
086: if ("character".equals(localName)) {
087: child = new BookCharacter();
088: }
089: return child;
090: }
091:
092: /**
093: * Called when a child element with simple content is read for character.
094: */
095: public void setValue(BookCharacter character,
096: UnmarshallingContext navigator, String namespaceURI,
097: String localName, String value) {
098: if ("name".equals(localName)) {
099: character.setName(value);
100: } else if ("friend-of".equals(localName)) {
101: character.setFriendOf(value);
102: } else if ("since".equals(localName)) {
103: if (value.endsWith("Z")) {
104: value = value.substring(0, value.length() - 1);
105: }
106: character.setSince(value);
107: } else if ("qualification".equals(localName)) {
108: character.setQualification(value);
109: }
110: }
111:
112: /**
113: * Called when parsing character is complete.
114: */
115: public void addChild(Book book, BookCharacter character,
116: UnmarshallingContext navigator, String namespaceURI,
117: String localName) {
118: book.addCharacter(character);
119: }
120: }
|