01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.extensions.xml;
20:
21: import java.util.HashMap;
22: import java.util.List;
23: import java.util.Map.Entry;
24: import org.dom4j.DocumentHelper;
25: import org.dom4j.InvalidXPathException;
26: import org.dom4j.XPath;
27: import org.mactor.framework.ConfigException;
28: import org.mactor.framework.MactorException;
29:
30: public class ParamUtil {
31: public static HashMap<String, String> parseParams(
32: List<String> params) throws MactorException {
33: HashMap<String, String> map = new HashMap<String, String>();
34: for (String string : params) {
35: int firstIndex = string.indexOf("==");
36: if (firstIndex > 0 && firstIndex + 2 < string.length()) {
37: map.put(string.substring(0, firstIndex), string
38: .substring(firstIndex + 2));
39: } else {
40: throw new MactorException("Unparseable param: "
41: + string);
42: }
43: }
44: return map;
45: }
46:
47: public static HashMap<XPath, String> parseXPathParams(
48: List<String> params) throws MactorException {
49: HashMap<String, String> m = parseParams(params);
50: HashMap<XPath, String> xpMap = new HashMap<XPath, String>();
51: for (Entry<String, String> e : m.entrySet()) {
52: try {
53: xpMap.put(DocumentHelper.createXPath(e.getKey()), e
54: .getValue());
55: } catch (InvalidXPathException ie) {
56: throw new ConfigException("Invalid xpath expression '"
57: + e.getKey() + "'. Error: " + ie.getMessage(),
58: ie);
59: }
60: }
61: return xpMap;
62: }
63:
64: public static boolean isEqual(String s1, String s2) {
65: if (s1 == null && s2 == null)
66: return true;
67: if (s1 == null)
68: return false;
69: return s1.equals(s2);
70: }
71:
72: public static boolean isMatch(String s1, String s2) {
73: if (s1 == null && s2 == null)
74: return true;
75: if (s1 == null)
76: return false;
77: return s1.matches(s2);
78: }
79: }
|