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