KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > verifier > Section


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.verifier;
23
24 /*
25  * Class org.jboss.verifier.Section
26  * Copyright (C) 2000 Juha Lindfors
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Lesser General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36  * Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public
39  * License along with this library; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41  *
42  * This package and its source code is available at www.jboss.org
43  * $Id: Section.java 37459 2005-10-30 00:04:02Z starksm $
44  */

45
46 // standard imports
47
import java.util.StringTokenizer JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.Collections JavaDoc;
50 import java.util.Arrays JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.text.ParseException JavaDoc;
53
54
55 /**
56  * Represents a section in the EJB spec.
57  *
58  * @author Juha Lindfors
59  * @version $Revision: 37459 $
60  * @since JDK 1.3
61  */

62 public class Section {
63
64    private String JavaDoc[] section;
65    private String JavaDoc info;
66
67    /**
68     * Default Constructor
69     */

70    public Section( String JavaDoc id )
71    {
72       try
73       {
74          section = parseSection( id );
75       } catch ( ParseException JavaDoc e )
76       {
77          throw new IllegalArgumentException JavaDoc( CONSTRUCTION_ERROR );
78       }
79    }
80
81    /**
82     * Constructor that takes an additional String parameter which
83     * gives a hint about the actual error that occured.
84     */

85    public Section( String JavaDoc id, String JavaDoc info )
86    {
87       this( id );
88       this.info = info;
89    }
90
91    /*
92     ********************************************************************
93     *
94     * PUBLIC INSTANCE METHODS
95     *
96     ********************************************************************
97     */

98
99    /**
100     * Returns the section number by index
101     */

102    public String JavaDoc getSectionToken( int index )
103    {
104       if( section.length >= index )
105          throw new IndexOutOfBoundsException JavaDoc(GET_SECTION_INDEX_ERROR);
106
107       return section[index];
108    }
109
110    public Iterator JavaDoc getSectionTokens()
111    {
112       return Collections.unmodifiableList(Arrays.asList(section)).iterator();
113    }
114
115    /**
116     * Returns the section string
117     */

118    public String JavaDoc getSection() {
119       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120
121       for ( int i = 0; i < section.length; ++i )
122       {
123          buffer.append( section[i] );
124          if ( i + 1 < section.length )
125             buffer.append(".");
126       }
127
128       return buffer.toString();
129    }
130
131    /**
132     * String representation of this object
133     */

134    public String JavaDoc toString()
135    {
136       if( info != null )
137       {
138          return getSection() + ": " + info;
139       } else
140       {
141          return getSection();
142       }
143    }
144
145    public boolean hasInfo()
146    {
147       return ( info != null ) ? true : false;
148    }
149
150    public String JavaDoc getInfo()
151    {
152       return info;
153    }
154
155    /*
156     ********************************************************************
157     *
158     * PRIVATE INSTANCE METHODS
159     *
160     ********************************************************************
161     */

162
163    /*
164     * parses the id string into section array
165     */

166    private String JavaDoc[] parseSection( String JavaDoc id )
167       throws ParseException JavaDoc
168    {
169       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( id, DELIMETER );
170       String JavaDoc[] token = new String JavaDoc[ tokenizer.countTokens() ];
171
172       for (int i = 0; tokenizer.hasMoreTokens(); ++i) {
173          token[i] = tokenizer.nextToken();
174       }
175
176       return token;
177    }
178
179
180    /*
181     ********************************************************************
182     *
183     * PRIVATE CONSTANTS
184     *
185     ********************************************************************
186     */

187
188    /*
189     * Used by the parseSection() to tokenize the section id string
190     */

191    private final static String JavaDoc DELIMETER = ".";
192
193    /*
194     * Error messages
195     */

196    private final static String JavaDoc PARSE_SECTION_ERROR =
197       "Section token cannot be longer than one character";
198    private final static String JavaDoc GET_SECTION_INDEX_ERROR =
199       "Section index too large";
200    private final static String JavaDoc CONSTRUCTION_ERROR =
201       "Cannot parse section string";
202 }
203
204 /*
205 vim:ts=3:sw=3:et
206 */

207
Popular Tags