KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jrexx > regex > PAutomaton


1 /*
2 * 01/07/2003 - 15:19:32
3 *
4 * PAutomaton.java -
5 * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
6 * ralf.meyer@karneim.com
7 * http://jrexx.sf.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

23 package com.tc.jrexx.regex;
24
25 import com.tc.jrexx.set.*;
26
27 import java.util.*;
28 import java.io.*;
29
30 public class PAutomaton extends SAutomaton {
31   private static final HashMap AUTOMATON_MAP = new HashMap();
32
33   protected PAutomaton(Automaton_Pattern automaton) {
34     super(automaton);
35   }
36
37   protected AutomatonSet_String getAutomaton() {
38     return this.automaton;
39   }
40
41   public PAutomaton() {
42     super(new Automaton_Pattern());
43   }
44
45   public PAutomaton(String JavaDoc regEx) {
46     super(new Automaton_Pattern(regEx));
47   }
48
49   public PAutomaton(FSAData data) {
50     super(new Automaton_Pattern());
51     this.init(data);
52   }
53
54   public PAutomaton(InputStream automatonDataStream) throws java.io.IOException JavaDoc,ClassNotFoundException JavaDoc {
55     super(new Automaton_Pattern());
56 // this.init((FSAData)automatonDataStream.readObject());
57
this.init(toFSAData(new ObjectInputStream(automatonDataStream).readObject()));
58     try {
59       ObjectInputStream oin = new ObjectInputStream( automatonDataStream) {
60         /**
61          * The readStreamHeader method is provided to allow subclasses to
62          * read and verify their own stream headers. It reads and
63          * verifies the magic number and version number.
64          *
65          * @throws IOException if there are I/O errors while reading from the
66          * underlying <code>InputStream</code>
67          * @throws StreamCorruptedException if control information in the
68          * stream is inconsistent
69         */

70         protected void readStreamHeader()
71             throws IOException, StreamCorruptedException
72         {
73             short incoming_magic = 0;
74             short incoming_version = 0;
75 // try {
76
incoming_magic = readShort();
77                 incoming_version = readShort();
78 // } catch (EOFException e) {
79
// throw new StreamCorruptedException("Caught EOFException " +
80
// "while reading the stream header");
81
// }
82
if (incoming_magic != STREAM_MAGIC)
83                 throw new StreamCorruptedException("InputStream does not contain a serialized object");
84
85             if (incoming_version != STREAM_VERSION)
86                 throw new StreamCorruptedException("Version Mismatch, Expected " +
87                                                    STREAM_VERSION + " and got " +
88                                                    incoming_version);
89         }
90       };
91       ((Automaton_Pattern)this.automaton).regEx = (String JavaDoc)oin.readObject();
92     } catch(EOFException e) {
93
94     }
95   }
96
97   /**
98    * writes this.toData() to the automatonDataStream and appends this.getRegEx() to the automatonDataStream.
99    */

100   public void toData(OutputStream automatonDataStream) throws IOException {
101     super.toData(automatonDataStream);
102     ObjectOutputStream oOut = new ObjectOutputStream(automatonDataStream) {
103       /**
104        * The writeStreamHeader method is provided so subclasses can
105        * append or prepend their own header to the stream.
106        * It writes the magic number and version to the stream.
107        *
108        * @throws IOException if I/O errors occur while writing to the underlying
109        * stream
110        */

111       protected void writeStreamHeader() throws IOException {
112           writeShort(STREAM_MAGIC);
113           writeShort(STREAM_VERSION);
114       }
115     };
116
117     oOut.writeObject(this.getRegEx());
118   }
119
120   public void addAll(String JavaDoc regEx) {
121     ((Automaton_Pattern)this.automaton).addAll(regEx);
122   }
123
124   public void retainAll(String JavaDoc regEx) {
125     ((Automaton_Pattern)this.automaton).retainAll(regEx);
126   }
127
128   public void removeAll(String JavaDoc regEx) {
129     ((Automaton_Pattern)this.automaton).removeAll(regEx);
130   }
131
132   public String JavaDoc getRegEx() {
133     return ((Automaton_Pattern)this.automaton ).regEx;
134   }
135 }
Popular Tags