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.schemagen;
28  
29  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
30  import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
31  import gov.nist.secauto.metaschema.core.model.IDefinition;
32  import gov.nist.secauto.metaschema.core.model.IModule;
33  import gov.nist.secauto.metaschema.core.model.INamedInstance;
34  import gov.nist.secauto.metaschema.core.model.IValuedDefinition;
35  import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValue;
36  import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint;
37  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
38  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
39  import gov.nist.secauto.metaschema.schemagen.datatype.IDatatypeManager;
40  
41  import java.util.ArrayList;
42  import java.util.LinkedList;
43  import java.util.List;
44  import java.util.Locale;
45  
46  import edu.umd.cs.findbugs.annotations.NonNull;
47  import edu.umd.cs.findbugs.annotations.Nullable;
48  
49  public abstract class AbstractGenerationState<WRITER, DATATYPE_MANAGER extends IDatatypeManager>
50      implements IGenerationState<WRITER> {
51    @NonNull
52    private final IModule module;
53    @NonNull
54    private final WRITER writer;
55    @NonNull
56    private final DATATYPE_MANAGER datatypeManager;
57    @NonNull
58    private final IInlineStrategy inlineStrategy;
59  
60    @NonNull
61    private final ModuleIndex moduleIndex;
62  
63    public AbstractGenerationState(
64        @NonNull IModule module,
65        @NonNull WRITER writer,
66        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration,
67        @NonNull DATATYPE_MANAGER datatypeManager) {
68      this.module = module;
69      this.writer = writer;
70      this.datatypeManager = datatypeManager;
71      this.inlineStrategy = IInlineStrategy.newInlineStrategy(configuration);
72      this.moduleIndex = ModuleIndex.indexDefinitions(module);
73    }
74  
75    @Override
76    public IModule getModule() {
77      return module;
78    }
79  
80    @Override
81    public WRITER getWriter() {
82      return writer;
83    }
84  
85    @NonNull
86    protected DATATYPE_MANAGER getDatatypeManager() {
87      return datatypeManager;
88    }
89  
90    @NonNull
91    public ModuleIndex getMetaschemaIndex() {
92      return moduleIndex;
93    }
94  
95    @Override
96    public boolean isInline(@NonNull IDefinition definition) {
97      return inlineStrategy.isInline(definition, getMetaschemaIndex());
98    }
99  
100   /**
101    * Retrieve any allowed values that are context independent, meaning they always
102    * apply regardless of the location of the node in the larger graph.
103    *
104    * @param definition
105    *          the definition to get allowed values for
106    * @return the list of allowed values or an empty list
107    */
108   @NonNull
109   protected static AllowedValueCollection getContextIndependentEnumeratedValues(
110       @NonNull IValuedDefinition definition) {
111     List<IAllowedValue> values = new LinkedList<>();
112     boolean closed = false;
113     for (IAllowedValuesConstraint constraint : definition.getAllowedValuesConstraints()) {
114       if (!constraint.isAllowedOther()) {
115         closed = true;
116       }
117 
118       if (!MetapathExpression.CONTEXT_NODE.equals(constraint.getTarget())) {
119         values = CollectionUtil.emptyList();
120         break;
121       }
122 
123       values.addAll(constraint.getAllowedValues().values());
124     }
125     return new AllowedValueCollection(closed, values);
126   }
127 
128   /**
129    * Get the name of the definition (and any parent instances/definition) to
130    * ensure an inline type is unique.
131    *
132    * @param definition
133    *          the definition to generate a type name for
134    * @param childModule
135    *          the module of the left node
136    * @return the unique type name
137    */
138   private CharSequence getTypeContext(
139       @NonNull IDefinition definition,
140       @NonNull IModule childModule) {
141     StringBuilder builder = new StringBuilder();
142     if (definition.isInline()) {
143       INamedInstance inlineInstance = definition.getInlineInstance();
144       IDefinition parentDefinition = inlineInstance.getContainingDefinition();
145 
146       builder
147           .append(getTypeContext(parentDefinition, childModule))
148           .append(toCamelCase(inlineInstance.getEffectiveName()));
149     } else {
150       builder.append(toCamelCase(definition.getEffectiveName()));
151     }
152     return builder;
153   }
154 
155   @NonNull
156   public String getTypeNameForDefinition(@NonNull IDefinition definition, @Nullable String suffix) {
157     StringBuilder builder = new StringBuilder()
158         .append(toCamelCase(definition.getModelType().name()))
159         .append(toCamelCase(definition.getContainingModule().getShortName()));
160 
161     if (isInline(definition)) {
162       builder.append(toCamelCase(definition.getEffectiveName()));
163     } else {
164       // need to append the parent name(s) to disambiguate this type name
165       builder.append(getTypeContext(definition, definition.getContainingModule()));
166     }
167     if (suffix != null && !suffix.isBlank()) {
168       builder.append(toCamelCase(suffix));
169     }
170     builder.append("Type");
171 
172     return ObjectUtils.notNull(builder.toString());
173   }
174 
175   @NonNull
176   protected static CharSequence toCamelCase(String text) {
177     StringBuilder builder = new StringBuilder();
178     for (String segment : text.split("\\p{Punct}")) {
179       if (segment.length() > 0) {
180         builder.append(segment.substring(0, 1).toUpperCase(Locale.ROOT));
181       }
182       if (segment.length() > 1) {
183         builder.append(segment.substring(1).toLowerCase(Locale.ROOT));
184       }
185     }
186     return builder;
187   }
188 
189   public static class AllowedValueCollection {
190     private final boolean closed;
191     @NonNull
192     private final List<IAllowedValue> values;
193 
194     public AllowedValueCollection(boolean closed, @NonNull List<IAllowedValue> values) {
195       this.closed = closed;
196       this.values = CollectionUtil.unmodifiableList(new ArrayList<>(values));
197     }
198 
199     public boolean isClosed() {
200       return closed;
201     }
202 
203     @NonNull
204     public List<IAllowedValue> getValues() {
205       return values;
206     }
207   }
208 }