001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/util/tags/sakai_2-4-1/util-util/util/src/java/org/sakaiproject/util/MapUtil.java $
003: * $Id: MapUtil.java 20987 2007-02-04 00:09:59Z csev@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.sakaiproject.util.Web;
027:
028: /**
029: * <p>
030: * MapUtil collects together some string utility classes.
031: * </p>
032: */
033: public class MapUtil {
034: /**
035: * Copies elements from a map to another map (similar to putall)
036: *
037: * @param destMap
038: * The Map to add the key/value pairs to.
039: * @param sourceMap
040: * The Map to add the key/value pairs from
041: * @return true if there were keys copied
042: */
043: public static boolean copy(Map destMap, Map sourceMap) {
044: if (destMap == null || sourceMap == null || sourceMap.isEmpty())
045: return false;
046: Set s = sourceMap.keySet();
047: if (s == null)
048: return false;
049: Iterator iSet = s.iterator();
050:
051: boolean retval = false;
052: while (iSet.hasNext()) {
053: Object item = iSet.next();
054: destMap.put(item, sourceMap.get(item));
055: // System.out.println("Copying "+item+" ="+sourceMap.get(item));
056: retval = true;
057: }
058: return retval;
059: }
060:
061: /**
062: * Assigns a value from one map to another
063: *
064: * destMap[destKey] = sourceMap[sourceKey];
065: *
066: * @param destMap
067: * The Map to add the key/value pair to.
068: * @param destKey
069: * The key to use in the destination map.
070: * @param sourceMap
071: * The Map to add the key/value pairs from
072: * @param sourceKey
073: * The key to use from the source map.
074: * @return true if there were keys copied
075: */
076: public static boolean copy(Map destMap, String destKey,
077: Map sourceMap, String sourceKey) {
078: if (destMap == null || sourceMap == null || sourceMap.isEmpty())
079: return false;
080: Object o = sourceMap.get(sourceKey);
081: if (o == null)
082: return false;
083: destMap.put(destKey, o);
084: // System.out.println("dest["+destKey+"]=source["+sourceKey+"] = "+ o);
085: return true;
086: }
087:
088: /**
089: * Assigns a value from one map to another - applying an HMTL filter to the value
090: *
091: * destMap[destKey] = sourceMap[sourceKey];
092: *
093: * @param destMap
094: * The Map to add the key/value pair to.
095: * @param destKey
096: * The key to use in the destination map.
097: * @param sourceMap
098: * The Map to add the key/value pairs from
099: * @param sourceKey
100: * The key to use from the source map.
101: * @return true if there were keys copied
102: */
103: public static boolean copyHtml(Map destMap, String destKey,
104: Map sourceMap, String sourceKey) {
105: if (destMap == null || sourceMap == null || sourceMap.isEmpty())
106: return false;
107: try {
108: String s = (String) sourceMap.get(sourceKey);
109: if (s == null)
110: return false;
111: destMap.put(destKey, Web.escapeHtml(s));
112: return true;
113: } catch (Throwable t) { // Not a string
114: return false;
115: }
116: }
117: }
|