001: /*
002: * Created on Aug 4, 2005
003: */
004: package uk.org.ponder.beanutil;
005:
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import uk.org.ponder.saxalizer.MethodAnalyser;
010: import uk.org.ponder.saxalizer.SAXalizerMappingContext;
011: import uk.org.ponder.util.UniversalRuntimeException;
012:
013: /**
014: * @author Antranig Basman (antranig@caret.cam.ac.uk)
015: *
016: */
017: public class BeanUtil {
018: /**
019: * The String prefix used for the ID of a freshly created entity to an
020: * "obstinate" BeanLocator following the standard OTP system. The text
021: * following the prefix is arbitrary. Custom OTP systems might use a different
022: * prefix.
023: */
024: public static String NEW_ENTITY_PREFIX = "new ";
025:
026: public static void copyBeans(Map source, WriteableBeanLocator target) {
027: for (Iterator sit = source.keySet().iterator(); sit.hasNext();) {
028: String name = (String) sit.next();
029: Object bean = source.get(name);
030: target.set(name, bean);
031: }
032: }
033:
034: public static void censorNullBean(String beanname, Object beanvalue) {
035: if (beanvalue == null) {
036: throw UniversalRuntimeException
037: .accumulate(
038: new BeanNotFoundException(),
039: "No bean with name "
040: + beanname
041: + " could be found in RSAC or application context");
042: }
043: }
044:
045: public static Object navigateOne(Object moveobj, String path,
046: SAXalizerMappingContext mappingcontext) {
047: if (path == null || path.equals("")) {
048: return moveobj;
049: }
050: if (moveobj == null) {
051: throw UniversalRuntimeException.accumulate(
052: new IllegalArgumentException(),
053: "Null value encounted in bean path at component "
054: + path);
055: } else {
056: PropertyAccessor pa = MethodAnalyser.getPropertyAccessor(
057: moveobj, mappingcontext);
058: if (pa.canGet(path)) {
059: return pa.getProperty(moveobj, path);
060: } else
061: return null;
062: }
063: }
064:
065: public static Object navigate(Object rootobj, String path,
066: SAXalizerMappingContext mappingcontext) {
067: if (path == null || path.equals("")) {
068: return rootobj;
069: }
070:
071: String[] components = PathUtil.splitPath(path);
072: Object moveobj = rootobj;
073: for (int comp = 0; comp < components.length; ++comp) {
074: if (moveobj == null) {
075: throw UniversalRuntimeException
076: .accumulate(
077: new IllegalArgumentException(),
078: "Null value encounted in bean path at component "
079: + (comp == 0 ? "<root>"
080: : components[comp - 1]
081: + " while traversing for "
082: + components[comp]));
083: } else {
084: moveobj = navigateOne(moveobj, components[comp],
085: mappingcontext);
086: }
087: }
088: return moveobj;
089: }
090:
091: /**
092: * Given a string representing an EL expression beginning #{ and ending },
093: * strip these off returning the bare expression. If the bracketing characters
094: * are not present, return null.
095: */
096: public static String stripEL(String el) {
097: if (el == null) {
098: return null;
099: } else if (el.startsWith("#{") && el.endsWith("}")) {
100: return el.substring(2, el.length() - 1);
101: } else
102: return null;
103: }
104:
105: public static String stripELNoisy(String el) {
106: String stripped = stripEL(el);
107: if (stripped == null) {
108: throw new IllegalArgumentException("EL expression \"" + el
109: + "\" is not bracketed with #{..}");
110: }
111: return stripped;
112: }
113:
114: }
|