001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.engine.node;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.io.InputStreamReader;
021    import java.util.Iterator;
022    
023    import org.apache.log4j.Logger;
024    import org.kuali.rice.kew.engine.RouteContext;
025    import org.kuali.rice.kew.engine.node.var.Property;
026    import org.kuali.rice.kew.engine.node.var.PropertyScheme;
027    
028    
029    /**
030     * A utility class for reading properties from a document.
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     */
034    public final class PropertiesUtil {
035        private static final Logger LOG = Logger.getLogger(PropertiesUtil.class);
036    
037            private PropertiesUtil() {
038                    throw new UnsupportedOperationException("do not call");
039            }
040    
041        public static String readResource(InputStream stream) throws IOException {
042            StringBuffer sb = new StringBuffer(2048);
043            InputStreamReader reader = new InputStreamReader(stream);
044            char[] buf = new char[1024];
045            int read;
046            try {
047                while ((read = reader.read(buf)) != -1) {
048                    sb.append(buf, 0, read);
049                }
050            } finally {
051                reader.close();
052            }
053            return sb.toString();
054        }
055    
056        /**
057         * Resolves the specified name as a qualified property
058         * @param name the qualified property name
059         * @return value if found, null otherwise
060         */
061        public static Object retrieveProperty(String name, RouteContext context) {
062            return retrieveProperty(new Property(name), context);
063        }
064    
065        /**
066         * Resolves the specified name as an unqualified property
067         * @param name the potentially unqualified property name
068         * @param defaultScheme the default scheme to use if the property is unqualified
069         * @return value if found, null otherwise
070         */
071        public static Object retrieveProperty(String name, PropertyScheme defaultScheme, RouteContext context) {
072            return retrieveProperty(new Property(name), defaultScheme, context);
073        }
074    
075        /**
076         * Resolves the specified name as an unqualified property
077         * @param prop the potentially unqualified property
078         * @param defaultScheme the default scheme to use if the property is unqualified
079         * @return value if found, null otherwise
080         */
081        public static Object retrieveProperty(Property prop, PropertyScheme defaultScheme, RouteContext context) {
082            if (prop.scheme == null && defaultScheme != null) {
083                prop.scheme = defaultScheme.getName();
084            }
085            return retrieveProperty(prop, context);
086        }
087    
088        /**
089         * Resolves the specified name as a qualified property
090         * @param prop the qualified property
091         * @return value if found, null otherwise
092         */
093        public static Object retrieveProperty(Property prop, RouteContext context) {
094            Iterator schemes = PropertyScheme.SCHEMES.iterator();
095            while (schemes.hasNext()) {
096                PropertyScheme scheme = (PropertyScheme) schemes.next();
097                if (scheme.getName().equals(prop.scheme) ||
098                    scheme.getShortName().equals(prop.scheme)) {
099                    LOG.debug("Loading prop " + prop + " with scheme " + scheme);
100                    return scheme.load(prop, context);
101                }
102            }
103            String message = "Invalid property scheme: '" + prop.scheme + "'"; 
104            LOG.error(message);
105            throw new RuntimeException(message);
106        }
107    }