Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;

import java.nio.charset.Charset;
import java.util.Objects;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    /**
     * Creates a {@link Marshaller} based on the given {@link JAXBContext}
     * and configures it to use the default {@link Charset}, and allows to
     * output the XML code to be generated formatted and as an XML fragment.
     * 
     * @param ctx the {@link JAXBContext} to create a {@link Marshaller} for
     * @param formatted {@code true} if the XML code should be formatted,
     *       {@code false} otherwise
     * @param fragment {@code false} if the XML code should start with
     *       {@code <?xml }, or {@code true} if just fragment XML code should
     *       get generated
     * 
     * @return a preconfigured {@link Marshaller}
     * 
     * @throws IOException in case no {@link Marshaller} could get created
     */
    public static Marshaller createMarshaller(JAXBContext ctx, boolean formatted, boolean fragment)
            throws IOException {
        return createMarshaller(ctx, Charset.defaultCharset(), formatted, fragment);
    }

    /**
     * Creates a {@link Marshaller} based on the given {@link JAXBContext}
     * and configures it to use the given {@link Charset}, and allows to
     * output the XML code to be generated formatted and as an XML fragment.
     * 
     * @param ctx the {@link JAXBContext} to create a {@link Marshaller} for
     * @param charset the {@link Charset} the XML code should be formatted
     * @param formatted {@code true} if the XML code should be formatted,
     *       {@code false} otherwise
     * @param fragment {@code false} if the XML code should start with
     *       {@code <?xml }, or {@code true} if just fragment XML code should
     *       get generated
     * 
     * @return a preconfigured {@link Marshaller}
     * 
     * @throws IOException in case no {@link Marshaller} could get created
     */
    public static Marshaller createMarshaller(JAXBContext ctx, Charset charset, boolean formatted, boolean fragment)
            throws IOException {
        if (charset == null) {
            return createMarshaller(ctx, Charset.defaultCharset(), formatted, fragment);
        }
        Objects.requireNonNull(ctx);
        try {
            Marshaller marshaller = ctx.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
            return marshaller;
        } catch (JAXBException e) {
            throw new IOException(e);
        }
    }
}