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: */package org.griphyn.cPlanner.provenance.pasoa.producer;
015:
016: import org.griphyn.cPlanner.provenance.pasoa.XMLProducer;
017:
018: import java.io.*;
019:
020: /**
021: * An implementation of the XMLProducer interface backed by a StringBuffer.
022: * It does not check for any wellformedness of the XML. It is basically a
023: * data store.
024: *
025: * @author Karan Vahi
026: * @version $Revision: 241 $
027: */
028: public class InMemory implements XMLProducer {
029:
030: /**
031: * The StringBuffer store.
032: */
033: private StringBuffer mStore;
034:
035: /**
036: * The initial size of the buffer.
037: */
038: private int mSize;
039:
040: /**
041: * The default constructor.
042: */
043: public InMemory() {
044: mSize = 32;
045: reset();
046: }
047:
048: /**
049: * The overloaded constructor.
050: *
051: * @param size the intial number of characters it can store.
052: */
053: public InMemory(int size) {
054: mSize = size;
055: reset();
056: }
057:
058: /**
059: * Adds to the internal XML representation.
060: *
061: * @param xml the XML fragment to be added.
062: *
063: */
064: public void add(String xml) {
065: mStore.append(xml);
066: }
067:
068: /**
069: * Clears the internal state.
070: *
071: *
072: */
073: public void clear() {
074: reset();
075: }
076:
077: /**
078: * Returns the xml description of the object.
079: *
080: * @param writer is a Writer opened and ready for writing. This can also
081: * be a StringWriter for efficient output.
082: * @throws IOException if something fishy happens to the stream.
083: */
084: public void toXML(Writer writer) throws IOException {
085: writer.write(mStore.toString());
086: }
087:
088: /**
089: * Returns the interaction assertions as a XML blob.
090: *
091: * @return String
092: * @throws IOException if something fishy happens to the stream.
093: */
094: public String toXML() throws IOException {
095: Writer writer = new StringWriter(mSize);
096: toXML(writer);
097: return writer.toString();
098:
099: }
100:
101: /**
102: * Resets the internal store.
103: */
104: private void reset() {
105: mStore = new StringBuffer(mSize);
106: }
107: }
|