001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.parser;
016:
017: import org.griphyn.vdl.classes.*;
018: import org.griphyn.vdl.util.*;
019: import org.xml.sax.*;
020: import java.util.List;
021: import java.util.ArrayList;
022:
023: /**
024: * This class adds a given Definition from the parser's callback into
025: * the already established in-memory storage.
026: *
027: * @author Jens-S. Vöckler
028: * @author Yong Zhao
029: * @version $Revision: 50 $
030: */
031: public class MemoryStorage implements DefinitionHandler {
032: /**
033: * This is a reference to the already established in-memory storage.
034: */
035: private Definitions m_memory;
036:
037: /**
038: * This determines the behavior: insert mode (false) or update mode (true)
039: */
040: private boolean m_overwrite;
041:
042: /**
043: * This variable determines whether to keep a list of rejects
044: */
045: private boolean m_dontcare;
046:
047: /**
048: * This is the list of rejects for insert, or old values for update.
049: */
050: private ArrayList m_rejects;
051:
052: /**
053: * The c'tor initializes the references to modify the in-memory database
054: * of definitions.
055: *
056: * @param definitions is a reference to the in-memory database.
057: * @param overwrite establishes insert or update mode.
058: */
059: public MemoryStorage(Definitions definitions, boolean overwrite,
060: boolean dontcare) {
061: if ((this .m_memory = definitions) == null)
062: throw new NullPointerException();
063: this .m_overwrite = overwrite;
064: this .m_dontcare = dontcare;
065: this .m_rejects = new ArrayList();
066: }
067:
068: /**
069: * Accessor: Provide an iterator to walk the rejects list.
070: * @return full access to the rejects list.
071: */
072: public java.util.List getRejects() {
073: return this .m_rejects;
074: }
075:
076: /**
077: * This method adds the given Definition to whatever storage is
078: * implemented underneath.
079: *
080: * @param d is the Definition that is ready to be stored.
081: * @return true, if new version was stored and database modified
082: */
083: public boolean store(Definition d) {
084: int position = this .m_memory.positionOfDefinition(d);
085: if (position != -1) {
086: // definition already exists
087: if (this .m_overwrite) {
088: Logging.instance().log("app", 2,
089: "Modifying " + d.shortID());
090: Definition old = this .m_memory.setDefinition(position,
091: d);
092: if (!this .m_dontcare)
093: this .m_rejects.add(old);
094: return true;
095: } else {
096: Logging.instance().log("app", 2,
097: "Rejecting " + d.shortID());
098: if (!this .m_dontcare)
099: this .m_rejects.add(d);
100: return false;
101: }
102: } else {
103: // definition does not exist
104: Logging.instance().log("app", 2, "Adding " + d.shortID());
105: this .m_memory.addDefinition(d);
106: return true;
107: }
108: }
109: }
|