ジャナ・ビジネス・コンサルティング有限会社トップページへ
/*
 * Berkeley DB XML .NET API
 * 
 * Copyright (C) 2007 Jana Business Consulting Ltd. All rights reserved.
 * 
 * For more information, see http://www.janabiz.com.
 * 
 */

using System;
using JanaBiz.Db;
using JanaBiz.DbXml;
using JanaBiz.DbXml.Exceptions;

public class SimpleXmlEventReader
{
    private static string theContainer = "namespaceExampleData.dbxml";

    private static void eventReadDocument(XmlContainer container, XmlUpdateContext uc)
    {
        XmlDocument doc = container.GetDocument ("ChineseBeans.xml");
        
        Console.WriteLine("The follewing is the content of 'ChineseBeans.xml':");
        Console.WriteLine(doc.StringContent);
        Console.WriteLine("");
        
        XmlEventReader eventReader = doc.ContentAsEventReader ;
        
        while(eventReader.HasNext ())
        {
            XmlEventType type = eventReader.Next ();

            String typeStr = type.ToString();
            while(typeStr.Length < 15)
            {
                typeStr = typeStr + " ";
            }
            typeStr = "[" + typeStr + "]";
            Console.Write(typeStr);
            
            switch (type)
            {
                case XmlEventType.StartDocument:
                    Console.WriteLine("Version=" + eventReader.Version + 
                        ";SystemId=" + eventReader.SystemId + 
                        ";StandaloneSet=" + eventReader.StandaloneSet +
                        ";IsStandalone=" + eventReader.IsStandalone
                        );
                    break;
                case XmlEventType.StartElement:
                    Console.WriteLine("LocalName=" + eventReader.LocalName);
                    Console.WriteLine("			                  AttributeCount=" + eventReader.AttributeCount);
                    for (int i = 0; i < eventReader.AttributeCount; i++)
                    {
                        Console.WriteLine("			                  Attribute['" + eventReader.GetAttributeLocalName(i) + "']=" +
                            eventReader.GetAttributeValue(i) + ";");
                    }
                    break;
                case XmlEventType.StartEntityReference:
                    Console.WriteLine("Value=" + eventReader.Value +";");
                    break;
                case XmlEventType.EndEntityReference:
                    Console.WriteLine("Value=" + eventReader.Value +";");
                    break;
                case XmlEventType.CDATA:
                    Console.WriteLine("Value=" + eventReader.Value + ";");
                    break;
                case XmlEventType.Characters:
                    Console.WriteLine("Value=" + eventReader.Value + ";");
                    break;
                case XmlEventType.Comment:
                    Console.WriteLine("Value=" + eventReader.Value + ";");
                    break;
                case XmlEventType.DTD:
                    Console.WriteLine("Value=" + eventReader.Value + ";");
                    break;
                case XmlEventType.EndElement:
                    Console.WriteLine("LocalName=" + eventReader.LocalName + ";");
                    break;
                case XmlEventType.ProcessingInstruction:
                    Console.WriteLine("Value=" + eventReader.Value  +";" );
                    break;
                case XmlEventType.Whitespace:
                    Console.WriteLine("" );
                    break;
                case XmlEventType.EndDocument:
                    break;
                default:
                    Console.WriteLine("Not Found!");
                    break;
            }
        }
    }

    public static void Main(string[] args)
    {
        string envdir = parseArguments(args);

        DbEnvironment dbEnv = null;
        XmlManager mgr = null;
        XmlContainer container = null;

        try 
        {
            // Open an environment 
            dbEnv = CreateDbEnvironment(envdir);

            // Open a manager
            using (mgr = CreateManager(dbEnv)) 
            {
                // Open a transactional container
                XmlContainerConfig containerconfig = new XmlContainerConfig();
                containerconfig.Transactional = true;
                using (container = mgr.OpenContainer(null, theContainer,
                    containerconfig)) 
                {
                    // Start a transaction
                    using (XmlTransaction txn = mgr.CreateTransaction()) 
                    {
                        // Create an update context
                        using (XmlUpdateContext uc = mgr.CreateUpdateContext()) 
                        {
                            eventReadDocument(container,uc);
                            
                            // Commit the index adds
                            txn.Commit();
                        }
                    }
                }
            }
        } 
        catch (DbXmlException e) 
        {
            Console.WriteLine("Error read event from container " + theContainer);
            Console.WriteLine(e.ToString());
        } 
        finally 
        {
            if (container != null) 
            {
                container.Close();
            }
            if (mgr != null) 
            {
                mgr.Close();
            }
            if (dbEnv != null) 
            {
                dbEnv.Close();
            }
        }
    }

    public static DbEnvironment CreateDbEnvironment(string envdir)
    {
        DbEnvironmentConfig envconf = new DbEnvironmentConfig();
        envconf.CacheSize = 50*1024;
        envconf.AllowCreate = true;
        envconf.InitializeCache = true;
        envconf.Transactional = true;
        envconf.InitializeLocking = true;
        envconf.InitializeLogging = true;
        envconf.RunRecovery = true;
        envconf.ErrorWriter = Console.Out;
        envconf.ErrorPrefix = "LOG";

        XmlManagerConfig mgrconfig = new XmlManagerConfig();
        mgrconfig.AdoptEnvironment = true;

        DbEnvironment env = new DbEnvironment(envdir, envconf);
        return env;
    }

    public static XmlManager CreateManager(DbEnvironment env)
    {
        XmlManagerConfig mgrconfig = new XmlManagerConfig();
        mgrconfig.AdoptEnvironment = true;

        try 
        {
            return new XmlManager(env, mgrconfig);
        } 
        catch (Exception e) 
        {
            env.Close();
            throw e;
        }
    }

    private static void Usage()
    {
        Console.WriteLine("This program adds several indexes to a DBXML container.");
        Console.WriteLine("You should run exampleLoadContainer before running this example.");
        Console.WriteLine("You are only required to pass this command the path location of the database");
        Console.WriteLine("environment that you specified when you loaded the examples data:");
        Console.WriteLine();
        Console.WriteLine("\t-h <dbenv directory>");
        Console.WriteLine("For example:");
        Console.WriteLine("\taddIndex.exe -h examplesEnvironment");

        Environment.Exit(-1);
    }

    private static string parseArguments(string[] args)
    {
        string envdir = null;
        for (int i = 0; i < args.Length; ++i) 
        {
            string arg = args[i];
            if ((arg.StartsWith("-")
#if WIN32
                || arg.StartsWith("/")
#endif
                ) && arg.Length > 1) 
            {
                switch (arg[1]) 
                {
                    case 'h':
                    {
                        ++i;
                        if (i >= args.Length) 
                        {
                            Console.WriteLine("Invalid option: " + arg);
                            Usage();
                        }
                        envdir = args[i];
                        break;
                    }
                    default:
                    {
                        Console.WriteLine("Unknown option: " + arg);
                        Usage();
                        break;
                    }
                }
            } 
            else 
            {
                Console.WriteLine("Too many arguments: " + arg);
                Usage();
            }
        }
        if (envdir == null) 
        {
            Console.WriteLine("Environment directory not found.");
            Usage();
        }
        return envdir;
    }
}