       1 /*
       2  * $Id$
       3  *
       4  * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
       5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       6  *
       7  * This code is free software; you can redistribute it and/or modify it
       8  * under the terms of the GNU General Public License version 2 only, as
       9  * published by the Free Software Foundation.  Oracle designates this
      10  * particular file as subject to the "Classpath" exception as provided
      11  * by Oracle in the LICENSE file that accompanied this code.
      12  *
      13  * This code is distributed in the hope that it will be useful, but WITHOUT
      14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16  * version 2 for more details (a copy is included in the LICENSE file that
      17  * accompanied this code).
      18  *
      19  * You should have received a copy of the GNU General Public License version
      20  * 2 along with this work; if not, write to the Free Software Foundation,
      21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      22  *
      23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      24  * or visit www.oracle.com if you need additional information or have any
      25  * questions.
      26  */
      27 package com.sun.javatest.cof;
      28
      29 import java.beans.BeanInfo;
      30 import java.beans.IntrospectionException;
      31 import java.beans.Introspector;
      32 import java.beans.PropertyDescriptor;
      33 import java.io.IOException;
      34 import java.lang.reflect.InvocationTargetException;
      35 import java.lang.reflect.Method;
      36 import java.util.Arrays;
      37 import java.util.Collection;
      38 import java.util.Comparator;
      39 import java.util.Date;
      40 import java.util.Iterator;
      41 import java.util.LinkedHashMap;
      42
      43 import com.sun.javatest.util.XMLWriter;
      44
      45 abstract class COFItem
      46 {
      47     BeanInfo bi = null;
      48     LinkedHashMap itemAttributes = getItemAttributes();
      49     LinkedHashMap itemElements = getItemElements();
      50     String itemTagName = getItemTagName();
      51     PropertyDescriptor [] pds = null;
      52
      53     private String[] getAttributeProperties() {
      54         return itemAttributes == null? null: (String []) itemAttributes.keySet().toArray(new String[itemAttributes.size()]);
      55     }
      56     LinkedHashMap getItemAttributes(){
      57         return null;
      58     }
      59     LinkedHashMap getItemElements() {
      60         return null;
      61     };
      62
      63     String getItemTagName(){
      64         return null;
      65     }
      66
      67     Object getProperty(String name) {
      68         if (bi == null) {
      69             try {
      70                 bi = Introspector.getBeanInfo(this.getClass());
      71                 pds = bi.getPropertyDescriptors();
      72                 Arrays.sort(pds, new Comparator() {
      73                     public int compare(Object o1, Object o2) {
      74                         PropertyDescriptor pd1 = (PropertyDescriptor) o1;
      75                         PropertyDescriptor pd2 = (PropertyDescriptor) o2;
      76                         return pd1.getName().compareTo(pd2.getName());
      77                     }
      78                 });
      79             } catch (IntrospectionException e) {
      80                 // TODO Auto-generated catch block
      81                 e.printStackTrace();
      82             }
      83         }
      84
      85         if (pds == null) return null;
      86
      87         Object result = null;
      88         int propIndex =  Arrays.binarySearch(pds, name, new Comparator() {
      89             public int compare(Object o1, Object o2) {
      90                 PropertyDescriptor pd = (PropertyDescriptor) o1;
      91                 String propName = (String) o2;
      92                 return pd.getName().compareTo(propName);
      93             }
      94
      95         });
      96
      97         if (propIndex < 0 || propIndex >= pds.length) { // no such property
      98 //            System.out.println("passing "+ name);
      99             return null;
     100         }
     101
     102         try {
     103
     104             Method readMethod = pds[propIndex].getReadMethod();
     105             if (readMethod == null) {
     106                 System.err.println("Error occured for property '" + name + "' - no read method defined. Please contact developers.");
     107                 return null;
     108             }
     109             /* null casted to Object[] for suppresing "non-varargs call" warning */
     110             result = readMethod.invoke(this,(Object[]) null);
     111         } catch (IllegalArgumentException e) {
     112             // TODO Auto-generated catch block
     113             e.printStackTrace();
     114         } catch (IllegalAccessException e) {
     115             // TODO Auto-generated catch block
     116             e.printStackTrace();
     117         } catch (InvocationTargetException e) {
     118             // TODO Auto-generated catch block
     119             e.printStackTrace();
     120         }
     121         return result;
     122     }
     123
     124     private String[] getPropOrder() {
     125         return itemElements == null? null: (String []) itemElements.keySet().toArray(new String[itemElements.size()]);
     126     }
     127     void write(XMLWriter out) throws IOException {
     128
     129         if (itemTagName == null)
     130             return;
     131         out.startTag(itemTagName);
     132         if (itemAttributes != null) {
     133                         String[] attrOrder = getAttributeProperties();
     134                         for (int i = 0; i < attrOrder.length; i++) {
     135                                 out.writeAttr(itemAttributes.get(attrOrder[i]).toString(),
     136                                                 (String) getProperty(attrOrder[i]));
     137                         }
     138                 }
     139                 if (itemElements != null) {
     140                         String[] propOrder = getPropOrder();
     141                         for (int i = 0; i < propOrder.length; i++) {
     142                                 Object propValue = getProperty(propOrder[i]);
     143                                 if (propValue instanceof COFItem) {
     144                                         COFItem item = (COFItem) propValue;
     145                                         item.write(out);
     146                                         continue;
     147                                 } else if (propValue instanceof Collection) {
     148                                         writeCollection(out, propOrder[i]);
     149                                         continue;
     150                                 }
     151                                 if (propValue == null)
     152                                         continue;
     153                                 out.startTag(itemElements.get(propOrder[i]).toString());
     154                                 write(out, propValue);
     155                                 out.endTag(itemElements.get(propOrder[i]).toString());
     156                         }
     157                 }
     158                 out.endTag(itemTagName);
     159     }
     160     protected void write(XMLWriter out, Object o) throws IOException {
     161                 if (o instanceof String) {
     162                         out.write((String)o);
     163                 } else if (o instanceof Date) {
     164                         out.writeDate((Date)o);
     165                 } else out.write(o.toString());
     166         }
     167     void writeCollection(XMLWriter out, String propName) throws IOException {
     168         Collection col = (Collection) getProperty(propName);
     169         for (Iterator iter = col.iterator(); iter.hasNext();) {
     170             Object value = iter.next();
     171             if (value instanceof COFItem) {
     172                 ((COFItem)value).write(out);
     173             } else {
     174                 out.startTag(itemElements.get(propName).toString());
     175                 write(out,value);
     176                 out.endTag(itemElements.get(propName).toString());
     177             }
     178         }
     179
     180     }
     181
     182 }
    --- EOF ---