Java Code Examples for javax.xml.bind.annotation.XmlAttribute

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project commons-j, under directory /src/main/java/nerds/antelax/commons/xml/.

Source file: JAXBUtils.java

  21 
vote

/** 
 * Checks a JAXB-annotated object to make sure all required data is present, however it does not check any data formatting constraints. Traverses the entire object graph to make sure sub-elements are also correctly populated.
 * @return <code>true</code> if all attributes and fields (and sub-fields) that are required are present, or if the object isnot a JAXB-generated class. Returns <code>false</code> otherwise.
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IntrospectionException
 */
public static boolean hasAllRequiredData(final Object jaxb) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException, IntrospectionException {
  Preconditions.checkNotNull(jaxb);
  final Class<?> clazz=jaxb.getClass();
  final XmlAccessorType xmlAccessorType=clazz.getAnnotation(XmlAccessorType.class);
  final XmlType xmlType=clazz.getAnnotation(XmlType.class);
  if (xmlAccessorType == null)   return true;
  Preconditions.checkArgument(xmlAccessorType.value() == XmlAccessType.FIELD,"Only JAXB fields are supported at this time");
  if (xmlType == null)   return true;
  final String[] properties=combine(xmlType.propOrder(),xmlAttributeProperties(clazz));
  for (  final String property : properties) {
    final Field field=clazz.getDeclaredField(property);
    Preconditions.checkNotNull(field);
    final boolean isList=field.getType() == List.class;
    if (field.getAnnotation(XmlTransient.class) != null)     continue;
    final XmlElement elementAnnotation=field.getAnnotation(XmlElement.class);
    final XmlAttribute attributeAnnotation=field.getAnnotation(XmlAttribute.class);
    final boolean required=elementAnnotation != null ? elementAnnotation.required() : attributeAnnotation != null ? attributeAnnotation.required() : false;
    if (isList) {
      final List<?> list=(List<?>)getJaxbProperty(jaxb,property);
      final Boolean emptyList=(Boolean)findMethod(list,"isEmpty").invoke(list);
      if (required && emptyList)       return false;
 else       for (      final Object listElement : list)       if (!hasAllRequiredData(listElement))       return false;
    }
 else {
      final Object propertyValue=getJaxbProperty(jaxb,property);
      if (required && propertyValue == null)       return false;
 else       if (propertyValue != null)       if (!hasAllRequiredData(propertyValue))       return false;
    }
  }
  return true;
}
 

Example 2

From project JMaNGOS, under directory /Tools/src/main/java/org/jmangos/tools/dbc/dataholder/.

Source file: DBCStruct.java

  21 
vote

public void cacheFields(final boolean mode){
  final Field[] f=this.getClass().getFields();
  final List<String> TFiledsName=new ArrayList<String>();
  for (int i=0; i < f.length; i++) {
    if (Modifier.isStatic(f[i].getModifiers()) || !f[i].isAnnotationPresent(XmlAttribute.class)) {
      continue;
    }
    final XmlAttribute property=f[i].getAnnotation(XmlAttribute.class);
    if ((property.name() != null) & (property.required() | mode)) {
      try {
        if (f[i].getType().isArray()) {
          final Object sd=f[i].get(this);
          for (int j=0; j < Array.getLength(sd); j++) {
            TFiledsName.add(property.name() + (j + 1));
          }
        }
 else         if ((f[i].getType() == InternalString.class) || (f[i].getType() == MultiInternalString.class)) {
          if (mode) {
            TFiledsName.add(property.name());
          }
        }
 else {
          TFiledsName.add(property.name());
        }
      }
 catch (      final IllegalArgumentException e) {
        e.printStackTrace();
      }
catch (      final IllegalAccessException e) {
        e.printStackTrace();
      }
    }
  }
  ;
  this.FiledsName=TFiledsName.toArray();
}
 

Example 3

From project commons-j, under directory /src/main/java/nerds/antelax/commons/xml/.

Source file: JAXBUtils.java

  19 
vote

private static String[] xmlAttributeProperties(final Class<?> clazz){
  Preconditions.checkNotNull(clazz);
  final List<String> xmlAttributes=new ArrayList<String>(clazz.getDeclaredFields().length);
  for (  final Field field : clazz.getDeclaredFields())   if (field.getAnnotation(XmlAttribute.class) != null)   xmlAttributes.add(field.getName());
  return xmlAttributes.toArray(new String[]{});
}