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.survey.generators;
017
018import java.io.IOException;
019
020import org.apache.cocoon.ProcessingException;
021import org.apache.cocoon.environment.ObjectModelHelper;
022import org.apache.cocoon.environment.Request;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.xml.sax.SAXException;
026
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.survey.repository.SurveyPage;
029import org.ametys.plugins.survey.repository.SurveyQuestion;
030import org.ametys.plugins.survey.repository.SurveyQuestion.QuestionType;
031
032/**
033 * SAX page branching
034 *
035 */
036public class PageBranchesGenerator extends QuestionRulesGenerator
037{
038
039    @Override
040    public void generate() throws IOException, SAXException, ProcessingException
041    {
042        Request request = ObjectModelHelper.getRequest(objectModel);
043        String id = request.getParameter("id");
044        
045        SurveyPage surveyPage = _resolver.resolveById(id);
046        
047        contentHandler.startDocument();
048        
049        AttributesImpl attr = new AttributesImpl();
050        attr.addCDATAAttribute("id", surveyPage.getId());
051        XMLUtils.startElement(contentHandler, "page", attr);
052        
053        AmetysObjectIterable<SurveyQuestion> questions = surveyPage.getChildren();
054        int index = 1;
055        for (SurveyQuestion question : questions)
056        {
057            if (question.getType() == QuestionType.SINGLE_CHOICE || question.getType() == QuestionType.MULTIPLE_CHOICE)
058            {
059                saxRules(question, index);
060            }
061            index++;
062        }
063        
064        // SAX page rule
065        saxRule(surveyPage);
066        
067        XMLUtils.endElement(contentHandler, "page");
068        contentHandler.endDocument();
069    }
070
071}