import GCF.*;
import java.net.URL;
/**
 * Class to show the use of the GXL Converter Framework.
 * It is shown how to initialize the GCF using the default impelementation 1 or 
 * default implementation 2.
 */
public class Demo extends Object {

    /**
     * Recursive method to traverse the syntax tree of a document.
     * The attribute depth is necessary to format the output.
     * It will be used to visualize the syntax tree of the source document using
     * the default implementation 2 of the GCF.
     */
    public static void printTree (int depth, DefaultImpl2.GXLStandardAPI tree) {
        // insert some blanks depending on the current depth of the document tree
        // and output the type of the current node
        for (int i=1; i<depth; i++) DefaultImpl2.GXLOutputAPI.write ("   ");
        if (depth>0) DefaultImpl2.GXLOutputAPI.write (" |-");
        DefaultImpl2.GXLOutputAPI.writeln (tree.toString());
        
        java.util.Iterator it=null;
        // iterate the Vector of the child nodes and print the subtree
        try {
            it = ((java.util.Vector)tree.getChildElements()).iterator();
        }
        catch (java.lang.NullPointerException npe) {
                    DefaultImpl2.GXLOutputAPI.writeln ("Error here"+tree.toString());
        }
        if (it!= null) 
        while (it.hasNext()) {
            Object next = it.next();
            if (next instanceof DefaultImpl2.GXLStandardAPI) {
                printTree (depth+1,(DefaultImpl2.GXLStandardAPI)next);        
            }
            // If the Object doesn't derive from DefaultImpl2.GXLStandardAPI it must 
            // be a simple type (float, int, string, boo, enum).
            // so catch the ClassCastException and simply output "SimpleType"
            else {
               for (int i=1; i<depth+1; i++) DefaultImpl2.GXLOutputAPI.write ("   ");
                DefaultImpl2.GXLOutputAPI.writeln (" |-SimpleType");
            }
        }
    }

    /**
     * Main method.
     */
    public static void main (String[] args) {

        try {
            
            // -------------------------------------------------------
            // Initialize the GCF and use the default implementation 1
            // -------------------------------------------------------
            
            // create the GXLObject
            GCF.GXLConverterAPI.createGXL(new DefaultImpl1.GXLGXLAPIImpl());
            // set the URL where the default implementation 1 is located
            GCF.GXLConverterAPI.setImplementationURL(
                new URL ("file:///C:/Programme/Programmierung/Java/Forte4j/Projects/GCF/"));
            // set the package name of the default implementation 1
            GCF.GXLConverterAPI.setPackageName("DefaultImpl1");
            // IMPORTANT : 1st create the GXLConnector...
            GCF.GXLConverterAPI.createConnector(GCF.GXLConverterAPI.CONNECTOR);
            // ...and then the GXLDocumentHandler !!!
            GCF.GXLConverterAPI.createDocumentHandler();

            // optionally create an outputFile, the default output
            // stream is System.out
            DefaultImpl1.GXLOutputAPI.createOutputFile ("Projects\\Demo.xml");

            // now parse the file given in the command line 
            // or parse gxldemo.xml if no file was specified...
            if (args.length<=0)
                GCF.GXLConverterAPI.parse("Projects\\GCF\\GXL-Bsp\\gxldemo.xml");
            else GCF.GXLConverterAPI.parse (args[0]);

            // close the opened outputStream
            DefaultImpl1.GXLOutputAPI.closeOutputFile();
            
            
            // -------------------------------------------------------
            // Initialize the GCF but use the default implementation 2
            // this time.
            // Thus the syntax tree of the result document is created.
            // The method outputTree() visualizes if the conversion was
            // successful.
            // -------------------------------------------------------
            
            // change the result object to GXLGXLAPIImpl (of the package DefaultImpl2 !!)
            GCF.GXLConverterAPI.createGXL(new DefaultImpl2.GXLGXLAPIImpl());
           
            // change the package to DefaultImpl2
            GCF.GXLConverterAPI.setPackageName("DefaultImpl2");
            
            // again create the GXLConnector and the GXLDocumentHandler
            GCF.GXLConverterAPI.createConnector(GCF.GXLConverterAPI.CONNECTOR);
            GCF.GXLConverterAPI.createDocumentHandler();
            
            // now parse the file given in the command line 
            // or parse gxldemo.xml if no file was specified...
            if (args.length<=0)
                GCF.GXLConverterAPI.parse("Projects\\GCF\\GXL-Bsp\\gxldemo.xml");
            else GCF.GXLConverterAPI.parse (args[0]);

            // create an output file
            DefaultImpl2.GXLOutputAPI.createOutputFile ("Projects\\Demo.txt");
            
            // output the result Tree of the parsed Object
            DefaultImpl2.GXLOutputAPI.writeln ();
            DefaultImpl2.GXLOutputAPI.writeln ("The document tree :");
            DefaultImpl2.GXLOutputAPI.writeln ("-------------------");
            DefaultImpl2.GXLOutputAPI.writeln ();
            printTree (0,(DefaultImpl2.GXLStandardAPI)GCF.GXLConverterAPI.getResultObject());
            
            // close the output file
            DefaultImpl2.GXLOutputAPI.closeOutputFile();
            
            
        }
        catch (java.net.MalformedURLException mfue) {}
    }
} // public class Demo extends Object