com.ovea.tajin.markup.web.DynamicMarkupDataBuilder Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Ovea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ovea.tajin.markup.web;
import com.ovea.tajin.io.Resource;
import com.ovea.tajin.markup.Template;
import com.ovea.tajin.markup.TemplateCompiler;
import com.ovea.tajin.markup.TemplateResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
/**
* @author Mathieu Carbou ([email protected])
*/
final class DynamicMarkupDataBuilder implements MarkupDataBuilder {
private final TemplateCompiler compiler;
private final TemplateResolver resolver;
private final LocaleProvider localeProvider;
private final ContextProvider contextProvider;
private final MarkupOptions markupOptions;
DynamicMarkupDataBuilder(TemplateCompiler compiler, ContextProvider contextProvider, LocaleProvider localeProvider, MarkupOptions markupOptions, TemplateResolver resolver) {
this.compiler = compiler;
this.contextProvider = contextProvider;
this.localeProvider = localeProvider;
this.markupOptions = markupOptions;
this.resolver = resolver;
}
@Override
public MarkupData build(HttpServletRequest request, HttpServletResponse response, String path) {
try {
Resource resource = resolver.resolve(path, localeProvider.get(request));
Template template = compiler.compile(resource);
String markup = template.merge(contextProvider.build(request, response));
return new MarkupData(markup.getBytes(markupOptions.defaultCharset));
} catch (RuntimeException e) {
return new MarkupData(e);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy