View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.xml.export;
18  
19  import java.io.StringReader;
20  import java.util.Iterator;
21  
22  import org.apache.log4j.Logger;
23  import org.jdom.Element;
24  import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
25  import org.kuali.rice.kew.exception.InvalidXmlException;
26  import org.kuali.rice.kew.export.ExportDataSet;
27  import org.kuali.rice.kew.util.XmlHelper;
28  import org.kuali.rice.kew.xml.XmlConstants;
29  import org.kuali.rice.kew.xml.XmlRenderer;
30  
31  
32  /**
33   * Exports Style definitions to XML.
34   *
35   * @see org.kuali.rice.kew.edl.service.StyleService
36   * @see org.kuali.rice.kew.xml.StyleXmlParser
37   * @see EDocLiteStyle
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class StyleXmlExporter implements XmlExporter, XmlConstants {
42  	private static final Logger LOG = Logger.getLogger(StyleXmlExporter.class);
43  
44  	private XmlRenderer renderer = new XmlRenderer(STYLE_NAMESPACE);
45  	
46  	public Element export(ExportDataSet dataSet) {
47  		if (!dataSet.getStyles().isEmpty()) {
48  			Element rootElement = renderer.renderElement(null, STYLE_STYLES);
49  			rootElement.setAttribute(SCHEMA_LOCATION_ATTR, STYLE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
50  			for (Iterator iter = dataSet.getStyles().iterator(); iter.hasNext();) {
51  				EDocLiteStyle edocLite = (EDocLiteStyle) iter.next();
52  				exportStyle(rootElement, edocLite);
53  			}
54  			return rootElement;
55  		}
56  		return null;
57  	}
58  
59  	private void exportStyle(Element parentEl, EDocLiteStyle style) {
60          if (style == null) {
61              LOG.error("Attempted to export style which was not found");
62              return;
63          }
64  
65          Element styleWrapperEl = renderer.renderElement(parentEl, STYLE_STYLE);
66          renderer.renderAttribute(styleWrapperEl, "name", style.getName());
67  
68          try {
69              Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
70              styleWrapperEl.addContent(styleEl.detach());
71  		} catch (InvalidXmlException e) {
72  			throw new RuntimeException("Error building JDom document for style", e);
73  		}
74  	}	
75  }