Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Node;
import javax.xml.bind.*;
import java.io.*;

import java.util.Hashtable;

public class Main {
    private static Hashtable<String, JAXBContext> contextbuf = new Hashtable<String, JAXBContext>();

    public static void exportXmlJAXB(Object obj, Class<?> clazz, File file) throws JAXBException, IOException {
        exportXmlJAXB(obj, new Class<?>[] { clazz }, file);
    }

    public static void exportXmlJAXB(Object obj, Class<?> clazz, Node node) throws JAXBException, IOException {
        exportXmlJAXB(obj, new Class<?>[] { clazz }, node);
    }

    public static void exportXmlJAXB(Object obj, Node node) throws JAXBException, IOException {
        exportXmlJAXB(obj, new Class<?>[] { obj.getClass() }, node);
    }

    public static void exportXmlJAXB(Object obj, OutputStream out) throws JAXBException, IOException {
        if (obj != null) {
            exportXmlJAXB(obj, obj.getClass(), out);
        }
    }

    public static void exportXmlJAXB(Object obj, Class<?> clazz, OutputStream out)
            throws JAXBException, IOException {
        exportXmlJAXB(obj, new Class<?>[] { clazz }, out);
    }

    public static void exportXmlJAXB(Object obj, Class<?> clazz, String filename)
            throws JAXBException, IOException {
        exportXmlJAXB(obj, clazz, new File(filename));
    }

    /**
     * @param obj
     * @param clazz
     * @param file
     * @throws JAXBException
     * @throws IOException   ((com.zte.cmre.api.Resource)((com.zte.cmdt.base.cmdset.CmdSetCmd
     *                       )((com.zte.cmdt.base.cmdset.CmdSet)((java.util.ArrayList)((
     *                       com.zte.ums.womcn.cmdt.base.cmdset.CmdSetModel)obj).cmdsets).
     *                       get(0)).getCmds().get(3)).getLabel()).getLabel(0).getBytes()
     */
    public static void exportXmlJAXB(Object obj, Class<?>[] clazz, File file) throws JAXBException, IOException {
        OutputStream os = new FileOutputStream(file);
        exportXmlJAXB(obj, clazz, os);
        os.close();
    }

    /**
     * @param obj
     * @param clazz
     * @param node
     * @throws JAXBException
     * @throws IOException
     */
    // @SuppressWarnings("unused")
    public static void exportXmlJAXB(Object obj, Class<?>[] clazz, Node node) throws JAXBException, IOException {
        JAXBContext jc = getJAXBContext(clazz);
        Marshaller m = jc.createMarshaller();
        setMarshallerProperty(m).marshal(obj, node);
    }

    public static void exportXmlJAXB(Object obj, Class<?>[] clazz, OutputStream out)
            throws JAXBException, IOException {
        JAXBContext jc = getJAXBContext(clazz);
        Marshaller m = jc.createMarshaller();
        // XMLWriter writer = new XMLWriter(new OutputWriter(out));
        setMarshallerProperty(m).marshal(obj, out);
    }

    /**
     * @param obj
     * @param clazz
     * @param filename
     * @throws JAXBException
     * @throws IOException
     */
    public static void exportXmlJAXB(Object obj, Class<?>[] clazz, String filename)
            throws JAXBException, IOException {
        exportXmlJAXB(obj, clazz, new File(filename));
    }

    private static JAXBContext getJAXBContext(Class<?>[] clazzs) throws JAXBException {
        if (clazzs != null) {
            StringBuilder sb = new StringBuilder();
            for (Class<?> c : clazzs) {
                sb.append(c.getName()).append(",");
            }
            String str = sb.toString();
            if (contextbuf.containsKey(str)) {
                return contextbuf.get(str);
            } else {
                JAXBContext c = JAXBContext.newInstance(clazzs);
                contextbuf.put(str, c);
                return c;
            }
        } else {
            throw new JAXBException(new NullPointerException("Class"));
        }

    }

    /**
     * @param m
     * @param encoding
     * @return
     * @throws PropertyException
     */
    private static Marshaller setMarshallerProperty(Marshaller m) throws PropertyException {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // set Output charset to project's charset;
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        return m;
    }
}