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