001/**
002 * Copyright 2013 John Ericksen
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.androidtransfuse.util;
017
018import org.androidtransfuse.model.manifest.Manifest;
019
020import javax.inject.Inject;
021import javax.xml.bind.JAXBContext;
022import javax.xml.bind.JAXBException;
023import javax.xml.bind.Marshaller;
024import java.io.*;
025
026/**
027 * Serializes the Manifest to and from xml
028 *
029 * @author John Ericksen
030 */
031public class ManifestSerializer {
032
033    private final JAXBContext context;
034    private final Logger logger;
035
036    @Inject
037    public ManifestSerializer(JAXBContext context, Logger logger) {
038        this.context = context;
039        this.logger = logger;
040    }
041
042    public Manifest readManifest(File manifestFile) {
043        try{
044            return (Manifest) context.createUnmarshaller().unmarshal(manifestFile);
045        } catch (JAXBException e) {
046            throw new TransfuseRuntimeException("JAXBException while unmarshalling manifest", e);
047        }
048    }
049
050    public Manifest readManifest(InputStream manifestInputStream) {
051        try{
052            return (Manifest) context.createUnmarshaller().unmarshal(manifestInputStream);
053        } catch (JAXBException e) {
054            throw new TransfuseRuntimeException("JAXBException while unmarshalling manifest", e);
055        }
056    }
057
058    public void writeManifest(Manifest manifest, OutputStream manifestStream) {
059        try {
060            Writer writer = new OutputStreamWriter(manifestStream, "UTF-8");
061
062            Marshaller marshaller = context.createMarshaller();
063
064            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
065            marshaller.marshal(manifest, writer);
066        } catch (IOException e) {
067            logger.error("IOException while writing manifest", e);
068            throw new TransfuseRuntimeException("IOException while writing manifest", e);
069        } catch (JAXBException e) {
070            logger.error("JAXBException while writing manifest", e);
071            throw new TransfuseRuntimeException("JAXBException while writing manifest", e);
072        }
073    }
074
075    public void writeManifest(Manifest manifest, File manifestFile) {
076        try {
077            writeManifest(manifest, new FileOutputStream(manifestFile));
078        } catch (IOException e) {
079            logger.error("IOException while writing manifest", e);
080            throw new TransfuseInjectionException("IOException while writing manifest", e);
081        }
082    }
083}