View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.model.xml;
28  
29  import com.vladsch.flexmark.parser.ListOptions;
30  
31  import gov.nist.secauto.metaschema.core.datatype.markup.IMarkupString;
32  import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.AbstractMarkupWriter;
33  import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupVisitor;
34  import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupWriter;
35  import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupVisitor;
36  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
37  
38  import org.apache.xmlbeans.XmlCursor;
39  import org.apache.xmlbeans.XmlObject;
40  
41  import java.util.Map;
42  
43  import javax.xml.namespace.QName;
44  
45  import edu.umd.cs.findbugs.annotations.NonNull;
46  
47  @SuppressWarnings("PMD.AvoidUncheckedExceptionsInSignatures") // intended
48  class XmlbeansMarkupVisitor // TODO: rename
49      extends AbstractMarkupWriter<XmlCursor, IllegalArgumentException> {
50  
51    /**
52     * Write the provided markup to the provided object.
53     *
54     * @param markup
55     *          the markup to write
56     * @param namespace
57     *          the XML namespace to use for markup elements
58     * @param obj
59     *          the XML beans object to write to
60     */
61    @SuppressWarnings("resource")
62    public static void visit(@NonNull IMarkupString<?> markup, @NonNull String namespace,
63        @NonNull XmlObject obj) {
64      try (XmlCursor cursor = ObjectUtils.notNull(obj.newCursor())) {
65        visit(markup, namespace, cursor);
66      }
67    }
68  
69    /**
70     * Write the provided markup to the provided object.
71     *
72     * @param markup
73     *          the markup to write
74     * @param namespace
75     *          the XML namespace to use for markup elements
76     * @param cursor
77     *          the XML beans cursor to write to
78     */
79    public static void visit(@NonNull IMarkupString<?> markup, @NonNull String namespace,
80        @NonNull XmlCursor cursor) {
81      IMarkupWriter<XmlCursor, IllegalArgumentException> writer = new XmlbeansMarkupVisitor(
82          namespace,
83          markup.getFlexmarkFactory().getListOptions(),
84          cursor);
85      IMarkupVisitor<XmlCursor, IllegalArgumentException> visitor = new MarkupVisitor<>(markup.isBlock());
86      visitor.visitDocument(markup.getDocument(), writer);
87    }
88  
89    /**
90     * Construct a new XML beans markup visitor used for writing XML.
91     *
92     * @param namespace
93     *          the XML namespace to use for markup elements
94     * @param options
95     *          Flexmark-based formatting options to control output formatting
96     * @param writer
97     *          the XML beans cursor to write to
98     */
99    protected XmlbeansMarkupVisitor(
100       @NonNull String namespace,
101       @NonNull ListOptions options,
102       @NonNull XmlCursor writer) {
103     super(namespace, options, writer);
104   }
105 
106   @Override
107   public void writeEmptyElement(QName qname, Map<String, String> attributes)
108       throws IllegalArgumentException {
109     @SuppressWarnings("resource") // not owned
110     XmlCursor cursor = getStream();
111     cursor.beginElement(qname);
112 
113     attributes.forEach((name, value) -> cursor.insertAttributeWithValue(name, value));
114 
115     // go to the end of the new element
116     cursor.toEndToken();
117 
118     // state advance past the end element
119     cursor.toNextToken();
120   }
121 
122   @Override
123   public void writeElementStart(QName qname, Map<String, String> attributes)
124       throws IllegalArgumentException {
125     @SuppressWarnings("resource") // not owned
126     XmlCursor cursor = getStream();
127     cursor.beginElement(qname);
128 
129     attributes.forEach((name, value) -> cursor.insertAttributeWithValue(name, value));
130 
131     // save the current location state
132     cursor.push();
133   }
134 
135   @Override
136   public void writeElementEnd(QName qname) throws IllegalArgumentException {
137     @SuppressWarnings("resource") // not owned
138     XmlCursor cursor = getStream();
139 
140     // restore location to end of start element
141     cursor.pop();
142 
143     // go to the end of the new element
144     cursor.toEndToken();
145 
146     // state advance past the end element
147     cursor.toNextToken();
148   }
149 
150   @Override
151   public void writeText(CharSequence text) throws IllegalArgumentException {
152     @SuppressWarnings("resource") // not owned
153     XmlCursor cursor = getStream();
154     cursor.insertChars(text.toString());
155   }
156 
157   @Override
158   protected void writeComment(CharSequence text) throws IllegalArgumentException {
159     @SuppressWarnings("resource") // not owned
160     XmlCursor cursor = getStream();
161     cursor.insertComment(text.toString());
162   }
163 }