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.web.repository.page.generators;
017
018import java.io.IOException;
019import java.text.Normalizer;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.page.PagesContainer;
035
036/**
037 * SAX pages which match filter regexp
038 */
039public class FilterPagesByRegexpGenerator extends ServiceableGenerator
040{
041    /** Ametys object resolver. */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    @Override
052    public void generate() throws IOException, SAXException, ProcessingException
053    {
054        Request request = ObjectModelHelper.getRequest(objectModel);
055        String regexp = request.getParameter("value");
056        String id = request.getParameter("id");
057        
058        PagesContainer container = _resolver.resolveById(id);
059        
060        String toMatch = Normalizer.normalize(regexp.toLowerCase(), Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").trim();
061        
062        contentHandler.startDocument();
063        XMLUtils.startElement(contentHandler, "pages");
064        
065        AmetysObjectIterable< ? extends Page> childrenPages = container.getChildrenPages();
066        for (Page childPage : childrenPages)
067        {
068            _saxMatchingPages(childPage, toMatch);
069        }
070        
071        XMLUtils.endElement(contentHandler, "pages");
072        contentHandler.endDocument();
073    }
074    
075    private void _saxMatchingPages (Page page, String regexp) throws SAXException
076    {
077        String title = Normalizer.normalize(page.getTitle().toLowerCase(), Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
078        if (title.contains(regexp))
079        {
080            AttributesImpl atts = new AttributesImpl();
081            atts.addCDATAAttribute("id", page.getId());
082            atts.addCDATAAttribute("path", page.getPathInSitemap());
083            XMLUtils.createElement(contentHandler, "page", atts);
084        }
085        
086        AmetysObjectIterable< ? extends Page> childrenPages = page.getChildrenPages();
087        for (Page childPage : childrenPages)
088        {
089            _saxMatchingPages (childPage, regexp);
090        }
091    }
092}