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 org.kuali.workflow.tools.xml;
018:
019: import java.io.ByteArrayInputStream;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.xml.xpath.XPath;
024: import javax.xml.xpath.XPathConstants;
025: import javax.xml.xpath.XPathExpressionException;
026: import javax.xml.xpath.XPathFactory;
027:
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030: import org.xml.sax.InputSource;
031:
032: /**
033: *
034: * @author ewestfal
035: */
036: public class Attribute {
037:
038: private XmlGenHelper helper;
039:
040: public Attribute(XmlGenHelper helper) {
041: this .helper = helper;
042: }
043:
044: private String name;
045: private String className;
046: private String xmlConfigData;
047: private List<String> fieldNames;
048:
049: public String getName() {
050: return name;
051: }
052:
053: public void setName(String name) {
054: this .name = name;
055: }
056:
057: public String getClassName() {
058: return className;
059: }
060:
061: public void setClassName(String className) {
062: this .className = className;
063: }
064:
065: public String getXmlConfigData() {
066: return xmlConfigData;
067: }
068:
069: public void setXmlConfigData(String xmlConfigData) {
070: this .xmlConfigData = xmlConfigData;
071: }
072:
073: public List<String> getFieldNames() {
074: if (fieldNames == null) {
075: generateFieldNames();
076: }
077: return fieldNames;
078: }
079:
080: public void setFieldNames(List<String> fieldNames) {
081: this .fieldNames = fieldNames;
082: }
083:
084: private void generateFieldNames() {
085: fieldNames = new ArrayList<String>();
086: try {
087: if (!isBlank(xmlConfigData)) {
088: XPath xpath = XPathFactory.newInstance().newXPath();
089: NodeList fieldDefs = (NodeList) xpath.evaluate(
090: "/routingConfig/fieldDef", new InputSource(
091: new ByteArrayInputStream(xmlConfigData
092: .getBytes())),
093: XPathConstants.NODESET);
094: for (int index = 0; index < fieldDefs.getLength(); index++) {
095: Element fieldDefElem = (Element) fieldDefs
096: .item(index);
097: String type = fieldDefElem
098: .getAttribute("workflowType");
099: if ("ALL".equals(type) || "RULE".equals(type)) {
100: fieldNames.add(fieldDefElem
101: .getAttribute("name"));
102: }
103: }
104: return;
105: } else if (helper != null) {
106: List<String> resolvedFieldNames = helper
107: .resolveFieldNames(this );
108: if (resolvedFieldNames != null) {
109: fieldNames.addAll(resolvedFieldNames);
110: }
111: }
112:
113: // Class<?> attributeClass = Class.forName(className);
114: // if (!WorkflowAttribute.class.isAssignableFrom(attributeClass)) {
115: // throw new RuntimeException("The attribute class '" + className + "' is not a valid instance of " + WorkflowAttribute.class.getName());
116: // }
117: // WorkflowAttribute workflowAttribute = (WorkflowAttribute)attributeClass.newInstance();
118: // if (workflowAttribute instanceof GenericXMLRuleAttribute) {
119: // if (isBlank(xmlConfigData)) {
120: // throw new RuntimeException("No XML configuration was available on attribute '" + name + "'");
121: // }
122: // RuleAttribute ruleAttribute = new RuleAttribute();
123: // ruleAttribute.setClassName(className);
124: // ruleAttribute.setName(name);
125: // ruleAttribute.setXmlConfigData(xmlConfigData);
126: // ((GenericXMLRuleAttribute)workflowAttribute).setRuleAttribute(ruleAttribute);
127: // }
128: // List<Row> rows = workflowAttribute.getRuleRows();
129: // for (Row row : rows) {
130: // for (Field field : row.getFields()) {
131: // fieldNames.add(field.getPropertyName());
132: // }
133: // }
134: } catch (XPathExpressionException e) {
135: // TODO Auto-generated catch block
136: e.printStackTrace();
137: }
138: }
139:
140: protected boolean isBlank(String string) {
141: return string == null || string.trim().equals("");
142: }
143:
144: }
|