View Javadoc

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