001/*
002 *  Copyright 2012 Anyware Services
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 */
016package org.ametys.core.ui;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Map.Entry;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.avalon.framework.thread.ThreadSafe;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.xml.sax.ContentHandler;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.ui.ClientSideElement.Script;
034import org.ametys.core.ui.ClientSideElement.ScriptFile;
035import org.ametys.core.util.JSONUtils;
036
037/**
038 * This helper allow to sax a client side element
039 */
040public class SAXClientSideElementHelper extends AbstractLogEnabled implements Component, Serviceable, ThreadSafe
041{
042    /** Avalon role */
043    public static final String ROLE = SAXClientSideElementHelper.class.getName();
044    private JSONUtils _jsonUtils;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
050    }
051    /**
052     * SAX a client side element
053     * @param tagName the tag name to create
054     * @param element The client side element to sax. Can not be null.
055     * @param handler The handler where to sax
056     * @param contextualParameters Contextuals parameters transmitted by the environment.
057     * @throws SAXException If an error occured
058     */
059    public void saxDefinition(String tagName, ClientSideElement element, ContentHandler handler, Map<String, Object> contextualParameters) throws SAXException
060    {
061        List<Script> scripts = element.getScripts(contextualParameters);
062        
063        for (Script script : scripts)
064        {
065            AttributesImpl clientSideElementAttrs = new AttributesImpl();
066            clientSideElementAttrs.addCDATAAttribute("id", script.getId());
067            clientSideElementAttrs.addCDATAAttribute("serverId", script.getServerId());
068            clientSideElementAttrs.addCDATAAttribute("plugin", element.getPluginName());
069            XMLUtils.startElement(handler, tagName, clientSideElementAttrs);
070            
071            // Initial parameters
072            Map<String, Object> parameters = script.getParameters();
073            
074            // SAX Action (classname and initial parameters)
075            AttributesImpl attrs = new AttributesImpl();
076            attrs.addCDATAAttribute("class", script.getScriptClassname());
077            XMLUtils.createElement(handler, "action", attrs, _jsonUtils.convertObjectToJson(parameters));
078            
079            // SAX Scripts
080            XMLUtils.startElement(handler, "scripts");
081            for (ScriptFile scriptFile : script.getScriptFiles())
082            {
083                saxScriptFile(handler, scriptFile);
084            }
085            XMLUtils.endElement(handler, "scripts");
086    
087            // SAX CSS
088            XMLUtils.startElement(handler, "css");
089            for (ScriptFile scriptFile : script.getCSSFiles())
090            {
091                saxScriptFile(handler, scriptFile);
092            }
093            XMLUtils.endElement(handler, "css");
094    
095            XMLUtils.endElement(handler, tagName);
096        }
097    }
098    
099    private void saxScriptFile(ContentHandler handler, ScriptFile scriptFile) throws SAXException
100    {
101        AttributesImpl fileAttrs = new AttributesImpl();
102        String debugMode = scriptFile.getDebugMode();
103        if (debugMode != null && !"all".equals(debugMode))
104        {
105            fileAttrs.addCDATAAttribute("debug", debugMode);
106        }
107        
108        if (!scriptFile.isLangSpecific())
109        {
110            String rtlMode = scriptFile.getRtlMode();
111            if (rtlMode != null && !"all".equals(rtlMode))
112            {
113                fileAttrs.addCDATAAttribute("rtl", rtlMode);
114            }
115            
116            XMLUtils.createElement(handler, "file", fileAttrs, scriptFile.getPath());
117        }
118        else
119        {
120            fileAttrs.addCDATAAttribute("lang", "true");
121            XMLUtils.startElement(handler, "file", fileAttrs);
122            
123            String defaultLang = scriptFile.getDefaultLang();
124            Map<String, String> langPaths = scriptFile.getLangPaths();
125            
126            for (Entry<String, String> langPath : langPaths.entrySet())
127            {
128                AttributesImpl langAttrs = new AttributesImpl();
129                
130                String codeLang = langPath.getKey();
131                langAttrs.addCDATAAttribute("code", codeLang);
132                if (codeLang.equals(defaultLang))
133                {
134                    langAttrs.addCDATAAttribute("default", "true");
135                }
136                
137                XMLUtils.createElement(handler, "lang", langAttrs, langPath.getValue());
138            }
139
140            XMLUtils.endElement(handler, "file");
141        }
142        
143    }
144}