001/*
002 * Copyright 2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl1.php
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.kuali.rice.kim.impl.jaxb;
017
018import java.io.InputStream;
019import java.io.OutputStream;
020import java.util.List;
021
022import javax.xml.bind.JAXBContext;
023import javax.xml.bind.JAXBException;
024import javax.xml.bind.Marshaller;
025import javax.xml.bind.Unmarshaller;
026
027import org.kuali.rice.core.impl.jaxb.DataXmlDTO;
028
029/**
030 * Helper class for importing and exporting KIM XML.
031 * 
032 * <p>TODO: Should this be converted into a service instead?
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public final class KimXmlUtil {
037    // Do not allow outside code to instantiate this class.
038    private KimXmlUtil() {}
039    
040    /**
041     * Parses permissions and/or roles from XML.
042     * 
043     * @param inputStream The input stream to read the XML from.
044     */
045    public static void parseKimXml(InputStream inputStream) {
046        try {
047            JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
048            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
049            unmarshaller.unmarshal(inputStream);
050        } catch (JAXBException e) {
051            throw new RuntimeException(e);
052        }
053    }
054    
055    /**
056     * Exports one or more sets of KIM objects to XML.
057     * 
058     * @param outputStream The output stream to write the XML to.
059     * @param permissions The KIM permissions to export; set to a null or empty list to prevent exportation of a &lt;permissionData&gt; element.
060     * @param roles The KIM roles to export; set to a null or empty list to prevent exportation of a &lt;roleData&gt; element.
061     */
062    public static void exportKimXml(OutputStream outputStream, List<? extends Object> permissions, List<? extends Object> roles) {
063        PermissionDataXmlDTO permissionData = (permissions != null && !permissions.isEmpty()) ?
064                new PermissionDataXmlDTO(new PermissionsXmlDTO(permissions)) : null;
065        RoleDataXmlDTO roleData = (roles != null && !roles.isEmpty()) ?
066                new RoleDataXmlDTO(new RolesXmlDTO(roles)) : null;
067        try {
068            JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
069            Marshaller marshaller = jaxbContext.createMarshaller();
070            marshaller.marshal(new DataXmlDTO(permissionData, roleData), outputStream);
071        } catch (JAXBException e) {
072            throw new RuntimeException(e);
073        }
074    }
075}