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.xml;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Vector;
023:
024: import org.jdom.Element;
025: import org.jdom.Namespace;
026:
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.exception.InvalidXmlException;
029: import edu.iu.uis.eden.routetemplate.RuleAttribute;
030: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
031: import edu.iu.uis.eden.routetemplate.RuleExtension;
032: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
033: import edu.iu.uis.eden.routetemplate.RuleTemplate;
034: import edu.iu.uis.eden.routetemplate.RuleTemplateAttribute;
035: import edu.iu.uis.eden.util.Utilities;
036: import edu.iu.uis.eden.util.XmlHelper;
037:
038: /**
039: * Parses {@link RuleExtension}s from XML.
040: *
041: * @see RuleExtension
042: * @see RuleExtensionValue
043: *
044: * @author ewestfal
045: */
046: public class RuleExtensionXmlParser {
047:
048: private static final Namespace NAMESPACE = Namespace.getNamespace(
049: "", "ns:workflow/Rule");
050: private static final String RULE_EXTENSION = "ruleExtension";
051: private static final String ATTRIBUTE = "attribute";
052: private static final String RULE_TEMPLATE = "ruleTemplate";
053: private static final String RULE_EXTENSION_VALUES = "ruleExtensionValues";
054: private static final String RULE_EXTENSION_VALUE = "ruleExtensionValue";
055: private static final String KEY = "key";
056: private static final String VALUE = "value";
057:
058: public List parseRuleExtensions(Element element, RuleBaseValues rule)
059: throws InvalidXmlException {
060: List ruleExtensions = new ArrayList();
061: Vector ruleElements = XmlHelper.findElements(element,
062: RULE_EXTENSION);
063: for (Iterator iterator = ruleElements.iterator(); iterator
064: .hasNext();) {
065: ruleExtensions.add(parseRuleExtension((Element) iterator
066: .next(), rule));
067: }
068: return ruleExtensions;
069: }
070:
071: private RuleExtension parseRuleExtension(Element element,
072: RuleBaseValues rule) throws InvalidXmlException {
073: String attributeName = element.getChildText(ATTRIBUTE,
074: NAMESPACE);
075: String templateName = element.getChildText(RULE_TEMPLATE,
076: NAMESPACE);
077: Element valuesElement = element.getChild(RULE_EXTENSION_VALUES,
078: NAMESPACE);
079: if (attributeName == null) {
080: throw new InvalidXmlException(
081: "Rule extension must have a valid attribute.");
082: }
083: if (templateName == null) {
084: throw new InvalidXmlException(
085: "Rule extension must have a valid rule template.");
086: }
087: RuleAttribute ruleAttribute = KEWServiceLocator
088: .getRuleAttributeService().findByName(attributeName);
089: if (ruleAttribute == null) {
090: throw new InvalidXmlException(
091: "Could not locate attribute for the given name '"
092: + attributeName + "'");
093: }
094: RuleTemplate ruleTemplate = KEWServiceLocator
095: .getRuleTemplateService().findByRuleTemplateName(
096: templateName);
097: if (ruleTemplate == null) {
098: throw new InvalidXmlException(
099: "Could not locate rule template for the given name '"
100: + templateName + "'");
101: }
102: RuleExtension extension = new RuleExtension();
103: extension.setRuleBaseValues(rule);
104: boolean attributeFound = false;
105: for (Iterator iter = ruleTemplate.getRuleTemplateAttributes()
106: .iterator(); iter.hasNext();) {
107: RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iter
108: .next();
109: if (templateAttribute.getRuleAttributeId().equals(
110: ruleAttribute.getRuleAttributeId())) {
111: extension.setRuleTemplateAttribute(templateAttribute);
112: extension.setRuleTemplateAttributeId(templateAttribute
113: .getRuleTemplateAttributeId());
114: attributeFound = true;
115: break;
116: }
117: }
118:
119: if (!attributeFound) {
120: // TODO: need test case for this
121: throw new InvalidXmlException("Attribute '" + attributeName
122: + "' not found on template '"
123: + ruleTemplate.getName() + "'");
124: }
125:
126: extension.setExtensionValues(parseRuleExtensionValues(
127: valuesElement, extension));
128: return extension;
129: }
130:
131: private List parseRuleExtensionValues(Element element,
132: RuleExtension ruleExtension) throws InvalidXmlException {
133: List values = new ArrayList();
134: if (element == null) {
135: return values;
136: }
137: List valueElements = XmlHelper.findElements(element,
138: RULE_EXTENSION_VALUE);
139: for (Iterator iterator = valueElements.iterator(); iterator
140: .hasNext();) {
141: Element valueElement = (Element) iterator.next();
142: values.add(parseRuleExtensionValue(valueElement,
143: ruleExtension));
144: }
145: return values;
146: }
147:
148: private RuleExtensionValue parseRuleExtensionValue(Element element,
149: RuleExtension ruleExtension) throws InvalidXmlException {
150: String key = element.getChildText(KEY, NAMESPACE);
151: String value = element.getChildText(VALUE, NAMESPACE);
152: if (Utilities.isEmpty(key)) {
153: throw new InvalidXmlException(
154: "RuleExtensionValue must have a non-empty key.");
155: }
156: if (value == null) {
157: throw new InvalidXmlException(
158: "RuleExtensionValue must have a non-null value.");
159: }
160: RuleExtensionValue extensionValue = new RuleExtensionValue(key,
161: value);
162: extensionValue.setExtension(ruleExtension);
163: return extensionValue;
164: }
165:
166: }
|