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 org.x2jb.bind;
020:
021: import java.text.MessageFormat;
022: import org.x2jb.bind.Messages;
023: import org.x2jb.bind.Messages.BundleKey;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Document;
026:
027: /**
028: * <p><b>XML 2 Java Binding</b> core class. It binds DOM elements and documents to Java interfaces.
029: * Users use only this class to interact with <b>X2JB runtime</b>.</p>
030: *
031: * <p><b>All methods of this class are thread safe.</b></p>
032: *
033: * <p>
034: * Sample usage:
035: * </p>
036: *
037: * <p><blockquote><pre>
038: * DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
039: * // call the following method only when dealing with namespaced document
040: * // otherwise never call it
041: * builderFactory.setNamespaceAware( true );
042: * DocumentBuilder builder = builderFactory.newDocumentBuilder();
043: * Document parsedDocument = builder.parse(
044: * new FileInputStream(
045: * new File( "config.xml" ) ) );
046: * Config cfg = ( Config ) XML2Java.bind( parsedDocument, Config.class );
047: * // do something with cfg
048: * </pre></blockquote></p>
049: *
050: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
051: * @version 1.0
052: */
053: public final class XML2Java {
054:
055: /**
056: * Constructor
057: */
058: private XML2Java() {
059: // no instances
060: }
061:
062: /**
063: * Binds DOM element to Java interface.
064: * Returns proxy wrapping the element content tree.
065: * @param element DOM element which content will be wrapped
066: * @param iface interface which instance will be returned
067: * @return proxy implementing the interface and wrapping the element content tree
068: * @throws BindingException if a binding error occurs
069: */
070: public static Object bind(Element element, Class iface)
071: throws BindingException {
072: if ((element == null) || (iface == null)) {
073: // method parameters cannot be null
074: throw new IllegalArgumentException(MessageFormat.format(
075: Messages.get(BundleKey.NULL_METHOD_PARAMETERS),
076: new Object[] {}));
077: }
078:
079: return Assembler.bind(iface, element, null);
080: }
081:
082: /**
083: * Binds DOM document to Java interface.
084: * Returns proxy wrapping the document content tree.
085: * @param document DOM document which content will be wrapped
086: * @param iface interface which instance will be returned
087: * @return proxy implementing the interface and wrapping the document content tree
088: * @throws BindingException if a binding error occurs
089: */
090: public static Object bind(Document document, Class iface)
091: throws BindingException {
092: if ((document == null) || (iface == null)) {
093: // method parameters cannot be null
094: throw new IllegalArgumentException(MessageFormat.format(
095: Messages.get(BundleKey.NULL_METHOD_PARAMETERS),
096: new Object[] {}));
097: }
098:
099: return bind(document.getDocumentElement(), iface);
100: }
101:
102: }
|