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 org.jivesoftware.smack.util.Objects; 020import org.jivesoftware.smack.util.StringUtils; 021 022import org.jivesoftware.smackx.xdata.FormField; 023import org.jivesoftware.smackx.xdata.TextSingleFormField; 024import org.jivesoftware.smackx.xdata.packet.DataForm; 025import org.jivesoftware.smackx.xdata.packet.DataForm.Type; 026 027public abstract class FilledForm implements FormReader { 028 029 private final DataForm dataForm; 030 031 protected final TextSingleFormField formTypeFormField; 032 033 public FilledForm(DataForm dataForm) { 034 this.dataForm = Objects.requireNonNull(dataForm); 035 String formType = dataForm.getFormType(); 036 if (StringUtils.isNullOrEmpty(formType)) { 037 throw new IllegalArgumentException("The provided data form has no hidden FROM_TYPE field."); 038 } 039 if (dataForm.getType() == Type.cancel) { 040 throw new IllegalArgumentException("Forms of type 'cancel' are not filled nor fillable"); 041 } 042 formTypeFormField = dataForm.getHiddenFormTypeField(); 043 } 044 045 @Override 046 public FormField getField(String fieldName) { 047 return dataForm.getField(fieldName); 048 } 049 050 public String getTitle() { 051 return dataForm.getTitle(); 052 } 053 054 public StringBuilder getInstructions() { 055 StringBuilder sb = new StringBuilder(); 056 for (String instruction : dataForm.getInstructions()) { 057 sb.append(instruction).append('\n'); 058 } 059 return sb; 060 } 061 062 public DataForm getDataForm() { 063 return dataForm; 064 } 065 066 public String getFormType() { 067 if (formTypeFormField == null) { 068 return null; 069 } 070 return formTypeFormField.getValue(); 071 } 072 073 public boolean hasField(String fieldName) { 074 return dataForm.hasField(fieldName); 075 } 076 077 protected FormField getFieldOrThrow(String fieldName) { 078 FormField formField = getField(fieldName); 079 if (formField == null) { 080 throw new IllegalArgumentException("No field named " + fieldName); 081 } 082 return formField; 083 } 084 085 protected static void ensureFormType(DataForm dataForm, String formType) { 086 String dataFormType = dataForm.getFormType(); 087 if (!formType.equals(dataFormType)) { 088 throw new IllegalArgumentException("The provided data form must be of type '" + formType 089 + "', this one was of type '" + dataFormType + '\''); 090 } 091 } 092 093}