001 /**
002 * ========================================
003 * JFreeReport : a free Java report library
004 * ========================================
005 *
006 * Project Info: http://reporting.pentaho.org/
007 *
008 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 * ------------
026 * $Id: BeanPropertyLookupParser.java,v 1.5 2007/04/01 18:49:34 taqua Exp $
027 * ------------
028 * (C) Copyright 2000-2005, by Object Refinery Limited.
029 * (C) Copyright 2005-2007, by Pentaho Corporation.
030 */
031 package org.jfree.report.util.beans;
032
033 import org.jfree.report.util.CSVTokenizer;
034 import org.jfree.report.util.PropertyLookupParser;
035
036 public abstract class BeanPropertyLookupParser extends PropertyLookupParser
037 {
038 public BeanPropertyLookupParser ()
039 {
040 }
041
042 /**
043 *
044 * @param name
045 * @return
046 */
047 protected abstract Object performInitialLookup (String name);
048
049 protected String lookupVariable (final String entity)
050 {
051 // first, split the entity into separate strings (separator is '.').
052
053 final CSVTokenizer tokenizer = new CSVTokenizer(entity, ".");
054 if (tokenizer.hasMoreTokens())
055 {
056 final String name = tokenizer.nextToken();
057 final Object base = performInitialLookup(name);
058 try
059 {
060 if (tokenizer.hasMoreTokens())
061 {
062 return continueLookupVariable(tokenizer, base);
063 }
064 else
065 {
066 return ConverterRegistry.toAttributeValue(base);
067 }
068 }
069 catch (BeanException e)
070 {
071 return entity;
072 }
073 }
074 return entity;
075 }
076
077 private static String continueLookupVariable (final CSVTokenizer tokenizer,
078 final Object parent)
079 throws BeanException
080 {
081 if (tokenizer.hasMoreTokens())
082 {
083 final String name = tokenizer.nextToken();
084 final Object base = ConverterRegistry.toPropertyValue(name, parent.getClass());
085 if (tokenizer.hasMoreTokens())
086 {
087 return continueLookupVariable(tokenizer, base);
088 }
089 else
090 {
091 return ConverterRegistry.toAttributeValue(base);
092 }
093 }
094 return null;
095 }
096
097 }