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 * @deprecated Since 1.1, this sink is not more supported. If you are looking for a <code>Sink</code> which produces
036 * pretty formatted XML, you could use the {@link XdocSink#XdocSink(java.io.Writer)} as usual and reformat the
037 * <code>Sink</code> content produced with {@link XmlUtil#prettyFormat(java.io.Reader, java.io.Writer)}.
038 */
039public class XmlWriterXdocSink
040    extends XdocSink
041{
042    /** Writer used by Xdoc */
043    private StringWriter xdocWriter;
044
045    private XMLWriter xmlWriter;
046
047    private XmlWriterXdocSink( StringWriter out, String encoding )
048    {
049        super( out, encoding );
050        this.xdocWriter = out;
051        this.xmlWriter = new PrettyPrintXMLWriter( out );
052    }
053
054    /**
055     * <p>Constructor for XmlWriterXdocSink.</p>
056     *
057     * @param out the wanted XML Writer.
058     * @deprecated since 1.1
059     */
060    public XmlWriterXdocSink( XMLWriter out )
061    {
062        this( new StringWriter(), "UTF-8" );
063        this.xmlWriter = out;
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    public void close()
070    {
071        super.close();
072
073        String xdocContent = xdocWriter.toString();
074        if ( getLog().isDebugEnabled() )
075        {
076            getLog().debug( "Xdoc content: " + xdocContent );
077        }
078        StringWriter formattedContent = new StringWriter();
079        try
080        {
081            XmlUtil.prettyFormat( new StringReader( xdocContent ), formattedContent );
082        }
083        catch ( IOException e )
084        {
085            if ( getLog().isDebugEnabled() )
086            {
087                getLog().error( "IOException: " + e.getMessage(), e );
088            }
089            formattedContent = new StringWriter();
090            formattedContent.write( xdocContent );
091        }
092        xmlWriter.writeMarkup( formattedContent.toString() );
093    }
094}