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