001/**
002 * Copyright 2010-2013 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/ecl2.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.common.util;
017
018import java.io.File;
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.Writer;
022
023import javax.xml.bind.JAXBContext;
024import javax.xml.bind.JAXBException;
025import javax.xml.bind.Marshaller;
026import javax.xml.bind.Unmarshaller;
027
028import org.apache.commons.io.IOUtils;
029
030/**
031 * @deprecated
032 */
033@Deprecated
034public class JAXBUtil {
035
036        public static void write(Object instance, File file) {
037                Writer writer = null;
038                try {
039                        writer = LocationUtils.openWriter(file);
040                        write(instance, writer);
041                } catch (IOException e) {
042                        throw new IllegalStateException("Unexpected IO error", e);
043                } finally {
044                        IOUtils.closeQuietly(writer);
045                }
046        }
047
048        public static void write(Object instance, Writer writer) {
049                try {
050                        JAXBContext context = JAXBContext.newInstance(instance.getClass());
051                        Marshaller marshaller = context.createMarshaller();
052                        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
053                        marshaller.marshal(instance, writer);
054                } catch (JAXBException e) {
055                        throw new IllegalStateException("Unexpected JAXB error", e);
056                }
057        }
058
059        @SuppressWarnings("unchecked")
060        public static <T> T getObject(InputStream in, Class<T> type) {
061                try {
062                        JAXBContext context = JAXBContext.newInstance(type);
063                        Unmarshaller unmarshaller = context.createUnmarshaller();
064                        return (T) unmarshaller.unmarshal(in);
065                } catch (JAXBException e) {
066                        throw new IllegalStateException("Unexpected JAXB error", e);
067                }
068        }
069
070        public static <T> T getObject(File file, Class<T> type) {
071                return getObject(LocationUtils.getCanonicalPath(file), type);
072        }
073
074        public static <T> T getObject(String location, Class<T> type) {
075                InputStream in = null;
076                try {
077                        in = LocationUtils.getInputStream(location);
078                        return getObject(in, type);
079                } catch (IOException e) {
080                        throw new IllegalStateException("Unexpected JAXB error", e);
081                } finally {
082                        IOUtils.closeQuietly(in);
083                }
084        }
085}