001: package org.obe.worklist;
002:
003: import java.io.StringWriter;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007: import javax.xml.transform.Transformer;
008: import javax.xml.transform.TransformerConfigurationException;
009: import javax.xml.transform.TransformerException;
010: import javax.xml.transform.TransformerFactory;
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.stream.StreamResult;
013: import org.obe.OBERuntimeException;
014: import org.w3c.dom.Document;
015: import org.w3c.dom.Node;
016:
017: /**
018: * @author Adrian Price
019: */
020: public class WorklistUtils {
021: private static final ThreadLocal _transformerFactory = new ThreadLocal();
022:
023: private WorklistUtils() {
024: }
025:
026: public static List iteratorToList(Iterator it) {
027: List list = new ArrayList();
028: while (it.hasNext())
029: list.add(it.next());
030: return list;
031: }
032:
033: /**
034: * Inserts a string immediately before the last occurrence of one string
035: * within another.
036: * <p/>
037: * For example: <code>splice("abc/xyz", "/", "-def")</code> returns
038: * <code>"abc-def/xyz"</code>.
039: *
040: * @param s1 The source string.
041: * @param s2 The string that marks the insertion point.
042: * @param s3 The string to insert before <code>s2</code>.
043: * @return The source string, with <code>s3</code> inserted before the last
044: * occurrence of <code>s2</code> in <code>s1</code>.
045: */
046: public static String splice(String s1, String s2, String s3) {
047: if (s1 != null) {
048: int i = s1.lastIndexOf(s2);
049: if (i > -1) {
050: StringBuffer sb = new StringBuffer(s1.length()
051: + s3.length());
052: char[] ca = s1.toCharArray();
053: sb.append(ca, 0, i);
054: sb.append(s3);
055: sb.append(ca, i, ca.length - i);
056: s1 = sb.toString();
057: }
058: }
059: return s1;
060: }
061:
062: /**
063: * Converts an object to a string. Object arrays are converted to
064: * comma-separated lists, and nulls are converted to a single space.
065: *
066: * @param value The object to convert.
067: * @return The string representation of the object's value.
068: */
069: public static String convert(Object value) {
070: try {
071: String s = null;
072: if (value instanceof Object[]) {
073: Object[] objects = (Object[]) value;
074: if (objects.length > 0) {
075: StringBuffer sb = new StringBuffer();
076: for (int i = 0; i < objects.length; i++) {
077: if (i > 0)
078: sb.append(',');
079: sb.append(objects[i]);
080: }
081: s = sb.toString();
082: }
083: } else if (value instanceof Document) {
084: StringWriter out = new StringWriter();
085: getTransformer().transform(new DOMSource((Node) value),
086: new StreamResult(out));
087: s = out.toString();
088: // if (s.length() > 256)
089: // s = s.substring(0, 256) + "...";
090: } else if (value != null) {
091: s = value.toString();
092: }
093: if (s == null || s.length() == 0)
094: s = " ";
095: return s;
096: } catch (TransformerException e) {
097: throw new OBERuntimeException(e);
098: }
099: }
100:
101: private static Transformer getTransformer()
102: throws TransformerConfigurationException {
103:
104: return getTransformerFactory().newTransformer();
105: }
106:
107: private static TransformerFactory getTransformerFactory() {
108: TransformerFactory factory = (TransformerFactory) _transformerFactory
109: .get();
110: if (factory == null) {
111: factory = TransformerFactory.newInstance();
112: _transformerFactory.set(factory);
113: }
114: return factory;
115: }
116: }
|