001/* 002 * Portions of this software was developed by employees of the National Institute 003 * of Standards and Technology (NIST), an agency of the Federal Government and is 004 * being made available as a public service. Pursuant to title 17 United States 005 * Code Section 105, works of NIST employees are not subject to copyright 006 * protection in the United States. This software may be subject to foreign 007 * copyright. Permission in the United States and in foreign countries, to the 008 * extent that NIST may hold copyright, to use, copy, modify, create derivative 009 * works, and distribute this software and its documentation without fee is hereby 010 * granted on a non-exclusive basis, provided that this notice and disclaimer 011 * of warranty appears in all copies. 012 * 013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY 015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF 016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM 017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE 018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT 019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, 020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, 021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, 022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR 023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT 024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. 025 */ 026 027package gov.nist.secauto.metaschema.databind.model; 028 029import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; 030import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; 031import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; 032import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; 033import gov.nist.secauto.metaschema.core.util.ObjectUtils; 034import gov.nist.secauto.metaschema.databind.IBindingContext; 035import gov.nist.secauto.metaschema.databind.model.annotations.Constants; 036import gov.nist.secauto.metaschema.databind.model.annotations.NullJavaTypeAdapter; 037import gov.nist.secauto.metaschema.databind.model.annotations.XmlSchema; 038 039import edu.umd.cs.findbugs.annotations.NonNull; 040import edu.umd.cs.findbugs.annotations.Nullable; 041 042public final class ModelUtil { 043 private ModelUtil() { 044 // disable construction 045 } 046 047 /** 048 * Resolves a provided local name value. If the value is {@code null} or 049 * "##default", then the provided default value will be used instead. If the 050 * value is "##none", then the value will be {@code null}. Otherwise, the value 051 * is returned. 052 * 053 * @param value 054 * the requested value 055 * @param defaultValue 056 * the default value 057 * @return the resolved value 058 */ 059 public static String resolveLocalName(String value, String defaultValue) { 060 String retval; 061 if (value == null || Constants.DEFAULT_STRING_VALUE.equals(value)) { 062 retval = defaultValue; 063 } else if (Constants.NO_STRING_VALUE.equals(value)) { 064 retval = null; // NOPMD - intentional 065 } else { 066 retval = value; 067 } 068 return retval; 069 } 070 071 @Nullable 072 public static String resolveOptionalNamespace(String annotationValue, IClassBinding classBinding) { 073 return resolveNamespace(annotationValue, classBinding, true); 074 } 075 076 @NonNull 077 public static String resolveNamespace(String annotationValue, IClassBinding classBinding) { 078 return ObjectUtils.notNull(resolveNamespace(annotationValue, classBinding, false)); 079 } 080 081 /** 082 * Resolves a provided namespace value. If the value is {@code null} or 083 * "##default", then the provided default value will be used instead. If the 084 * value is {@code null} or "##none", then a {@code null} value will be used if 085 * allowNone is {@code true}. Otherwise, the value is returned. 086 * 087 * @param value 088 * the requested value 089 * @param classBinding 090 * a class with the {@link XmlSchema} annotation 091 * @param allowNone 092 * if the "##none" value is honored 093 * @return the resolved value or {@code null} if no namespace is defined 094 */ 095 private static String resolveNamespace(String value, IClassBinding classBinding, boolean allowNone) { 096 String retval; 097 if (value == null || Constants.DEFAULT_STRING_VALUE.equals(value)) { 098 // get namespace from the metaschema 099 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}