001package org.apache.maven.doxia.module.docbook; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.Writer; 023 024import java.util.Locale; 025 026import javax.swing.text.MutableAttributeSet; 027import javax.swing.text.SimpleAttributeSet; 028 029import org.apache.maven.doxia.sink.Sink; 030import org.apache.maven.doxia.sink.impl.AbstractSinkTest; 031import org.apache.maven.doxia.sink.impl.SinkUtils; 032import org.apache.maven.doxia.util.DoxiaUtils; 033 034import org.codehaus.plexus.util.FileUtils; 035 036/** 037 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 038 * @version $Id: DocBookSinkTest.html 979316 2016-02-02 21:51:43Z hboutemy $ 039 */ 040public class DocBookSinkTest extends AbstractSinkTest 041{ 042 /** {@inheritDoc} */ 043 protected String outputExtension() 044 { 045 return "docbook"; 046 } 047 048 /** {@inheritDoc} */ 049 protected Sink createSink( Writer writer ) 050 { 051 return new DocBookSink( writer, "UTF-8" ); 052 } 053 054 /** {@inheritDoc} */ 055 protected boolean isXmlSink() 056 { 057 return true; 058 } 059 060 /** {@inheritDoc} */ 061 protected String getTitleBlock( String title ) 062 { 063 return "<articleinfo><title>" + title + "</title>"; 064 } 065 066 /** {@inheritDoc} */ 067 protected String getAuthorBlock( String author ) 068 { 069 return "<corpauthor>" + author + "</corpauthor>"; 070 } 071 072 /** {@inheritDoc} */ 073 protected String getDateBlock( String date ) 074 { 075 return "<date>" + date + "</date>"; 076 } 077 078 /** {@inheritDoc} */ 079 protected String getHeadBlock() 080 { 081 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE article PUBLIC \"" 082 + SimplifiedDocbookMarkup.DEFAULT_XML_PUBLIC_ID + "\" " 083 + "\"" + SimplifiedDocbookMarkup.DEFAULT_XML_SYSTEM_ID + "\"><article>"; 084 } 085 086 /** {@inheritDoc} */ 087 protected String getBodyBlock() 088 { 089 return "</article>"; 090 } 091 092 /** {@inheritDoc} */ 093 protected String getSectionTitleBlock( String title ) 094 { 095 return "<title>" + title + "</title>"; 096 } 097 098 /** {@inheritDoc} */ 099 protected String getSection1Block( String title ) 100 { 101 return "<section><title>" + title + "</title>" + "</section>"; 102 } 103 104 /** {@inheritDoc} */ 105 protected String getSection2Block( String title ) 106 { 107 return "<section><title>" + title + "</title>" + "</section>"; 108 } 109 110 /** {@inheritDoc} */ 111 protected String getSection3Block( String title ) 112 { 113 return "<section><title>" + title + "</title>" + "</section>"; 114 } 115 116 /** {@inheritDoc} */ 117 protected String getSection4Block( String title ) 118 { 119 return "<section><title>" + title + "</title>" + "</section>"; 120 } 121 122 /** {@inheritDoc} */ 123 protected String getSection5Block( String title ) 124 { 125 return "<section><title>" + title + "</title>" + "</section>"; 126 } 127 128 /** {@inheritDoc} */ 129 protected String getListBlock( String item ) 130 { 131 return "<itemizedlist><listitem><para>" + item + "</para></listitem>" + "</itemizedlist>"; 132 } 133 134 /** {@inheritDoc} */ 135 protected String getNumberedListBlock( String item ) 136 { 137 return "<orderedlist numeration=\"lowerroman\"><listitem><para>" 138 + item + "</para></listitem>" + "</orderedlist>"; 139 } 140 141 /** {@inheritDoc} */ 142 protected String getDefinitionListBlock( String definum, String definition ) 143 { 144 return "<variablelist><varlistentry><term>" + definum 145 + "</term>" + "<listitem><para>" + definition 146 + "</para></listitem>" + "</varlistentry>" + "</variablelist>"; 147 } 148 149 /** {@inheritDoc} */ 150 protected String getFigureBlock( String source, String caption ) 151 { 152 String format = FileUtils.extension( source ).toUpperCase( Locale.ENGLISH ); 153 String figureBlock = "<mediaobject><imageobject>" 154 + "<imagedata fileref=\"" + source + "\" format=\"" + format + "\" />" 155 + "</imageobject>"; 156 if ( caption != null ) 157 { 158 figureBlock += "<caption><para>" + caption + "</para></caption>"; 159 } 160 figureBlock += "</mediaobject>"; 161 162 return figureBlock; 163 } 164 165 /** {@inheritDoc} */ 166 protected String getTableBlock( String cell, String caption ) 167 { 168 // Using the same set ordering than the JVM 169 MutableAttributeSet att = new SimpleAttributeSet(); 170 att.addAttribute( "frame", "none" ); 171 att.addAttribute( "rowsep", "0" ); 172 att.addAttribute( "colsep", "0" ); 173 174 return "<table" + SinkUtils.getAttributeString( att ) + "><title>" + caption 175 + "</title>" + "<tgroup cols=\"1\"><colspec align=\"center\" />" + "<tbody><row><entry>" 176 + cell + "</entry>" + "</row>" + "</tbody></tgroup>" + "</table>"; 177 } 178 179 /** {@inheritDoc} */ 180 protected String getParagraphBlock( String text ) 181 { 182 return "<para>" + text + "</para>"; 183 } 184 185 /** {@inheritDoc} */ 186 protected String getVerbatimBlock( String text ) 187 { 188 return "<programlisting>" + text + "</programlisting>"; 189 } 190 191 /** {@inheritDoc} */ 192 protected String getHorizontalRuleBlock() 193 { 194 return "<!-- HR -->"; 195 } 196 197 /** {@inheritDoc} */ 198 protected String getPageBreakBlock() 199 { 200 return "<!-- PB -->"; 201 } 202 203 /** {@inheritDoc} */ 204 protected String getAnchorBlock( String anchor ) 205 { 206 return "<anchor id=\"" + anchor + "\" />" + anchor + "<!-- anchor_end -->"; 207 } 208 209 /** {@inheritDoc} */ 210 protected String getLinkBlock( String link, String text ) 211 { 212 String linkend = DoxiaUtils.isInternalLink( link ) ? link.substring( 1 ) : link; 213 return "<link linkend=\"" + linkend + "\">" + text + "</link>"; 214 } 215 216 /** {@inheritDoc} */ 217 protected String getItalicBlock( String text ) 218 { 219 return "<emphasis>" + text + "</emphasis>"; 220 } 221 222 /** {@inheritDoc} */ 223 protected String getBoldBlock( String text ) 224 { 225 return "<emphasis role=\"bold\">" + text + "</emphasis>"; 226 } 227 228 /** {@inheritDoc} */ 229 protected String getMonospacedBlock( String text ) 230 { 231 return "<literal>" + text + "</literal>"; 232 } 233 234 /** {@inheritDoc} */ 235 protected String getLineBreakBlock() 236 { 237 return "<!-- LB -->"; 238 } 239 240 /** {@inheritDoc} */ 241 protected String getNonBreakingSpaceBlock() 242 { 243 return " "; 244 } 245 246 /** {@inheritDoc} */ 247 protected String getTextBlock( String text ) 248 { 249 // TODO: retreive those from the sink 250 return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\"; 251 } 252 253 /** {@inheritDoc} */ 254 protected String getRawTextBlock( String text ) 255 { 256 // TODO 257 return ""; 258 } 259 260 /** {@inheritDoc} */ 261 protected String getCommentBlock( String text ) 262 { 263 return "<!--" + toXmlComment( text ) + "-->"; 264 } 265}