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.core.util.xml;
18  
19  import java.util.Date;
20  
21  import org.jdom.CDATA;
22  import org.jdom.Element;
23  import org.jdom.Namespace;
24  import org.kuali.rice.core.util.RiceConstants;
25  
26  /**
27   * A helper class which helps with building the XML for objects.
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class XmlRenderer {
32  
33      private Namespace namespace;
34      
35      public XmlRenderer() {}
36      
37      public XmlRenderer(Namespace namespace) {
38          this.namespace = namespace;
39      }
40      
41      public Element renderElement(Element parent, String elementName) {
42          Element element = new Element(elementName, namespace);
43          if (parent != null) {
44              parent.addContent(element);
45          }
46          return element;
47      }
48      
49      public Element renderTextElement(Element parent, String elementName, String text) {
50          Element element = null;
51          if (text != null) {
52              element = renderElement(parent, elementName);
53              element.setText(text);
54          }
55          return element;
56      }
57      
58      public Element renderBooleanElement(Element parent, String elementName, Boolean bool, boolean defaultValue) {
59          if (bool == null) {
60              bool = new Boolean(defaultValue);
61          }
62          return renderTextElement(parent, elementName, (bool.booleanValue() ? "true" : "false"));   
63      }
64      
65      public Element renderDateElement(Element parent, String elementName, Date date) {
66          Element element = null;
67          if (date != null) {
68              element = renderTextElement(parent, elementName, RiceConstants.getDefaultDateFormat().format(date));
69          }
70          return element;
71      }
72      
73      public void renderAttribute(Element element, String attributeName, String attributeValue) {
74          element.setAttribute(attributeName, attributeValue);
75      }
76      
77      public Element renderCDATAElement(Element parent, String elementName, String data) {
78      	Element element = null;
79          if (data != null) {
80              element = renderElement(parent, elementName);
81              element.addContent(new CDATA(data));
82          }
83          return element;
84      }
85  
86  }