001: /*
002: * Created on Feb 6, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.text;
008:
009: import java.util.HashMap;
010: import java.util.ArrayList;
011:
012: import java.util.regex.Pattern;
013: import java.util.regex.Matcher;
014:
015: import org.xdev.base.xssl.XSSLReturn;
016:
017: /**
018: * @author AYegorov
019: *
020: * To change the template for this generated type comment go to
021: * Window>Preferences>Java>Code Generation>Code and Comments
022: */
023: public class TextRegEx extends AbstractText {
024: public static final String REG_EX = "expression";
025: public static final String DO_FIND = "find";
026:
027: /**
028: * @param id
029: */
030: public TextRegEx(String id) {
031: super (id);
032: // XXX Auto-generated constructor stub
033: }
034:
035: /**
036: * @param id
037: * @param properties
038: */
039: public TextRegEx(String id, HashMap properties) {
040: super (id, properties);
041: // XXX Auto-generated constructor stub
042: }
043:
044: /* (non-Javadoc)
045: * @see org.xdev.base.xssl.text.AbstractText#processText(java.lang.String)
046: */
047: protected Object processText(String text) throws Exception {
048:
049: String exp = this .getProperty(TextRegEx.REG_EX);
050:
051: int count = 0;
052:
053: if (exp == null || "".equals(exp)) {
054:
055: if (this .getCount() > count) {
056: XSSLReturn regex = (XSSLReturn) this .getElement(count);
057:
058: if (regex.execute(this )) {
059: Object obj = regex.getObjectValue();
060:
061: exp = obj != null ? obj.toString() : "";
062: }
063: }
064: }
065:
066: if (text == null && this .getCount() > count) {
067: XSSLReturn txt = (XSSLReturn) this .getElement(count++);
068:
069: if (txt.execute(this )) {
070: Object t = txt.getObjectValue();
071:
072: text = t != null ? t.toString() : "";
073: }
074: }
075:
076: Pattern pattern = Pattern.compile(exp);
077:
078: Matcher matcher = pattern.matcher(text);
079:
080: Object find = null;
081:
082: this .logDebug("Regular Expression: " + exp + " vs Text: "
083: + text);
084:
085: if ((!this .getBooleanProperty("strict") && matcher.find())
086: || matcher.matches()) {
087:
088: if (this .getBooleanProperty(TextRegEx.DO_FIND)) {
089: int size = matcher.groupCount();
090:
091: if (size > 1) {
092:
093: if (this .hasProperty("group")) {
094: find = matcher.group(this
095: .getIntegerProperty("group"));
096: } else {
097:
098: ArrayList list = new ArrayList();
099:
100: for (int i = 0; i < size; i++) {
101: list.add(matcher.group(i));
102: }
103: }
104: } else {
105: find = matcher.group();
106: }
107:
108: this .logDebug("Regular Expression: " + exp
109: + " vs Text: " + text + " matched " + find);
110: }
111: } else {
112: this .setExecutionStatus(false);
113: }
114:
115: return find;
116: }
117: }
|