--- /dev/null
+/*
+ * Web IDE - Builder
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ * Bonyong Lee <bonyong.lee@samsung.com>
+ * Taeyoung Son <taeyoung2.son@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.common.builder.core;
+
+import java.io.ByteArrayOutputStream;
+
+import org.tizen.common.builder.BuildResult;
+import org.tizen.common.builder.Resource;
+import org.tizen.common.builder.exception.BuildException;
+import org.tizen.common.util.IOUtil;
+import org.tizen.common.verrari.IModelProvider;
+import org.tizen.common.verrari.ITemplateEngine;
+import org.tizen.common.verrari.ITemplateProvider;
+
+/**
+ * Template engine builder.
+ * When generate resource using template engine, use this abstract builder.
+ * @author Taeyoung Son {@literal <taeyoung2.son@samsung.com>} (S-Core)
+ * @since May 10, 2013
+ *
+ */
+public abstract class TemplateEngineBuilder extends AbstractBuilder {
+
+ protected ITemplateEngine templateEngine;
+ protected IModelProvider modelProvider;
+
+ /**
+ * @param engine Set {@link ITemplateEngine}. It must not be null.
+ * @param modelProvider Set {@link IModelProvider}. It must not be null.
+ * @param templateProvider Set {@link ITemplateProvider}. It must not be null.
+ */
+ public TemplateEngineBuilder(ITemplateEngine engine
+ , IModelProvider modelProvider
+ , ITemplateProvider templateProvider) {
+ this(engine, modelProvider);
+ this.templateEngine.setTemplateProvider(templateProvider);
+ }
+
+ /**
+ * @param engine Set {@link ITemplateEngine}. It must not be null and must set {@link ITemplateProvider} to engine.
+ * @param modelProvider Set {@link IModelProvider}. It must not be null.
+ */
+ public TemplateEngineBuilder(ITemplateEngine engine, IModelProvider modelProvider) {
+ super();
+ this.templateEngine = engine;
+ this.modelProvider = modelProvider;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.tizen.common.builder.Builder#build(org.tizen.common.builder.Resource)
+ */
+ @Override
+ public BuildResult build(Resource resource) throws BuildException {
+ ByteArrayOutputStream baos = null;
+ try {
+ baos = new ByteArrayOutputStream();
+ String templateKey = getTemplateKey(resource);
+ templateEngine.generate( templateKey, modelProvider, baos ); //$NON-NLS-1$
+ Resource out = layer.addResource( resource.getPath(), baos.toByteArray() );
+ final BuildResult ret = new BuildResult();
+ ret.addDependency( resource, out );
+ return ret;
+ } catch (Exception e) {
+ throw new BuildException( e );
+ } finally {
+ IOUtil.tryClose( baos );
+ }
+ }
+
+ /**
+ * Get template key. It can be used {@link ITemplateEngine#generate(String, IModelProvider, java.io.OutputStream)}.
+ * @param resource
+ * @return Template key.
+ */
+ protected abstract String getTemplateKey(Resource resource);
+}