001: /*
002: * 01/07/2003 - 15:19:32
003: *
004: * PAutomaton.java -
005: * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
006: * ralf.meyer@karneim.com
007: * http://jrexx.sf.net
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023: package com.tc.jrexx.regex;
024:
025: import com.tc.jrexx.set.*;
026:
027: import java.util.*;
028: import java.io.*;
029:
030: public class PAutomaton extends SAutomaton {
031: private static final HashMap AUTOMATON_MAP = new HashMap();
032:
033: protected PAutomaton(Automaton_Pattern automaton) {
034: super (automaton);
035: }
036:
037: protected AutomatonSet_String getAutomaton() {
038: return this .automaton;
039: }
040:
041: public PAutomaton() {
042: super (new Automaton_Pattern());
043: }
044:
045: public PAutomaton(String regEx) {
046: super (new Automaton_Pattern(regEx));
047: }
048:
049: public PAutomaton(FSAData data) {
050: super (new Automaton_Pattern());
051: this .init(data);
052: }
053:
054: public PAutomaton(InputStream automatonDataStream)
055: throws java.io.IOException, ClassNotFoundException {
056: super (new Automaton_Pattern());
057: // this.init((FSAData)automatonDataStream.readObject());
058: this .init(toFSAData(new ObjectInputStream(automatonDataStream)
059: .readObject()));
060: try {
061: ObjectInputStream oin = new ObjectInputStream(
062: automatonDataStream) {
063: /**
064: * The readStreamHeader method is provided to allow subclasses to
065: * read and verify their own stream headers. It reads and
066: * verifies the magic number and version number.
067: *
068: * @throws IOException if there are I/O errors while reading from the
069: * underlying <code>InputStream</code>
070: * @throws StreamCorruptedException if control information in the
071: * stream is inconsistent
072: */
073: protected void readStreamHeader() throws IOException,
074: StreamCorruptedException {
075: short incoming_magic = 0;
076: short incoming_version = 0;
077: // try {
078: incoming_magic = readShort();
079: incoming_version = readShort();
080: // } catch (EOFException e) {
081: // throw new StreamCorruptedException("Caught EOFException " +
082: // "while reading the stream header");
083: // }
084: if (incoming_magic != STREAM_MAGIC)
085: throw new StreamCorruptedException(
086: "InputStream does not contain a serialized object");
087:
088: if (incoming_version != STREAM_VERSION)
089: throw new StreamCorruptedException(
090: "Version Mismatch, Expected "
091: + STREAM_VERSION + " and got "
092: + incoming_version);
093: }
094: };
095: ((Automaton_Pattern) this .automaton).regEx = (String) oin
096: .readObject();
097: } catch (EOFException e) {
098:
099: }
100: }
101:
102: /**
103: * writes this.toData() to the automatonDataStream and appends this.getRegEx() to the automatonDataStream.
104: */
105: public void toData(OutputStream automatonDataStream)
106: throws IOException {
107: super .toData(automatonDataStream);
108: ObjectOutputStream oOut = new ObjectOutputStream(
109: automatonDataStream) {
110: /**
111: * The writeStreamHeader method is provided so subclasses can
112: * append or prepend their own header to the stream.
113: * It writes the magic number and version to the stream.
114: *
115: * @throws IOException if I/O errors occur while writing to the underlying
116: * stream
117: */
118: protected void writeStreamHeader() throws IOException {
119: writeShort(STREAM_MAGIC);
120: writeShort(STREAM_VERSION);
121: }
122: };
123:
124: oOut.writeObject(this .getRegEx());
125: }
126:
127: public void addAll(String regEx) {
128: ((Automaton_Pattern) this .automaton).addAll(regEx);
129: }
130:
131: public void retainAll(String regEx) {
132: ((Automaton_Pattern) this .automaton).retainAll(regEx);
133: }
134:
135: public void removeAll(String regEx) {
136: ((Automaton_Pattern) this .automaton).removeAll(regEx);
137: }
138:
139: public String getRegEx() {
140: return ((Automaton_Pattern) this.automaton).regEx;
141: }
142: }
|