1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.io.xml;
7   
8   import gov.nist.secauto.metaschema.core.model.IBoundObject;
9   import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil;
10  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11  import gov.nist.secauto.metaschema.databind.io.AbstractProblemHandler;
12  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
13  
14  import org.apache.logging.log4j.LogManager;
15  import org.apache.logging.log4j.Logger;
16  
17  import java.util.HashSet;
18  import java.util.Set;
19  
20  import javax.xml.namespace.QName;
21  import javax.xml.stream.events.Attribute;
22  
23  /**
24   * Handles problems identified in the parsed XML.
25   * <p>
26   * The default problem handler will report unknown attributes, and provide empty
27   * collections for multi-valued model items and default values for flags and
28   * single valued fields.
29   */
30  public class DefaultXmlProblemHandler
31      extends AbstractProblemHandler
32      implements IXmlProblemHandler {
33    private static final Logger LOGGER = LogManager.getLogger(DefaultXmlProblemHandler.class);
34  
35    private static final QName XSI_SCHEMA_LOCATION
36        = new QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
37    private static final Set<QName> IGNORED_QNAMES;
38  
39    static {
40      IGNORED_QNAMES = new HashSet<>();
41      IGNORED_QNAMES.add(XSI_SCHEMA_LOCATION);
42    }
43  
44    @Override
45    public boolean handleUnknownAttribute(
46        IBoundDefinitionModelComplex parentDefinition,
47        IBoundObject targetObject,
48        Attribute attribute,
49        IXmlParsingContext parsingContext) {
50      QName qname = attribute.getName();
51      // check if warning is needed
52      if (LOGGER.isWarnEnabled() && !IGNORED_QNAMES.contains(qname)) {
53        LOGGER.atWarn().log("Skipping unrecognized attribute '{}'{}.",
54            qname,
55            XmlEventUtil.generateLocationMessage(ObjectUtils.notNull(attribute.getLocation())));
56      }
57      // always ignore
58      return true;
59    }
60  }