001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.webengine.fm.extensions;
023
024import java.io.ByteArrayInputStream;
025import java.io.IOException;
026import java.io.StringWriter;
027import java.io.Writer;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033import javax.xml.parsers.DocumentBuilderFactory;
034import javax.xml.parsers.ParserConfigurationException;
035
036import org.w3c.dom.DOMException;
037import org.w3c.dom.Document;
038import org.w3c.dom.NamedNodeMap;
039import org.w3c.dom.Node;
040import org.w3c.dom.NodeList;
041import org.xml.sax.SAXException;
042
043import freemarker.core.Environment;
044import freemarker.template.TemplateDirectiveBody;
045import freemarker.template.TemplateDirectiveModel;
046import freemarker.template.TemplateException;
047import freemarker.template.TemplateModel;
048import freemarker.template.TemplateModelException;
049
050/**
051 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a>
052 */
053public class NXThemesTabsDirective implements TemplateDirectiveModel {
054
055    @SuppressWarnings("unchecked")
056    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
057            throws TemplateException, IOException {
058
059        if (loopVars.length != 0) {
060            throw new TemplateModelException("This directive doesn't allow loop variables.");
061        }
062        if (body == null) {
063            throw new TemplateModelException("Expected a body");
064        }
065
066        Map<String, String> attributes = Utils.getTemplateDirectiveParameters(params);
067        String identifier = attributes.get("identifier");
068        String styleClass = attributes.get("styleClass");
069        String controlledBy = attributes.get("controlledBy");
070
071        // view
072        final Map<String, Object> view = new HashMap<String, Object>();
073        view.put("id", identifier);
074        final Map<String, Object> widget = new HashMap<String, Object>();
075        widget.put("type", "tabs");
076        if (styleClass != null) {
077            widget.put("styleClass", styleClass);
078        }
079        if (null != controlledBy) {
080            view.put("controllers", controlledBy.split(","));
081        }
082
083        StringWriter sw = new StringWriter();
084        body.render(sw);
085        String content = String.format("<tabs>%s</tabs>", sw.getBuffer().toString());
086
087        final List<Map<String, String>> items = new ArrayList<Map<String, String>>();
088
089        // Parse the XML content
090        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
091        try {
092            Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(content.getBytes()));
093            NodeList itemList = doc.getElementsByTagName("tab");
094            for (int i = 0; i < itemList.getLength(); i++) {
095                Node itemNode = itemList.item(i);
096                NamedNodeMap attrs = itemNode.getAttributes();
097                Node link = attrs.getNamedItem("link");
098                Node label = attrs.getNamedItem("label");
099                Node switchTo = attrs.getNamedItem("switchTo");
100                Map<String, String> itemMap = new HashMap<String, String>();
101                if (link != null) {
102                    itemMap.put("link", link.getNodeValue());
103                }
104                if (label != null) {
105                    itemMap.put("label", label.getNodeValue());
106                }
107                if (switchTo != null) {
108                    itemMap.put("switchTo", switchTo.getNodeValue());
109                }
110                items.add(itemMap);
111            }
112        } catch (ParserConfigurationException | SAXException | DOMException e) {
113        }
114
115        widget.put("items", items);
116        view.put("widget", widget);
117
118        Writer writer = env.getOut();
119        writer.write(String.format("<ins class=\"view\">%s</ins>", org.nuxeo.theme.html.Utils.toJson(view)));
120
121    }
122}