View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.sink.impl;
20  
21  import javax.swing.text.MutableAttributeSet;
22  import javax.swing.text.html.HTML.Tag;
23  
24  import java.util.Objects;
25  
26  import org.apache.maven.doxia.markup.XmlMarkup;
27  
28  /**
29   * An abstract <code>Sink</code> for xml markup syntax.
30   *
31   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   * @since 1.0
33   */
34  public abstract class AbstractXmlSink extends SinkAdapter implements XmlMarkup {
35      /** Default namespace prepended to all tags */
36      private String nameSpace;
37  
38      private boolean firstTag = true;
39  
40      private boolean insertNewline = true;
41  
42      /**
43       * <p>Setter for the field <code>insertNewline</code>.</p>
44       *
45       * @param insertNewline a boolean.
46       */
47      public void setInsertNewline(boolean insertNewline) {
48          this.insertNewline = insertNewline;
49      }
50  
51      /**
52       * Sets the default namespace that is prepended to all tags written by this sink.
53       *
54       * @param ns the default namespace.
55       * @since 1.1
56       */
57      public void setNameSpace(String ns) {
58          this.nameSpace = ns;
59      }
60  
61      /**
62       * Return the default namespace that is prepended to all tags written by this sink.
63       *
64       * @return the current default namespace.
65       * @since 1.1
66       */
67      public String getNameSpace() {
68          return this.nameSpace;
69      }
70  
71      /**
72       * Starts a Tag. For instance:
73       * <pre>
74       * &lt;tag&gt;
75       * </pre>
76       *
77       * @param t a non null tag
78       * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet)
79       */
80      protected void writeStartTag(Tag t) {
81          writeStartTag(t, null);
82      }
83  
84      /**
85       * Starts a Tag with attributes. For instance:
86       * <pre>
87       * &lt;tag attName="attValue"&gt;
88       * </pre>
89       *
90       * @param t a non null tag.
91       * @param att a set of attributes. May be null.
92       * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean)
93       */
94      protected void writeStartTag(Tag t, MutableAttributeSet att) {
95          writeStartTag(t, att, false);
96      }
97  
98      /**
99       * Starts a Tag with attributes. For instance:
100      * <pre>
101      * &lt;tag attName="attValue"&gt;
102      * </pre>
103      *
104      * @param t a non null tag.
105      * @param att a set of attributes. May be null.
106      * @param isSimpleTag boolean to write as a simple tag.
107      */
108     protected void writeStartTag(Tag t, MutableAttributeSet att, boolean isSimpleTag) {
109         Objects.requireNonNull(t, "t cannot be null");
110 
111         StringBuilder sb = new StringBuilder();
112 
113         if (insertNewline && t.isBlock() && !firstTag) {
114             sb.append(EOL);
115         }
116         firstTag = false;
117 
118         sb.append(LESS_THAN);
119 
120         if (nameSpace != null) {
121             sb.append(nameSpace).append(':');
122         }
123 
124         sb.append(t.toString());
125 
126         sb.append(SinkUtils.getAttributeString(att));
127 
128         if (isSimpleTag) {
129             sb.append(SPACE).append(SLASH);
130         }
131 
132         sb.append(GREATER_THAN);
133 
134         write(sb.toString());
135     }
136 
137     /**
138      * Writes a system EOL.
139      *
140      * @since 1.1
141      */
142     protected void writeEOL() {
143         write(EOL);
144     }
145 
146     /**
147      * Ends a Tag without writing an EOL. For instance: <pre>&lt;/tag&gt;</pre>.
148      *
149      * @param t a tag.
150      */
151     protected void writeEndTag(Tag t) {
152         Objects.requireNonNull(t, "t cannot be null");
153 
154         StringBuilder sb = new StringBuilder();
155         sb.append(LESS_THAN);
156         sb.append(SLASH);
157 
158         if (nameSpace != null) {
159             sb.append(nameSpace).append(':');
160         }
161 
162         sb.append(t.toString());
163         sb.append(GREATER_THAN);
164 
165         write(sb.toString());
166     }
167 
168     /**
169      * Starts a simple Tag. For instance:
170      * <pre>
171      * &lt;tag /&gt;
172      * </pre>
173      *
174      * @param t a non null tag
175      * @see #writeSimpleTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet)
176      */
177     protected void writeSimpleTag(Tag t) {
178         writeSimpleTag(t, null);
179     }
180 
181     /**
182      * Starts a simple Tag with attributes. For instance:
183      * <pre>
184      * &lt;tag attName="attValue" /&gt;
185      * </pre>
186      *
187      * @param t a non null tag.
188      * @param att a set of attributes. May be null.
189      * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean)
190      */
191     protected void writeSimpleTag(Tag t, MutableAttributeSet att) {
192         writeStartTag(t, att, true);
193     }
194 
195     /**
196      * Write a text to the sink.
197      *
198      * @param text the given text to write
199      */
200     protected abstract void write(String text);
201 }