001/**
002 *
003 * Copyright 2014 Anno van Vliet
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdatavalidation.provider;
018
019import java.io.IOException;
020import java.util.logging.Logger;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.datatypes.UInt32;
025import org.jivesoftware.smack.packet.XmlEnvironment;
026import org.jivesoftware.smack.util.ParserUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProvider;
031import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
032import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement;
033import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange;
034import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement;
035import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
036import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement;
037
038/**
039 * Extension Provider for Data validation of forms.
040 *
041 * @author Anno van Vliet
042 *
043 */
044public class DataValidationProvider extends FormFieldChildElementProvider<ValidateElement> {
045    private static final Logger LOGGER = Logger.getLogger(DataValidationProvider.class.getName());
046
047    @Override
048    public ValidateElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
049                    throws XmlPullParserException, IOException {
050        final String dataType = parser.getAttributeValue("", "datatype");
051        ValidateElement dataValidation = null;
052        ListRange listRange = null;
053
054        outerloop: while (true) {
055            XmlPullParser.Event eventType = parser.next();
056            switch (eventType) {
057            case START_ELEMENT:
058                String name = parser.getName();
059                switch (name) {
060                case OpenValidateElement.METHOD:
061                    dataValidation = new OpenValidateElement(dataType);
062                    break;
063                case BasicValidateElement.METHOD:
064                    dataValidation = new BasicValidateElement(dataType);
065                    break;
066                case RangeValidateElement.METHOD:
067                    dataValidation = new RangeValidateElement(dataType,
068                                    parser.getAttributeValue("", "min"),
069                                    parser.getAttributeValue("", "max")
070                                    );
071                    break;
072                case RegexValidateElement.METHOD:
073                    dataValidation = new RegexValidateElement(dataType, parser.nextText());
074                    break;
075                case ListRange.ELEMENT:
076                    UInt32 min = ParserUtils.getUInt32Attribute(parser, "min");
077                    UInt32 max = ParserUtils.getUInt32Attribute(parser, "max");
078                    if (min != null || max != null) {
079                        listRange = new ListRange(min, max);
080                    } else {
081                        LOGGER.fine("Ignoring list-range element without min or max attribute");
082                    }
083                    break;
084                default:
085                    break;
086                }
087                break;
088            case END_ELEMENT:
089                if (parser.getDepth() == initialDepth) {
090                    if (dataValidation == null) {
091                        // XEP-122 ยง 3.2 states that "If no validation method is specified,
092                        // form processors MUST assume <basic/> validation."
093                        dataValidation = new BasicValidateElement(dataType);
094                    }
095                    dataValidation.setListRange(listRange);
096                    break outerloop;
097                }
098                break;
099            default:
100                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
101                break;
102            }
103        }
104        return dataValidation;
105    }
106
107    @Override
108    public QName getQName() {
109        return ValidateElement.QNAME;
110    }
111
112}