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 */ 017 package org.apache.commons.monitoring.reporting.web.handler.api; 018 019 import org.apache.commons.monitoring.reporting.web.template.MapBuilder; 020 import org.apache.commons.monitoring.reporting.web.template.Templates; 021 022 import java.io.PrintWriter; 023 import java.util.Collections; 024 import java.util.Map; 025 026 public class TemplateHelper { 027 private final PrintWriter writer; 028 private final Map<String, ?> params; 029 030 public TemplateHelper(final PrintWriter writer, final Map<String, ?> params) { 031 this.writer = writer; 032 this.params = params; 033 } 034 035 public void renderHtml(final String template) { 036 renderHtml(template, Collections.<String, Object>emptyMap()); 037 } 038 039 public void renderHtml(final String template, final Map<String, ?> userParams) { 040 Templates.htmlRender(writer, template, new MapBuilder<String, Object>().set(Map.class.cast(params)).set(Map.class.cast(userParams)).build()); 041 } 042 043 public void renderPlain(final String template, final Map<String, ?> params) { 044 Templates.render(writer, template, params); 045 } 046 047 public void renderPlain(final String template) { 048 renderPlain(template, Collections.<String, Object>emptyMap()); 049 } 050 051 public void write(final String message) { 052 writer.write(message); 053 } 054 }