1 /**
2 * Copyright 2005-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.kim.impl.jaxb;
17
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.util.List;
21
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.JAXBException;
24 import javax.xml.bind.Marshaller;
25 import javax.xml.bind.Unmarshaller;
26
27 import org.kuali.rice.core.impl.jaxb.DataXmlDTO;
28
29 /**
30 * Helper class for importing and exporting KIM XML.
31 *
32 * <p>TODO: Should this be converted into a service instead?
33 *
34 * @author Kuali Rice Team (rice.collab@kuali.org)
35 */
36 public final class KimXmlUtil {
37 // Do not allow outside code to instantiate this class.
38 private KimXmlUtil() {}
39
40 /**
41 * Parses permissions and/or roles from XML.
42 *
43 * @param inputStream The input stream to read the XML from.
44 */
45 public static void parseKimXml(InputStream inputStream) {
46 try {
47 JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
48 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
49 unmarshaller.unmarshal(inputStream);
50 } catch (JAXBException e) {
51 throw new RuntimeException(e);
52 }
53 }
54
55 /**
56 * Exports one or more sets of KIM objects to XML.
57 *
58 * @param outputStream The output stream to write the XML to.
59 * @param permissions The KIM permissions to export; set to a null or empty list to prevent exportation of a <permissionData> element.
60 * @param roles The KIM roles to export; set to a null or empty list to prevent exportation of a <roleData> element.
61 */
62 public static void exportKimXml(OutputStream outputStream, List<? extends Object> permissions, List<? extends Object> roles) {
63 PermissionDataXmlDTO permissionData = (permissions != null && !permissions.isEmpty()) ?
64 new PermissionDataXmlDTO(new PermissionsXmlDTO(permissions)) : null;
65 RoleDataXmlDTO roleData = (roles != null && !roles.isEmpty()) ?
66 new RoleDataXmlDTO(new RolesXmlDTO(roles)) : null;
67 try {
68 JAXBContext jaxbContext = JAXBContext.newInstance(DataXmlDTO.class);
69 Marshaller marshaller = jaxbContext.createMarshaller();
70 marshaller.marshal(new DataXmlDTO(permissionData, roleData), outputStream);
71 } catch (JAXBException e) {
72 throw new RuntimeException(e);
73 }
74 }
75 }