001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049: /*
050: * FindSection.java
051: *
052: * Created on April 10, 2003, 10:38 AM
053: */
054:
055: package org.jaffa.tools.patternmetaengine;
056:
057: /** Class for finding a section of text in a string based on a start and end string
058: * @author PaulE
059: */
060: public class FindSection {
061:
062: private int start = -1;
063: private int end = -1;
064: private int contentStart = -1;
065: private int contentEnd = -1;
066: private String section = null;
067: private String fullSection = null;
068: private String source = null;
069: private String head = null;
070: private String tail = null;
071:
072: /** Creates a new instance of FindSection
073: * @param source Text to be searched
074: * @param startStr Start phrase to look for
075: * @param endStr End phrase to look for
076: */
077: public FindSection(String source, String startStr, String endStr) {
078: if (source == null || startStr == null || endStr == null)
079: throw new IllegalArgumentException(
080: "A null Value is not a valid argument to the FindSection routine");
081: this .source = source;
082: start = source.indexOf(startStr);
083: if (start != -1) {
084: contentStart = start + startStr.length();
085: contentEnd = source.indexOf(endStr, contentStart);
086: if (found()) {
087: end = contentEnd + endStr.length();
088: section = source.substring(contentStart, contentEnd);
089: fullSection = startStr + section + endStr;
090: }
091: }
092:
093: // Calculate head and tail.
094: if (!found())
095: head = source;
096: else
097: head = source.substring(0, start);
098: if (!found())
099: tail = null;
100: else
101: tail = source.substring(end);
102:
103: }
104:
105: /** Was the section found in source
106: * @return true if found
107: */
108: public boolean found() {
109: return contentEnd != -1;
110: }
111:
112: /** Get start position of section. This is the location where the 'start string' is found.
113: * @return start postion of section, returns -1 if section not found
114: */
115: public int getStartPos() {
116: return start;
117: }
118:
119: /** Get end position of section. This is the location after the 'end' string's location.
120: * @return end postion of section, returns -1 if section not found
121: */
122: public int getEndPos() {
123: return end;
124: }
125:
126: /** Get the found section, null if not found
127: * @return The section text excluding the start and end strings
128: */
129: public String getSection() {
130: return section;
131: }
132:
133: /** Get the found section, null if not found
134: * @return The section text including the start and end strings
135: */
136: public String getFullSection() {
137: return fullSection;
138: }
139:
140: /** This returns the text that preceeds the found section.
141: * If there is no section found, this returns the source string.
142: *
143: * Note: getSectionHead() + getFullSection() + getSectionTail() == source
144: * @return Header string, null if the section was the first thing in the source string
145: */
146: public String getSectionHead() {
147: return head;
148: }
149:
150: /** This returns the text that follows the found section.
151: * If there is no section found, this returns null.
152: *
153: * Note: getSectionHead() + getFullSection() + getSectionTail() == source
154: * @return Tail string, null if the section was not found or the last thing in the source string
155: */
156: public String getSectionTail() {
157: return tail;
158: }
159:
160: /** Test rig
161: * @param args the command line arguments
162: */
163: public static void main(String[] args) {
164:
165: String test = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Root></Root>";
166:
167: FindSection s = new FindSection(test, "<?", "?>");
168: System.out.println("Section=" + s.getSection());
169: System.out.println("Full=" + s.getFullSection());
170: System.out.println("Head=" + s.getSectionHead());
171: System.out.println("Tail=" + s.getSectionTail());
172: System.out.println("StartPos=" + s.getStartPos());
173: System.out.println("EndPos=" + s.getEndPos());
174:
175: }
176:
177: }
|