001/* 002 * Copyright 2009 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.core.right; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.generation.ServiceableGenerator; 024import org.apache.cocoon.xml.XMLUtils; 025import org.xml.sax.SAXException; 026import org.xml.sax.helpers.AttributesImpl; 027 028import org.ametys.core.right.Profile; 029import org.ametys.core.right.RightProfilesDAO; 030 031 032/** 033 * Generates profiles 034 */ 035public class ProfilesListGenerator extends ServiceableGenerator 036{ 037 /** The profiles DAO */ 038 protected RightProfilesDAO _profilesDAO; 039 040 @Override 041 public void service(ServiceManager m) throws ServiceException 042 { 043 super.service(m); 044 _profilesDAO = (RightProfilesDAO) m.lookup(RightProfilesDAO.ROLE); 045 } 046 047 public void generate() throws IOException, SAXException, ProcessingException 048 { 049 contentHandler.startDocument(); 050 051 XMLUtils.startElement(contentHandler, "Profiles"); 052 053 for (Profile profile : _profilesDAO.getProfiles()) 054 { 055 saxProfile(profile); 056 } 057 058 XMLUtils.endElement(contentHandler, "Profiles"); 059 060 contentHandler.endDocument(); 061 } 062 063 /** 064 * SAX a profile with its rights 065 * @param profile The profile 066 * @throws SAXException if an error occurred while saxing 067 */ 068 protected void saxProfile (Profile profile) throws SAXException 069 { 070 AttributesImpl atts = new AttributesImpl(); 071 atts.addAttribute("", "id", "id", "CDATA", profile.getId()); 072 XMLUtils.startElement(contentHandler, "profile", atts); 073 074 XMLUtils.createElement(contentHandler, "label", profile.getLabel()); 075 076 String context = profile.getContext(); 077 if (context != null) 078 { 079 XMLUtils.createElement(contentHandler, "context", context); 080 } 081 082 contentHandler.startElement("", "rights", "rights", new AttributesImpl()); 083 084 for (String right : _profilesDAO.getRights(profile)) 085 { 086 AttributesImpl attsRight = new AttributesImpl(); 087 attsRight.addAttribute("", "id", "id", "CDATA", right); 088 XMLUtils.createElement(contentHandler, "right", attsRight); 089 } 090 091 XMLUtils.endElement(contentHandler, "rights"); 092 XMLUtils.endElement(contentHandler, "profile"); 093 } 094}