PropertiesBindingFactoryImpl.java :  » XML » X2JB » com » x2jb » bind » provider » Java Open Source

Java Open Source » XML » X2JB 
X2JB » com » x2jb » bind » provider » PropertiesBindingFactoryImpl.java
/*
 * XML 2 Java Binding (X2JB) - the excellent Java tool.
 * Copyright 2007, by Richard Opalka.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not see the FSF site:
 * http://www.fsf.org/ and search for the LGPL License document there.
 */
package com.x2jb.bind.provider;

import java.lang.reflect.Method;
import org.x2jb.bind.BindingException;
import org.x2jb.bind.provider.BindingFactory;
import org.x2jb.bind.provider.BindingDefinition;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Binding factory extension for Java resource bundles
 *
 * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
 * @version 1.0
 */
public final class PropertiesBindingFactoryImpl implements BindingFactory {

    private static final char DOT = '.';
    private static final char SEPARATOR = '/';
    private static final String BINDING_SUFFIX = ".binding";
    private static final String NAMESPACE_SUFFIX = ".nodeNamespace";
    private static final String NAME_SUFFIX = ".nodeName";
    private static final String IS_ELEMENT_SUFFIX = ".isElementNode";
    private static final String IS_UNIQUE_SUFFIX = ".isNodeUnique";
    private static final String IS_MANDATORY_SUFFIX = ".isNodeMandatory";
    private static final String TYPE_HANDLER_SUFFIX = ".typeHandler";

    private static BindingDefinition getBinding( Properties bindingProperties, Method method )
    throws BindingException {
        if ( bindingProperties == null ) return null;

        return convertToBinding( method.getName(), bindingProperties );
    }

    private static boolean toBoolean( String b ) {
        return ( b == null ) ? true : Boolean.valueOf( b ).booleanValue();
    }
    
    private static BindingDefinition convertToBinding( String prefix, Properties properties )
    throws BindingException {
        String namespace = properties.getProperty( prefix + NAMESPACE_SUFFIX );
        String name = properties.getProperty( prefix + NAME_SUFFIX );
        String isElement = properties.getProperty( prefix + IS_ELEMENT_SUFFIX );
        String isUnique = properties.getProperty( prefix + IS_UNIQUE_SUFFIX );
        String isMandatory = properties.getProperty( prefix + IS_MANDATORY_SUFFIX );
        String typeHandler = properties.getProperty( prefix + TYPE_HANDLER_SUFFIX );

        if ( ( namespace == null ) && ( name == null ) && ( isElement == null )
            && ( isMandatory == null ) && ( typeHandler == null ) && ( isUnique == null ) ) {
            return null;
        }

        return new PropertiesBindingImpl(
            namespace, name, toBoolean( isElement ), toBoolean( isMandatory ), toBoolean( isUnique ), typeHandler
        );
    }
    
    private static Properties getBindingProperties( Class clazz ) throws BindingException {
        String bindingResourceName = SEPARATOR + clazz.getName().replace( DOT, SEPARATOR ) + BINDING_SUFFIX;
        InputStream bindingResource = clazz.getResourceAsStream( bindingResourceName );
        if ( bindingResource == null ) return null;

        Properties bindingProperties = new Properties();
        try {
            bindingProperties.load( bindingResource );
        } catch ( IOException ioe ) {
            throw new BindingException( ioe.getMessage(), ioe );
        } finally {
            try { bindingResource.close(); } catch ( IOException ignore ) {}
        }

        return bindingProperties;
    }

    public final BindingDefinition getBinding( Method method ) throws BindingException {
        return getBinding( getBindingProperties( method.getDeclaringClass() ), method );
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.