Search in sources :

Example 1 with RegexValidateElement

use of org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement in project Smack by igniterealtime.

the class DataValidationProvider method parse.

public static ValidateElement parse(XmlPullParser parser) throws XmlPullParserException, IOException {
    final int initialDepth = parser.getDepth();
    final String dataType = parser.getAttributeValue("", "datatype");
    ValidateElement dataValidation = null;
    ListRange listRange = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                switch(parser.getName()) {
                    case OpenValidateElement.METHOD:
                        dataValidation = new OpenValidateElement(dataType);
                        break;
                    case BasicValidateElement.METHOD:
                        dataValidation = new BasicValidateElement(dataType);
                        break;
                    case RangeValidateElement.METHOD:
                        dataValidation = new RangeValidateElement(dataType, parser.getAttributeValue("", "min"), parser.getAttributeValue("", "max"));
                        break;
                    case RegexValidateElement.METHOD:
                        dataValidation = new RegexValidateElement(dataType, parser.nextText());
                        break;
                    case ListRange.ELEMENT:
                        Long min = ParserUtils.getLongAttribute(parser, "min");
                        Long max = ParserUtils.getLongAttribute(parser, "max");
                        if (min != null || max != null) {
                            listRange = new ListRange(min, max);
                        } else {
                            LOGGER.fine("Ignoring list-range element without min or max attribute");
                        }
                        break;
                    default:
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    if (dataValidation == null) {
                        // XEP-122 ยง 3.2 states that "If no validation method is specified,
                        // form processors MUST assume <basic/> validation."
                        dataValidation = new BasicValidateElement(dataType);
                    }
                    dataValidation.setListRange(listRange);
                    break outerloop;
                }
                break;
        }
    }
    return dataValidation;
}
Also used : ListRange(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) OpenValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement) RegexValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement) OpenValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RegexValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement)

Example 2 with RegexValidateElement

use of org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement in project Smack by igniterealtime.

the class DataValidationHelperTest method testCheckConsistencyFormFieldRegexValidateElement.

@Test
public void testCheckConsistencyFormFieldRegexValidateElement() {
    FormField field = new FormField("var");
    field.setType(FormField.Type.list_multi);
    RegexValidateElement element = new RegexValidateElement(null, ".*");
    try {
        element.checkConsistency(field);
        fail("No correct check on consistency");
    } catch (ValidationConsistencyException e) {
        assertEquals("Field type 'list-multi' is not consistent with validation method 'regex'.", e.getMessage());
    }
}
Also used : RegexValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement) FormField(org.jivesoftware.smackx.xdata.FormField) Test(org.junit.Test)

Aggregations

RegexValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement)2 FormField (org.jivesoftware.smackx.xdata.FormField)1 ValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement)1 BasicValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement)1 ListRange (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange)1 OpenValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement)1 RangeValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement)1 Test (org.junit.Test)1