001: /*
002: * XML 2 Java Binding (X2JB) - the excellent Java tool.
003: * Copyright 2007, by Richard Opalka.
004: *
005: * This is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as
007: * published by the Free Software Foundation; either version 2.1 of
008: * the License, or (at your option) any later version.
009: *
010: * This software is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this software; if not see the FSF site:
017: * http://www.fsf.org/ and search for the LGPL License document there.
018: */
019: package com.x2jb.bind.provider;
020:
021: import java.lang.reflect.Method;
022: import org.x2jb.bind.BindingException;
023: import org.x2jb.bind.provider.BindingFactory;
024: import org.x2jb.bind.provider.BindingDefinition;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.util.Properties;
028:
029: /**
030: * Binding factory extension for Java resource bundles
031: *
032: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
033: * @version 1.0
034: */
035: public final class PropertiesBindingFactoryImpl implements
036: BindingFactory {
037:
038: private static final char DOT = '.';
039: private static final char SEPARATOR = '/';
040: private static final String BINDING_SUFFIX = ".binding";
041: private static final String NAMESPACE_SUFFIX = ".nodeNamespace";
042: private static final String NAME_SUFFIX = ".nodeName";
043: private static final String IS_ELEMENT_SUFFIX = ".isElementNode";
044: private static final String IS_UNIQUE_SUFFIX = ".isNodeUnique";
045: private static final String IS_MANDATORY_SUFFIX = ".isNodeMandatory";
046: private static final String TYPE_HANDLER_SUFFIX = ".typeHandler";
047:
048: private static BindingDefinition getBinding(
049: Properties bindingProperties, Method method)
050: throws BindingException {
051: if (bindingProperties == null)
052: return null;
053:
054: return convertToBinding(method.getName(), bindingProperties);
055: }
056:
057: private static boolean toBoolean(String b) {
058: return (b == null) ? true : Boolean.valueOf(b).booleanValue();
059: }
060:
061: private static BindingDefinition convertToBinding(String prefix,
062: Properties properties) throws BindingException {
063: String namespace = properties.getProperty(prefix
064: + NAMESPACE_SUFFIX);
065: String name = properties.getProperty(prefix + NAME_SUFFIX);
066: String isElement = properties.getProperty(prefix
067: + IS_ELEMENT_SUFFIX);
068: String isUnique = properties.getProperty(prefix
069: + IS_UNIQUE_SUFFIX);
070: String isMandatory = properties.getProperty(prefix
071: + IS_MANDATORY_SUFFIX);
072: String typeHandler = properties.getProperty(prefix
073: + TYPE_HANDLER_SUFFIX);
074:
075: if ((namespace == null) && (name == null)
076: && (isElement == null) && (isMandatory == null)
077: && (typeHandler == null) && (isUnique == null)) {
078: return null;
079: }
080:
081: return new PropertiesBindingImpl(namespace, name,
082: toBoolean(isElement), toBoolean(isMandatory),
083: toBoolean(isUnique), typeHandler);
084: }
085:
086: private static Properties getBindingProperties(Class clazz)
087: throws BindingException {
088: String bindingResourceName = SEPARATOR
089: + clazz.getName().replace(DOT, SEPARATOR)
090: + BINDING_SUFFIX;
091: InputStream bindingResource = clazz
092: .getResourceAsStream(bindingResourceName);
093: if (bindingResource == null)
094: return null;
095:
096: Properties bindingProperties = new Properties();
097: try {
098: bindingProperties.load(bindingResource);
099: } catch (IOException ioe) {
100: throw new BindingException(ioe.getMessage(), ioe);
101: } finally {
102: try {
103: bindingResource.close();
104: } catch (IOException ignore) {
105: }
106: }
107:
108: return bindingProperties;
109: }
110:
111: public final BindingDefinition getBinding(Method method)
112: throws BindingException {
113: return getBinding(getBindingProperties(method
114: .getDeclaringClass()), method);
115: }
116:
117: }
|