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.cli.commands; 028 029import gov.nist.secauto.metaschema.cli.processor.CLIProcessor; 030import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext; 031import gov.nist.secauto.metaschema.cli.processor.ExitCode; 032import gov.nist.secauto.metaschema.cli.processor.ExitStatus; 033import gov.nist.secauto.metaschema.cli.processor.InvalidArgumentException; 034import gov.nist.secauto.metaschema.cli.processor.command.AbstractTerminalCommand; 035import gov.nist.secauto.metaschema.cli.processor.command.DefaultExtraArgument; 036import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument; 037import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor; 038import gov.nist.secauto.metaschema.cli.util.LoggingValidationHandler; 039import gov.nist.secauto.metaschema.core.model.util.XmlUtil; 040import gov.nist.secauto.metaschema.core.model.validation.IContentValidator; 041import gov.nist.secauto.metaschema.core.model.validation.IValidationResult; 042import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader; 043import gov.nist.secauto.metaschema.core.util.CollectionUtil; 044import gov.nist.secauto.metaschema.core.util.ObjectUtils; 045 046import org.apache.commons.cli.CommandLine; 047import org.apache.logging.log4j.LogManager; 048import org.apache.logging.log4j.Logger; 049import org.xml.sax.SAXException; 050 051import java.io.File; 052import java.io.IOException; 053import java.nio.file.Path; 054import java.nio.file.Paths; 055import java.util.LinkedList; 056import java.util.List; 057 058import javax.xml.transform.Source; 059 060import edu.umd.cs.findbugs.annotations.NonNull; 061 062public class ValidateModuleCommand 063 extends AbstractTerminalCommand { 064 private static final Logger LOGGER = LogManager.getLogger(ValidateModuleCommand.class); 065 @NonNull 066 private static final String COMMAND = "validate"; 067 @NonNull 068 private static final List<ExtraArgument> EXTRA_ARGUMENTS; 069 070 static { 071 EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of( 072 new DefaultExtraArgument("Module file to validate", true))); 073 } 074 075 @Override 076 public String getName() { 077 return COMMAND; 078 } 079 080 @Override 081 public String getDescription() { 082 return "Validate that the specified Module is well-formed and valid to the Module model"; 083 } 084 085 @Override 086 public List<ExtraArgument> getExtraArguments() { 087 return EXTRA_ARGUMENTS; 088 } 089 090 @NonNull 091 protected List<Source> getXmlSchemaSources() throws IOException { 092 List<Source> retval = new LinkedList<>(); 093 retval.add(XmlUtil.getStreamSource( 094 ObjectUtils.requireNonNull( 095 ModuleLoader.class.getResource("/schema/xml/metaschema.xsd"), 096 "Unable to load '/schema/xml/metaschema.xsd' on the classpath"))); 097 return CollectionUtil.unmodifiableList(retval); 098 } 099 100 @Override 101 public void validateOptions(CallingContext callingContext, CommandLine cmdLine) throws InvalidArgumentException { 102 List<String> extraArgs = cmdLine.getArgList(); 103 if (extraArgs.size() != 1) { 104 throw new InvalidArgumentException("The source to validate must be provided."); 105 } 106 107 File target = new File(extraArgs.get(0)); 108 if (!target.exists()) { 109 throw new InvalidArgumentException("The provided source file '" + target.getPath() + "' does not exist."); 110 } 111 if (!target.canRead()) { 112 throw new InvalidArgumentException("The provided source file '" + target.getPath() + "' is not readable."); 113 } 114 } 115 116 @Override 117 public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine cmdLine) { 118 return ICommandExecutor.using(callingContext, cmdLine, this::executeCommand); 119 } 120 121 @SuppressWarnings({ "PMD.OnlyOneReturn", "unused" }) // readability 122 @NonNull 123 protected ExitStatus executeCommand(CallingContext callingContext, CommandLine cmdLine) { 124 List<String> extraArgs = cmdLine.getArgList(); 125 Path target = Paths.get(extraArgs.get(0)); 126 assert target != null; 127 128 IValidationResult schemaValidationResult; 129 try { 130 List<Source> schemaSources = getXmlSchemaSources(); 131 schemaValidationResult = IContentValidator.validateWithXmlSchema( 132 ObjectUtils.notNull(target.toUri()), 133 schemaSources); 134 } catch (IOException | SAXException ex) { 135 return ExitCode.PROCESSING_ERROR.exit().withThrowable(ex); 136 } 137 138 if (!schemaValidationResult.isPassing()) { 139 if (LOGGER.isErrorEnabled()) { 140 LOGGER.error("The file '{}' has validation issue(s). The issues are:", target); 141 } 142 LoggingValidationHandler.instance().handleValidationResults(schemaValidationResult); 143 return ExitCode.FAIL.exit(); 144 } 145 146 if (schemaValidationResult.isPassing() && !cmdLine.hasOption(CLIProcessor.QUIET_OPTION) && LOGGER.isInfoEnabled()) { 147 LOGGER.info("The file '{}' is valid.", target); 148 } 149 return ExitCode.OK.exit(); 150 } 151}