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 */
016package org.ametys.cms.transformation;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.SourceResolver;
024import org.apache.cocoon.transformation.AbstractTransformer;
025import org.xml.sax.Attributes;
026import org.xml.sax.SAXException;
027import org.xml.sax.helpers.AttributesImpl;
028
029/**
030 * Transformer "hierarchying" flat docbook input 
031 */
032public class HierarchizeTransformer extends AbstractTransformer
033{
034    /** Tag for section */
035    public static final String TAG_SECTION = "section";
036    
037    /** Root tag */
038    public static final String TAG_ARTICLE = "article";
039    
040    private int _level;
041
042    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException
043    {
044        _level = 0;
045    }
046
047    @Override
048    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
049    {
050        if (!TAG_SECTION.equals(localName))
051        {
052            contentHandler.startElement(uri, localName, qName, atts);
053            return;
054        }
055        
056        int currentLevel = Integer.parseInt(atts.getValue("level"));
057        
058        int previousLevel = _level;
059        
060        if (currentLevel == previousLevel)
061        {
062            contentHandler.endElement("", TAG_SECTION, TAG_SECTION);
063        }
064        else if (currentLevel > previousLevel)
065        {
066            for (int i = previousLevel + 1; i < currentLevel; i++)
067            {
068                AttributesImpl attrsSection = new AttributesImpl(atts);
069                attrsSection.removeAttribute(attrsSection.getIndex("level"));
070                
071                contentHandler.startElement("", TAG_SECTION, TAG_SECTION, attrsSection);
072                contentHandler.startElement("", "title", "title", new AttributesImpl());
073                contentHandler.endElement("", "title", "title");
074            }
075        }
076        else
077        {
078            for (int i = previousLevel; i >= currentLevel; i--)
079            {
080                contentHandler.endElement("", TAG_SECTION, TAG_SECTION);
081            }
082        }
083               
084        AttributesImpl attrsSection = new AttributesImpl(atts);
085        attrsSection.removeAttribute(attrsSection.getIndex("level"));
086
087        _level = currentLevel;
088        contentHandler.startElement("", TAG_SECTION, TAG_SECTION, attrsSection);
089    }
090    
091    @Override
092    public void endElement(String uri, String localName, String qName) throws SAXException
093    {
094        if (TAG_ARTICLE.equals(localName))
095        {
096            int lastLevel = _level;
097            
098            for (int i = lastLevel; i > 0; i--)
099            {
100                contentHandler.endElement("", TAG_SECTION, TAG_SECTION);
101            }
102            
103            contentHandler.endElement(uri, localName, qName);
104        }
105        else if (!TAG_SECTION.equals(localName))
106        {
107            contentHandler.endElement(uri, localName, qName);
108            return;
109        }
110    }
111}