001/*
002 *  Copyright 2015 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.runtime.plugin;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.Map;
021
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.generation.AbstractGenerator;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.apache.commons.lang3.StringUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.runtime.plugin.PluginsManager.InactivityStatus;
030
031/**
032 * SAX plugins' informations in order to be able to generate a plugin by file tree
033 */
034public class PluginsGenerator extends AbstractGenerator
035{
036    public void generate() throws IOException, SAXException, ProcessingException
037    {
038        contentHandler.startDocument();
039        XMLUtils.startElement(contentHandler, "list");
040        
041        Map<String, Plugin> plugins = PluginsManager.getInstance().getPlugins();
042        Map<String, InactivityStatus> inactiveFeatures = PluginsManager.getInstance().getInactiveFeatures();
043
044        _saxPlugins(plugins, inactiveFeatures);
045        
046        // Extension points
047        _saxExtensionPoints();
048        
049        // Components
050        _saxComponents();
051        
052        XMLUtils.endElement(contentHandler, "list");
053        contentHandler.endDocument();
054    }
055    
056    private void _saxPlugins(Map<String, Plugin> plugins, Map<String, InactivityStatus> inactiveFeatures) throws SAXException
057    {
058        XMLUtils.startElement(contentHandler, "plugins");
059
060        for (String pluginName : plugins.keySet())
061        {
062            AttributesImpl psAttrs = new AttributesImpl();
063            psAttrs.addCDATAAttribute("name", pluginName);
064            
065            String version = PluginsManager.getInstance().getVersion(pluginName);
066            if (StringUtils.isNotBlank(version))
067            {
068                psAttrs.addCDATAAttribute("version", version);
069            }
070            
071            XMLUtils.startElement(contentHandler, "plugin", psAttrs);
072            
073            Plugin plugin = plugins.get(pluginName);
074            Map<String, Feature> features = plugin.getFeatures();
075            
076            for (String featureId : features.keySet())
077            {
078                Feature feature = features.get(featureId);
079                
080                AttributesImpl pAttrs = new AttributesImpl();
081                pAttrs.addCDATAAttribute("name", feature.getFeatureName());
082                
083                InactivityStatus cause = inactiveFeatures.get(featureId);
084                if (cause != null)
085                {
086                    pAttrs.addCDATAAttribute("inactive", "true");
087                    pAttrs.addCDATAAttribute("cause", cause.cause().toString());
088                    
089                    Collection<String> sources = cause.features();
090                    if (sources != null && !sources.isEmpty())
091                    {
092                        pAttrs.addCDATAAttribute("source", sources.iterator().next());
093                    }
094                }
095                
096                XMLUtils.startElement(contentHandler, "feature", pAttrs);
097                
098                // Get extension by extension point
099                Map<String, Collection<String>> exts = feature.getExtensionsIds();
100                for (String extensionPoint : exts.keySet())
101                {
102                    AttributesImpl epAttrs = new AttributesImpl();
103                    epAttrs.addCDATAAttribute("name", extensionPoint);
104                    XMLUtils.startElement(contentHandler, "extensionPoint", epAttrs);
105
106                    // Récupérer les extensions du plugin courant (par id)
107                    for (String extension : exts.get(extensionPoint))
108                    {
109                        XMLUtils.createElement(contentHandler, "extension", extension);
110                    }
111
112                    XMLUtils.endElement(contentHandler, "extensionPoint");
113                }
114
115                // Get components
116                Map<String, String> componentsIds = feature.getComponentsIds();
117                for (String componentRole : componentsIds.keySet())
118                {
119                    String component = componentsIds.get(componentRole);
120                    AttributesImpl cmpAttrs = new AttributesImpl();
121                    cmpAttrs.addCDATAAttribute("role", componentRole);
122                    XMLUtils.createElement(contentHandler, "component", cmpAttrs, component);
123                }
124
125                XMLUtils.endElement(contentHandler, "feature");
126            }
127            
128            XMLUtils.endElement(contentHandler, "plugin");
129        }
130        
131        XMLUtils.endElement(contentHandler, "plugins");
132    }
133    
134    private void _saxExtensionPoints() throws SAXException
135    {
136        XMLUtils.startElement(contentHandler, "extension-points");
137        
138        for (String extPoint : PluginsManager.getInstance().getExtensionPoints().keySet())
139        {
140            AttributesImpl ePAttrs = new AttributesImpl();
141            ePAttrs.addCDATAAttribute("id", extPoint);
142            XMLUtils.createElement(contentHandler, "extension-point", ePAttrs);
143        }
144        
145        XMLUtils.endElement(contentHandler, "extension-points");
146    }
147    
148    private void _saxComponents() throws SAXException
149    {
150        XMLUtils.startElement(contentHandler, "components");
151        
152        for (String componentRole : PluginsManager.getInstance().getComponents())
153        {
154            AttributesImpl cmpAttrs = new AttributesImpl();
155            cmpAttrs.addCDATAAttribute("id", componentRole);
156            XMLUtils.createElement(contentHandler, "component", cmpAttrs);
157        }
158        
159        XMLUtils.endElement(contentHandler, "components");
160    }
161}