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 */
019package org.apache.wiki.tags;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.api.core.Engine;
023import org.apache.wiki.ui.EditorManager;
024import org.apache.wiki.ui.TemplateManager;
025
026import javax.servlet.ServletException;
027import javax.servlet.jsp.JspException;
028import java.io.IOException;
029
030
031/**
032 *  Creates an editor component with all the necessary parts
033 *  to get it working.
034 *  <p>
035 *  In the future, this component should be expanded to provide
036 *  a customized version of the editor according to user preferences.
037 *
038 *  @since 2.2
039 */
040public class EditorTag extends WikiBodyTag {
041
042    private static final long serialVersionUID = 0L;
043    private static final Logger log = Logger.getLogger( EditorTag.class );
044    
045    @Override
046    public final int doWikiStartTag() throws IOException {
047        return SKIP_BODY;
048    }
049       
050    @Override
051    public int doEndTag() throws JspException {
052        final Engine engine = m_wikiContext.getEngine();
053        final EditorManager mgr = engine.getManager( EditorManager.class );
054        final String editorPath = mgr.getEditorPath( m_wikiContext );
055
056        try {
057            final String page = engine.getManager( TemplateManager.class ).findJSP( pageContext, m_wikiContext.getTemplate(), editorPath );
058
059            if( page == null ) {
060                //FIXME: should be I18N ...
061                pageContext.getOut().println( "Unable to find editor '" + editorPath + "'" );
062            } else {
063                pageContext.include( page );
064            }
065        } catch( final ServletException e ) {
066            log.error( "Failed to include editor", e );
067            throw new JspException( "Failed to include editor: " + e.getMessage() );
068        } catch( final IOException e ) {
069            throw new JspException( "Could not print Editor tag: " + e.getMessage() );
070        }
071        
072        return EVAL_PAGE;
073    }
074
075}