KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > AbstractGrammar


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.dsmlv2;
22
23
24 import java.io.IOException JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29
30
31 /**
32  * The abstract IGrammar which is the Mother of all the grammars. It contains
33  * the transitions table.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev$, $Date$
37  */

38 public abstract class AbstractGrammar implements IGrammar
39 {
40
41     /**
42      * Table of transitions. It's a two dimension array, the first dimension
43      * indice the states, the second dimension indices the Tag value, so it is
44      * 256 wide.
45      */

46     protected HashMap JavaDoc<Tag, GrammarTransition>[] transitions;
47
48     /** The grammar name */
49     protected String JavaDoc name;
50
51     /** The grammar's states */
52     protected IStates statesEnum;
53
54
55     /**
56      * Return the grammar's name
57      *
58      * @return The grammar name
59      */

60     public String JavaDoc getName()
61     {
62         return name;
63     }
64
65
66     /**
67      * Set the grammar's name
68      *
69      * @param name
70      * the name to set
71      */

72     public void setName( String JavaDoc name )
73     {
74         this.name = name;
75     }
76
77
78     /**
79      * Get the transition associated with the state and tag
80      *
81      * @param state
82      * The current state
83      * @param tag
84      * The current tag
85      * @return A valid transition if any, or null.
86      */

87     public GrammarTransition getTransition( int state, Tag tag )
88     {
89         return transitions[state].get( tag );
90     }
91
92
93     /**
94      * Get the states of the current grammar
95      *
96      * @return
97      * Returns the statesEnum.
98      */

99     public IStates getStatesEnum()
100     {
101         return statesEnum;
102     }
103
104
105     /**
106      * Set the states for this grammar
107      *
108      * @param statesEnum
109      * The statesEnum to set.
110      */

111     public void setStatesEnum( IStates statesEnum )
112     {
113         this.statesEnum = statesEnum;
114     }
115
116
117     /* (non-Javadoc)
118      * @see org.apache.directory.ldapstudio.dsmlv2.IGrammar#executeAction(org.apache.directory.ldapstudio.dsmlv2.Dsmlv2Container)
119      */

120     public void executeAction( Dsmlv2Container container ) throws XmlPullParserException, IOException JavaDoc
121     {
122         XmlPullParser xpp = container.getParser();
123
124         int eventType = xpp.getEventType();
125         do
126         {
127             if ( eventType == XmlPullParser.START_DOCUMENT )
128             {
129                 container.setState( Dsmlv2StatesEnum.INIT_GRAMMAR_STATE );
130             }
131             else if ( eventType == XmlPullParser.END_DOCUMENT )
132             {
133                 container.setState( Dsmlv2StatesEnum.END_STATE );
134             }
135             else if ( eventType == XmlPullParser.START_TAG )
136             {
137                 processTag( container, Tag.START );
138             }
139             else if ( eventType == XmlPullParser.END_TAG )
140             {
141                 processTag( container, Tag.END );
142             }
143             eventType = xpp.next();
144         }
145         while ( eventType != XmlPullParser.END_DOCUMENT );
146     }
147
148
149     /**
150      * Processes the task required in the grammar to the given tag type
151      *
152      * @param container
153      * the DSML container
154      * @param tagType
155      * the tag type
156      * @throws XmlPullParserException
157      * when an error occurs during the parsing
158      */

159     private void processTag( Dsmlv2Container container, int tagType ) throws XmlPullParserException
160     {
161         XmlPullParser xpp = container.getParser();
162
163         String JavaDoc tagName = xpp.getName().toLowerCase();
164
165         GrammarTransition transition = getTransition( container.getState(), new Tag( tagName, tagType ) );
166
167         if ( transition != null )
168         {
169             container.setState( transition.getNextState() );
170
171             if ( transition.hasAction() )
172             {
173                 transition.getAction().action( container );
174             }
175         }
176         else
177         {
178             throw new XmlPullParserException( "The tag " + new Tag( tagName, tagType )
179                 + " can't be found at this position", xpp, null );
180         }
181     }
182 }
183
Popular Tags