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 defaultvalues;
020:
021: import defaultvalues.ifaces.DefaultValues;
022: import org.x2jb.bind.XML2Java;
023: import org.w3c.dom.Document;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: /**
029: * Default values sample
030: *
031: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
032: * @version 1.0
033: */
034: public final class Main {
035:
036: /**
037: * Lookups specified resource on the classpath and returns parsed document instance
038: * @param classpath resource to return
039: */
040: private static Document getDocument(String resource)
041: throws Exception {
042: Document retVal = null;
043: try {
044: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
045: .newInstance();
046: builderFactory.setIgnoringComments(true);
047: DocumentBuilder builder = builderFactory
048: .newDocumentBuilder();
049: retVal = builder.parse(Main.class
050: .getResourceAsStream(resource));
051: } catch (ParserConfigurationException e) {
052: e.printStackTrace(System.err);
053: System.exit(1);
054: }
055: return retVal;
056: }
057:
058: public static void main(String[] args) throws Exception {
059: Document parsedDocument = getDocument("/defaultvalues.xml");
060: DefaultValues defaultvalues = (DefaultValues) XML2Java.bind(
061: parsedDocument, DefaultValues.class);
062:
063: // Java primitives default values are identical to those specified in the Java Language Specification
064: System.out.println("Default boolean value is "
065: + defaultvalues.getBooleanPrimitive());
066: System.out.println("Default byte value is "
067: + defaultvalues.getBytePrimitive());
068: System.out.println("Default char value is "
069: + defaultvalues.getCharPrimitive());
070: System.out.println("Default short value is "
071: + defaultvalues.getShortPrimitive());
072: System.out.println("Default int value is "
073: + defaultvalues.getIntPrimitive());
074: System.out.println("Default long value is "
075: + defaultvalues.getLongPrimitive());
076: System.out.println("Default float value is "
077: + defaultvalues.getFloatPrimitive());
078: System.out.println("Default double value is "
079: + defaultvalues.getDoublePrimitive());
080: // Supported Java nonprimitives are always mapped to null. Exception to this rule are arrays
081: System.out.println("Default Boolean value is "
082: + defaultvalues.getBoolean());
083: System.out.println("Default Byte value is "
084: + defaultvalues.getByte());
085: System.out.println("Default Character value is "
086: + defaultvalues.getCharacter());
087: System.out.println("Default Short value is "
088: + defaultvalues.getShort());
089: System.out.println("Default Integer value is "
090: + defaultvalues.getInteger());
091: System.out.println("Default Long value is "
092: + defaultvalues.getLong());
093: System.out.println("Default Float value is "
094: + defaultvalues.getFloat());
095: System.out.println("Default Double value is "
096: + defaultvalues.getDouble());
097: System.out.println("Default String value is "
098: + defaultvalues.getString());
099: System.out.println("Default BigInteger value is "
100: + defaultvalues.getBigInteger());
101: System.out.println("Default BigDecimal value is "
102: + defaultvalues.getBigDecimal());
103: System.out.println("Default Element value is "
104: + defaultvalues.getElement());
105: // Array is always mapped to zero length array regardless the array component type
106: System.out.println("Default Boolean[] value is "
107: + defaultvalues.getBooleanArray().length
108: + " length array.");
109: }
110:
111: }
|