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