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.datatype.markup.flexmark;
28  
29  import com.vladsch.flexmark.ast.AutoLink;
30  import com.vladsch.flexmark.ast.BlockQuote;
31  import com.vladsch.flexmark.ast.Code;
32  import com.vladsch.flexmark.ast.CodeBlock;
33  import com.vladsch.flexmark.ast.FencedCodeBlock;
34  import com.vladsch.flexmark.ast.HardLineBreak;
35  import com.vladsch.flexmark.ast.Heading;
36  import com.vladsch.flexmark.ast.HtmlBlock;
37  import com.vladsch.flexmark.ast.HtmlCommentBlock;
38  import com.vladsch.flexmark.ast.HtmlEntity;
39  import com.vladsch.flexmark.ast.HtmlInline;
40  import com.vladsch.flexmark.ast.Image;
41  import com.vladsch.flexmark.ast.IndentedCodeBlock;
42  import com.vladsch.flexmark.ast.Link;
43  import com.vladsch.flexmark.ast.ListBlock;
44  import com.vladsch.flexmark.ast.ListItem;
45  import com.vladsch.flexmark.ast.MailLink;
46  import com.vladsch.flexmark.ast.Paragraph;
47  import com.vladsch.flexmark.ast.Text;
48  import com.vladsch.flexmark.ast.TextBase;
49  import com.vladsch.flexmark.ast.ThematicBreak;
50  import com.vladsch.flexmark.ext.escaped.character.EscapedCharacter;
51  import com.vladsch.flexmark.ext.tables.TableBlock;
52  import com.vladsch.flexmark.ext.typographic.TypographicQuotes;
53  import com.vladsch.flexmark.ext.typographic.TypographicSmarts;
54  import com.vladsch.flexmark.util.ast.Node;
55  
56  import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode;
57  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
58  
59  import java.util.Map;
60  
61  import javax.xml.namespace.QName;
62  
63  import edu.umd.cs.findbugs.annotations.NonNull;
64  import edu.umd.cs.findbugs.annotations.Nullable;
65  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
66  
67  @SuppressFBWarnings(value = "THROWS_METHOD_THROWS_CLAUSE_THROWABLE",
68      justification = "There is a need to support varying exceptions from multiple stream writers")
69  public interface IMarkupWriter<T, E extends Throwable> { // NOPMD
70    @NonNull
71    QName asQName(@NonNull String localName);
72  
73    default void writeElement(
74        @NonNull String localName,
75        @NonNull Node node,
76        @Nullable ChildHandler<T, E> childHandler) throws E {
77      writeElement(localName, node, CollectionUtil.emptyMap(), childHandler);
78    }
79  
80    default void writeElement(
81        @NonNull String localName,
82        @NonNull Node node,
83        @NonNull Map<String, String> attributes,
84        @Nullable ChildHandler<T, E> childHandler) throws E {
85      QName qname = asQName(localName);
86      writeElement(qname, node, attributes, childHandler);
87    }
88  
89    void writeElement(
90        @NonNull QName qname,
91        @NonNull Node node,
92        @NonNull Map<String, String> attributes,
93        @Nullable ChildHandler<T, E> childHandler) throws E;
94  
95    default void writeEmptyElement(
96        @NonNull String localName,
97        @NonNull Map<String, String> attributes) throws E {
98      QName qname = asQName(localName);
99      writeEmptyElement(qname, attributes);
100   }
101 
102   void writeEmptyElement(
103       @NonNull QName qname,
104       @NonNull Map<String, String> attributes) throws E;
105 
106   default void writeElementStart(
107       @NonNull QName qname) throws E {
108     writeElementStart(qname, CollectionUtil.emptyMap());
109   }
110 
111   void writeElementStart(
112       @NonNull QName qname,
113       @NonNull Map<String, String> attributes) throws E;
114 
115   void writeElementEnd(@NonNull QName qname) throws E;
116 
117   void writeText(@NonNull Text node) throws E;
118 
119   /**
120    * Handle a combination of {@link Text} and {@link EscapedCharacter} node
121    * children.
122    *
123    * @param node
124    *          the text node to write
125    * @throws E
126    *           if an error occured while writing
127    */
128   void writeText(@NonNull TextBase node) throws E;
129 
130   void writeText(@NonNull CharSequence text) throws E;
131 
132   void writeHtmlEntity(@NonNull HtmlEntity node) throws E;
133 
134   void writeHtmlEntity(@NonNull TypographicSmarts node) throws E;
135 
136   void writeParagraph(
137       @NonNull Paragraph node,
138       @NonNull ChildHandler<T, E> childHandler) throws E;
139 
140   void writeLink(
141       @NonNull Link node,
142       @NonNull ChildHandler<T, E> childHandler) throws E;
143 
144   void writeLink(@NonNull MailLink node) throws E;
145 
146   void writeLink(@NonNull AutoLink node) throws E;
147 
148   void writeTypographicQuotes(
149       @NonNull TypographicQuotes node,
150       @NonNull ChildHandler<T, E> childHandler) throws E;
151 
152   void writeInlineHtml(@NonNull HtmlInline node) throws E;
153 
154   void writeBlockHtml(@NonNull HtmlBlock node) throws E;
155 
156   void writeTable(
157       @NonNull TableBlock node,
158       @NonNull ChildHandler<T, E> cellChilddHandler) throws E;
159 
160   void writeImage(@NonNull Image node) throws E;
161 
162   void writeInsertAnchor(@NonNull InsertAnchorNode node) throws E;
163 
164   void writeHeading(
165       @NonNull Heading node,
166       @NonNull ChildHandler<T, E> childHandler) throws E;
167 
168   void writeCode(
169       @NonNull Code node,
170       @NonNull ChildHandler<T, E> childHandler) throws E;
171 
172   void writeCodeBlock(
173       @NonNull IndentedCodeBlock node,
174       @NonNull ChildHandler<T, E> childHandler) throws E;
175 
176   void writeCodeBlock(
177       @NonNull FencedCodeBlock node,
178       @NonNull ChildHandler<T, E> childHandler) throws E;
179 
180   void writeCodeBlock(
181       @NonNull CodeBlock node,
182       @NonNull ChildHandler<T, E> childHandler) throws E;
183 
184   void writeBlockQuote(
185       @NonNull BlockQuote node,
186       @NonNull ChildHandler<T, E> childHandler) throws E;
187 
188   default void writeList(
189       @NonNull String localName,
190       @NonNull ListBlock node,
191       @NonNull ChildHandler<T, E> listItemHandler) throws E {
192     QName qname = asQName(localName);
193     writeList(qname, node, listItemHandler);
194   }
195 
196   void writeList(
197       @NonNull QName qname,
198       @NonNull ListBlock node,
199       @NonNull ChildHandler<T, E> listItemHandler) throws E;
200 
201   void writeListItem(
202       @NonNull ListItem node,
203       @NonNull ChildHandler<T, E> listItemHandler) throws E;
204 
205   void writeBreak(@NonNull HardLineBreak node) throws E;
206 
207   void writeBreak(@NonNull ThematicBreak node) throws E;
208 
209   void writeComment(@NonNull HtmlCommentBlock node) throws E;
210 
211   /**
212    * Provides a callback to handle node children.
213    *
214    * @param <T>
215    *          the type of stream to write to
216    * @param <E>
217    *          the type of exception that can be thrown when a writing error occurs
218    */
219   @FunctionalInterface
220   interface ChildHandler<T, E extends Throwable> { // NOPMD
221     void accept(@NonNull Node node, @NonNull IMarkupWriter<T, E> writer) throws E;
222   }
223 
224 }