Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 org.apache.marmotta.platform.core.services.templating;
 
 import java.io.*;
 import java.util.Map;
 
 
 
 
 
 
 
Template Service Implementation

Author(s):
Sergio Fernández
 
 public class TemplatingServiceImpl implements TemplatingService {
 
     private Map<StringStringcommon;
 
     @Inject
 
     @Inject
 
     private static Logger log = LoggerFactory.getLogger(ConfigurationService.class);
 
     private File templateDir;
 
     public TemplatingServiceImpl() {
         super();
          = new HashMap<StringString>();
     }
 
     @PostConstruct
     public void initDataModel() {
         Preconditions.checkState(.isHostStarted());
 
         String project = .getStringConfiguration("kiwi.pages.project");
         .put("PROJECT"project);
         .put("DEFAULT_STYLE".getStringConfiguration("kiwi.pages.style_path"));
         .put("SERVER_URL".getServerUri());
         .put("BASIC_URL".getBaseUri());
         .put("LOGO".getStringConfiguration("kiwi.pages.project." + project + ".logo"project + ".png"));
         .put("FOOTER".getStringConfiguration("kiwi.pages.project." + project + ".footer""(footer not properly configured for project " + project + ")"));
 
 
         if (!.exists()) .mkdirs();
 
     }
 
     private void loadTemplateFromClasspath(String templateNameClass<?> clazz) {
         final String src = . + templateName;
         final File tpl = new File(templateName);
         if (!tpl.exists()) {
             try {
                 .info("template {} not found in {}, copying fallback..."templateName.getAbsolutePath());
                 final InputStream in = clazz.getResourceAsStream(src);
                 if (in == null) {
                     throw new IOException("Resource " + src + " not found in ClassLoader (" + clazz + ")");
                 }
                 FileUtils.copyInputStreamToFile(intpl);
            } catch (IOException e) {
                .error("Could not load template from classpath, templating might react weird!"e);
            }
        }
    }

    
Update the data model in case an important value has changed

Parameters:
event
        if (event.getKeys().contains("kiwi.context")
                || event.getKeys().contains("kiwi.host")
                || event.getKeys().contains("templating.sort_by_weight")
                || event.getKeys().contains("kiwi.pages.project")) {
            initDataModel();
        }
    }
    @Override
    public Configuration getConfiguration() {
        return getConfiguration(TemplatingServiceImpl.class);
    }
    @Override
    public Configuration getConfiguration(Class<?> cls) {
        Configuration cfg = new Configuration();
        cfg.setDefaultEncoding("utf-8");
        cfg.setURLEscapingCharset("utf-8");
        try {
            cfg.setDirectoryForTemplateLoading();
        } catch (IOException e) {
            .warn("not a directory");
            cfg.setClassForTemplateLoading(cls.);
        }
        return cfg;
    }
    @Override
    public Template getTemplate(String namethrows IOException {
        // make sure template exists
        return getConfiguration().getTemplate(name);
    }
    @Override
    public Template getTemplate(Class<?> clsString namethrows IOException {
        // make sure template exists
        loadTemplateFromClasspath(namecls);
        return getConfiguration(cls).getTemplate(name);
    }
    @Override
    public String process(String namethrows IOExceptionTemplateException {
        return process(namenew HashMap<StringObject>());
    }
    @Override
    public String process(String nameMap<StringObjectdatathrows IOExceptionTemplateException {
        OutputStream os = new ByteArrayOutputStream();
        Writer writer = new BufferedWriter(new OutputStreamWriter(os));
        process(namedatawriter);
        return os.toString();
    }
    @Override
    public void process(String nameWriter writerthrows IOExceptionTemplateException {
        process(namenew HashMap<StringObject>(), writer);
    }
    @Override
    public void process(String nameMap<StringObjectdataWriter writerthrows IOExceptionTemplateException {
        process(TemplatingServiceImpl.classnamedatawriter);
    }
    @Override
    public void process(Class<?> clsString nameMap<StringObjectdataWriter writerthrows IOExceptionTemplateException {
        Template tpl = getTemplate(clsname);
        data.putAll();
        tpl.process(datawriter);
        writer.flush();
    }
New to GrepCode? Check out our FAQ X