001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels.groupsmanager;
007:
008: import java.io.StringWriter;
009: import java.util.Iterator;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: import org.jasig.portal.groups.IEntityGroup;
015: import org.jasig.portal.groups.IGroupMember;
016: import org.w3c.dom.Document;
017: import org.w3c.dom.Element;
018:
019: import org.apache.xml.serialize.OutputFormat;
020: import org.apache.xml.serialize.XMLSerializer;
021:
022: /**
023: * A class holding utility functions used by the Groups Manager channel.
024: * @author Don Fracapane
025: * @version $Revision: 36729 $
026: */
027: public class Utility implements GroupsManagerConstants {
028:
029: private static final Log log = LogFactory.getLog(Utility.class);
030:
031: /** Creates new Utility */
032: public Utility() {
033: }
034:
035: /**
036: * Compares 2 strings to determine if they are functionally the same. For this
037: * method null is the same as "".
038: * @param one String
039: * @param two String
040: * @return an <code>boolean</code> object
041: */
042: public static boolean areEqual(String one, String two) {
043: String str1 = (one == null ? "" : one).trim();
044: String str2 = (two == null ? "" : two).trim();
045: return str1.equals(str2);
046: }
047:
048: /**
049: * Answers if testString has a value other that null and "".
050: * @param aString String
051: * @return an <code>boolean</code> object
052: */
053: public static boolean notEmpty(String aString) {
054: String testString = (aString == null ? "" : aString).trim();
055: return !testString.equals("");
056: }
057:
058: /**
059: * This method will prints to the log when an exception is not passed.
060: * @param msgTypeStr String
061: * @param msg String
062: */
063: public static void logMessage(String msgTypeStr, String msg) {
064: Utility.logMessage(msgTypeStr, msg, null);
065: return;
066: }
067:
068: /**
069: * An attempt to extract all calls to logger to expedite assumed future upgrades.
070: * @param msgTypeStr String
071: * @param msg String
072: * @param th Throwable
073: */
074: public static void logMessage(String msgTypeStr, String msg,
075: Throwable th) {
076:
077: if (msgTypeStr != null && msgTypeStr.equals("ERROR"))
078: log.error(msg, th);
079: else if (msgTypeStr != null && msgTypeStr.equals("WARN"))
080: log.warn(msg, th);
081: else if (msgTypeStr != null && msgTypeStr.equals("INFO"))
082: log.info(msg, th);
083: else if (msgTypeStr != null && msgTypeStr.equals("FATAL"))
084: log.fatal(msg, th);
085: else
086: log.debug(msg, th);
087:
088: return;
089: }
090:
091: /**
092: * Extracts the value of a key sandwiched by delimiters from a string.
093: * @param fromDelim
094: * @param source
095: * @param toDelim
096: * @return an <code>String</code> object
097: */
098: public static String parseStringDelimitedBy(String fromDelim,
099: String source, String toDelim) {
100: Utility.logMessage("DEBUG",
101: "Utility::parseStringDelimitedBy(): fromDelim = "
102: + fromDelim + " source = " + source
103: + " toDelim = " + toDelim);
104: String parsedString = null;
105: String tagString = null;
106: int idxFrom = source.indexOf(fromDelim);
107: int idxTo;
108: //Utility.logMessage("DEBUG", "Utility::parseStringDelimitedBy(): idxFrom = " + idxFrom);
109: if (idxFrom > -1) {
110: tagString = source.substring(idxFrom);
111: //Utility.logMessage("DEBUG", "Utility::parseStringDelimitedBy(): tagString = " + tagString);
112: idxTo = tagString.indexOf(toDelim);
113: if (idxTo < 0) {
114: parsedString = tagString.substring(fromDelim.length());
115: } else {
116: parsedString = tagString.substring(fromDelim.length(),
117: tagString.indexOf(toDelim));
118: }
119: }
120: Utility.logMessage("DEBUG",
121: "Utility::parseStringDelimitedBy(): Returning parsedString = "
122: + parsedString);
123: return parsedString;
124: }
125:
126: /**
127: * Prints a Document. Used for debugging.
128: * @param aDoc
129: * @param aMessage
130: */
131: public static void printDoc(Document aDoc, String aMessage) {
132: String aMsg = (aMessage != null ? aMessage : "");
133: try {
134: Utility.logMessage("DEBUG",
135: "****************************************");
136: Utility.logMessage("DEBUG", aMsg + "\n" + toString(aDoc));
137: Utility.logMessage("DEBUG",
138: "########################################");
139: } catch (Exception e) {
140: Utility.logMessage("ERROR", e.toString(), e);
141: }
142: return;
143: }
144:
145: /**
146: * Prints an Element. Used for debugging.
147: * @param anElem
148: * @param aMessage
149: */
150: public static void printElement(Element anElem, String aMessage) {
151: Document prtDoc = GroupsManagerXML.getNewDocument();
152: //org.w3c.dom.Node cpy = anElem.cloneNode(true);
153: //prtDoc.adoptNode(cpy);
154: Element cpy = (Element) prtDoc.importNode(anElem, true);
155: prtDoc.appendChild(cpy);
156: Utility.printDoc(prtDoc, aMessage);
157: return;
158: }
159:
160: /**
161: * Retrieves a Group Member for the provided key and of the provided type.
162: * @param key
163: * @param type
164: * @return an <code>IGroupMember</code> object
165: */
166: public static IGroupMember retrieveGroupMemberForKeyAndType(
167: String key, String type) {
168: IGroupMember gm = null;
169:
170: if (type.equals(GROUP_CLASSNAME)) {
171: gm = GroupsManagerXML.retrieveGroup(key);
172: Utility.logMessage("DEBUG",
173: "Utility::retrieveGroupMemberForKeyAndType(): Retrieved group for Type: = "
174: + type + " and key: " + key);
175: } else {
176: gm = GroupsManagerXML.retrieveEntity(key, type);
177: Utility.logMessage("DEBUG",
178: "Utility::retrieveGroupMemberForKeyAndType(): Retrieved entity for Type: = "
179: + type + " and key: " + key);
180: }
181: return gm;
182: }
183:
184: /**
185: * Represents a document as a string.
186: * @param aDoc
187: * @return the document serialized out as a human-readable String
188: */
189: public static String toString(Document aDoc) {
190: StringWriter sw = new StringWriter();
191: try {
192: /* TODO: This should be reviewed at some point to see if we can use the
193: * DOM3 LS capability and hence a standard way of doing this rather
194: * than using an internal implementation class.
195: */
196: OutputFormat format = new OutputFormat();
197: format.setOmitXMLDeclaration(true);
198: format.setIndenting(true);
199: XMLSerializer serial = new XMLSerializer(sw, format);
200: serial.serialize(aDoc);
201: } catch (Exception e) {
202: Utility.logMessage("ERROR", "Utility::asString(): " + e, e);
203: }
204: return sw.toString();
205: }
206:
207: /**
208: * given a group key, return an xml group id that matches it in the provided document
209: *
210: * @param grpKey String
211: * @param sd CGroupsManagerUnrestrictedSessionData
212: * @return an <code>String</code> object
213: */
214: public static String translateKeytoID(String grpKey,
215: CGroupsManagerUnrestrictedSessionData sd) {
216: Document viewDoc = sd.model;
217: String id = null;
218: Element grpViewKeyElem;
219: Iterator grpItr = GroupsManagerXML.getNodesByTagNameAndKey(
220: viewDoc, GROUP_TAGNAME, grpKey);
221: IEntityGroup gm = GroupsManagerXML.retrieveGroup(grpKey);
222: if (gm != null) {
223: if (!grpItr.hasNext()) {
224: grpViewKeyElem = GroupsManagerXML.getGroupMemberXml(gm,
225: true, null, sd);
226: Element rootElem = viewDoc.getDocumentElement();
227: rootElem.appendChild(grpViewKeyElem);
228: } else {
229: grpViewKeyElem = (Element) grpItr.next();
230: GroupsManagerXML.getGroupMemberXml(gm, true,
231: grpViewKeyElem, sd);
232: }
233: id = grpViewKeyElem.getAttribute("id");
234: }
235: return id;
236: }
237:
238: /**
239: * Determines if an object has a value other than null or blank.
240: * @param o Object
241: * @return boolean
242: */
243: public static boolean hasValue(Object o) {
244: boolean rval = false;
245: if (o != null && !o.toString().trim().equals("")) {
246: rval = true;
247: }
248: return rval;
249: }
250:
251: /**
252: * Determines if an object has a value other than null or blank and is equal to
253: * the test parameter.
254: * @param o Object
255: * @param test String
256: * @return boolean
257: */
258: public static boolean hasValue(Object o, String test) {
259: boolean rval = false;
260: if (hasValue(o)) {
261: if (String.valueOf(o).equals(test)) {
262: rval = true;
263: }
264: }
265: return rval;
266: }
267:
268: }
|