001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package javax.xml.bind;
018    
019    import java.io.File;
020    import java.io.OutputStream;
021    import java.io.Writer;
022    import java.io.IOException;
023    import java.io.Reader;
024    import java.io.InputStream;
025    import java.net.URI;
026    import java.net.URL;
027    import java.net.URLConnection;
028    import java.net.URISyntaxException;
029    import java.net.MalformedURLException;
030    import java.beans.Introspector;
031    
032    import javax.xml.transform.Result;
033    import javax.xml.transform.Source;
034    import javax.xml.transform.stream.StreamResult;
035    import javax.xml.transform.stream.StreamSource;
036    import javax.xml.bind.annotation.XmlRootElement;
037    import javax.xml.namespace.QName;
038    
039    final public class JAXB {
040    
041        private JAXB() {
042        }
043    
044        public static void marshal(Object object, File file) {
045            if (file == null) {
046                throw new IllegalStateException("No file is given");
047            }
048            marshal(object, new StreamResult(file));
049        }
050    
051        public static void marshal(Object object, OutputStream os) {
052            if (os == null) {
053                throw new IllegalStateException("No output stream is given");
054            }
055            marshal(object, new StreamResult(os));
056        }
057    
058        public static void marshal(Object object, Writer writer) {
059            if (writer == null) {
060                throw new IllegalStateException("No writer is given");
061            }
062            marshal(object, new StreamResult(writer));
063        }
064    
065        public static void marshal(Object object, String str) {
066            if (str == null) {
067                throw new IllegalStateException("No string destination is given");
068            }
069            try {
070                marshal(object, new URI(str));
071            } catch (URISyntaxException e) {
072                marshal(object, new File(str));
073            }
074        }
075    
076        public static void marshal(Object object, URI uri) {
077            if (uri == null) {
078                throw new IllegalStateException("No uri is given");
079            }
080            try {
081                marshal(object, uri.toURL());
082            } catch (IOException e) {
083                throw new DataBindingException(e);
084            }
085        }
086    
087        public static void marshal(Object object, URL url) {
088            if (url == null) {
089                throw new IllegalStateException("No url is given");
090            }
091            try {
092                URLConnection con = url.openConnection();
093                con.setDoOutput(true);
094                con.setDoInput(false);
095                con.connect();
096                marshal(object, new StreamResult(con.getOutputStream()));
097            } catch (IOException e) {
098                throw new DataBindingException(e);
099            }
100        }
101    
102        public static void marshal(Object object, Result result) {
103            try {
104                JAXBContext context;
105                if (object instanceof JAXBElement) {
106                    context = getContext(((JAXBElement<?>) object).getDeclaredType());
107                } else {
108                    Class<?> clazz = object.getClass();
109                    XmlRootElement r = clazz.getAnnotation(XmlRootElement.class);
110                    if (r == null) {
111                        // we need to infer the name
112                        object = new JAXBElement(new QName(Introspector.decapitalize(clazz.getSimpleName())),
113                                                 clazz, object);
114                    }
115                    context = getContext(clazz);
116                }
117                Marshaller m = context.createMarshaller();
118                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
119                m.marshal(object, result);
120            } catch (JAXBException e) {
121                throw new DataBindingException(e);
122            }
123        }
124    
125        public static <T> T unmarshal(File file, Class<T> type) {
126            if (file == null) {
127                throw new IllegalStateException("No file is given");
128            }
129            return unmarshal(new StreamSource(file), type);
130        }
131    
132        public static <T> T unmarshal(URL url, Class<T> type) {
133            if (url == null) {
134                throw new IllegalStateException("No url is given");
135            }
136            return unmarshal(new StreamSource(url.toExternalForm()), type);
137        }
138    
139        public static <T> T unmarshal(URI uri, Class<T> type) {
140            if (uri == null) {
141                throw new IllegalStateException("No uri is given");
142            }
143            try {
144                return unmarshal(uri.toURL(), type);
145            } catch (MalformedURLException e) {
146                throw new DataBindingException(e);
147            }
148        }
149    
150        public static <T> T unmarshal(String str, Class<T> type) {
151            if (str == null) {
152                throw new IllegalStateException("No string destination is given");
153            }
154            try {
155                return unmarshal(new URI(str), type);
156            } catch (URISyntaxException e) {
157                return unmarshal(new File(str), type);
158            }
159        }
160    
161        public static <T> T unmarshal(InputStream is, Class<T> type) {
162            if (is == null) {
163                throw new IllegalStateException("No input stream is given");
164            }
165            return unmarshal(new StreamSource(is), type);
166        }
167    
168        public static <T> T unmarshal(Reader reader, Class<T> type) {
169            if (reader == null) {
170                throw new IllegalStateException("No reader is given");
171            }
172            return unmarshal(new StreamSource(reader), type);
173        }
174    
175        public static <T> T unmarshal(Source source, Class<T> type) {
176            try {
177                JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(source, type);
178                return item.getValue();
179            } catch (JAXBException e) {
180                throw new DataBindingException(e);
181            }
182        }
183    
184        private static <T> JAXBContext getContext(Class<T> type) throws JAXBException {
185            return JAXBContext.newInstance(type);
186        }
187    
188    
189    }