001/*
002 *  Copyright 2010 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 */
016
017package org.ametys.web.editor;
018
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.xml.sax.Attributes;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.transformation.htmledition.AbstractHTMLEditionHandler;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 * Do stuffs during save process such as repairing copy/paste links
037 */
038public class WebHTMLEditionHandler extends AbstractHTMLEditionHandler
039{
040    private SiteManager _siteManager;
041    
042    @Override
043    public void service(ServiceManager sManager) throws ServiceException
044    {
045        super.service(sManager);
046        _siteManager = (SiteManager) sManager.lookup(SiteManager.ROLE);
047    }
048    
049    @Override
050    public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException
051    {
052        if ("a".equals(raw))
053        {
054            String type = attrs.getValue("data-ametys-type");
055            
056            if (type == null)
057            {
058                // link may be copied from elsewhere
059                String href = attrs.getValue("href");
060                
061                if (href != null && href.startsWith("/"))
062                {
063                    // it may be an internal URL
064                    Request request = ContextHelper.getRequest(_context);
065                    String contextPath = request.getContextPath();
066                    
067                    if (href.startsWith(contextPath))
068                    {
069                        try
070                        {
071                            String ctx = href.substring(contextPath.length());
072                            Matcher matcher = Pattern.compile("/([^/]+)/([^/]+)/(.*)\\.html").matcher(ctx);
073                            
074                            if (matcher.matches())
075                            {
076                                String siteName = matcher.group(1);
077                                String sitemapName = matcher.group(2);
078                                String pagePath = matcher.group(3);
079                                
080                                Site site = _siteManager.getSite(siteName);
081                                Page page = site.getSitemap(sitemapName).getChild(pagePath);
082                                String id = page.getId();
083                                
084                                AttributesImpl newAttrs = new AttributesImpl();
085                                
086                                for (int i = 0; i < attrs.getLength(); i++)
087                                {
088                                    String name = attrs.getQName(i);
089                                    
090                                    if (!"data-ametys-href".equals(name))
091                                    {
092                                        newAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), name, attrs.getType(i), attrs.getValue(i));
093                                    }
094                                }
095                                
096                                newAttrs.addAttribute("", "data-ametys-href", "data-ametys-href", "CDATA", id);
097                                newAttrs.addAttribute("", "data-ametys-type", "data-ametys-type", "CDATA", "page");
098                                
099                                super.startElement(uri, loc, raw, newAttrs);
100                                return;
101                            }
102                        }
103                        catch (Exception e)
104                        {
105                            // cannot find page
106                            // ignore and pass the SAX element through
107                        }
108                    }
109                }
110            }
111        }
112        
113        super.startElement(uri, loc, raw, attrs);
114    }
115}