001package org.apache.maven.doxia.module.xdoc;
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.IOException;
023import java.io.StringReader;
024import java.io.StringWriter;
025
026import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
027import org.codehaus.plexus.util.xml.XMLWriter;
028import org.codehaus.plexus.util.xml.XmlUtil;
029
030/**
031 * A Doxia Sink which produces an xdoc document.
032 *
033 * @author juan <a href="mailto:james@jamestaylor.org">James Taylor</a>
034 * @author Juan F. Codagnone  (replaced println with XmlWriterXdocSink)
035 * @version $Id: XmlWriterXdocSink.html 905940 2014-04-12 16:27:29Z hboutemy $
036 * @deprecated Since 1.1, this sink is not more supported. If you are looking for a <code>Sink</code> which produces
037 * pretty formatted XML, you could use the {@link XdocSink#XdocSink(java.io.Writer)} as usual and reformat the
038 * <code>Sink</code> content produced with {@link XmlUtil#prettyFormat(java.io.Reader, java.io.Writer)}.
039 */
040public class XmlWriterXdocSink
041    extends XdocSink
042{
043    /** Writer used by Xdoc */
044    private StringWriter xdocWriter;
045
046    private XMLWriter xmlWriter;
047
048    private XmlWriterXdocSink( StringWriter out, String encoding )
049    {
050        super( out, encoding );
051        this.xdocWriter = out;
052        this.xmlWriter = new PrettyPrintXMLWriter( out );
053    }
054
055    /**
056     * <p>Constructor for XmlWriterXdocSink.</p>
057     *
058     * @param out the wanted XML Writer.
059     * @deprecated since 1.1
060     */
061    public XmlWriterXdocSink( XMLWriter out )
062    {
063        this( new StringWriter(), "UTF-8" );
064        this.xmlWriter = out;
065    }
066
067    /** {@inheritDoc} */
068    public void close()
069    {
070        super.close();
071
072        String xdocContent = xdocWriter.toString();
073        if ( getLog().isDebugEnabled() )
074        {
075            getLog().debug( "Xdoc content: " + xdocContent );
076        }
077        StringWriter formattedContent = new StringWriter();
078        try
079        {
080            XmlUtil.prettyFormat( new StringReader( xdocContent ), formattedContent );
081        }
082        catch ( IOException e )
083        {
084            if ( getLog().isDebugEnabled() )
085            {
086                getLog().error( "IOException: " + e.getMessage(), e );
087            }
088            formattedContent = new StringWriter();
089            formattedContent.write( xdocContent );
090        }
091        xmlWriter.writeMarkup( formattedContent.toString() );
092    }
093}