001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.verifier;
023:
024: /*
025: * Class org.jboss.verifier.Section
026: * Copyright (C) 2000 Juha Lindfors
027: *
028: * This library is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU Lesser General Public
030: * License as published by the Free Software Foundation; either
031: * version 2 of the License, or (at your option) any later version
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Lesser General Public License for more details.
037: *
038: * You should have received a copy of the GNU Lesser General Public
039: * License along with this library; if not, write to the Free Software
040: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
041: *
042: * This package and its source code is available at www.jboss.org
043: * $Id: Section.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
044: */
045:
046: // standard imports
047: import java.util.Arrays;
048: import java.util.Collections;
049: import java.util.Iterator;
050: import java.util.StringTokenizer;
051:
052: /**
053: * Represents a section in the EJB spec.
054: *
055: * @author Juha Lindfors
056: * @version $Revision: 57209 $
057: * @since JDK 1.3
058: */
059: public class Section {
060:
061: private String[] section;
062: private String info;
063:
064: /**
065: * Default Constructor
066: */
067: public Section(String id) {
068: section = parseSection(id);
069: }
070:
071: /**
072: * Constructor that takes an additional String parameter which
073: * gives a hint about the actual error that occured.
074: */
075: public Section(String id, String info) {
076: this (id);
077: this .info = info;
078: }
079:
080: /*
081: ********************************************************************
082: *
083: * PUBLIC INSTANCE METHODS
084: *
085: ********************************************************************
086: */
087:
088: /**
089: * Returns the section number by index
090: */
091: public String getSectionToken(int index) {
092: if (section.length >= index)
093: throw new IndexOutOfBoundsException(GET_SECTION_INDEX_ERROR);
094:
095: return section[index];
096: }
097:
098: public Iterator getSectionTokens() {
099: return Collections.unmodifiableList(Arrays.asList(section))
100: .iterator();
101: }
102:
103: /**
104: * Returns the section string
105: */
106: public String getSection() {
107: StringBuffer buffer = new StringBuffer();
108:
109: for (int i = 0; i < section.length; ++i) {
110: buffer.append(section[i]);
111: if (i + 1 < section.length)
112: buffer.append(".");
113: }
114:
115: return buffer.toString();
116: }
117:
118: /**
119: * String representation of this object
120: */
121: public String toString() {
122: if (info != null) {
123: return getSection() + ": " + info;
124: } else {
125: return getSection();
126: }
127: }
128:
129: public boolean hasInfo() {
130: return (info != null) ? true : false;
131: }
132:
133: public String getInfo() {
134: return info;
135: }
136:
137: /*
138: ********************************************************************
139: *
140: * PRIVATE INSTANCE METHODS
141: *
142: ********************************************************************
143: */
144:
145: /*
146: * parses the id string into section array
147: */
148: private String[] parseSection(String id) {
149: StringTokenizer tokenizer = new StringTokenizer(id, DELIMETER);
150: String[] token = new String[tokenizer.countTokens()];
151:
152: for (int i = 0; tokenizer.hasMoreTokens(); ++i) {
153: token[i] = tokenizer.nextToken();
154: }
155:
156: return token;
157: }
158:
159: /*
160: ********************************************************************
161: *
162: * PRIVATE CONSTANTS
163: *
164: ********************************************************************
165: */
166:
167: /*
168: * Used by the parseSection() to tokenize the section id string
169: */
170: private final static String DELIMETER = ".";
171:
172: /*
173: * Error messages
174: */
175: private final static String GET_SECTION_INDEX_ERROR = "Section index too large";
176: }
177:
178: /*
179: vim:ts=3:sw=3:et
180: */
|