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.parser;
020
021import org.apache.commons.text.StringEscapeUtils;
022import org.apache.wiki.WikiContext;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.exceptions.NoSuchVariableException;
025import org.apache.wiki.variables.VariableManager;
026import org.jdom2.Text;
027
028/**
029 *  Stores the contents of a WikiVariable in a WikiDocument DOM tree.
030 *  <p>
031 *  When the WikiDocument is rendered, if the {@link WikiContext#VAR_WYSIWYG_EDITOR_MODE} is set to {@link Boolean#TRUE}, the
032 *  variable declaration is rendered instead of the variable value.
033 *
034 *  @since  2.4
035 */
036public class VariableContent extends Text {
037
038    private static final long serialVersionUID = 1L;
039
040    private final String m_varName;
041    
042    /**
043     *  Create a VariableContent for the given variable.
044     *  
045     *  @param varName The name of the variable.
046     */
047    public VariableContent( final String varName ) {
048        m_varName = varName;
049    }
050    
051    /**
052     *   Evaluates the variable and returns the contents. 
053     *   
054     *   @return The rendered value of the variable.
055     */
056    @Override
057    public String getValue() {
058        String result;
059        final WikiDocument root = ( WikiDocument )getDocument();
060
061        if( root == null ) {
062            // See similar note in PluginContent
063            return m_varName;
064        }
065        
066        final Context context = root.getContext();
067        if( context == null ) {
068            return "No WikiContext available: INTERNAL ERROR";
069        }
070    
071        final Boolean wysiwygEditorMode = context.getVariable( Context.VAR_WYSIWYG_EDITOR_MODE );
072        if( wysiwygEditorMode != null && wysiwygEditorMode ) {
073            result = "[" + m_varName + "]";
074        } else {
075            try {
076                result = context.getEngine().getManager( VariableManager.class ).parseAndGetValue( context, m_varName );
077            } catch( final NoSuchVariableException e ) {
078                result = MarkupParser.makeError( "No such variable: " + e.getMessage() ).getText(); 
079            }
080        }
081
082        return StringEscapeUtils.escapeXml11( result );
083    }
084
085    /**
086     * Returns exactly getValue().
087     *
088     * @return Whatever getValue() returns.
089     */
090    @Override
091    public String getText() {
092        return getValue();
093    }
094
095    /**
096     * Returns a debug-suitable string.
097     *
098     * @return Debug string
099     */
100    @Override
101    public String toString() {
102        return "VariableElement[\"" + m_varName + "\"]";
103    }
104
105}