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.jdom2.Text; 023import org.apache.wiki.WikiContext; 024import org.apache.wiki.api.exceptions.NoSuchVariableException; 025import org.apache.wiki.render.RenderingManager; 026 027/** 028 * Stores the contents of a WikiVariable in a WikiDocument DOM tree. 029 * <p> 030 * When the WikiDocument is rendered, if the {@link RenderingManager#WYSIWYG_EDITOR_MODE} 031 * is set to {@link Boolean#TRUE}, the variable declaration is rendered instead 032 * 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 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( String varName ) 048 { 049 m_varName = varName; 050 } 051 052 /** 053 * Evaluates the variable and returns the contents. 054 * 055 * @return The rendered value of the variable. 056 */ 057 public String getValue() 058 { 059 String result = ""; 060 WikiDocument root = (WikiDocument) getDocument(); 061 062 if( root == null ) 063 { 064 // See similar note in PluginContent 065 return m_varName; 066 } 067 068 WikiContext context = root.getContext(); 069 070 if( context == null ) 071 return "No WikiContext available: INTERNAL ERROR"; 072 073 Boolean wysiwygEditorMode = (Boolean)context.getVariable(RenderingManager.WYSIWYG_EDITOR_MODE); 074 075 if( wysiwygEditorMode != null && wysiwygEditorMode.booleanValue() ) 076 { 077 result = "[" + m_varName + "]"; 078 } 079 else 080 { 081 try 082 { 083 result = context.getEngine().getVariableManager().parseAndGetValue( context, m_varName ); 084 } 085 catch( NoSuchVariableException e ) 086 { 087 result = MarkupParser.makeError( "No such variable: " + e.getMessage() ).getText(); 088 } 089 } 090 091 return StringEscapeUtils.escapeXml11( result ); 092 } 093 094 /** 095 * Returns exactly getValue(). 096 * @return Whatever getValue() returns. 097 */ 098 public String getText() 099 { 100 return getValue(); 101 } 102 103 /** 104 * Returns a debug-suitable string. 105 * @return Debug string 106 */ 107 public String toString() 108 { 109 return "VariableElement[\""+m_varName+"\"]"; 110 } 111}