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.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.transform.TransformerException;
027: import javax.xml.xpath.XPath;
028: import javax.xml.xpath.XPathConstants;
029: import javax.xml.xpath.XPathExpressionException;
030:
031: import org.apache.commons.lang.StringUtils;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035: import org.xml.sax.InputSource;
036:
037: import edu.iu.uis.eden.EdenConstants;
038: import edu.iu.uis.eden.KEWServiceLocator;
039: import edu.iu.uis.eden.exception.InvalidXmlException;
040: import edu.iu.uis.eden.routetemplate.RuleAttribute;
041: import edu.iu.uis.eden.routetemplate.xmlrouting.XPathHelper;
042: import edu.iu.uis.eden.util.Utilities;
043: import edu.iu.uis.eden.util.XmlHelper;
044:
045: /**
046: * Parses {@link RuleAttribute}s from XML.
047: *
048: * @see RuleAttribute
049: *
050: * @author ewestfal
051: */
052: public class RuleAttributeXmlParser implements XmlConstants {
053: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
054: .getLogger(RuleAttributeXmlParser.class);
055:
056: private static final String XPATH_RULE_ATTRIBUTES = "//ruleAttributes/ruleAttribute";
057: private static final String NAME = "name";
058: private static final String CLASS_NAME = "className";
059: private static final String LABEL = "label";
060: private static final String DESCRIPTION = "description";
061: private static final String TYPE = "type";
062: private static final String ROUTING_CONFIG = "routingConfig";
063: private static final String SEARCHING_CONFIG = "searchingConfig";
064: private static final String SEARCH_RESULT_CONFIG = "searchResultConfig";
065:
066: public List parseRuleAttributes(InputStream input)
067: throws IOException, InvalidXmlException {
068: try {
069: Element root = DocumentBuilderFactory.newInstance()
070: .newDocumentBuilder().parse(new InputSource(input))
071: .getDocumentElement();
072: return parseRuleAttributes(root);
073: } catch (Exception e) {
074: throw new InvalidXmlException("error parsing xml data", e);
075: }
076: }
077:
078: public List parseRuleAttributes(Element element)
079: throws InvalidXmlException {
080: List ruleAttributes = new ArrayList();
081: try {
082: XPath xpath = XPathHelper.newXPath();
083: NodeList nodeList = (NodeList) xpath.evaluate(
084: XPATH_RULE_ATTRIBUTES, element,
085: XPathConstants.NODESET);
086: for (int i = 0; i < nodeList.getLength(); i++) {
087: Node ruleAttributeNode = nodeList.item(i);
088: ruleAttributes
089: .add(parseRuleAttribute(ruleAttributeNode));
090: }
091:
092: for (Iterator iterator = ruleAttributes.iterator(); iterator
093: .hasNext();) {
094: RuleAttribute ruleAttribute = (RuleAttribute) iterator
095: .next();
096: try {
097: RuleAttribute existingAttribute = KEWServiceLocator
098: .getRuleAttributeService().findByName(
099: ruleAttribute.getName());
100: if (existingAttribute != null) {
101: ruleAttribute
102: .setRuleAttributeId(existingAttribute
103: .getRuleAttributeId());
104: ruleAttribute.setLockVerNbr(existingAttribute
105: .getLockVerNbr());
106: }
107: KEWServiceLocator.getRuleAttributeService().save(
108: ruleAttribute);
109: } catch (Exception e) {
110: LOG
111: .error(
112: "Error saving rule attribute entered by XML",
113: e);
114: }
115: }
116: } catch (XPathExpressionException e1) {
117: throw new InvalidXmlException(
118: "Could not find a rule attribute.", e1);
119: }
120: return ruleAttributes;
121: }
122:
123: private RuleAttribute parseRuleAttribute(Node ruleAttributeNode)
124: throws InvalidXmlException {
125: String name = "";
126: String className = "";
127: String label = "";
128: String description = "";
129: String type = "";
130: String messageEntity = null;
131: Node xmlConfig = null;
132: for (int i = 0; i < ruleAttributeNode.getChildNodes()
133: .getLength(); i++) {
134: Node childNode = ruleAttributeNode.getChildNodes().item(i);
135: if (NAME.equals(childNode.getNodeName())) {
136: name = childNode.getFirstChild().getNodeValue();
137: } else if (CLASS_NAME.equals(childNode.getNodeName())) {
138: className = childNode.getFirstChild().getNodeValue();
139: } else if (LABEL.equals(childNode.getNodeName())) {
140: label = childNode.getFirstChild().getNodeValue();
141: } else if (DESCRIPTION.equals(childNode.getNodeName())) {
142: description = childNode.getFirstChild().getNodeValue();
143: } else if (TYPE.equals(childNode.getNodeName())) {
144: type = childNode.getFirstChild().getNodeValue();
145: } else if (ROUTING_CONFIG.equals(childNode.getNodeName())
146: || SEARCHING_CONFIG.equals(childNode.getNodeName())
147: || SEARCH_RESULT_CONFIG.equals(childNode
148: .getNodeName())) {
149: xmlConfig = childNode;
150: } else if (MESSAGE_ENTITY.equals(childNode.getNodeName())) {
151: messageEntity = childNode.getFirstChild()
152: .getNodeValue();
153: }
154: }
155: if (Utilities.isEmpty(name)) {
156: throw new InvalidXmlException(
157: "RuleAttribute must have a name");
158: }
159: if (Utilities.isEmpty(className)) {
160: throw new InvalidXmlException(
161: "RuleAttribute must have a className");
162: }
163: if (Utilities.isEmpty(label)) {
164: LOG.warn("Label empty defaulting to name");
165: label = name;
166: }
167: if (Utilities.isEmpty(type)) {
168: LOG.debug("No type specified, default to "
169: + EdenConstants.RULE_ATTRIBUTE_TYPE);
170: type = EdenConstants.RULE_ATTRIBUTE_TYPE;
171: //throw new InvalidXmlException("RuleAttribute must have an attribute type");
172: }
173: RuleAttribute ruleAttribute = new RuleAttribute();
174: ruleAttribute.setName(name);
175: ruleAttribute.setClassName(className);
176: ruleAttribute.setType(type);
177: ruleAttribute.setLabel(label);
178: // default description to label
179: if (StringUtils.isEmpty(description)) {
180: description = label;
181: }
182: ruleAttribute.setDescription(description);
183: ruleAttribute.setMessageEntity(messageEntity);
184:
185: if (xmlConfig != null) {
186: try {
187: ruleAttribute.setXmlConfigData(XmlHelper
188: .writeNode(xmlConfig));
189: } catch (TransformerException e) {
190: throw new InvalidXmlException("XML config is invalid",
191: e);
192: }
193: } else {
194: if (EdenConstants.RULE_XML_ATTRIBUTE_TYPE.equals(type)) {
195: throw new InvalidXmlException(
196: "A routing config must be present to be of type: "
197: + type);
198: } else if (EdenConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE
199: .equals(type)) {
200: throw new InvalidXmlException(
201: "A searching config must be present to be of type: "
202: + type);
203: } else if (EdenConstants.SEARCH_RESULT_XML_PROCESSOR_ATTRIBUTE_TYPE
204: .equals(type)) {
205: throw new InvalidXmlException(
206: "A searching config must be present to be of type: "
207: + type);
208: }
209: }
210: return ruleAttribute;
211: }
212: }
|