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.xml.sax.SAXException;
027
028import org.ametys.runtime.plugin.PluginsManager.InactivityCause;
029
030/**
031 * SAX plugins' informations in order to be able to generate a plugin by file tree
032 */
033public class PluginsGenerator extends AbstractGenerator
034{
035    public void generate() throws IOException, SAXException, ProcessingException
036    {
037        contentHandler.startDocument();
038        XMLUtils.startElement(contentHandler, "list");
039        
040        Map<String, Plugin> plugins = PluginsManager.getInstance().getPlugins();
041        Map<String, Feature> activeFeatures = PluginsManager.getInstance().getFeatures();
042        Map<String, InactivityCause> inactiveFeatures = PluginsManager.getInstance().getInactiveFeatures();
043
044        _saxPlugins(plugins, activeFeatures, 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, Feature> activeFeatures, Map<String, InactivityCause> 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            XMLUtils.startElement(contentHandler, "plugin", psAttrs);
065            
066            Plugin plugin = plugins.get(pluginName);
067            Map<String, Feature> features = plugin.getFeatures();
068            
069            for (String featureId : features.keySet())
070            {
071                Feature feature = features.get(featureId);
072                
073                if (activeFeatures.containsKey(featureId))
074                {
075                    AttributesImpl pAttrs = new AttributesImpl();
076                    pAttrs.addCDATAAttribute("name", feature.getFeatureName());
077                    XMLUtils.startElement(contentHandler, "feature", pAttrs);
078                    
079                    // Get extension by extension point
080                    Map<String, Collection<String>> exts = feature.getExtensionsIds();
081                    for (String extensionPoint : exts.keySet())
082                    {
083                        AttributesImpl epAttrs = new AttributesImpl();
084                        epAttrs.addCDATAAttribute("name", extensionPoint);
085                        XMLUtils.startElement(contentHandler, "extensionPoint", epAttrs);
086    
087                        // Récupérer les extensions du plugin courant (par id)
088                        for (String extension : exts.get(extensionPoint))
089                        {
090                            XMLUtils.createElement(contentHandler, "extension", extension);
091                        }
092    
093                        XMLUtils.endElement(contentHandler, "extensionPoint");
094                    }
095    
096                    // Get components
097                    Map<String, String> componentsIds = feature.getComponentsIds();
098                    for (String componentRole : componentsIds.keySet())
099                    {
100                        String component = componentsIds.get(componentRole);
101                        AttributesImpl cmpAttrs = new AttributesImpl();
102                        cmpAttrs.addCDATAAttribute("role", componentRole);
103                        XMLUtils.createElement(contentHandler, "component", cmpAttrs, component);
104                    }
105    
106                    XMLUtils.endElement(contentHandler, "feature");
107                }
108                else
109                {
110                    InactivityCause cause = inactiveFeatures.get(featureId);
111                    
112                    AttributesImpl pAttrs = new AttributesImpl();
113                    pAttrs.addCDATAAttribute("name", feature.getFeatureName());
114                    pAttrs.addCDATAAttribute("inactive", "true");
115                    pAttrs.addCDATAAttribute("cause", cause.toString());
116                    XMLUtils.createElement(contentHandler, "feature", pAttrs);
117                }
118            }
119            
120            XMLUtils.endElement(contentHandler, "plugin");
121        }
122        
123        XMLUtils.endElement(contentHandler, "plugins");
124    }
125    
126    private void _saxExtensionPoints() throws SAXException
127    {
128        XMLUtils.startElement(contentHandler, "extension-points");
129        
130        for (String extPoint : PluginsManager.getInstance().getExtensionPoints().keySet())
131        {
132            AttributesImpl ePAttrs = new AttributesImpl();
133            ePAttrs.addCDATAAttribute("id", extPoint);
134            XMLUtils.createElement(contentHandler, "extension-point", ePAttrs);
135        }
136        
137        XMLUtils.endElement(contentHandler, "extension-points");
138    }
139    
140    private void _saxComponents() throws SAXException
141    {
142        XMLUtils.startElement(contentHandler, "components");
143        
144        for (String componentRole : PluginsManager.getInstance().getComponents())
145        {
146            AttributesImpl cmpAttrs = new AttributesImpl();
147            cmpAttrs.addCDATAAttribute("id", componentRole);
148            XMLUtils.createElement(contentHandler, "component", cmpAttrs);
149        }
150        
151        XMLUtils.endElement(contentHandler, "components");
152    }
153}