001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.extensions.xml;
021
022 import java.util.HashMap;
023 import java.util.List;
024 import java.util.Map.Entry;
025
026 import org.dom4j.DocumentHelper;
027 import org.dom4j.InvalidXPathException;
028 import org.dom4j.XPath;
029 import org.mactor.framework.ConfigException;
030 import org.mactor.framework.MactorException;
031
032 public class ParamUtil {
033 public static HashMap<String, String> parseParams(List<String> params) throws MactorException {
034 HashMap<String, String> map = new HashMap<String, String>();
035 for (String string : params) {
036 int firstIndex = string.indexOf("==");
037 if (firstIndex > 0 && firstIndex + 2 < string.length()) {
038 map.put(string.substring(0, firstIndex), string.substring(firstIndex + 2));
039 } else {
040 throw new MactorException("Unparseable param: " + string);
041 }
042 }
043 return map;
044 }
045 public static HashMap<XPath, String> parseXPathParams(List<String> params) throws MactorException {
046 HashMap<String, String> m = parseParams(params);
047 HashMap<XPath, String> xpMap = new HashMap<XPath, String>();
048 for (Entry<String, String> e : m.entrySet()) {
049 try {
050 xpMap.put(DocumentHelper.createXPath(e.getKey()), e.getValue());
051 } catch (InvalidXPathException ie) {
052 throw new ConfigException("Invalid xpath expression '" + e.getKey() + "'. Error: " + ie.getMessage(), ie);
053 }
054 }
055 return xpMap;
056 }
057 public static boolean isEqual(String s1, String s2) {
058 if (s1 == null && s2 == null)
059 return true;
060 if (s1 == null)
061 return false;
062 return s1.equals(s2);
063 }
064 public static boolean isMatch(String s1, String s2) {
065 if (s1 == null && s2 == null)
066 return true;
067 if (s1 == null)
068 return false;
069 return s1.matches(s2);
070 }
071 }