Search in sources :

Example 1 with ValidateElement

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

the class DataValidationTest method testRange.

@Test
public void testRange() throws XmlPullParserException, IOException {
    ValidateElement dv = new RangeValidateElement("xs:string", "min-val", "max-val");
    ListRange listRange = new ListRange(111L, 999L);
    dv.setListRange(listRange);
    assertNotNull(dv.toXML());
    String output = dv.toXML().toString();
    assertEquals(TEST_OUTPUT_RANGE, output);
    XmlPullParser parser = getParser(output);
    dv = DataValidationProvider.parse(parser);
    assertNotNull(dv);
    assertEquals("xs:string", dv.getDatatype());
    assertTrue(dv instanceof RangeValidateElement);
    RangeValidateElement rdv = (RangeValidateElement) dv;
    assertEquals("min-val", rdv.getMin());
    assertEquals("max-val", rdv.getMax());
    assertNotNull(rdv.getListRange());
    assertEquals(Long.valueOf(111), rdv.getListRange().getMin());
    assertEquals(999, rdv.getListRange().getMax().intValue());
    assertNotNull(dv.toXML());
    output = dv.toXML().toString();
    assertEquals(TEST_OUTPUT_RANGE, output);
}
Also used : RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) ListRange(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) Test(org.junit.Test)

Example 2 with ValidateElement

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

the class DataValidationTest method testMin.

@Test
public void testMin() throws XmlPullParserException, IOException {
    ValidateElement dv = new BasicValidateElement(null);
    assertNotNull(dv.toXML());
    String output = dv.toXML().toString();
    assertEquals(TEST_OUTPUT_MIN, output);
    XmlPullParser parser = getParser(TEST_INPUT_MIN);
    dv = DataValidationProvider.parse(parser);
    assertNotNull(dv);
    assertEquals("xs:string", dv.getDatatype());
    assertTrue(dv instanceof BasicValidateElement);
    assertNotNull(dv.toXML());
    output = dv.toXML().toString();
    assertEquals(TEST_OUTPUT_MIN, output);
}
Also used : RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement) Test(org.junit.Test)

Example 3 with ValidateElement

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

the class DataValidationTest method testRange2.

@Test
public void testRange2() throws XmlPullParserException, IOException {
    ValidateElement dv = new RangeValidateElement(null, null, null);
    assertNotNull(dv.toXML());
    String output = dv.toXML().toString();
    assertEquals(TEST_OUTPUT_RANGE2, output);
    XmlPullParser parser = getParser(output);
    dv = DataValidationProvider.parse(parser);
    assertNotNull(dv);
    assertEquals("xs:string", dv.getDatatype());
    assertTrue(dv instanceof RangeValidateElement);
    RangeValidateElement rdv = (RangeValidateElement) dv;
    assertEquals(null, rdv.getMin());
    assertEquals(null, rdv.getMax());
    assertNotNull(rdv.toXML());
    output = rdv.toXML().toString();
    assertEquals(TEST_OUTPUT_RANGE2, output);
}
Also used : RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) BasicValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) Test(org.junit.Test)

Example 4 with ValidateElement

use of org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement 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 5 with ValidateElement

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

the class DataFormTest method testValidation.

@Test
public void testValidation() throws Exception {
    //Build a Form
    DataForm df = new DataForm(DataForm.Type.submit);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField field = new FormField("testField1");
    df.addField(field);
    ValidateElement dv = new RangeValidateElement("xs:integer", "1111", "9999");
    field.setValidateElement(dv);
    assertNotNull(df.toXML());
    String output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);
    XmlPullParser parser = PacketParserUtils.getParserFor(output);
    df = pr.parse(parser);
    assertNotNull(df);
    assertNotNull(df.getFields());
    assertEquals(1, df.getFields().size());
    Element element = df.getFields().get(0).getValidateElement();
    assertNotNull(element);
    dv = (ValidateElement) element;
    assertEquals("xs:integer", dv.getDatatype());
    assertNotNull(df.toXML());
    output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);
}
Also used : RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) Element(org.jivesoftware.smack.packet.Element) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) FormField(org.jivesoftware.smackx.xdata.FormField) Test(org.junit.Test)

Aggregations

ValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement)5 RangeValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement)5 BasicValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement)4 Test (org.junit.Test)4 XmlPullParser (org.xmlpull.v1.XmlPullParser)4 ListRange (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange)2 Element (org.jivesoftware.smack.packet.Element)1 FormField (org.jivesoftware.smackx.xdata.FormField)1 OpenValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement)1 RegexValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement)1