Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    public static final String XML_EXTENSION = ".xml";

    /**
     * Creates a new temporal XML file and writes the content of the specified object into the XML file. 
     * @param filename            name of the temporal XML file.
     * @param content            Content as an object of the specified class.
     * @param typeParameterClass   Class of the object with the content.
     * @return                  File.
     */
    public static <T> File writeTemp(String filename, T content, Class<T> typeParameterClass) {
        File fileTemp = null;
        try {
            fileTemp = File.createTempFile(filename, XML_EXTENSION);
            fileTemp = write(fileTemp, content, typeParameterClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileTemp;
    }

    /**
     * Creates a new XML file and writes the content of the specified object into the XML file. 
     * @param filepath            Path to the XML file.
     * @param content            Content as an object of the specified class.
     * @param typeParameterClass   Class of the object with the content.
     * @return                  File.
     */
    public static <T> File write(String filepath, T content, Class<T> typeParameterClass) {
        File file = null;
        try {
            file = new File(filepath);
            if (!file.exists()) {
                file.createNewFile();
            }
            file = write(file, content, typeParameterClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * Writes the content of the specified object into the specified XML file.
     * @param filename            Path to the XML file.
     * @param content            Content as an object of the specified class.
     * @param typeParameterClass   Class of the object with the content.
     * @return                  File.
     */
    public static <T> File write(File file, T content, Class<T> typeParameterClass) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(content, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static <T> String write(T content, Class<T> typeParameterClass) {
        ByteArrayOutputStream baos = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            baos = new ByteArrayOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
            jaxbMarshaller.marshal(content, osw);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return baos != null ? baos.toString() : null;
    }
}