com.tera.common.dataload.xml.XMLDataLoadService.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.common.dataload.xml.XMLDataLoadService.java

Source

/**
 * This file is part of tera-api.
 * 
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.common.dataload.xml;

import java.io.File;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tera.common.data.CombinableContainer;
import com.tera.common.dataload.DataLoadService;
import com.tera.common.dataload.DataLoadUtil;
import com.tera.common.util.JaxbUtil;

/**
 * @author ATracer
 * @param <T>
 */
public abstract class XMLDataLoadService<T extends CombinableContainer<T>> implements DataLoadService<T> {

    private static final Logger log = LoggerFactory.getLogger(XMLDataLoadService.class);

    /**
     * @param directory
     * @param clazz
     * @param xsdLocation
     * @param classLoader
     * @return
     */
    protected final T loadFrom(String directory, String clazz, String xsdLocation, ClassLoader classLoader) {
        if (directory.startsWith("vc:")) {
            directory = directory.replaceFirst("vc:", "");
            return loadFromVirtual(directory, clazz, xsdLocation, classLoader);
        }
        return loadFromReal(directory, clazz, xsdLocation, classLoader);
    }

    /**
     * @param directory
     * @param clazz
     * @param xsdLocation
     * @return
     */
    protected final T loadFromReal(String directory, String clazz, String xsdLocation, ClassLoader classLoader) {
        try {
            Unmarshaller un = JaxbUtil.create(clazz, classLoader, classLoader.getResource(xsdLocation));
            File dir = new File(directory);
            T targetContainer = loadFromRealDirectory(un, dir);
            return targetContainer;
        } catch (Exception e) {
            log.error("Exception during unmarshalling", e);
        }
        return null;
    }

    /**
     * For all files in specified directory recursively unmarshall all xml's into
     * one object
     * 
     * @param un
     * @param dir
     * @return
     * @throws JAXBException
     */
    private T loadFromRealDirectory(Unmarshaller un, File dir) throws JAXBException {
        T container = null;
        for (File file : FileUtils.listFiles(dir, new String[] { "xml" }, true)) {
            @SuppressWarnings("unchecked")
            T data = (T) un.unmarshal(file);
            if (container == null) {
                container = data;
            } else {
                container.combine(data);
            }
        }
        return container;
    }

    /**
     * @param directory
     * @param clazz
     * @param xsdLocation
     * @param classLoader
     * @return
     */
    @SuppressWarnings("unchecked")
    protected final T loadFromVirtual(String directory, String clazz, String xsdLocation, ClassLoader classLoader) {
        Object loadedTemplate = DataLoadUtil.loadFromVContext(directory, clazz, xsdLocation, classLoader);
        return (T) loadedTemplate;
    }

}