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 java.text.SimpleDateFormat;
025: import java.text.ParseException;
026: import java.util.Calendar;
027: import java.util.TimeZone;
028:
029: import org.jboss.xb.binding.MarshallingContext;
030: import org.jboss.xb.binding.ObjectModelProvider;
031:
032: /**
033: * org.jboss.xb.binding.ObjectModelProvider implementation that provides data to marshaller given
034: * specific Book instance.
035: *
036: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
037: * @version <tt>$Revision: 57211 $</tt>
038: */
039: public class BookObjectProvider implements ObjectModelProvider {
040: public Object getRoot(Object o, MarshallingContext ctx,
041: String namespaceURI, String localName) {
042: return o;
043: }
044:
045: public Object getChildren(Book book, String namespaceUri,
046: String localName) {
047: Object children = null;
048: if (localName.equals("book")) {
049: children = book;
050: } else if (localName.equals("character")) {
051: children = book.getCharacters();
052: }
053: return children;
054: }
055:
056: public Object getAttributeValue(Book book, String namespaceUri,
057: String localName) {
058: Object value;
059: if ("isbn".equals(localName)) {
060: value = book.getIsbn();
061: } else {
062: value = null;
063: }
064: return value;
065: }
066:
067: public Object getElementValue(Book book, String namespaceUri,
068: String localName) {
069: Object value;
070: if ("title".equals(localName)) {
071: value = book.getTitle();
072: } else if ("author".equals(localName)) {
073: value = book.getAuthor();
074: } else {
075: value = null;
076: }
077: return value;
078: }
079:
080: public Object getElementValue(BookCharacter character,
081: String namespaceUri, String localName) {
082: Object value = null;
083: if ("name".equals(localName)) {
084: value = character.getName();
085: } else if ("friend-of".equals(localName)) {
086: value = character.getFriendOf();
087: } else if ("since".equals(localName)) {
088: String since = character.getSince();
089: if (since == null) {
090: value = null;
091: } else {
092: SimpleDateFormat dateFormat = new SimpleDateFormat(
093: "yyyy-MM-dd");
094: dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
095: java.util.Date date = null;
096: try {
097: date = dateFormat.parse(since);
098: } catch (ParseException e) {
099: throw new IllegalStateException(
100: "Failed to parse date " + since + ": "
101: + e.getMessage());
102: }
103:
104: Calendar cal = Calendar.getInstance(TimeZone
105: .getTimeZone("GMT"));
106: cal.setTime(date);
107: value = cal;
108: }
109: } else if ("qualification".equals(localName)) {
110: value = character.getQualification();
111: }
112: return value;
113: }
114: }
|