001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package gov.nist.secauto.metaschema.databind.io.xml; 007 008import gov.nist.secauto.metaschema.core.model.IBoundObject; 009import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil; 010import gov.nist.secauto.metaschema.core.qname.IEnhancedQName; 011import gov.nist.secauto.metaschema.core.util.ObjectUtils; 012import gov.nist.secauto.metaschema.databind.io.AbstractProblemHandler; 013import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; 014 015import org.apache.logging.log4j.LogManager; 016import org.apache.logging.log4j.Logger; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import javax.xml.stream.events.Attribute; 022 023/** 024 * Handles problems identified in the parsed XML. 025 * <p> 026 * The default problem handler will report unknown attributes, and provide empty 027 * collections for multi-valued model items and default values for flags and 028 * single valued fields. 029 */ 030public class DefaultXmlProblemHandler 031 extends AbstractProblemHandler 032 implements IXmlProblemHandler { 033 private static final Logger LOGGER = LogManager.getLogger(DefaultXmlProblemHandler.class); 034 035 private static final IEnhancedQName XSI_SCHEMA_LOCATION 036 = IEnhancedQName.of("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation"); 037 private static final Set<IEnhancedQName> IGNORED_QNAMES; 038 039 static { 040 IGNORED_QNAMES = new HashSet<>(); 041 IGNORED_QNAMES.add(XSI_SCHEMA_LOCATION); 042 } 043 044 @Override 045 public boolean handleUnknownAttribute( 046 IBoundDefinitionModelComplex parentDefinition, 047 IBoundObject targetObject, 048 Attribute attribute, 049 IXmlParsingContext parsingContext) { 050 IEnhancedQName qname = IEnhancedQName.of(attribute.getName()); 051 // check if warning is needed 052 if (LOGGER.isWarnEnabled() && !IGNORED_QNAMES.contains(qname)) { 053 LOGGER.atWarn().log("Skipping unrecognized attribute '{}'{}.", 054 qname, 055 XmlEventUtil.generateLocationMessage( 056 ObjectUtils.notNull(attribute.getLocation()), 057 parsingContext.getSource())); 058 } 059 // always ignore 060 return true; 061 } 062}