1 package org.apache.maven.doxia.module.xdoc;
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 java.io.IOException;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25
26 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
27 import org.codehaus.plexus.util.xml.XMLWriter;
28 import org.codehaus.plexus.util.xml.XmlUtil;
29
30 /**
31 * A Doxia Sink which produces an xdoc document.
32 *
33 * @author juan <a href="mailto:james@jamestaylor.org">James Taylor</a>
34 * @author Juan F. Codagnone (replaced println with XmlWriterXdocSink)
35 * @deprecated Since 1.1, this sink is not more supported. If you are looking for a <code>Sink</code> which produces
36 * pretty formatted XML, you could use the {@link XdocSink#XdocSink(java.io.Writer)} as usual and reformat the
37 * <code>Sink</code> content produced with {@link XmlUtil#prettyFormat(java.io.Reader, java.io.Writer)}.
38 */
39 public class XmlWriterXdocSink
40 extends XdocSink
41 {
42 /** Writer used by Xdoc */
43 private StringWriter xdocWriter;
44
45 private XMLWriter xmlWriter;
46
47 private XmlWriterXdocSink( StringWriter out, String encoding )
48 {
49 super( out, encoding );
50 this.xdocWriter = out;
51 this.xmlWriter = new PrettyPrintXMLWriter( out );
52 }
53
54 /**
55 * <p>Constructor for XmlWriterXdocSink.</p>
56 *
57 * @param out the wanted XML Writer.
58 * @deprecated since 1.1
59 */
60 public XmlWriterXdocSink( XMLWriter out )
61 {
62 this( new StringWriter(), "UTF-8" );
63 this.xmlWriter = out;
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 public void close()
70 {
71 super.close();
72
73 String xdocContent = xdocWriter.toString();
74 if ( getLog().isDebugEnabled() )
75 {
76 getLog().debug( "Xdoc content: " + xdocContent );
77 }
78 StringWriter formattedContent = new StringWriter();
79 try
80 {
81 XmlUtil.prettyFormat( new StringReader( xdocContent ), formattedContent );
82 }
83 catch ( IOException e )
84 {
85 if ( getLog().isDebugEnabled() )
86 {
87 getLog().error( "IOException: " + e.getMessage(), e );
88 }
89 formattedContent = new StringWriter();
90 formattedContent.write( xdocContent );
91 }
92 xmlWriter.writeMarkup( formattedContent.toString() );
93 }
94 }