com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundleFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundleFactory.java

Source

/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.toolkit.util.resourcebundle.xml;

import java.io.InputStream;

import com.alibaba.toolkit.util.resourcebundle.AbstractResourceBundleFactory;
import com.alibaba.toolkit.util.resourcebundle.ResourceBundle;
import com.alibaba.toolkit.util.resourcebundle.ResourceBundleConstant;
import com.alibaba.toolkit.util.resourcebundle.ResourceBundleCreateException;
import com.alibaba.toolkit.util.resourcebundle.ResourceBundleLoader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
 * XML<code>ResourceBundle</code>.
 *
 * @author Michael Zhou
 * @version $Id: XMLResourceBundleFactory.java,v 1.1 2003/07/03 07:26:35 baobao
 *          Exp $
 */
public class XMLResourceBundleFactory extends AbstractResourceBundleFactory {
    /** factory, ?context class loaderbundle. */
    public XMLResourceBundleFactory() {
        super();
    }

    /**
     * factory, class loaderbundle.
     *
     * @param classLoader bundleclass loader
     */
    public XMLResourceBundleFactory(ClassLoader classLoader) {
        super(classLoader);
    }

    /**
     * factory, loaderbundle
     *
     * @param loader bundle
     */
    public XMLResourceBundleFactory(ResourceBundleLoader loader) {
        super(loader);
    }

    /**
     * ?bundle???resource??.
     *
     * @param bundleName bundle??
     * @return resource??
     */
    @Override
    protected String getFilename(String bundleName) {
        return super.getFilename(bundleName) + ResourceBundleConstant.RB_RESOURCE_EXT_XML;
    }

    /**
     * XML???, <code>ResourceBundle</code>.
     *
     * @param stream   ?
     * @param systemId ?
     * @return resource bundle
     * @throws ResourceBundleCreateException ?
     */
    @Override
    protected ResourceBundle parse(InputStream stream, String systemId) throws ResourceBundleCreateException {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(stream, systemId);

            return new XMLResourceBundle(doc);
        } catch (DocumentException e) {
            throw new ResourceBundleCreateException(ResourceBundleConstant.RB_FAILED_READING_XML_DOCUMENT,
                    new Object[] { systemId }, e);
        }
    }
}