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.formtypes; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.util.Objects; 025import org.jivesoftware.smack.util.StringUtils; 026import org.jivesoftware.smack.util.XmlUtil; 027import org.jivesoftware.smackx.xdata.FormField; 028import org.jivesoftware.smackx.xdata.TextSingleFormField; 029import org.jivesoftware.smackx.xdata.packet.DataForm; 030 031public class FormFieldRegistry { 032 033 private static final Logger LOGGER = Logger.getLogger(FormFieldRegistry.class.getName()); 034 035 private static final Map<String, Map<String, FormField.Type>> REGISTRY = new HashMap<>(); 036 037 private static final Map<String, FormField.Type> CLARK_NOTATION_FIELD_REGISTRY = new ConcurrentHashMap<>(); 038 039 private static final Map<String, FormField.Type> LOOKASIDE_FIELD_REGISTRY = new ConcurrentHashMap<>(); 040 041 @SuppressWarnings("ReferenceEquality") 042 public static void register(DataForm dataForm) { 043 // TODO: Also allow forms of type 'result'? 044 if (dataForm.getType() != DataForm.Type.form) { 045 throw new IllegalArgumentException(); 046 } 047 048 String formType = null; 049 TextSingleFormField hiddenFormTypeField = dataForm.getHiddenFormTypeField(); 050 if (hiddenFormTypeField != null) { 051 formType = hiddenFormTypeField.getValue(); 052 } 053 054 for (FormField formField : dataForm.getFields()) { 055 // Note that we can compare here by reference equality to skip the hidden form type field. 056 if (formField == hiddenFormTypeField) { 057 continue; 058 } 059 060 FormField.Type type = formField.getType(); 061 if (type == FormField.Type.fixed) { 062 continue; 063 } 064 065 String fieldName = formField.getFieldName(); 066 register(formType, fieldName, type); 067 } 068 } 069 070 public static void register(String formType, String fieldName, FormField.Type fieldType) { 071 StringUtils.requireNotNullNorEmpty(fieldName, "fieldName must be provided"); 072 Objects.requireNonNull(fieldType); 073 074 if (formType == null) { 075 if (XmlUtil.isClarkNotation(fieldName)) { 076 CLARK_NOTATION_FIELD_REGISTRY.put(fieldName, fieldType); 077 } 078 return; 079 } 080 081 FormField.Type previousType; 082 synchronized (REGISTRY) { 083 Map<String, FormField.Type> fieldNameToType = REGISTRY.get(formType); 084 if (fieldNameToType == null) { 085 fieldNameToType = new HashMap<>(); 086 REGISTRY.put(formType, fieldNameToType); 087 } else { 088 previousType = fieldNameToType.get(fieldName); 089 if (previousType != null && previousType != fieldType) { 090 throw new IllegalArgumentException(); 091 } 092 } 093 previousType = fieldNameToType.put(fieldName, fieldType); 094 } 095 if (previousType != null && fieldType != previousType) { 096 LOGGER.warning("Form field registry inconsitency detected: Registered field '" + fieldName + "' of type " + fieldType + " but previous type was " + previousType); 097 } 098 099 } 100 101 public static FormField.Type lookup(String formType, String fieldName) { 102 if (formType == null) { 103 if (XmlUtil.isClarkNotation(fieldName)) { 104 return CLARK_NOTATION_FIELD_REGISTRY.get(fieldName); 105 } 106 107 return LOOKASIDE_FIELD_REGISTRY.get(fieldName); 108 } 109 110 synchronized (REGISTRY) { 111 Map<String, FormField.Type> fieldNameToTypeMap = REGISTRY.get(formType); 112 if (fieldNameToTypeMap != null) { 113 FormField.Type type = fieldNameToTypeMap.get(fieldName); 114 if (type != null) { 115 return type; 116 } 117 } 118 } 119 120 return null; 121 } 122 123 public static synchronized FormField.Type lookup(String fieldName) { 124 return lookup(null, fieldName); 125 } 126 127 public static void addLookasideFieldRegistryEntry(String fieldName, FormField.Type formFieldType) { 128 LOOKASIDE_FIELD_REGISTRY.put(fieldName, formFieldType); 129 } 130}