001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.html; 014 015import java.util.*; 016 017import org.apache.juneau.html.annotation.*; 018import org.apache.juneau.svl.*; 019 020/** 021 * HTML widget variable resolver. 022 * 023 * <p> 024 * The format for this var is <js>"$W{widgetName}"</js>. 025 * 026 * <p> 027 * Widgets are simple class that produce some sort of string based on a passed-in HTTP request. 028 * 029 * <p> 030 * They're registered via the following mechanisms: 031 * <ul> 032 * <li>{@link HtmlDocConfig#widgets() @HtmlDocConfig(widgets)} 033 * </ul> 034 * 035 * <ul class='seealso'> 036 * <li class='link'>{@doc RestSvlVariables} 037 * </ul> 038 */ 039public class HtmlWidgetVar extends SimpleVar { 040 041 /** 042 * The name of the session or context object that identifies the object containing the widgets to resolve. 043 */ 044 public static final String SESSION_htmlWidgets = "htmlWidgets"; 045 046 /** 047 * The name of this variable. 048 */ 049 public static final String NAME = "W"; 050 051 /** 052 * Constructor. 053 */ 054 public HtmlWidgetVar() { 055 super(NAME); 056 } 057 058 @SuppressWarnings("unchecked") 059 @Override /* Parameter */ 060 public String resolve(VarResolverSession session, String key) throws Exception { 061 Map<String,HtmlWidget> widgets = (Map<String,HtmlWidget>)session.getSessionObject(Object.class, SESSION_htmlWidgets, false); 062 063 HtmlWidget w = widgets.get(key); 064 if (w == null) 065 return "unknown-widget-"+key; 066 067 return w.getHtml(session); 068 } 069 070 @Override 071 public boolean canResolve(VarResolverSession session) { 072 return session.hasSessionObject(SESSION_htmlWidgets); 073 } 074}