001package org.apache.commons.digester3.examples.xmlrules.addressbook;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */ 
019
020import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
021
022import java.io.File;
023import java.io.IOException;
024
025import org.apache.commons.digester3.Digester;
026import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
027import org.xml.sax.SAXException;
028
029/**
030 * A simple program to demonstrate the basic functionality of the
031 * Commons Digester module with the xmlrules extension.
032 * <p>
033 * This code will parse the provided "example.xml" file to build a tree
034 * of java objects, then cause those objects to print out their values
035 * to demonstrate that the input file has been processed correctly.
036 * <p>
037 * Unlike the "addressbook" example in the "api" section, this implementation
038 * has no parsing rules hard-wired into the code in this class. Instead, the
039 * parsing rules are loaded from an external file at runtime. This allows
040 * the parsing rules to be modified without compiling the code, and 
041 * potentially makes it somewhat easier to review the parsing rules.
042 * <p>
043 * Note, however, that there is tyically quite a tight coupling between
044 * the parsing rules and the <i>purpose</i> of the application which means
045 * that it may not be all that common for parsing rules to be altered
046 * without the application code also being altered, so only in some cases
047 * will this prove of benefit. As with all software, it must be determined
048 * whether this feature provides a true benefit in the context of the 
049 * application it is being applied to.
050 * <p>
051 * Usage: java Main xmlrules.xml example.xml
052 */
053public class Main
054{
055
056    /**
057     * Main method : entry point for running this example program.
058     * <p>
059     * Usage: java Example example.xml
060     */
061    public static void main( String[] args )
062        throws Exception
063    {
064        if ( args.length != 2 )
065        {
066            usage();
067            System.exit( -1 );
068        }
069
070        final String rulesfileName = args[0];
071        String datafileName = args[1];
072
073        // Create a Digester instance which has been initialised with
074        // rules loaded from the specified file.
075        Digester d = newLoader( new FromXmlRulesModule()
076        {
077
078            @Override
079            protected void loadRules()
080            {
081                loadXMLRules( rulesfileName );
082            }
083
084        } ).newDigester();
085
086        // Prime the digester stack with an object for rules to
087        // operate on. Note that it is quite common for "this"
088        // to be the object pushed.
089        AddressBook book = new AddressBook();
090        d.push( book );
091
092        // Process the input file.
093        try
094        {
095            File srcfile = new java.io.File( datafileName );
096            d.parse( srcfile );
097        }
098        catch ( IOException ioe )
099        {
100            System.out.println( "Error reading input file:" + ioe.getMessage() );
101            System.exit( -1 );
102        }
103        catch ( SAXException se )
104        {
105            System.out.println( "Error parsing input file:" + se.getMessage() );
106            System.exit( -1 );
107        }
108
109        // Print out all the contents of the address book, as loaded from
110        // the input file.
111        book.print();
112    }
113
114    private static void usage()
115    {
116        System.out.println( "Usage: java Main xmlrules.xml example.xml" );
117    }
118
119}