KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > dtd > schema > Dfm


1 /*******************************************************************************
2  * Copyright (c) 2002, 2005 Object Factory Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Object Factory Inc. - Initial implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.dtd.schema;
12
13 import org.eclipse.ant.internal.ui.dtd.IAtom;
14 import org.eclipse.ant.internal.ui.dtd.IDfm;
15 import org.eclipse.ant.internal.ui.dtd.util.Factory;
16 import org.eclipse.ant.internal.ui.dtd.util.FactoryObject;
17 import org.eclipse.ant.internal.ui.dtd.util.MapHolder;
18 import org.eclipse.ant.internal.ui.dtd.util.SortedMap;
19
20
21 /**
22  * Deterministic finite state machine.
23  * Once constructed DFM is immutable and can be used by multiple threads.
24  * A Dfm node is essentially an accepting flag and a hashtable mapping atoms to
25  * Dfm nodes. (Almost of org.eclipse.ant.internal.ui.dtd.util is aimed at reducing the storage
26  * overhead of hundreds of little hashtables.)
27  * @author Bob Foster
28  */

29 public class Dfm extends MapHolder implements IDfm, FactoryObject {
30
31     public boolean accepting;
32     public boolean empty, any;
33     public int id;
34     private static int unique = 0;
35     private static Factory factory = new Factory();
36     private Dfm fNext;
37     
38     public static Dfm dfm(boolean accepting) {
39         Dfm dfm = free();
40         dfm.accepting = accepting;
41         return dfm;
42     }
43
44     protected Dfm() {
45     }
46     
47     private static Dfm free() {
48         Dfm dfm = (Dfm) factory.getFree();
49         if (dfm == null)
50             dfm = new Dfm();
51         dfm.accepting = dfm.empty = dfm.any = false;
52         dfm.id = unique++;
53         return dfm;
54     }
55
56     public static Dfm dfm(IAtom accept, Dfm follow) {
57         Dfm dfm = free();
58         dfm.keys = new Object JavaDoc[1];
59         dfm.keys[0] = accept;
60         dfm.values = new Object JavaDoc[1];
61         dfm.values[0] = follow;
62         return dfm;
63     }
64     
65     public static void free(Dfm dfm) {
66         dfm.setKeys(null);
67         dfm.setValues(null);
68         factory.setFree(dfm);
69     }
70     
71     public boolean isAccepting() {
72         return accepting;
73     }
74     
75     public IDfm advance(String JavaDoc name) {
76         if (any)
77             return this;
78         if (empty)
79             return null;
80         if (keys == null)
81             return null;
82         SortedMap map = getIndirectStringMap(this);
83         Dfm dfm = (Dfm) map.get(name);
84         freeMap(map);
85         return dfm;
86     }
87     
88     public String JavaDoc[] getAccepts() {
89         if (keys == null)
90             return new String JavaDoc[0];
91         String JavaDoc[] s = new String JavaDoc[keys.length];
92         for (int i = 0; i < s.length; i++) {
93             s[i] = keys[i].toString();
94         }
95         return s;
96     }
97
98     public Dfm[] getFollows() {
99         if (values == null)
100             return new Dfm[0];
101         Dfm[] s = new Dfm[values.length];
102         System.arraycopy(values,0,s,0,values.length);
103         return s;
104     }
105
106     public void merge(Dfm other) {
107         accepting |= other.accepting;
108         SortedMap map = getIndirectStringMap(this);
109         SortedMap othermap = getIndirectStringMap(other);
110         map.merge(othermap);
111         freeMap(map);
112         freeMap(othermap);
113     }
114     
115     public SortedMap getMap() {
116         return getIndirectStringMap(this);
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.ant.internal.ui.dtd.util.FactoryObject#next()
121      */

122     public FactoryObject next() {
123         return fNext;
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.ant.internal.ui.dtd.util.FactoryObject#next(org.eclipse.ant.internal.ui.dtd.util.FactoryObject)
128      */

129     public void next(FactoryObject obj) {
130         fNext = (Dfm) obj;
131     }
132
133     /* (non-Javadoc)
134      * @see org.eclipse.ant.internal.ui.dtd.IDfm#isAny()
135      */

136     public boolean isAny() {
137         return any;
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.ant.internal.ui.dtd.IDfm#isEmpty()
142      */

143     public boolean isEmpty() {
144         return empty;
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.ant.internal.ui.dtd.IDfm#getAtom(java.lang.String)
149      */

150     public IAtom getAtom(String JavaDoc name) {
151         Object JavaDoc[] allKeys = getKeys();
152         if (empty || allKeys == null){
153             return null;
154         }
155         SortedMap map = getIndirectStringMap(this);
156         int index = map.keyIndex(name);
157         if (index < 0) {
158             return null;
159         }
160         return (IAtom) allKeys[index];
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.ant.internal.ui.dtd.IDfm#advance(java.lang.String, java.lang.String)
165      */

166     public IDfm advance(String JavaDoc namespace, String JavaDoc localname) {
167         // no namespace support here
168
return advance(localname);
169     }
170 }
Popular Tags