1 package org.apache.maven.doxia.sink.impl; 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 java.util.Objects; 23 24 import javax.swing.text.MutableAttributeSet; 25 import javax.swing.text.html.HTML.Tag; 26 27 import org.apache.maven.doxia.markup.XmlMarkup; 28 29 /** 30 * An abstract <code>Sink</code> for xml markup syntax. 31 * 32 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 33 * @since 1.0 34 */ 35 public abstract class AbstractXmlSink 36 extends SinkAdapter 37 implements XmlMarkup 38 { 39 /** Default namespace prepended to all tags */ 40 private String nameSpace; 41 42 private boolean firstTag = true; 43 44 private boolean insertNewline = true; 45 46 /** 47 * <p>Setter for the field <code>insertNewline</code>.</p> 48 * 49 * @param insertNewline a boolean. 50 */ 51 public void setInsertNewline( boolean insertNewline ) 52 { 53 this.insertNewline = insertNewline; 54 } 55 56 /** 57 * Sets the default namespace that is prepended to all tags written by this sink. 58 * 59 * @param ns the default namespace. 60 * @since 1.1 61 */ 62 public void setNameSpace( String ns ) 63 { 64 this.nameSpace = ns; 65 } 66 67 /** 68 * Return the default namespace that is prepended to all tags written by this sink. 69 * 70 * @return the current default namespace. 71 * @since 1.1 72 */ 73 public String getNameSpace() 74 { 75 return this.nameSpace; 76 } 77 78 /** 79 * Starts a Tag. For instance: 80 * <pre> 81 * <tag> 82 * </pre> 83 * 84 * @param t a non null tag 85 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet) 86 */ 87 protected void writeStartTag( Tag t ) 88 { 89 writeStartTag ( t, null ); 90 } 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 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean) 101 */ 102 protected void writeStartTag( Tag t, MutableAttributeSet att ) 103 { 104 writeStartTag ( t, att, false ); 105 } 106 107 /** 108 * Starts a Tag with attributes. For instance: 109 * <pre> 110 * <tag attName="attValue"> 111 * </pre> 112 * 113 * @param t a non null tag. 114 * @param att a set of attributes. May be null. 115 * @param isSimpleTag boolean to write as a simple tag. 116 */ 117 protected void writeStartTag( Tag t, MutableAttributeSet att, boolean isSimpleTag ) 118 { 119 Objects.requireNonNull( t, "t cannot be null" ); 120 121 StringBuilder sb = new StringBuilder(); 122 123 if ( insertNewline && t.isBlock() && !firstTag ) 124 { 125 sb.append( EOL ); 126 } 127 firstTag = false; 128 129 sb.append( LESS_THAN ); 130 131 if ( nameSpace != null ) 132 { 133 sb.append( nameSpace ).append( ':' ); 134 } 135 136 sb.append( t.toString() ); 137 138 sb.append( SinkUtils.getAttributeString( att ) ); 139 140 if ( isSimpleTag ) 141 { 142 sb.append( SPACE ).append( SLASH ); 143 } 144 145 sb.append( GREATER_THAN ); 146 147 write( sb.toString() ); 148 } 149 150 /** 151 * Writes a system EOL. 152 * 153 * @since 1.1 154 */ 155 protected void writeEOL() 156 { 157 write( EOL ); 158 } 159 160 /** 161 * Ends a Tag without writing an EOL. For instance: <pre></tag></pre>. 162 * 163 * @param t a tag. 164 */ 165 protected void writeEndTag( Tag t ) 166 { 167 Objects.requireNonNull( t, "t cannot be null" ); 168 169 StringBuilder sb = new StringBuilder(); 170 sb.append( LESS_THAN ); 171 sb.append( SLASH ); 172 173 if ( nameSpace != null ) 174 { 175 sb.append( nameSpace ).append( ':' ); 176 } 177 178 sb.append( t.toString() ); 179 sb.append( GREATER_THAN ); 180 181 write( sb.toString() ); 182 } 183 184 /** 185 * Starts a simple Tag. For instance: 186 * <pre> 187 * <tag /> 188 * </pre> 189 * 190 * @param t a non null tag 191 * @see #writeSimpleTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet) 192 */ 193 protected void writeSimpleTag( Tag t ) 194 { 195 writeSimpleTag( t, null ); 196 } 197 198 /** 199 * Starts a simple Tag with attributes. For instance: 200 * <pre> 201 * <tag attName="attValue" /> 202 * </pre> 203 * 204 * @param t a non null tag. 205 * @param att a set of attributes. May be null. 206 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean) 207 */ 208 protected void writeSimpleTag( Tag t, MutableAttributeSet att ) 209 { 210 writeStartTag ( t, att, true ); 211 } 212 213 /** 214 * Write a text to the sink. 215 * 216 * @param text the given text to write 217 */ 218 protected abstract void write( String text ); 219 }