1
2
3
4
5
6 package gov.nist.secauto.metaschema.cli.commands;
7
8 import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
9 import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException;
10 import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
11 import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration;
12 import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
13 import gov.nist.secauto.metaschema.core.model.IModule;
14 import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
15 import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
16 import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator;
17 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
18 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
19 import gov.nist.secauto.metaschema.databind.IBindingContext;
20 import gov.nist.secauto.metaschema.databind.IBindingContext.ISchemaValidationProvider;
21 import gov.nist.secauto.metaschema.schemagen.ISchemaGenerator;
22 import gov.nist.secauto.metaschema.schemagen.ISchemaGenerator.SchemaFormat;
23 import gov.nist.secauto.metaschema.schemagen.SchemaGenerationFeature;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.Option;
27 import org.json.JSONObject;
28 import org.json.JSONTokener;
29 import org.xml.sax.SAXException;
30
31 import java.io.IOException;
32 import java.io.Reader;
33 import java.io.StringReader;
34 import java.io.StringWriter;
35 import java.net.URL;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.Set;
40
41 import javax.xml.transform.stream.StreamSource;
42
43 import edu.umd.cs.findbugs.annotations.NonNull;
44
45
46
47
48
49 class ValidateContentUsingModuleCommand
50 extends AbstractValidateContentCommand {
51 @NonNull
52 private static final String COMMAND = "validate-content";
53
54 @Override
55 public String getName() {
56 return COMMAND;
57 }
58
59 @Override
60 public String getDescription() {
61 return "Verify that the provided resource is well-formed and valid to the provided Module-based model.";
62 }
63
64 @Override
65 public Collection<? extends Option> gatherOptions() {
66 Collection<? extends Option> orig = super.gatherOptions();
67
68 List<Option> retval = new ArrayList<>(orig.size() + 1);
69 retval.addAll(orig);
70 retval.add(MetaschemaCommands.METASCHEMA_REQUIRED_OPTION);
71
72 return CollectionUtil.unmodifiableCollection(retval);
73 }
74
75 @Override
76 public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
77 return new CommandExecutor(callingContext, commandLine);
78 }
79
80 private final class CommandExecutor
81 extends AbstractValidationCommandExecutor {
82
83 private CommandExecutor(
84 @NonNull CallingContext callingContext,
85 @NonNull CommandLine commandLine) {
86 super(callingContext, commandLine);
87 }
88
89 @Override
90 protected IBindingContext getBindingContext(@NonNull Set<IConstraintSet> constraintSets)
91 throws CommandExecutionException {
92 return MetaschemaCommands.newBindingContextWithDynamicCompilation(constraintSets);
93 }
94
95 @SuppressWarnings("synthetic-access")
96 @Override
97 protected IModule getModule(
98 CommandLine commandLine,
99 IBindingContext bindingContext) throws CommandExecutionException {
100 return MetaschemaCommands.loadModule(
101 commandLine,
102 MetaschemaCommands.METASCHEMA_REQUIRED_OPTION,
103 ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()),
104 bindingContext);
105 }
106
107 @Override
108 protected ISchemaValidationProvider getSchemaValidationProvider(
109 IModule module,
110 CommandLine commandLine,
111 IBindingContext bindingContext) {
112 return new ModuleValidationProvider(module);
113 }
114
115 }
116
117 private static final class ModuleValidationProvider implements ISchemaValidationProvider {
118 @NonNull
119 private final IModule module;
120
121 public ModuleValidationProvider(@NonNull IModule module) {
122 this.module = module;
123 }
124
125 @Override
126 public XmlSchemaContentValidator getXmlSchemas(
127 @NonNull URL targetResource,
128 @NonNull IBindingContext bindingContext) throws IOException, SAXException {
129 IMutableConfiguration<SchemaGenerationFeature<?>> configuration = new DefaultConfiguration<>();
130
131 try (StringWriter writer = new StringWriter()) {
132 ISchemaGenerator.generateSchema(module, writer, SchemaFormat.XML, configuration);
133 try (Reader reader = new StringReader(writer.toString())) {
134 return new XmlSchemaContentValidator(
135 ObjectUtils.notNull(List.of(new StreamSource(reader))));
136 }
137 }
138 }
139
140 @Override
141 public JsonSchemaContentValidator getJsonSchema(
142 @NonNull JSONObject json,
143 @NonNull IBindingContext bindingContext) throws IOException {
144 IMutableConfiguration<SchemaGenerationFeature<?>> configuration = new DefaultConfiguration<>();
145
146 try (StringWriter writer = new StringWriter()) {
147 ISchemaGenerator.generateSchema(module, writer, SchemaFormat.JSON, configuration);
148 return new JsonSchemaContentValidator(
149 new JSONObject(new JSONTokener(writer.toString())));
150 }
151 }
152 }
153 }