1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen.xml; // NOPMD
7   
8   import com.ctc.wstx.stax.WstxOutputFactory;
9   
10  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
11  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
12  import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
13  import gov.nist.secauto.metaschema.core.model.IModule;
14  import gov.nist.secauto.metaschema.core.util.AutoCloser;
15  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
16  import gov.nist.secauto.metaschema.schemagen.AbstractSchemaGenerator;
17  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationException;
18  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationFeature;
19  import gov.nist.secauto.metaschema.schemagen.xml.datatype.XmlDatatypeManager;
20  import gov.nist.secauto.metaschema.schemagen.xml.impl.XmlGenerationState;
21  import gov.nist.secauto.metaschema.schemagen.xml.schematype.IXmlType;
22  
23  import org.codehaus.stax2.XMLOutputFactory2;
24  import org.codehaus.stax2.XMLStreamWriter2;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.StringReader;
29  import java.io.StringWriter;
30  import java.io.Writer;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.xml.namespace.QName;
36  import javax.xml.stream.XMLOutputFactory;
37  import javax.xml.stream.XMLStreamException;
38  import javax.xml.transform.Source;
39  import javax.xml.transform.Transformer;
40  import javax.xml.transform.TransformerConfigurationException;
41  import javax.xml.transform.TransformerException;
42  import javax.xml.transform.TransformerFactory;
43  import javax.xml.transform.stream.StreamResult;
44  import javax.xml.transform.stream.StreamSource;
45  
46  import edu.umd.cs.findbugs.annotations.NonNull;
47  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
48  
49  public class XmlSchemaGenerator
50      extends AbstractSchemaGenerator<
51          AutoCloser<XMLStreamWriter2, SchemaGenerationException>,
52          XmlDatatypeManager,
53          XmlGenerationState> {
54    // private static final Logger LOGGER =
55    // LogManager.getLogger(XmlSchemaGenerator.class);
56  
57    @NonNull
58    public static final String PREFIX_XML_SCHEMA = "xs";
59    @NonNull
60    public static final String NS_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
61    @NonNull
62    private static final String PREFIX_XML_SCHEMA_VERSIONING = "vs";
63    @NonNull
64    private static final String NS_XML_SCHEMA_VERSIONING = "http://www.w3.org/2007/XMLSchema-versioning";
65    @NonNull
66    public static final String NS_XHTML = "http://www.w3.org/1999/xhtml";
67  
68    @NonNull
69    private final XMLOutputFactory2 xmlOutputFactory;
70  
71    @NonNull
72    private static XMLOutputFactory2 defaultXMLOutputFactory() {
73      XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory.newInstance();
74      assert xmlOutputFactory instanceof WstxOutputFactory;
75      xmlOutputFactory.configureForSpeed();
76      xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
77      return xmlOutputFactory;
78    }
79  
80    public XmlSchemaGenerator() {
81      this(defaultXMLOutputFactory());
82    }
83  
84    @SuppressFBWarnings("EI_EXPOSE_REP2")
85    public XmlSchemaGenerator(@NonNull XMLOutputFactory2 xmlOutputFactory) {
86      this.xmlOutputFactory = xmlOutputFactory;
87    }
88  
89    protected XMLOutputFactory2 getXmlOutputFactory() {
90      return xmlOutputFactory;
91    }
92  
93    @Override
94    protected AutoCloser<XMLStreamWriter2, SchemaGenerationException> newWriter(
95        Writer out) {
96      XMLStreamWriter2 writer;
97      try {
98        writer = ObjectUtils.notNull((XMLStreamWriter2) getXmlOutputFactory().createXMLStreamWriter(out));
99      } catch (XMLStreamException ex) {
100       throw new SchemaGenerationException(ex);
101     }
102     return AutoCloser.autoClose(writer, t -> {
103       try {
104         t.close();
105       } catch (XMLStreamException ex) {
106         throw new SchemaGenerationException(ex);
107       }
108     });
109   }
110 
111   @Override
112   protected XmlGenerationState newGenerationState(
113       IModule module,
114       AutoCloser<XMLStreamWriter2, SchemaGenerationException> schemaWriter,
115       IConfiguration<SchemaGenerationFeature<?>> configuration) {
116     return new XmlGenerationState(module, schemaWriter, configuration);
117   }
118 
119   @Override
120   public void generateFromModule(
121       @NonNull IModule module,
122       @NonNull Writer out,
123       @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration) {
124     // super.generateFromModule(module, out, configuration);
125 
126     String generatedSchema;
127     try (StringWriter stringWriter = new StringWriter()) {
128       super.generateFromModule(module, stringWriter, configuration);
129       generatedSchema = stringWriter.toString();
130     } catch (IOException ex) {
131       throw new SchemaGenerationException(ex);
132     }
133 
134     try (InputStream is = getClass().getResourceAsStream("/identity.xsl")) {
135       Source xsltSource = new StreamSource(is);
136 
137       // TransformerFactory transformerFactory = TransformerFactory.newInstance();
138       TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl();
139       Transformer transformer = transformerFactory.newTransformer(xsltSource);
140 
141       try (StringReader stringReader = new StringReader(generatedSchema)) {
142         Source xmlSource = new StreamSource(stringReader);
143 
144         StreamResult result = new StreamResult(out);
145         transformer.transform(xmlSource, result);
146       } catch (TransformerException ex) {
147         throw new SchemaGenerationException(ex);
148       }
149     } catch (IOException | TransformerConfigurationException ex) {
150       throw new SchemaGenerationException(ex);
151     }
152   }
153 
154   @Override
155   protected void generateSchema(XmlGenerationState state) {
156 
157     try {
158       String targetNS = state.getDefaultNS();
159 
160       // analyze all definitions
161       Map<String, String> prefixToNamespaceMap = new HashMap<>(); // NOPMD concurrency not needed
162       final List<IAssemblyDefinition> rootAssemblyDefinitions = analyzeDefinitions(
163           state,
164           (entry, definition) -> {
165             assert entry != null;
166             assert definition != null;
167             IXmlType type = state.getXmlForDefinition(definition);
168             if (!entry.isInline()) {
169               QName qname = type.getQName();
170               String namespace = qname.getNamespaceURI();
171               if (!targetNS.equals(namespace)) {
172                 // collect namespaces and prefixes for definitions with a different namespace
173                 prefixToNamespaceMap.computeIfAbsent(qname.getPrefix(), x -> namespace);
174               }
175             }
176           });
177 
178       // write some root elements
179       XMLStreamWriter2 writer = state.getXMLStreamWriter();
180       writer.writeStartDocument("UTF-8", "1.0");
181       writer.writeStartElement(PREFIX_XML_SCHEMA, "schema", NS_XML_SCHEMA);
182       writer.writeDefaultNamespace(targetNS);
183       writer.writeNamespace(PREFIX_XML_SCHEMA_VERSIONING, NS_XML_SCHEMA_VERSIONING);
184 
185       // write namespaces for all indexed definitions
186       for (Map.Entry<String, String> entry : prefixToNamespaceMap.entrySet()) {
187         state.writeNamespace(entry.getKey(), entry.getValue());
188       }
189 
190       IModule module = state.getModule();
191 
192       // write remaining root attributes
193       writer.writeAttribute("targetNamespace", targetNS);
194       writer.writeAttribute("elementFormDefault", "qualified");
195       writer.writeAttribute(NS_XML_SCHEMA_VERSIONING, "minVersion", "1.0");
196       writer.writeAttribute(NS_XML_SCHEMA_VERSIONING, "maxVersion", "1.1");
197       writer.writeAttribute("version", module.getVersion());
198 
199       generateSchemaMetadata(module, state);
200 
201       for (IAssemblyDefinition definition : rootAssemblyDefinitions) {
202         QName xmlQName = definition.getRootXmlQName();
203         if (xmlQName != null
204             && (xmlQName.getNamespaceURI() == null || state.getDefaultNS().equals(xmlQName.getNamespaceURI()))) {
205           generateRootElement(definition, state);
206         }
207       }
208 
209       state.generateXmlTypes();
210 
211       writer.writeEndElement(); // xs:schema
212       writer.writeEndDocument();
213       writer.flush();
214     } catch (XMLStreamException ex) {
215       throw new SchemaGenerationException(ex);
216     }
217   }
218 
219   protected static void generateSchemaMetadata(
220       @NonNull IModule module,
221       @NonNull XmlGenerationState state)
222       throws XMLStreamException {
223     String targetNS = ObjectUtils.notNull(module.getXmlNamespace().toASCIIString());
224     state.writeStartElement(PREFIX_XML_SCHEMA, "annotation", NS_XML_SCHEMA);
225     state.writeStartElement(PREFIX_XML_SCHEMA, "appinfo", NS_XML_SCHEMA);
226 
227     state.writeStartElement(targetNS, "schema-name");
228 
229     module.getName().writeXHtml(targetNS, state.getXMLStreamWriter());
230 
231     state.writeEndElement();
232 
233     state.writeStartElement(targetNS, "schema-version");
234     state.writeCharacters(module.getVersion());
235     state.writeEndElement();
236 
237     state.writeStartElement(targetNS, "short-name");
238     state.writeCharacters(module.getShortName());
239     state.writeEndElement();
240 
241     state.writeEndElement();
242 
243     MarkupMultiline remarks = module.getRemarks();
244     if (remarks != null) {
245       state.writeStartElement(PREFIX_XML_SCHEMA, "documentation", NS_XML_SCHEMA);
246 
247       remarks.writeXHtml(targetNS, state.getXMLStreamWriter());
248       state.writeEndElement();
249     }
250 
251     state.writeEndElement();
252   }
253 
254   private static void generateRootElement(@NonNull IAssemblyDefinition definition, @NonNull XmlGenerationState state)
255       throws XMLStreamException {
256     assert definition.isRoot();
257 
258     XMLStreamWriter2 writer = state.getXMLStreamWriter();
259     QName xmlQName = definition.getRootXmlQName();
260 
261     writer.writeStartElement(PREFIX_XML_SCHEMA, "element", NS_XML_SCHEMA);
262     writer.writeAttribute("name", xmlQName.getLocalPart());
263     writer.writeAttribute("type", state.getXmlForDefinition(definition).getTypeReference());
264 
265     writer.writeEndElement();
266   }
267 }