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 org.apache.juneau.html.annotation.*;
016import org.apache.juneau.svl.*;
017
018/**
019 * HTML widget variable resolver.
020 *
021 * <p>
022 * The format for this var is <js>"$W{widgetName}"</js>.
023 *
024 * <p>
025 * Widgets are simple class that produce some sort of string based on a passed-in HTTP request.
026 *
027 * <p>
028 * They're registered via the following mechanisms:
029 * <ul>
030 *    <li>{@link HtmlDocConfig#widgets() @HtmlDocConfig(widgets)}
031 * </ul>
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="../../../../index.html#jrs.HtmlWidgets">Widgets</a>
035 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.HtmlDetails">HTML Details</a>
036 * </ul>
037 */
038public class HtmlWidgetVar extends SimpleVar {
039
040   /**
041    * The name of this variable.
042    */
043   public static final String NAME = "W";
044
045   /**
046    * Constructor.
047    */
048   public HtmlWidgetVar() {
049      super(NAME);
050   }
051
052   @Override /* Parameter */
053   public String resolve(VarResolverSession session, String key) throws Exception {
054      HtmlWidgetMap m = session.getBean(HtmlWidgetMap.class).orElseThrow(RuntimeException::new);
055
056      HtmlWidget w = m.get(key);
057      if (w == null)
058         return "unknown-widget-"+key;
059
060      return w.getHtml(session);
061   }
062
063   @Override
064   public boolean canResolve(VarResolverSession session) {
065      return session.getBean(HtmlWidgetMap.class).isPresent();
066   }
067}