001/**
002 *
003 * Copyright 2020 Florian Schmaus
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.xdata.form;
018
019import java.text.ParseException;
020import java.util.Collections;
021import java.util.Date;
022import java.util.List;
023
024import org.jivesoftware.smackx.xdata.AbstractMultiFormField;
025import org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField;
026import org.jivesoftware.smackx.xdata.BooleanFormField;
027import org.jivesoftware.smackx.xdata.FormField;
028
029import org.jxmpp.util.XmppDateTime;
030
031public interface FormReader {
032
033    FormField getField(String fieldName);
034
035    default String readFirstValue(String fieldName) {
036        FormField formField = getField(fieldName);
037        if (formField == null) {
038            return null;
039        }
040        return formField.getFirstValue();
041    }
042
043    default List<? extends CharSequence> readValues(String fieldName) {
044        FormField formField = getField(fieldName);
045        if (formField == null) {
046            return Collections.emptyList();
047        }
048        return formField.getValues();
049    }
050
051    default List<String> readStringValues(String fieldName) {
052        FormField formField = getField(fieldName);
053        if (formField == null) {
054            return Collections.emptyList();
055        }
056        AbstractMultiFormField multiFormField = formField.ifPossibleAs(AbstractMultiFormField.class);
057        return multiFormField.getValues();
058    }
059
060    default Boolean readBoolean(String fieldName) {
061        FormField formField = getField(fieldName);
062        if (formField == null) {
063            return null;
064        }
065        BooleanFormField booleanFormField = formField.ifPossibleAs(BooleanFormField.class);
066        return booleanFormField.getValueAsBoolean();
067    }
068
069    default Integer readInteger(String fieldName) {
070        FormField formField = getField(fieldName);
071        if (formField == null) {
072            return null;
073        }
074        AbstractSingleStringValueFormField textSingleFormField = formField.ifPossibleAs(AbstractSingleStringValueFormField.class);
075        return textSingleFormField.getValueAsInt();
076    }
077
078    default Date readDate(String fieldName) throws ParseException {
079        FormField formField = getField(fieldName);
080        if (formField == null) {
081            return null;
082        }
083        AbstractSingleStringValueFormField textSingleFormField = formField.ifPossibleAs(AbstractSingleStringValueFormField.class);
084        String value = textSingleFormField.getValue();
085        if (value == null) {
086            return null;
087        }
088        return XmppDateTime.parseDate(value);
089    }
090}