Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.mylib » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17    
   18   package org.apache.jdo.tck.pc.mylib;
   19   
   20   import java.io.InputStream;
   21   import java.text.SimpleDateFormat;
   22   import java.util.Date;
   23   import java.util.List;
   24   import java.util.Locale;
   25   import java.util.TimeZone;
   26   
   27   import org.springframework.beans.propertyeditors.CustomDateEditor;
   28   import org.springframework.beans.factory.xml.XmlBeanFactory;
   29   import org.springframework.core.io.ClassPathResource;
   30   import org.springframework.core.io.InputStreamResource;
   31   
   32   /**
   33    * Utility class to create a mylib instances from an xml
   34    * representation. 
   35    */
   36   public class MylibReader extends XmlBeanFactory {
   37   
   38       /** The format of date values in the xml representation */
   39       public static final String DATE_PATTERN = "d/MMM/yyyy";
   40   
   41       /** The name of the root list bean. */
   42       public static final String ROOT_LIST_NAME = "root";
   43   
   44       /** Teardown classes 
   45        */
   46       private static final Class[] tearDownClasses = new Class[] {
   47           PrimitiveTypes.class, PCClass.class
   48       };
   49       
   50       /** 
   51        * Create a MylibReader for the specified resourceName. 
   52        * @param resourceName the name of the resource
   53        */
   54       public MylibReader(String resourceName) {
   55           // Use the class loader of the PrimitiveTypes class to find the resource
   56           this(resourceName, PrimitiveTypes.class.getClassLoader());
   57       }
   58   
   59       /** 
   60        * Create a MylibReader for the specified resourceName. 
   61        * @param resourceName the name of the resource
   62        */
   63       public MylibReader(String resourceName, ClassLoader classLoader) {
   64           super(new ClassPathResource(resourceName, classLoader));
   65           configureFactory();
   66       }
   67   
   68       /**
   69        * Create a MylibReader for the specified InputStream.
   70        * @param stream the input stream
   71        */
   72       public MylibReader(InputStream stream) {
   73           super(new InputStreamResource(stream));
   74           configureFactory();
   75       }
   76   
   77       /** 
   78        * Returns a list of root objects. The method expects to find a bean
   79        * called "root" of type list in the xml and returns it.
   80        * @return a list of root instances
   81        */
   82       public List getRootList() {
   83           return (List)getBean(ROOT_LIST_NAME);
   84       }
   85       
   86       /** 
   87        * Configure the MylibReader, e.g. register CustomEditor classes
   88        * to convert the string representation of a property into an instance
   89        * of the right type.
   90        */
   91       private void configureFactory() {
   92           SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN, Locale.US);
   93           formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
   94           CustomDateEditor dateEditor = 
   95               new CustomDateEditor(formatter, true);
   96           registerCustomEditor(Date.class, dateEditor);
   97       }
   98       
   99       // Convenience methods
  100   
  101       /** 
  102        * Convenience method returning an Address instance for the specified
  103        * name. The method returns <code>null</code> if there is no Address
  104        * bean with the specified name. 
  105        * @param name the name of the bean to return.
  106        * @return the instance of the bean or <code>null</code> if there no
  107        * Address bean.
  108        */
  109       public PrimitiveTypes getPrimitiveTypes(String name) {
  110           return (PrimitiveTypes)getBean(name, PrimitiveTypes.class);
  111       }
  112   
  113       /**
  114        * @return Returns the tearDownClasses.
  115        */
  116       public static Class[] getTearDownClasses() {
  117           return tearDownClasses;
  118       }
  119   }
  120   

Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.mylib » [javadoc | source]