001/*
002 *  Copyright 2011 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.plugins.glossary;
017
018import java.io.IOException;
019import java.text.Normalizer;
020import java.text.Normalizer.Form;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.plugins.repository.AmetysObjectIterable;
034import org.ametys.plugins.repository.TraversableAmetysObject;
035import org.ametys.web.repository.page.Page;
036import org.ametys.web.repository.site.SiteManager;
037
038/**
039 * Generate the list of word definitions as XML.
040 */
041public class DefinitionsGenerator extends ServiceableGenerator
042{
043    private SiteManager _siteManager;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
050    }
051    
052    @Override
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        String siteName = parameters.getParameter("siteName", (String) request.getAttribute("site"));
057        String language = parameters.getParameter("language", (String) request.getAttribute("sitemapLanguage"));
058        String letter = parameters.getParameter("letter", request.getParameter("letter"));
059        letter = StringUtils.isBlank(letter) ? "A" : letter.toUpperCase();
060        
061        Page page = (Page) request.getAttribute(Page.class.getName());
062        
063        contentHandler.startDocument();
064        
065        AttributesImpl attrs = new AttributesImpl();
066        attrs.addCDATAAttribute("siteName", siteName);
067        attrs.addCDATAAttribute("language", language);
068        attrs.addCDATAAttribute("letter", letter);
069        if (page != null)
070        {
071            attrs.addCDATAAttribute("page", page.getId());
072        }
073        XMLUtils.startElement(contentHandler, "wordDefinitions", attrs);
074        
075        TraversableAmetysObject definitionsNode = GlossaryHelper.getDefinitionsNode(_siteManager.getSite(siteName), language);
076        
077        try (AmetysObjectIterable<DefaultDefinition> definitions = definitionsNode.getChildren();)
078        {
079            for (DefaultDefinition definition : definitions)
080            {
081                saxDefinition(definition);
082            }
083        }
084        
085        XMLUtils.endElement(contentHandler, "wordDefinitions");
086        contentHandler.endDocument();
087    }
088    
089    /**
090     * Generate a word definition.
091     * @param definition the definition to generate.
092     * @throws SAXException If an error occurs while generating the XML.
093     */
094    protected void saxDefinition(DefaultDefinition definition) throws SAXException
095    {
096        AttributesImpl attrs = new AttributesImpl();
097        attrs.addCDATAAttribute("id", definition.getId());
098        
099        String word = definition.getWord();
100        if (StringUtils.isNotEmpty(word))
101        {
102            String firstLetter = Normalizer.normalize(word.substring(0, 1), Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
103            attrs.addCDATAAttribute("firstLetter", firstLetter.toUpperCase());
104        }
105        attrs.addCDATAAttribute("word", definition.getWord());
106        attrs.addCDATAAttribute("variantsText", StringUtils.join(definition.getVariants(), ", "));
107        attrs.addCDATAAttribute("displayOnText", Boolean.toString(definition.displayOnText()));
108        
109        XMLUtils.startElement(contentHandler, "wordDefinition", attrs);
110        
111        // Variants.
112        XMLUtils.startElement(contentHandler, "variants");
113        
114        for (String variant : definition.getVariants())
115        {
116            AttributesImpl variantAttrs = new AttributesImpl();
117            variantAttrs.addCDATAAttribute("word", variant);
118            XMLUtils.createElement(contentHandler, "variant", variantAttrs);
119        }
120        
121        XMLUtils.endElement(contentHandler, "variants");
122        
123        // Content
124        XMLUtils.createElement(contentHandler, "content", definition.getContent());
125        
126        XMLUtils.endElement(contentHandler, "wordDefinition");
127    }
128    
129}