Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractXmlSink |
|
| 1.7;1,7 |
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 | 98 | 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 | 2 | this.nameSpace = ns; |
50 | 2 | } |
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 | 10 | return this.nameSpace; |
61 | } | |
62 | ||
63 | /** | |
64 | * Starts a Tag. For instance: | |
65 | * <pre> | |
66 | * <tag> | |
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 | 18 | writeStartTag ( t, null ); |
75 | 16 | } |
76 | ||
77 | /** | |
78 | * Starts a Tag with attributes. For instance: | |
79 | * <pre> | |
80 | * <tag attName="attValue"> | |
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 | 210 | writeStartTag ( t, att, false ); |
90 | 208 | } |
91 | ||
92 | /** | |
93 | * Starts a Tag with attributes. For instance: | |
94 | * <pre> | |
95 | * <tag attName="attValue"> | |
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 | 226 | if ( t == null ) |
105 | { | |
106 | 2 | throw new IllegalArgumentException( "A tag is required" ); |
107 | } | |
108 | ||
109 | 224 | StringBuilder sb = new StringBuilder(); |
110 | 224 | sb.append( LESS_THAN ); |
111 | ||
112 | 224 | if ( nameSpace != null ) |
113 | { | |
114 | 4 | sb.append( nameSpace ).append( ':' ); |
115 | } | |
116 | ||
117 | 224 | sb.append( t.toString() ); |
118 | ||
119 | 224 | sb.append( SinkUtils.getAttributeString( att ) ); |
120 | ||
121 | 224 | if ( isSimpleTag ) |
122 | { | |
123 | 22 | sb.append( SPACE ).append( SLASH ); |
124 | } | |
125 | ||
126 | 224 | sb.append( GREATER_THAN ); |
127 | ||
128 | 224 | write( sb.toString() ); |
129 | 224 | } |
130 | ||
131 | /** | |
132 | * Writes a system EOL. | |
133 | * | |
134 | * @since 1.1 | |
135 | */ | |
136 | protected void writeEOL() | |
137 | { | |
138 | 2 | write( EOL ); |
139 | 2 | } |
140 | ||
141 | /** | |
142 | * Ends a Tag without writing an EOL. For instance: <pre></tag></pre>. | |
143 | * | |
144 | * @param t a tag. | |
145 | */ | |
146 | protected void writeEndTag( Tag t ) | |
147 | { | |
148 | 198 | if ( t == null ) |
149 | { | |
150 | 2 | throw new IllegalArgumentException( "A tag is required" ); |
151 | } | |
152 | ||
153 | 196 | StringBuilder sb = new StringBuilder(); |
154 | 196 | sb.append( LESS_THAN ); |
155 | 196 | sb.append( SLASH ); |
156 | ||
157 | 196 | if ( nameSpace != null ) |
158 | { | |
159 | 2 | sb.append( nameSpace ).append( ':' ); |
160 | } | |
161 | ||
162 | 196 | sb.append( t.toString() ); |
163 | 196 | sb.append( GREATER_THAN ); |
164 | ||
165 | 196 | write( sb.toString() ); |
166 | 196 | } |
167 | ||
168 | /** | |
169 | * Starts a simple Tag. For instance: | |
170 | * <pre> | |
171 | * <tag /> | |
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 | 6 | writeSimpleTag( t, null ); |
180 | 6 | } |
181 | ||
182 | /** | |
183 | * Starts a simple Tag with attributes. For instance: | |
184 | * <pre> | |
185 | * <tag attName="attValue" /> | |
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 | 14 | writeStartTag ( t, att, true ); |
195 | 14 | } |
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 | } |