View Javadoc

1   /**
2    * Copyright 2010-2013 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.common.util.xml;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.UnsupportedEncodingException;
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.apache.commons.io.FileUtils;
31  import org.apache.commons.io.IOUtils;
32  import org.kuali.common.util.LocationUtils;
33  
34  public class DefaultXmlService implements XmlService {
35  
36  	@Override
37  	public <T> void write(File file, T instance) {
38  		OutputStream out = null;
39  		try {
40  			out = FileUtils.openOutputStream(file);
41  			write(out, instance);
42  		} catch (IOException e) {
43  			throw new IllegalStateException("Unexpected IO error", e);
44  		} finally {
45  			IOUtils.closeQuietly(out);
46  		}
47  	}
48  
49  	@Override
50  	public <T> void write(OutputStream out, T instance) {
51  		try {
52  			JAXBContext context = JAXBContext.newInstance(instance.getClass());
53  			Marshaller marshaller = context.createMarshaller();
54  			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
55  			marshaller.marshal(instance, out);
56  		} catch (JAXBException e) {
57  			throw new IllegalStateException("Unexpected JAXB error", e);
58  		}
59  	}
60  
61  	@Override
62  	@SuppressWarnings("unchecked")
63  	public <T> T getObject(InputStream in, Class<T> type) {
64  		try {
65  			JAXBContext context = JAXBContext.newInstance(type);
66  			Unmarshaller unmarshaller = context.createUnmarshaller();
67  			return (T) unmarshaller.unmarshal(in);
68  		} catch (JAXBException e) {
69  			throw new IllegalStateException("Unexpected JAXB error", e);
70  		}
71  	}
72  
73  	@Override
74  	public <T> T getObject(File file, Class<T> type) {
75  		return getObject(LocationUtils.getCanonicalPath(file), type);
76  	}
77  
78  	@Override
79  	public <T> T getObject(String location, Class<T> type) {
80  		InputStream in = null;
81  		try {
82  			in = LocationUtils.getInputStream(location);
83  			return getObject(in, type);
84  		} catch (IOException e) {
85  			throw new IllegalStateException("Unexpected JAXB error", e);
86  		} finally {
87  			IOUtils.closeQuietly(in);
88  		}
89  	}
90  
91  	@Override
92  	public <T> String toString(T instance, String encoding) {
93  		ByteArrayOutputStream out = new ByteArrayOutputStream();
94  		write(out, instance);
95  		try {
96  			return out.toString(encoding);
97  		} catch (UnsupportedEncodingException e) {
98  			throw new IllegalArgumentException(e);
99  		}
100 	}
101 }