001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Jean-Marc Orliaguet, Chalmers 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.theme.webengine.fm.extensions; 021 022import java.io.ByteArrayInputStream; 023import java.io.IOException; 024import java.io.StringWriter; 025import java.io.Writer; 026import java.util.ArrayList; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031import javax.xml.parsers.DocumentBuilderFactory; 032import javax.xml.parsers.ParserConfigurationException; 033 034import org.w3c.dom.DOMException; 035import org.w3c.dom.Document; 036import org.w3c.dom.NamedNodeMap; 037import org.w3c.dom.Node; 038import org.w3c.dom.NodeList; 039import org.xml.sax.SAXException; 040 041import freemarker.core.Environment; 042import freemarker.template.TemplateDirectiveBody; 043import freemarker.template.TemplateDirectiveModel; 044import freemarker.template.TemplateException; 045import freemarker.template.TemplateModel; 046import freemarker.template.TemplateModelException; 047 048/** 049 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a> 050 */ 051public class NXThemesTabsDirective implements TemplateDirectiveModel { 052 053 @SuppressWarnings("unchecked") 054 public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 055 throws TemplateException, IOException { 056 057 if (loopVars.length != 0) { 058 throw new TemplateModelException("This directive doesn't allow loop variables."); 059 } 060 if (body == null) { 061 throw new TemplateModelException("Expected a body"); 062 } 063 064 Map<String, String> attributes = Utils.getTemplateDirectiveParameters(params); 065 String identifier = attributes.get("identifier"); 066 String styleClass = attributes.get("styleClass"); 067 String controlledBy = attributes.get("controlledBy"); 068 069 // view 070 final Map<String, Object> view = new HashMap<String, Object>(); 071 view.put("id", identifier); 072 final Map<String, Object> widget = new HashMap<String, Object>(); 073 widget.put("type", "tabs"); 074 if (styleClass != null) { 075 widget.put("styleClass", styleClass); 076 } 077 if (null != controlledBy) { 078 view.put("controllers", controlledBy.split(",")); 079 } 080 081 StringWriter sw = new StringWriter(); 082 body.render(sw); 083 String content = String.format("<tabs>%s</tabs>", sw.getBuffer().toString()); 084 085 final List<Map<String, String>> items = new ArrayList<Map<String, String>>(); 086 087 // Parse the XML content 088 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 089 try { 090 Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(content.getBytes())); 091 NodeList itemList = doc.getElementsByTagName("tab"); 092 for (int i = 0; i < itemList.getLength(); i++) { 093 Node itemNode = itemList.item(i); 094 NamedNodeMap attrs = itemNode.getAttributes(); 095 Node link = attrs.getNamedItem("link"); 096 Node label = attrs.getNamedItem("label"); 097 Node switchTo = attrs.getNamedItem("switchTo"); 098 Map<String, String> itemMap = new HashMap<String, String>(); 099 if (link != null) { 100 itemMap.put("link", link.getNodeValue()); 101 } 102 if (label != null) { 103 itemMap.put("label", label.getNodeValue()); 104 } 105 if (switchTo != null) { 106 itemMap.put("switchTo", switchTo.getNodeValue()); 107 } 108 items.add(itemMap); 109 } 110 } catch (ParserConfigurationException | SAXException | DOMException e) { 111 } 112 113 widget.put("items", items); 114 view.put("widget", widget); 115 116 Writer writer = env.getOut(); 117 writer.write(String.format("<ins class=\"view\">%s</ins>", org.nuxeo.theme.html.Utils.toJson(view))); 118 119 } 120}