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 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.
 import java.util.Map;
User: Thomas Kurz Date: 22.07.11 Time: 13:06
 public class AdminTemplatingServiceImpl implements AdminInterfaceService {
     private ServletContext context;
     private static enum Properties { HEAD, CONTENT }
     private ModuleService moduleService;
     //some statics
     private static final String DEFAULT_ICON = "icon-beaker";
     //pattern to filter comments content
     private static final Pattern PATTERN = Pattern.compile("\\<!--###BEGIN_([^#]+)###--\\>(.+)\\<!--###END_\\1###--\\>",.);
     private MenuItem menu;

inits a freemarker template service with a servlet context

context a servlet context
     public void init(ServletContext contextthrows TemplatingException {
          = buildMenu();
         this. = context;

this method wraps a file with a specified admin template. If the file is not a admin page, the bytes are returned unprocessed

bytes content represented in a byte array
the processed (templated) byte array
     public byte[] process(byte[] bytesString paththrows TemplatingException {
         if(!.getBooleanConfiguration("templating.cache.enabled",true) && !=null) {
         //set active
         //fill data model
         Map<StringObjectdatamodel = new HashMap<String,Object>();
         for(Properties p : Properties.values()) {
             datamodel.put(,"<!-- "" not defined -->");
         //begin hack!!!
        //end hack!!!
        //add menu
        try {
            String s = new String(bytes);
            Matcher m = .matcher(s);
            while (m.find()) {
        } catch (Exception e) {
            throw new TemplatingException(e.getMessage());
        //make magic happen
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            .process(.datamodelnew OutputStreamWriter(bos));
            bytes = bos.toByteArray();
        } catch (Exception e) {
            throw new TemplatingException(e.getMessage());
        return bytes;
    public boolean isMenuEntry(String path) {
        return .setActive(path);

build menu

    private MenuItem buildMenu() {
        MenuItem menu = new MenuItem("MENU".);
        for(String container_string : .listSortedContainers()) {
            MenuItem container = new MenuItem(container_string.);
            //add modules
            for(String module_string : .listSortedModules(container_string)) {
                MenuItem module = new MenuItem(module_string.);
                if(.getIcon(module_string) != null)
                //add pages
                for(HashMap<String,Stringpage_object : .getAdminPageObjects(module_string)) {
                    MenuItem page = new MenuItem(page_object.get("title"), .);
                //add webservice
                if(!.getWebservices(module_string).isEmpty()) {
                    MenuItem page = new MenuItem(..);
                //add if there are pages to display
                if(!module.isEmpty()) container.addItem(module);
        return menu;
New to GrepCode? Check out our FAQ X