001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.doxia.sink.impl; 020 021import javax.swing.text.MutableAttributeSet; 022import javax.swing.text.html.HTML.Tag; 023 024import java.util.Objects; 025 026import org.apache.maven.doxia.markup.XmlMarkup; 027 028/** 029 * An abstract <code>Sink</code> for xml markup syntax. 030 * 031 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 032 * @since 1.0 033 */ 034public abstract class AbstractXmlSink extends SinkAdapter implements XmlMarkup { 035 /** Default namespace prepended to all tags */ 036 private String nameSpace; 037 038 private boolean firstTag = true; 039 040 private boolean insertNewline = true; 041 042 /** 043 * <p>Setter for the field <code>insertNewline</code>.</p> 044 * 045 * @param insertNewline a boolean. 046 */ 047 public void setInsertNewline(boolean insertNewline) { 048 this.insertNewline = insertNewline; 049 } 050 051 /** 052 * Sets the default namespace that is prepended to all tags written by this sink. 053 * 054 * @param ns the default namespace. 055 * @since 1.1 056 */ 057 public void setNameSpace(String ns) { 058 this.nameSpace = ns; 059 } 060 061 /** 062 * Return the default namespace that is prepended to all tags written by this sink. 063 * 064 * @return the current default namespace. 065 * @since 1.1 066 */ 067 public String getNameSpace() { 068 return this.nameSpace; 069 } 070 071 /** 072 * Starts a Tag. For instance: 073 * <pre> 074 * <tag> 075 * </pre> 076 * 077 * @param t a non null tag 078 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet) 079 */ 080 protected void writeStartTag(Tag t) { 081 writeStartTag(t, null); 082 } 083 084 /** 085 * Starts a Tag with attributes. For instance: 086 * <pre> 087 * <tag attName="attValue"> 088 * </pre> 089 * 090 * @param t a non null tag. 091 * @param att a set of attributes. May be null. 092 * @see #writeStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, boolean) 093 */ 094 protected void writeStartTag(Tag t, MutableAttributeSet att) { 095 writeStartTag(t, att, false); 096 } 097 098 /** 099 * Starts a Tag with attributes. For instance: 100 * <pre> 101 * <tag attName="attValue"> 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></tag></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 * <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 writeSimpleTag(t, null); 179 } 180 181 /** 182 * Starts a simple Tag with attributes. For instance: 183 * <pre> 184 * <tag attName="attValue" /> 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}