KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > NodeDataUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.cms.core.NodeData;
16
17 import java.util.Date JavaDoc;
18
19 import javax.jcr.PropertyType;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23
24
25 /**
26  * @author Sameer Charles
27  * @version $Revision: 1111 $ ($Author: fgiust $)
28  */

29 public class NodeDataUtil {
30
31     /**
32      * Logger.
33      */

34     private static Logger log = Logger.getLogger(NodeDataUtil.class);
35
36     NodeData nodeData;
37
38     public NodeDataUtil(NodeData nodeData) {
39         this.setNodeData(nodeData);
40     }
41
42     public void setNodeData(NodeData nodeData) {
43         this.nodeData = nodeData;
44     }
45
46     public NodeData getNodeData() {
47         return this.nodeData;
48     }
49
50     /**
51      * <p>
52      * Returns the representation of the value as a String:
53      * </p>
54      * @return String
55      */

56     public String JavaDoc getValueString() {
57         return getValueString(null);
58     }
59
60     /**
61      * <p>
62      * Returns the representation of the value as a String:
63      * </p>
64      * @return String
65      */

66     public String JavaDoc getValueString(String JavaDoc dateFormat) {
67         try {
68             NodeData nodeData = this.getNodeData();
69             switch (nodeData.getType()) {
70                 case (PropertyType.STRING):
71                     return nodeData.getString();
72                 case (PropertyType.DOUBLE):
73                     return Double.toString(nodeData.getDouble());
74                 case (PropertyType.LONG):
75                     return Long.toString(nodeData.getLong());
76                 case (PropertyType.BOOLEAN):
77                     return Boolean.toString(nodeData.getBoolean());
78                 case (PropertyType.DATE):
79                     Date JavaDoc valueDate = nodeData.getDate().getTime();
80                     return new DateUtil().getFormattedDate(valueDate, dateFormat);
81                 case (PropertyType.BINARY):
82                 // ???
83
default:
84                     return StringUtils.EMPTY;
85             }
86         }
87         catch (Exception JavaDoc e) {
88             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
89
}
90         return StringUtils.EMPTY;
91     }
92
93     public String JavaDoc getTypeName(int type) {
94
95         switch (type) {
96             case PropertyType.STRING:
97                 return PropertyType.TYPENAME_STRING;
98             case PropertyType.BOOLEAN:
99                 return PropertyType.TYPENAME_BOOLEAN;
100             case PropertyType.DATE:
101                 return PropertyType.TYPENAME_DATE;
102             case PropertyType.LONG:
103                 return PropertyType.TYPENAME_LONG;
104             case PropertyType.DOUBLE:
105                 return PropertyType.TYPENAME_DOUBLE;
106             case PropertyType.BINARY:
107                 return PropertyType.TYPENAME_BINARY;
108             default:
109                 return StringUtils.EMPTY;
110         }
111
112     }
113 }
114
Popular Tags