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.plugin.manifest;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.jdom.Document;
029: import org.jdom.Element;
030: import org.jdom.JDOMException;
031: import org.jdom.input.SAXBuilder;
032: import org.kuali.rice.config.Config;
033:
034: import edu.iu.uis.eden.exception.InvalidXmlException;
035: import edu.iu.uis.eden.plugin.PluginException;
036: import edu.iu.uis.eden.util.Utilities;
037:
038: /**
039: * Parses a {@link PluginManifest} configuration from an XML file.
040: *
041: * @author ewestfal
042: */
043: public class PluginManifestParser {
044:
045: private static final String PARAMETER_TAG = "parameter";
046: private static final String LISTENER_TAG = "listener";
047: private static final String LISTENER_CLASS_TAG = "listener-class";
048: private static final String RESOURCE_LOADER_TAG = "resourceLoader";
049:
050: private static final String NAME_ATTRIBUTE = "name";
051: private static final String VALUE_ATTRIBUTE = "value";
052: private static final String CLASS_ATTRIBUTE = "class";
053:
054: public PluginManifest parse(File manifestFile, Config parentConfig)
055: throws IOException, InvalidXmlException {
056: return parse(manifestFile.toURL(), parentConfig);
057: }
058:
059: public PluginManifest parse(URL url, Config parentConfig)
060: throws IOException, InvalidXmlException {
061: SAXBuilder builder = new SAXBuilder(false);
062: try {
063: // NOTE: need to be wary of whether streams are closed properly
064: // by builder
065: Document doc = builder.build(url);
066: Element root = doc.getRootElement();
067: PluginManifest pluginManifest = new PluginManifest(url,
068: parentConfig);
069: parseResourceLoader(root, pluginManifest);
070: parseListeners(root, pluginManifest);
071: return pluginManifest;
072: } catch (JDOMException e) {
073: throw new PluginException(
074: "Error when parsing the plugin manifest file.", e);
075: }
076: }
077:
078: public void parseResourceLoader(Element element,
079: PluginManifest pluginManifest) throws InvalidXmlException {
080: List loaderElements = element.getChildren(RESOURCE_LOADER_TAG);
081: if (loaderElements.size() > 1) {
082: throw new InvalidXmlException(
083: "Only one <resourceLoader> tag may be defined.");
084: } else if (!loaderElements.isEmpty()) {
085: Element loaderElement = (Element) loaderElements.get(0);
086: String attributeClass = loaderElement
087: .getAttributeValue(CLASS_ATTRIBUTE);
088: if (StringUtils.isEmpty(attributeClass)) {
089: throw new InvalidXmlException(
090: "<resourceLoader> element must define a 'class' attribute.");
091: }
092: pluginManifest.setResourceLoaderClassname(attributeClass);
093: }
094: }
095:
096: public void parseListeners(Element element,
097: PluginManifest pluginManifest) throws InvalidXmlException {
098: List listeners = element.getChildren(LISTENER_TAG);
099: for (Iterator iterator = listeners.iterator(); iterator
100: .hasNext();) {
101: pluginManifest
102: .addListener(parseListenerProperties((Element) iterator
103: .next()));
104: }
105: }
106:
107: private String parseListenerProperties(Element element)
108: throws InvalidXmlException {
109: String listenerClass = element.getChildText(LISTENER_CLASS_TAG);
110: if (Utilities.isEmpty(listenerClass)) {
111: throw new InvalidXmlException(
112: "Listener Class tag must have a class property defined");
113: }
114: return listenerClass;
115: }
116:
117: public Map parseParameters(Element element)
118: throws InvalidXmlException {
119: Map parsedParms = new HashMap();
120: List parameters = element.getChildren(PARAMETER_TAG);
121: for (Iterator iterator = parameters.iterator(); iterator
122: .hasNext();) {
123: String[] parm = parseParameter((Element) iterator.next());
124: parsedParms.put(parm[0], parm[1]);
125: }
126: return parsedParms;
127: }
128:
129: private String[] parseParameter(Element element)
130: throws InvalidXmlException {
131: String name = element.getAttributeValue(NAME_ATTRIBUTE);
132: String value = element.getAttributeValue(VALUE_ATTRIBUTE);
133: if (Utilities.isEmpty(name)) {
134: throw new InvalidXmlException(
135: "Parameter tag must have a name attribute defined");
136: }
137: if (Utilities.isEmpty(value)) {
138: throw new InvalidXmlException(
139: "Parameter tag must have a value attribute defined");
140: }
141: return new String[] { name, value };
142: }
143:
144: }
|