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 org.jivesoftware.smack.util.ParserUtils;
023
024import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
025import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement;
026import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange;
027import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement;
028import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
029import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement;
030
031import org.xmlpull.v1.XmlPullParser;
032import org.xmlpull.v1.XmlPullParserException;
033
034/**
035 * Extension Provider for Data validation of forms.
036 *
037 * @author Anno van Vliet
038 *
039 */
040public class DataValidationProvider {
041    private static final Logger LOGGER = Logger.getLogger(DataValidationProvider.class.getName());
042
043    public static ValidateElement parse(XmlPullParser parser) throws XmlPullParserException, IOException {
044        final int initialDepth = parser.getDepth();
045        final String dataType = parser.getAttributeValue("", "datatype");
046        ValidateElement dataValidation = null;
047        ListRange listRange = null;
048
049        outerloop: while (true) {
050            int eventType = parser.next();
051            switch (eventType) {
052            case XmlPullParser.START_TAG:
053                String name = parser.getName();
054                switch (name) {
055                case OpenValidateElement.METHOD:
056                    dataValidation = new OpenValidateElement(dataType);
057                    break;
058                case BasicValidateElement.METHOD:
059                    dataValidation = new BasicValidateElement(dataType);
060                    break;
061                case RangeValidateElement.METHOD:
062                    dataValidation = new RangeValidateElement(dataType,
063                                    parser.getAttributeValue("", "min"),
064                                    parser.getAttributeValue("", "max")
065                                    );
066                    break;
067                case RegexValidateElement.METHOD:
068                    dataValidation = new RegexValidateElement(dataType,parser.nextText());
069                    break;
070                case ListRange.ELEMENT:
071                    Long min = ParserUtils.getLongAttribute(parser, "min");
072                    Long max = ParserUtils.getLongAttribute(parser, "max");
073                    if (min != null || max != null) {
074                        listRange = new ListRange(min, max);
075                    } else {
076                        LOGGER.fine("Ignoring list-range element without min or max attribute");
077                    }
078                    break;
079                default:
080                    break;
081                }
082                break;
083            case XmlPullParser.END_TAG:
084                if (parser.getDepth() == initialDepth) {
085                    if (dataValidation == null) {
086                        // XEP-122 ยง 3.2 states that "If no validation method is specified,
087                        // form processors MUST assume <basic/> validation."
088                        dataValidation = new BasicValidateElement(dataType);
089                    }
090                    dataValidation.setListRange(listRange);
091                    break outerloop;
092                }
093                break;
094            }
095        }
096        return dataValidation;
097    }
098
099}