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     */
019    package org.apache.wiki.render;
020    
021    import java.io.IOException;
022    import java.io.StringWriter;
023    
024    import org.jdom2.output.Format;
025    import org.jdom2.output.XMLOutputter;
026    
027    import org.apache.wiki.WikiContext;
028    import org.apache.wiki.parser.WikiDocument;
029    
030    /**
031     *  Implements a WikiRendered that outputs XHTML.  Because the internal DOM
032     *  representation is in XHTML already, this just basically dumps out everything
033     *  out in a non-prettyprinted format.
034     *  
035     *  @since  2.4
036     */
037    public class XHTMLRenderer
038        extends WikiRenderer 
039    {
040        private static final String LINEBREAK = "\n";
041    
042        /**
043         *  Creates an XHTML 1.0 renderer.
044         *  
045         *  @param context {@inheritDoc}
046         *  @param doc {@inheritDoc}
047         */
048        public XHTMLRenderer( WikiContext context, WikiDocument doc )
049        {
050            super( context, doc );
051        }
052        
053        /**
054         *  {@inheritDoc}
055         */
056        public String getString()
057            throws IOException
058        {
059            m_document.setContext( m_context );
060    
061            CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor();
062            XMLOutputter output = new XMLOutputter(processor);
063            
064            StringWriter out = new StringWriter();
065            
066            Format fmt = Format.getRawFormat();
067            fmt.setExpandEmptyElements( false );
068            fmt.setLineSeparator( LINEBREAK );
069    
070            output.setFormat( fmt );
071            output.outputElementContent( m_document.getRootElement(), out );
072            
073            String result = out.toString();
074            return result;
075        }
076    }