001: /*
002: * Copyright 2005-2007 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.test.web.framework;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.log4j.Logger;
027: import org.w3c.dom.NamedNodeMap;
028: import org.w3c.dom.Node;
029:
030: /**
031: * Utility class
032: * @author Aaron Hamid (arh14 at cornell dot edu)
033: */
034: public final class Util {
035: private static final Logger LOG = Logger.getLogger(Util.class);
036:
037: private Util() {
038: }
039:
040: // could DOM be any less usable...
041: public static String getAttribute(Node node, String name) {
042: NamedNodeMap attributes = node.getAttributes();
043: if (attributes == null)
044: return null;
045: Node attrNode = attributes.getNamedItem(name);
046: if (attrNode == null)
047: return null;
048: return attrNode.getNodeValue();
049: }
050:
051: private static String composeAttrName(String name, String type) {
052: if (name != null) {
053: return name + "-" + type;
054: } else {
055: return type;
056: }
057: }
058:
059: public static String getAsString(Object o, String name) {
060: if (!(o instanceof String)) {
061: LOG.warn(name + " is not a string, coercing...");
062: if (o == null) {
063: return "";
064: } else {
065: return o.toString();
066: }
067: } else {
068: return (String) o;
069: }
070: }
071:
072: /**
073: * Looks up a resolvable Property attribute from the given Element node. Explicit variants of the attribute
074: * name are inspected first, and then the plain name itself is inspected and a defaultScheme is used if none
075: * is specified in the value. e.g.:
076: * <someelement value-literal="foo" value-resource="foo" value-url="foo" value-variable="foo" value="foo"/>
077: * The following code would look for exactly one of the above "value" attributes. If only the "value" attribute
078: * was found, then the defaultScheme would be used to qualify the value if the value did not already contain
079: * a scheme.
080: * If more than one variant is present, a RuntimeException is thrown.
081: * @param node the element whose attributes to check
082: * @param name the name of the attribute
083: * @param defaultScheme the defaultScheme to qualify an unqualified value found in the attribute with the plain name
084: * @return a Property if found, or null
085: * @throws RuntimeException if more than one variant is found (or more than one variant of a given scheme, e.g. value-lit/value-literal)
086: */
087: public static Property getResolvableAttribute(Node node,
088: String name, PropertyScheme defaultScheme) {
089: NamedNodeMap allAttributes = node.getAttributes();
090: if (allAttributes == null)
091: return null;
092:
093: Iterator schemes = PropertyScheme.SCHEMES.iterator();
094: Map properties = new HashMap();
095: while (schemes.hasNext()) {
096: PropertyScheme scheme = (PropertyScheme) schemes.next();
097: String[] names = new String[] { scheme.getName(),
098: scheme.getShortName() };
099: for (int i = 0; i < names.length; i++) {
100: String attrName = composeAttrName(name, names[i]);
101: //LOG.info("looking for attribute: " + attrName);
102: Node attrNode = allAttributes.getNamedItem(attrName);
103: if (attrNode != null) {
104: //LOG.info("found attribute: " + attrNode);
105: if (properties.containsKey(scheme)) {
106: throw new RuntimeException(
107: "Already specified explicit attribute for scheme: "
108: + scheme);
109: } else {
110: properties.put(scheme, new Property(names[i],
111: attrNode.getNodeValue()));
112: }
113: }
114: }
115: }
116:
117: if (name != null) {
118: Node attrNode = allAttributes.getNamedItem(name);
119: if (attrNode != null) {
120: Property property = new Property(attrNode
121: .getNodeValue());
122: if (property.scheme == null && defaultScheme != null) {
123: property.scheme = defaultScheme.getName();
124: }
125: properties.put(defaultScheme, property);
126: }
127: }
128:
129: if (properties.size() > 1) {
130: throw new RuntimeException(
131: "Mutually exclusive explicit attributes present");
132: } else if (properties.size() == 0) {
133: return null;
134: }
135:
136: // there can only be 1 at this point
137: return (Property) properties.values().iterator().next();
138: }
139:
140: public static String getContent(Node node) {
141: Node textNode = node.getFirstChild();
142: if (textNode != null
143: && textNode.getNodeType() == Node.TEXT_NODE) {
144: return textNode.getNodeValue();
145: } else {
146: return null;
147: }
148: }
149:
150: public static String readResource(InputStream stream)
151: throws IOException {
152: StringBuffer sb = new StringBuffer(2048);
153: InputStreamReader reader = new InputStreamReader(stream);
154: char[] buf = new char[1024];
155: int read;
156: try {
157: while ((read = reader.read(buf)) != -1) {
158: sb.append(buf, 0, read);
159: }
160: } finally {
161: reader.close();
162: }
163: return sb.toString();
164: }
165: }
|