Java Code Examples for javax.xml.bind.annotation.XmlElement#required()

The following examples show how to use javax.xml.bind.annotation.XmlElement#required() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean hasRequiredMarker(AnnotatedMember m)
{
    ApiParam apiParam = m.getAnnotation(ApiParam.class);
    if (apiParam != null) {
        return apiParam.required();
    }

    ApiModelProperty ann = m.getAnnotation(ApiModelProperty.class);
    if (ann != null) {
        return ann.required();
    }
    XmlElement elem = m.getAnnotation(XmlElement.class);
    if (elem != null) {
        if (elem.required()) {
            return true;
        }
    }
    return null;
}
 
Example 2
Source File: DeployerValidator.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void validateCollection(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
    XmlType xtype = obj.getClass().getAnnotation(XmlType.class);
    if (xtype == null) {
        return;
    }
    String elementName = xtype.name();
    logger.debug(String.format("validating %s->%s", elementName, f.getName()));

    Collection l = (Collection) f.get(obj);
    XmlElement eat = f.getAnnotation(XmlElement.class);
    if (eat != null && (eat.required() && (l == null || l.isEmpty()))) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", f.getName(), elementName));
    }
    XmlAttribute aat = f.getAnnotation(XmlAttribute.class);
    if (aat != null && (aat.required() && (l == null || l.isEmpty()))) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", aat.name(), elementName));
    }

    if (l != null) {
        Object val = l.iterator().next();
        if (val != null) {
            validateObject(val);
        }
    }
}
 
Example 3
Source File: DeployerValidator.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void validateField(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
    XmlType xtype = obj.getClass().getAnnotation(XmlType.class);
    if (xtype == null) {
        return;
    }

    Object val = f.get(obj);
    String elementName = xtype.name();
    logger.debug(String.format("validating %s->%s", elementName, f.getName()));

    XmlElement eat = f.getAnnotation(XmlElement.class);
    if (eat != null && eat.required() && val == null) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", f.getName(), elementName));
    }

    XmlAttribute aat = f.getAnnotation(XmlAttribute.class);
    if (aat != null && aat.required() && val == null) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", aat.name(), elementName));
    }

    if (val != null) {
        validateObject(val);
    }
}
 
Example 4
Source File: JAXBSchemaInitializer.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void addElement(XmlSchema schema,
                          XmlSchemaSequence seq, JAXBBeanInfo beanInfo,
                          QName name, boolean isArray, XmlElement xmlElementAnno) {
    XmlSchemaElement el = new XmlSchemaElement(schema, false);
    if (isArray) {
        el.setMinOccurs(0);
        el.setMaxOccurs(Long.MAX_VALUE);
    } else {
        if (xmlElementAnno == null) {
            el.setMinOccurs(0);
            el.setNillable(false);
        } else {
            el.setNillable(xmlElementAnno.nillable());
            int minOccurs = xmlElementAnno.required() ? 1 : 0;
            el.setMinOccurs(minOccurs);
        }
    }

    if (beanInfo.isElement()) {
        QName ename = new QName(beanInfo.getElementNamespaceURI(null),
                               beanInfo.getElementLocalName(null));
        XmlSchemaElement el2 = schemas.getElementByQName(ename);
        el.setNillable(false);
        el.getRef().setTargetQName(el2.getQName());
    } else {
        if (xmlElementAnno != null && !StringUtils.isEmpty(xmlElementAnno.name())) {
            el.setName(xmlElementAnno.name());
        } else {
            el.setName(name.getLocalPart());
        }
        Iterator<QName> itr = beanInfo.getTypeNames().iterator();
        if (!itr.hasNext()) {
            return;
        }
        QName typeName = itr.next();
        el.setSchemaTypeName(typeName);
    }

    seq.getItems().add(el);
}
 
Example 5
Source File: JaxWsServiceConfiguration.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Long getWrapperPartMinOccurs(MessagePartInfo mpi) {
    Annotation[] a = (Annotation[])mpi.getProperty(ReflectionServiceFactoryBean.PARAM_ANNOTATION);
    if (a != null) {
        for (Annotation a2 : a) {
            if (a2 instanceof XmlElement) {
                XmlElement e = (XmlElement)a2;
                if (e.required()) {
                    return 1L;
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: InvalidMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	List<Field> fields = ReflectionUtil.getFields(object.getClass());

	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		if(value instanceof List){
			List<?> collection = (List<?>)value;

			// The getter method may have initialized the field with an empty ArrayList instance
			if(collection.isEmpty()){
				value = null;
			}
		} // End if

		// The field is set
		if(value != null){
			continue;
		}

		XmlElement element = field.getAnnotation(XmlElement.class);
		if(element != null && element.required()){
			report(new MissingElementException(object, field));
		}

		XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);
		if(attribute != null && attribute.required()){
			report(new MissingAttributeException(object, field));
		}
	}

	return super.visit(object);
}