View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.databind.model;
28  
29  import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter;
30  import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider;
31  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
32  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
33  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34  import gov.nist.secauto.metaschema.databind.IBindingContext;
35  import gov.nist.secauto.metaschema.databind.model.annotations.Constants;
36  import gov.nist.secauto.metaschema.databind.model.annotations.NullJavaTypeAdapter;
37  import gov.nist.secauto.metaschema.databind.model.annotations.XmlSchema;
38  
39  import edu.umd.cs.findbugs.annotations.NonNull;
40  import edu.umd.cs.findbugs.annotations.Nullable;
41  
42  public final class ModelUtil {
43    private ModelUtil() {
44      // disable construction
45    }
46  
47    /**
48     * Resolves a provided local name value. If the value is {@code null} or
49     * "##default", then the provided default value will be used instead. If the
50     * value is "##none", then the value will be {@code null}. Otherwise, the value
51     * is returned.
52     *
53     * @param value
54     *          the requested value
55     * @param defaultValue
56     *          the default value
57     * @return the resolved value
58     */
59    public static String resolveLocalName(String value, String defaultValue) {
60      String retval;
61      if (value == null || Constants.DEFAULT_STRING_VALUE.equals(value)) {
62        retval = defaultValue;
63      } else if (Constants.NO_STRING_VALUE.equals(value)) {
64        retval = null; // NOPMD - intentional
65      } else {
66        retval = value;
67      }
68      return retval;
69    }
70  
71    @Nullable
72    public static String resolveOptionalNamespace(String annotationValue, IClassBinding classBinding) {
73      return resolveNamespace(annotationValue, classBinding, true);
74    }
75  
76    @NonNull
77    public static String resolveNamespace(String annotationValue, IClassBinding classBinding) {
78      return ObjectUtils.notNull(resolveNamespace(annotationValue, classBinding, false));
79    }
80  
81    /**
82     * Resolves a provided namespace value. If the value is {@code null} or
83     * "##default", then the provided default value will be used instead. If the
84     * value is {@code null} or "##none", then a {@code null} value will be used if
85     * allowNone is {@code true}. Otherwise, the value is returned.
86     *
87     * @param value
88     *          the requested value
89     * @param classBinding
90     *          a class with the {@link XmlSchema} annotation
91     * @param allowNone
92     *          if the "##none" value is honored
93     * @return the resolved value or {@code null} if no namespace is defined
94     */
95    private static String resolveNamespace(String value, IClassBinding classBinding, boolean allowNone) {
96      String retval;
97      if (value == null || Constants.DEFAULT_STRING_VALUE.equals(value)) {
98        // get namespace from the metaschema
99        retval = classBinding.getContainingModule().getXmlNamespace().toASCIIString();
100     } else if (allowNone && Constants.NO_STRING_VALUE.equals(value)) {
101       retval = null; // NOPMD - intentional
102     } else {
103       retval = value;
104     }
105     return retval;
106   }
107 
108   /**
109    * Get the markup value of a markdown string.
110    *
111    * @param annotationValue
112    *          markdown text or {@code "##none"} if no text is provided
113    * @return the markup line content or {@code null} if no markup content was
114    *         provided
115    */
116   @Nullable
117   public static MarkupLine resolveToMarkupLine(@NonNull String annotationValue) {
118     return Constants.NO_STRING_VALUE.equals(annotationValue) ? null : MarkupLine.fromMarkdown(annotationValue);
119   }
120 
121   /**
122    * Get the markup value of a markdown string.
123    *
124    * @param annotationValue
125    *          markdown text or {@code "##none"} if no text is provided
126    * @return the markup line content or {@code null} if no markup content was
127    *         provided
128    */
129   @Nullable
130   public static MarkupMultiline resolveToMarkupMultiline(@NonNull String annotationValue) {
131     return Constants.NO_STRING_VALUE.equals(annotationValue) ? null : MarkupMultiline.fromMarkdown(annotationValue);
132   }
133 
134   /**
135    * Get the string value of a string.
136    *
137    * @param annotationValue
138    *          text or {@code "##none"} if no text is provided
139    * @return the string content or {@code null} if no string content was provided
140    */
141   @Nullable
142   public static String resolveToString(@NonNull String annotationValue) {
143     return Constants.NO_STRING_VALUE.equals(annotationValue) ? null : annotationValue;
144   }
145 
146   @NonNull
147   public static IDataTypeAdapter<?> getDataTypeAdapter(@NonNull Class<? extends IDataTypeAdapter<?>> adapterClass,
148       IBindingContext bindingContext) {
149     IDataTypeAdapter<?> retval;
150     if (NullJavaTypeAdapter.class.equals(adapterClass)) {
151       retval = MetaschemaDataTypeProvider.DEFAULT_DATA_TYPE;
152     } else {
153       retval = ObjectUtils.requireNonNull(bindingContext.getJavaTypeAdapterInstance(adapterClass));
154     }
155     return retval;
156   }
157 
158   @Nullable
159   public static Object resolveDefaultValue(@NonNull String defaultValue, IDataTypeAdapter<?> adapter) {
160     Object retval = null;
161     if (!Constants.NULL_VALUE.equals(defaultValue)) {
162       retval = adapter.parse(defaultValue);
163     }
164     return retval;
165   }
166 }