001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.runtime.cbr;
025:
026: import java.util.regex.Matcher;
027: import java.util.regex.Pattern;
028: import java.util.regex.PatternSyntaxException;
029:
030: import javax.xml.transform.dom.DOMSource;
031: import javax.xml.xpath.XPath;
032: import javax.xml.xpath.XPathExpressionException;
033: import javax.xml.xpath.XPathFactory;
034:
035: import org.apache.commons.lang.StringUtils;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.w3c.dom.Document;
039:
040: import com.bostechcorp.cbesb.common.util.Dom;
041:
042: public class ExpressionHandler {
043:
044: protected static transient Log logger = LogFactory
045: .getLog(ExpressionHandler.class);
046:
047: public static boolean processXpathExpression(DOMSource domSrc,
048: String expression) throws CBRException {
049: String compareString = StringUtils.strip(StringUtils
050: .substringAfter(expression, "="));
051: expression = StringUtils.strip(StringUtils.substringBefore(
052: expression, "="));
053:
054: //Need to evalue the result to true or false
055: XPath xpath = XPathFactory.newInstance().newXPath();
056: Document dom;
057: if (domSrc.getNode() instanceof Document) {
058: dom = (Document) domSrc.getNode();
059: } else {
060: dom = domSrc.getNode().getOwnerDocument();
061: }
062:
063: try {
064: String evalString = xpath.evaluate(expression, dom);
065: logger.debug("XPath evaluation result = " + evalString);
066: return evalString.equals(compareString);
067: } catch (XPathExpressionException e) {
068: CBRException ce = new CBRException(
069: "CBR XPath expression in route '" + expression
070: + "' could not be evaluated - "
071: + e.getMessage(), e);
072: ce.setRemedy("Correct the expression.");
073: throw ce;
074: }
075:
076: }
077:
078: public static boolean processExactExpression(String trxId,
079: String expression) throws CBRException {
080: if (expression != null) {
081: return expression.equals(trxId);
082: } else {
083: CBRException ce = new CBRException(
084: "CBR Expression in route is null.");
085: ce.setRemedy("Correct the expression.");
086: throw ce;
087: }
088:
089: }
090:
091: public static boolean processRegexpExpression(String trxId,
092: String expression) throws CBRException {
093: Log log = LogFactory.getLog(ExpressionHandler.class);
094:
095: try {
096: Pattern p = Pattern.compile(expression);
097: Matcher m = p.matcher(trxId);
098: return m.matches();
099: } catch (PatternSyntaxException e) {
100: CBRException ce = new CBRException("CBR RegEx expression '"
101: + expression + "' in route is invalid - "
102: + e.getMessage(), e);
103: ce.setRemedy("Correct the Regular Expression.");
104: throw ce;
105: }
106: }
107:
108: }
|