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.tags;
020    
021    import java.io.IOException;
022    
023    import org.apache.log4j.Logger;
024    import org.apache.wiki.WikiEngine;
025    import org.apache.wiki.WikiPage;
026    import org.apache.wiki.api.exceptions.ProviderException;
027    
028    /**
029     *  Returns the currently requested page or attachment size.
030     *
031     *  @since 2.0
032     */
033    public class PageSizeTag
034        extends WikiTagBase
035    {
036        private static final long serialVersionUID = 0L;
037        private static final Logger log = Logger.getLogger( PageSizeTag.class );
038        
039        public final int doWikiStartTag()
040            throws IOException
041        {
042            WikiEngine engine = m_wikiContext.getEngine();
043            WikiPage   page   = m_wikiContext.getPage();
044    
045            try
046            {
047                if( page != null )
048                {
049                    long size = page.getSize();
050    
051                    if( size == -1 && engine.pageExists(page) ) // should never happen with attachments
052                    {
053                        size = engine.getPureText( page.getName(), page.getVersion() ).length();
054                        page.setSize( size );
055                    }
056    
057                    pageContext.getOut().write( Long.toString(size) );
058                }
059            }
060            catch( ProviderException e )
061            {
062                log.warn("Providers did not work: ",e);
063                pageContext.getOut().write("Error determining page size: "+e.getMessage());
064            }
065    
066            return SKIP_BODY;
067        }
068    }