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 1410919 2012-11-18 16:34:59Z hboutemy $ 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 private boolean firstTag = true; 42 43 private boolean insertNewline = true; 44 45 public void setInsertNewline( boolean insertNewline ) 46 { 47 this.insertNewline = insertNewline; 48 } 49 50 /** 51 * Sets the default namespace that is prepended to all tags written by this sink. 52 * 53 * @param ns the default namespace. 54 * @since 1.1 55 */ 56 public void setNameSpace( String ns ) 57 { 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 { 69 return this.nameSpace; 70 } 71 72 /** 73 * Starts a Tag. For instance: 74 * <pre> 75 * <tag> 76 * </pre> 77 * 78 * @param t a non null tag 79 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet) 80 */ 81 protected void writeStartTag( Tag t ) 82 { 83 writeStartTag ( t, null ); 84 } 85 86 /** 87 * Starts a Tag with attributes. For instance: 88 * <pre> 89 * <tag attName="attValue"> 90 * </pre> 91 * 92 * @param t a non null tag. 93 * @param att a set of attributes. May be null. 94 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean). 95 */ 96 protected void writeStartTag( Tag t, MutableAttributeSet att ) 97 { 98 writeStartTag ( t, att, false ); 99 } 100 101 /** 102 * Starts a Tag with attributes. For instance: 103 * <pre> 104 * <tag attName="attValue"> 105 * </pre> 106 * 107 * @param t a non null tag. 108 * @param att a set of attributes. May be null. 109 * @param isSimpleTag boolean to write as a simple tag. 110 */ 111 protected void writeStartTag( Tag t, MutableAttributeSet att, boolean isSimpleTag ) 112 { 113 if ( t == null ) 114 { 115 throw new IllegalArgumentException( "A tag is required" ); 116 } 117 118 StringBuilder sb = new StringBuilder(); 119 120 if ( insertNewline && t.isBlock() && !firstTag ) 121 { 122 sb.append( EOL ); 123 } 124 firstTag = false; 125 126 sb.append( LESS_THAN ); 127 128 if ( nameSpace != null ) 129 { 130 sb.append( nameSpace ).append( ':' ); 131 } 132 133 sb.append( t.toString() ); 134 135 sb.append( SinkUtils.getAttributeString( att ) ); 136 137 if ( isSimpleTag ) 138 { 139 sb.append( SPACE ).append( SLASH ); 140 } 141 142 sb.append( GREATER_THAN ); 143 144 write( sb.toString() ); 145 } 146 147 /** 148 * Writes a system EOL. 149 * 150 * @since 1.1 151 */ 152 protected void writeEOL() 153 { 154 write( EOL ); 155 } 156 157 /** 158 * Ends a Tag without writing an EOL. For instance: <pre></tag></pre>. 159 * 160 * @param t a tag. 161 */ 162 protected void writeEndTag( Tag t ) 163 { 164 if ( t == null ) 165 { 166 throw new IllegalArgumentException( "A tag is required" ); 167 } 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 }