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