View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.xml.export;
17  
18  import org.jdom.Document;
19  import org.jdom.Element;
20  import org.jdom.input.SAXBuilder;
21  import org.kuali.rice.core.api.impex.ExportDataSet;
22  import org.kuali.rice.core.api.util.xml.XmlHelper;
23  import org.kuali.rice.core.api.util.xml.XmlRenderer;
24  import org.kuali.rice.core.framework.impex.xml.XmlExporter;
25  import org.kuali.rice.kew.api.WorkflowRuntimeException;
26  import org.kuali.rice.kew.export.KewExportDataSet;
27  import org.kuali.rice.kew.rule.bo.RuleAttribute;
28  
29  import java.io.StringReader;
30  import java.util.Iterator;
31  
32  import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
33  
34  /**
35   * Exports {@link org.kuali.rice.kew.rule.bo.RuleAttribute}s to XML.
36   * 
37   * @see org.kuali.rice.kew.rule.bo.RuleAttribute
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class RuleAttributeXmlExporter implements XmlExporter {
42  
43      protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
44      
45      private XmlRenderer renderer = new XmlRenderer(RULE_ATTRIBUTE_NAMESPACE);
46      
47  	@Override
48  	public boolean supportPrettyPrint() {
49  		return true;
50  	}
51  
52      public Element export(ExportDataSet exportDataSet) {
53      	KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
54          if (!dataSet.getRuleAttributes().isEmpty()) {
55              Element rootElement = renderer.renderElement(null, RULE_ATTRIBUTES);
56              rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_ATTRIBUTE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
57              for (Iterator iterator = dataSet.getRuleAttributes().iterator(); iterator.hasNext();) {
58                  RuleAttribute template = (RuleAttribute)iterator.next();
59                  exportRuleAttribute(rootElement, template);
60              }
61              return rootElement;
62          }
63          return null;
64      }
65      
66      private void exportRuleAttribute(Element parent, RuleAttribute ruleAttribute) {
67          Element attributeElement = renderer.renderElement(parent, RULE_ATTRIBUTE);
68          renderer.renderTextElement(attributeElement, NAME, ruleAttribute.getName());
69          renderer.renderTextElement(attributeElement, CLASS_NAME, ruleAttribute.getResourceDescriptor());
70          renderer.renderTextElement(attributeElement, LABEL, ruleAttribute.getLabel());
71          renderer.renderTextElement(attributeElement, DESCRIPTION, ruleAttribute.getDescription());
72          renderer.renderTextElement(attributeElement, TYPE, ruleAttribute.getType());
73          renderer.renderTextElement(attributeElement, APPLICATION_ID, ruleAttribute.getApplicationId());
74          if (!org.apache.commons.lang.StringUtils.isEmpty(ruleAttribute.getXmlConfigData())) {
75              try {
76                  Document configDoc = new SAXBuilder().build(new StringReader(ruleAttribute.getXmlConfigData()));
77                  XmlHelper.propagateNamespace(configDoc.getRootElement(), RULE_ATTRIBUTE_NAMESPACE);
78                  attributeElement.addContent(configDoc.getRootElement().detach());
79              } catch (Exception e) {
80              	LOG.error("Error parsing attribute XML configuration.", e);
81                  throw new WorkflowRuntimeException("Error parsing attribute XML configuration.");
82              }
83          }
84      }
85      
86  }