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