Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package javax.xml.bind;
 
 import java.io.File;
 import java.io.Writer;
 import java.io.Reader;
 import java.net.URI;
 import java.net.URL;
 
 
 final public class JAXB {
 
     private JAXB() {
     }
 
     public static void marshal(Object objectFile file) {
         if (file == null) {
             throw new IllegalStateException("No file is given");
         }
         marshal(objectnew StreamResult(file));
     }
 
     public static void marshal(Object objectOutputStream os) {
         if (os == null) {
             throw new IllegalStateException("No output stream is given");
         }
         marshal(objectnew StreamResult(os));
     }
 
     public static void marshal(Object objectWriter writer) {
         if (writer == null) {
             throw new IllegalStateException("No writer is given");
         }
         marshal(objectnew StreamResult(writer));
     }
 
     public static void marshal(Object objectString str) {
         if (str == null) {
             throw new IllegalStateException("No string destination is given");
         }
         try {
             marshal(objectnew URI(str));
         } catch (URISyntaxException e) {
             marshal(objectnew File(str));
         }
     }
 
     public static void marshal(Object objectURI uri) {
         if (uri == null) {
             throw new IllegalStateException("No uri is given");
         }
         try {
             marshal(objecturi.toURL());
         } catch (IOException e) {
             throw new DataBindingException(e);
         }
     }
 
     public static void marshal(Object objectURL url) {
         if (url == null) {
             throw new IllegalStateException("No url is given");
         }
         try {
             URLConnection con = url.openConnection();
             con.setDoOutput(true);
             con.setDoInput(false);
             con.connect();
             marshal(objectnew StreamResult(con.getOutputStream()));
         } catch (IOException e) {
             throw new DataBindingException(e);
         }
    }
    public static void marshal(Object objectResult result) {
        try {
            JAXBContext context;
            if (object instanceof JAXBElement) {
                context = getContext(((JAXBElement<?>) object).getDeclaredType());
            } else {
                Class<?> clazz = object.getClass();
                XmlRootElement r = clazz.getAnnotation(XmlRootElement.class);
                if (r == null) {
                    // we need to infer the name
                    object = new JAXBElement(new QName(Introspector.decapitalize(clazz.getSimpleName())),
                                             clazzobject);
                }
                context = getContext(clazz);
            }
            Marshaller m = context.createMarshaller();
            m.setProperty(.,true);
            m.marshal(objectresult);
        } catch (JAXBException e) {
            throw new DataBindingException(e);
        }
    }
    public static <T> T unmarshal(File fileClass<T> type) {
        if (file == null) {
            throw new IllegalStateException("No file is given");
        }
        return unmarshal(new StreamSource(file), type);
    }
    public static <T> T unmarshal(URL urlClass<T> type) {
        if (url == null) {
            throw new IllegalStateException("No url is given");
        }
        return unmarshal(new StreamSource(url.toExternalForm()), type);
    }
    public static <T> T unmarshal(URI uriClass<T> type) {
        if (uri == null) {
            throw new IllegalStateException("No uri is given");
        }
        try {
            return unmarshal(uri.toURL(), type);
        } catch (MalformedURLException e) {
            throw new DataBindingException(e);
        }
    }
    public static <T> T unmarshal(String strClass<T> type) {
        if (str == null) {
            throw new IllegalStateException("No string destination is given");
        }
        try {
            return unmarshal(new URI(str), type);
        } catch (URISyntaxException e) {
            return unmarshal(new File(str), type);
        }
    }
    public static <T> T unmarshal(InputStream isClass<T> type) {
        if (is == null) {
            throw new IllegalStateException("No input stream is given");
        }
        return unmarshal(new StreamSource(is), type);
    }
    public static <T> T unmarshal(Reader readerClass<T> type) {
        if (reader == null) {
            throw new IllegalStateException("No reader is given");
        }
        return unmarshal(new StreamSource(reader), type);
    }
    public static <T> T unmarshal(Source sourceClass<T> type) {
        try {
            JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(sourcetype);
            return item.getValue();
        } catch (JAXBException e) {
            throw new DataBindingException(e);
        }
    }
    private static <T> JAXBContext getContext(Class<T> typethrows JAXBException {
        return JAXBContext.newInstance(type);
    }
New to GrepCode? Check out our FAQ X