1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind;
7   
8   import gov.nist.secauto.metaschema.core.model.IBoundObject;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  import gov.nist.secauto.metaschema.databind.IBindingContext.IBindingMatcher;
11  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
12  
13  import javax.xml.namespace.QName;
14  
15  import edu.umd.cs.findbugs.annotations.NonNull;
16  import nl.talsmasoftware.lazy4j.Lazy;
17  
18  class RootAssemblyBindingMatcher implements IBindingMatcher {
19    @NonNull
20    private final IBoundDefinitionModelAssembly definition;
21    @NonNull
22    private final Lazy<QName> rootQName = ObjectUtils.notNull(
23        Lazy.lazy(() -> getDefinition().getRootQName().toQName()));
24  
25    public RootAssemblyBindingMatcher(
26        @NonNull IBoundDefinitionModelAssembly definition) {
27      this.definition = definition;
28    }
29  
30    protected IBoundDefinitionModelAssembly getDefinition() {
31      return definition;
32    }
33  
34    protected Class<? extends IBoundObject> getClazz() {
35      return getDefinition().getBoundClass();
36    }
37  
38    @NonNull
39    protected QName getRootQName() {
40      return ObjectUtils.notNull(rootQName.get());
41    }
42  
43    @SuppressWarnings("null")
44    @NonNull
45    protected String getRootJsonName() {
46      return getDefinition().getRootJsonName();
47    }
48  
49    @Override
50    public Class<? extends IBoundObject> getBoundClassForXmlQName(QName rootQName) {
51      return getRootQName().equals(rootQName) ? getClazz() : null;
52    }
53  
54    @Override
55    public Class<? extends IBoundObject> getBoundClassForJsonName(String rootName) {
56      return getRootJsonName().equals(rootName) ? getClazz() : null;
57    }
58  
59    @Override
60    public String toString() {
61      return getDefinition().getRootQName().toString();
62    }
63  }