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.io; 028 029import gov.nist.secauto.metaschema.core.configuration.IConfiguration; 030import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration; 031import gov.nist.secauto.metaschema.core.metapath.DynamicContext; 032import gov.nist.secauto.metaschema.core.metapath.StaticContext; 033import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; 034import gov.nist.secauto.metaschema.core.model.constraint.DefaultConstraintValidator; 035import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidationHandler; 036import gov.nist.secauto.metaschema.core.model.constraint.LoggingConstraintValidationHandler; 037import gov.nist.secauto.metaschema.core.util.ObjectUtils; 038import gov.nist.secauto.metaschema.databind.model.IAssemblyClassBinding; 039 040import java.io.IOException; 041import java.io.Reader; 042import java.net.URI; 043 044import edu.umd.cs.findbugs.annotations.NonNull; 045 046/** 047 * The base class of all format-specific deserializers. 048 * 049 * @param <CLASS> 050 * the bound class to deserialize to 051 */ 052public abstract class AbstractDeserializer<CLASS> 053 extends AbstractSerializationBase<DeserializationFeature<?>> 054 implements IDeserializer<CLASS> { 055 056 private IConstraintValidationHandler constraintValidationHandler; 057 058 /** 059 * Construct a new deserializer. 060 * 061 * @param classBinding 062 * the bound class information for the Java type this deserializer is 063 * operating on 064 */ 065 protected AbstractDeserializer(@NonNull IAssemblyClassBinding classBinding) { 066 super(classBinding); 067 } 068 069 /** 070 * Get the constraint validation handler configured for this deserializer, which 071 * will be used to validate loaded data. 072 * 073 * @return the deserializer 074 */ 075 @Override 076 @NonNull 077 public IConstraintValidationHandler getConstraintValidationHandler() { 078 synchronized (this) { 079 if (constraintValidationHandler == null) { 080 constraintValidationHandler = new LoggingConstraintValidationHandler(); 081 } 082 return ObjectUtils.notNull(constraintValidationHandler); 083 } 084 } 085 086 @Override 087 public void setConstraintValidationHandler(@NonNull IConstraintValidationHandler constraintValidationHandler) { 088 synchronized (this) { 089 this.constraintValidationHandler = constraintValidationHandler; 090 } 091 } 092 093 @Override 094 public INodeItem deserializeToNodeItem(Reader reader, URI documentUri) throws IOException { 095 096 INodeItem nodeItem; 097 try { 098 nodeItem = deserializeToNodeItemInternal(reader, documentUri); 099 } 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}