View Javadoc

    1   package org.codehaus.xfire.aegis.type.basic;
    2
    3   import java.beans.PropertyDescriptor;
    4
    5   import javax.xml.namespace.QName;
    6
    7   import org.apache.commons.logging.Log;
    8   import org.apache.commons.logging.LogFactory;
    9   import org.codehaus.xfire.XFireRuntimeException;
    10  import org.codehaus.xfire.aegis.type.Type;
    11  import org.codehaus.xfire.aegis.type.TypeMapping;
    12  import org.codehaus.xfire.util.ClassLoaderUtils;
    13  import org.codehaus.yom.Element;
    14  import org.codehaus.yom.Elements;
    15
    16  public class XMLBeanTypeInfo
    17      extends BeanTypeInfo
    18  {
    19      private static final Log logger = LogFactory.getLog(XMLBeanTypeInfo.class);
    20      private String encodingUri;
    21      private Element mapping;
    22      private QName name;
    23
    24      public XMLBeanTypeInfo(TypeMapping tm,
    25                             Class typeClass,
    26                             Element mapping)
    27      {
    28          super(typeClass, tm.getEncodingStyleURI());
    29
    30          this.mapping = mapping;
    31          setTypeMapping(tm);
    32      }
    33
    34      public QName getSchemaType()
    35      {
    36          if (name == null)
    37          {
    38              name = createQName(mapping, mapping.getAttributeValue("name"));
    39          }
    40
    41          return name;
    42      }
    43
    44      protected void mapProperty(PropertyDescriptor pd)
    45      {
    46          Element e = getPropertyElement(mapping, pd.getName());
    47          String style = null;
    48          QName name = null;
    49
    50          if (e != null)
    51          {
    52              String ignore = e.getAttributeValue("ignore");
    53              if (ignore != null && ignore.equals("true"))
    54                  return;
    55
    56              logger.debug("Found mapping for property " + pd.getName());
    57
    58              style = e.getAttributeValue("style");
    59              name = createQName(e, e.getAttributeValue("mappedName"));
    60          }
    61
    62          if (style == null) style = "element";
    63          if (name == null) name = createQName(pd);
    64
    65          try
    66          {
    67              Type type = getTypeMapping().getTypeCreator().createType(pd);
    68
    69              getTypeMapping().register(type);
    70
    71              if (style.equals("element"))
    72                  mapElement(pd.getName(), name);
    73              else if (style.equals("attribute"))
    74                  mapAttribute(pd.getName(), name);
    75              else
    76                  throw new XFireRuntimeException("Invalid style: " + style);
    77          }
    78          catch(XFireRuntimeException ex)
    79          {
    80              ex.prepend("Couldn't create type for property " + pd.getName()
    81                        + " on " + getTypeClass());
    82
    83              throw ex;
    84          }
    85      }
    86
    87      private Element getPropertyElement(Element mapping2, String name2)
    88      {
    89          Elements elements = mapping2.getChildElements("property");
    90          for (int i = 0; i < elements.size(); i++)
    91          {
    92              Element e = elements.get(i);
    93              String name = e.getAttributeValue("name");
    94
    95              if (name != null && name.equals(name2))
    96              {
    97                  return e;
    98              }
    99          }
    100
    101         return null;
    102     }
    103
    104     private Class loadClass(String componentType)
    105     {
    106         try
    107         {
    108             return ClassLoaderUtils.loadClass(componentType, getClass());
    109         }
    110         catch (ClassNotFoundException e)
    111         {
    112             throw new XFireRuntimeException("Couldn't find component type: " + componentType, e);
    113         }
    114     }
    115
    116     protected QName createQName(Element e, String value)
    117     {
    118         if (value == null) return null;
    119
    120         int index = value.indexOf(":");
    121
    122         if (index == -1)
    123             throw new XFireRuntimeException("Invalid QName in mapping: " + value);
    124
    125         String prefix = value.substring(0, index);
    126         String localName = value.substring(index+1);
    127         String ns = e.getNamespaceURI(prefix);
    128
    129         if (ns == null || localName == null)
    130             throw new XFireRuntimeException("Invalid QName in mapping: " + value);
    131
    132         return new QName(ns, localName, prefix);
    133     }
    134 }