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.io;
28  
29  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
30  import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
31  import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
32  import gov.nist.secauto.metaschema.core.metapath.StaticContext;
33  import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
34  import gov.nist.secauto.metaschema.core.model.constraint.DefaultConstraintValidator;
35  import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidationHandler;
36  import gov.nist.secauto.metaschema.core.model.constraint.LoggingConstraintValidationHandler;
37  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
38  import gov.nist.secauto.metaschema.databind.model.IAssemblyClassBinding;
39  
40  import java.io.IOException;
41  import java.io.Reader;
42  import java.net.URI;
43  
44  import edu.umd.cs.findbugs.annotations.NonNull;
45  
46  /**
47   * The base class of all format-specific deserializers.
48   *
49   * @param <CLASS>
50   *          the bound class to deserialize to
51   */
52  public abstract class AbstractDeserializer<CLASS>
53      extends AbstractSerializationBase<DeserializationFeature<?>>
54      implements IDeserializer<CLASS> {
55  
56    private IConstraintValidationHandler constraintValidationHandler;
57  
58    /**
59     * Construct a new deserializer.
60     *
61     * @param classBinding
62     *          the bound class information for the Java type this deserializer is
63     *          operating on
64     */
65    protected AbstractDeserializer(@NonNull IAssemblyClassBinding classBinding) {
66      super(classBinding);
67    }
68  
69    /**
70     * Get the constraint validation handler configured for this deserializer, which
71     * will be used to validate loaded data.
72     *
73     * @return the deserializer
74     */
75    @Override
76    @NonNull
77    public IConstraintValidationHandler getConstraintValidationHandler() {
78      synchronized (this) {
79        if (constraintValidationHandler == null) {
80          constraintValidationHandler = new LoggingConstraintValidationHandler();
81        }
82        return ObjectUtils.notNull(constraintValidationHandler);
83      }
84    }
85  
86    @Override
87    public void setConstraintValidationHandler(@NonNull IConstraintValidationHandler constraintValidationHandler) {
88      synchronized (this) {
89        this.constraintValidationHandler = constraintValidationHandler;
90      }
91    }
92  
93    @Override
94    public INodeItem deserializeToNodeItem(Reader reader, URI documentUri) throws IOException {
95  
96      INodeItem nodeItem;
97      try {
98        nodeItem = deserializeToNodeItemInternal(reader, documentUri);
99      } catch (Exception ex) { // NOPMD - this is intentional
100       throw new IOException(ex);
101     }
102 
103     if (isValidating()) {
104       StaticContext staticContext = StaticContext.newInstance();
105       DynamicContext dynamicContext = staticContext.newDynamicContext();
106       dynamicContext.setDocumentLoader(getBindingContext().newBoundLoader());
107       DefaultConstraintValidator validator = new DefaultConstraintValidator(
108           dynamicContext,
109           getConstraintValidationHandler());
110       validator.validate(nodeItem);
111       validator.finalizeValidation();
112     }
113     return nodeItem;
114   }
115 
116   /**
117    * This abstract method delegates parsing to the concrete implementation.
118    *
119    * @param reader
120    *          the reader instance to read data from
121    * @param documentUri
122    *          the URI of the document that is being read
123    * @return a new node item containing the read contents
124    * @throws IOException
125    *           if an error occurred while reading data from the stream
126    */
127   @NonNull
128   protected abstract INodeItem deserializeToNodeItemInternal(@NonNull Reader reader, @NonNull URI documentUri)
129       throws IOException;
130 
131   @Override
132   public IDeserializer<CLASS> enableFeature(DeserializationFeature<?> feature) {
133     return set(feature, true);
134   }
135 
136   @Override
137   public IDeserializer<CLASS> disableFeature(DeserializationFeature<?> feature) {
138     return set(feature, false);
139   }
140 
141   @Override
142   public IDeserializer<CLASS> applyConfiguration(
143       @NonNull IConfiguration<DeserializationFeature<?>> other) {
144     IMutableConfiguration<DeserializationFeature<?>> config = getConfiguration();
145     config.applyConfiguration(other);
146     configurationChanged(config);
147     return this;
148   }
149 
150   @Override
151   public IDeserializer<CLASS> set(DeserializationFeature<?> feature, Object value) {
152     IMutableConfiguration<DeserializationFeature<?>> config = getConfiguration();
153     config.set(feature, value);
154     configurationChanged(config);
155     return this;
156   }
157 }