1 /*
2 * The Kuali Financial System, a comprehensive financial management system for higher education.
3 *
4 * Copyright 2005-2014 The Kuali Foundation
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.kuali.rice.kim.impl.jaxb;
20
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.List;
24
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.Marshaller;
28 import javax.xml.bind.Unmarshaller;
29
30 import org.kuali.rice.core.impl.jaxb.DataXmlDTO;
31
32 /**
33 * Helper class for importing and exporting KIM XML.
34 *
35 * <p>TODO: Should this be converted into a service instead?
36 *
37 * @author Kuali Rice Team (rice.collab@kuali.org)
38 */
39 public final class KimXmlUtil {
40 // Do not allow outside code to instantiate this class.
41 private KimXmlUtil() {}
42
43 /**
44 * Parses permissions and/or roles from XML.
45 *
46 * @param inputStream The input stream to read the XML from.
47 */
48 public static void parseKimXml(InputStream inputStream) {
49 try {
50 JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
51 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
52 unmarshaller.unmarshal(inputStream);
53 } catch (JAXBException e) {
54 throw new RuntimeException(e);
55 }
56 }
57
58 /**
59 * Exports one or more sets of KIM objects to XML.
60 *
61 * @param outputStream The output stream to write the XML to.
62 * @param permissions The KIM permissions to export; set to a null or empty list to prevent exportation of a <permissionData> element.
63 * @param roles The KIM roles to export; set to a null or empty list to prevent exportation of a <roleData> element.
64 */
65 public static void exportKimXml(OutputStream outputStream, List<? extends Object> permissions, List<? extends Object> roles) {
66 PermissionDataXmlDTO permissionData = (permissions != null && !permissions.isEmpty()) ?
67 new PermissionDataXmlDTO(new PermissionsXmlDTO(permissions)) : null;
68 RoleDataXmlDTO roleData = (roles != null && !roles.isEmpty()) ?
69 new RoleDataXmlDTO(new RolesXmlDTO(roles)) : null;
70 try {
71 JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
72 Marshaller marshaller = jaxbContext.createMarshaller();
73 marshaller.marshal(new DataXmlDTO(permissionData, roleData), outputStream);
74 } catch (JAXBException e) {
75 throw new RuntimeException(e);
76 }
77 }
78 }