Include value of of date properties as ISO formatted date into XML output

git-svn-id: https://svn.libreccm.org/ccm/trunk@5868 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2019-03-18 19:24:33 +00:00
parent bd2de4c0ca
commit 63037eb2e2
2 changed files with 111 additions and 61 deletions

View File

@ -39,16 +39,19 @@ import java.util.Locale;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;
/** /**
* An implementation of DomainObjectTraversal that generates an XML tree representing the * An implementation of DomainObjectTraversal that generates an XML tree
* DomainObject. The output format of the XML can be controlled using the various setWrapXXX methods * representing the DomainObject. The output format of the XML can be controlled
* detailed below. * using the various setWrapXXX methods detailed below.
* *
* @version $Id: DomainObjectXMLRenderer.java 2141 2011-01-16 12:17:15Z pboy $ * @version $Id: DomainObjectXMLRenderer.java 2141 2011-01-16 12:17:15Z pboy $
*/ */
public class DomainObjectXMLRenderer extends DomainObjectTraversal { public class DomainObjectXMLRenderer extends DomainObjectTraversal {
private static final Logger s_log = Logger.getLogger(DomainObjectXMLRenderer.class); private static final Logger s_log = Logger.getLogger(
DomainObjectXMLRenderer.class);
private static Map s_formatters = new HashMap(); private static Map s_formatters = new HashMap();
private Stack m_elements = new Stack(); private Stack m_elements = new Stack();
protected Element m_element; protected Element m_element;
@ -81,7 +84,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
* @param type the object type whose items will be traversed * @param type the object type whose items will be traversed
* @param context the context in which the formatter should be used * @param context the context in which the formatter should be used
*/ */
public static void unregisterFormatter(final ObjectType type, final String context) { public static void unregisterFormatter(final ObjectType type,
final String context) {
s_formatters.remove(new AdapterKey(type, context)); s_formatters.remove(new AdapterKey(type, context));
} }
@ -97,7 +101,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
final String context) { final String context) {
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("Registering formatter " s_log.debug("Registering formatter "
+ formatter.getClass().getName() + " for type " + type + formatter.getClass().getName() + " for type "
+ type
+ " in context " + context); + " in context " + context);
} }
@ -124,27 +129,32 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
* @param type the object type to lookup * @param type the object type to lookup
* @param context the formatter context * @param context the formatter context
*/ */
public static DomainObjectXMLFormatter getFormatter(final ObjectType type, final String context) { public static DomainObjectXMLFormatter getFormatter(final ObjectType type,
return (DomainObjectXMLFormatter) s_formatters.get(new AdapterKey(type, context)); final String context) {
return (DomainObjectXMLFormatter) s_formatters.get(new AdapterKey(type,
context));
} }
/** /**
* Retrieves the closest matching traversal formatter for an object type in a given context. The * Retrieves the closest matching traversal formatter for an object type in
* algorithm looks for an exact match, then considers the supertype, and the supertype's * a given context. The algorithm looks for an exact match, then considers
* supertype. If no match could be found at all, returns null * the supertype, and the supertype's supertype. If no match could be found
* at all, returns null
* *
* @param type the object type to search for * @param type the object type to search for
* @param context the formatter context * @param context the formatter context
* *
* @return * @return
*/ */
public static DomainObjectXMLFormatter findFormatter(final ObjectType type, final String context) { public static DomainObjectXMLFormatter findFormatter(final ObjectType type,
final String context) {
DomainObjectXMLFormatter formatter = null; DomainObjectXMLFormatter formatter = null;
ObjectType curType = type; ObjectType curType = type;
while (formatter == null && curType != null) { while (formatter == null && curType != null) {
formatter = getFormatter(curType, context); formatter = getFormatter(curType, context);
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("getFormatter(" + curType + "," + context + ")=" + formatter); s_log.debug("getFormatter(" + curType + "," + context + ")="
+ formatter);
} }
curType = curType.getSupertype(); curType = curType.getSupertype();
} }
@ -152,8 +162,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
/** /**
* Creates a new DomainObject XML renderer that outputs XML into the element passed into the * Creates a new DomainObject XML renderer that outputs XML into the element
* constructor. * passed into the constructor.
* *
* @param root the XML element in which to output children * @param root the XML element in which to output children
*/ */
@ -167,13 +177,15 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
m_namespaceURI = uri; m_namespaceURI = uri;
} }
protected Object format(final DomainObject obj, final String path, final Property prop, protected Object format(final DomainObject obj, final String path,
final Property prop,
final Object value) { final Object value) {
if (m_formatter != null) { if (m_formatter != null) {
final String propertyPath = appendToPath(path, prop.getName()); final String propertyPath = appendToPath(path, prop.getName());
Object rendered = m_formatter.format(obj, propertyPath, prop, value); Object rendered = m_formatter.format(obj, propertyPath, prop, value);
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("FORMAT " + obj + " m_formatter=" + m_formatter + " rendered=" s_log.debug("FORMAT " + obj + " m_formatter=" + m_formatter
+ " rendered="
+ rendered); + rendered);
} }
@ -181,15 +193,18 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
// try supertype formatters // try supertype formatters
ObjectType objectType = obj.getObjectType().getSupertype(); ObjectType objectType = obj.getObjectType().getSupertype();
DomainObjectXMLFormatter formatter = m_formatter; DomainObjectXMLFormatter formatter = m_formatter;
while (rendered == null && formatter != null && objectType != null) { while (rendered == null && formatter != null && objectType
!= null) {
formatter = findFormatter(objectType, m_context); formatter = findFormatter(objectType, m_context);
if (formatter == null) { if (formatter == null) {
rendered = null; rendered = null;
} else { } else {
rendered = formatter.format(obj, propertyPath, prop, value); rendered = formatter.format(obj, propertyPath, prop,
value);
} }
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("FALLBACK supertype " + objectType + " formatter=" + formatter s_log.debug("FALLBACK supertype " + objectType
+ " formatter=" + formatter
+ " rendered=" + " rendered="
+ rendered); + rendered);
} }
@ -224,8 +239,9 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
/** /**
* Determines XML output for root object. If set to true a separate element will be output for * Determines XML output for root object. If set to true a separate element
* the root object, if false, then the element passed into the constructor will be used. * will be output for the root object, if false, then the element passed
* into the constructor will be used.
* *
* @param value * @param value
*/ */
@ -234,9 +250,10 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
/** /**
* Determines XML output used for objects. If set to true, then a wrapper XML element will be * Determines XML output used for objects. If set to true, then a wrapper
* generated for the association, and then individual elements generated for each object. If * XML element will be generated for the association, and then individual
* false then no wrapper XML element will be produced. * elements generated for each object. If false then no wrapper XML element
* will be produced.
* *
* @param value * @param value
*/ */
@ -245,16 +262,18 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
/** /**
* Determines XML output used for scalar attributes. If set to true, then each attribute is * Determines XML output used for scalar attributes. If set to true, then
* output as a separate element, otherwise, attributes are output as simple attributes. * each attribute is output as a separate element, otherwise, attributes are
* output as simple attributes.
*/ */
public void setWrapAttributes(final boolean value) { public void setWrapAttributes(final boolean value) {
m_wrapAttributes = value; m_wrapAttributes = value;
} }
/** /**
* Determines XML output used for objects. If set to true, then repeated objects will generate * Determines XML output used for objects. If set to true, then repeated
* full xml. If false then only the OID will be printed. * objects will generate full xml. If false then only the OID will be
* printed.
*/ */
public void setRevisitFullObject(final boolean value) { public void setRevisitFullObject(final boolean value) {
m_revisitFullObject = value; m_revisitFullObject = value;
@ -341,51 +360,76 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
// locale-independent date output // locale-independent date output
element.addAttribute("year", element.addAttribute("year",
Integer.toString(calDate.get(Calendar.YEAR))); Integer.toString(calDate.get(
Calendar.YEAR)));
element.addAttribute("month", element.addAttribute("month",
Integer.toString(calDate.get(Calendar.MONTH) + 1)); Integer.toString(calDate.get(
Calendar.MONTH) + 1));
element.addAttribute("day", element.addAttribute("day",
Integer.toString(calDate.get(Calendar.DAY_OF_MONTH))); Integer.toString(calDate.get(
Calendar.DAY_OF_MONTH)));
element.addAttribute("dayOfWeek", element.addAttribute("dayOfWeek",
Integer.toString(calDate.get(Calendar.DAY_OF_WEEK))); Integer.toString(calDate.get(
Calendar.DAY_OF_WEEK)));
element.addAttribute("hour", element.addAttribute("hour",
Integer.toString(calDate.get(Calendar.HOUR_OF_DAY))); Integer.toString(calDate.get(
Calendar.HOUR_OF_DAY)));
element.addAttribute("minute", element.addAttribute("minute",
Integer.toString(calDate.get(Calendar.MINUTE))); Integer.toString(calDate.get(
Calendar.MINUTE)));
element.addAttribute("second", element.addAttribute("second",
Integer.toString(calDate.get(Calendar.SECOND))); Integer.toString(calDate.get(
Calendar.SECOND)));
// Quasimodo: BEGIN // Quasimodo: BEGIN
// Add attributes for date and time // Add attributes for date and time
final Locale negLocale = GlobalizationHelper.getNegotiatedLocale(); final Locale negLocale = GlobalizationHelper
final DateFormat dateFormatter = DateFormat.getDateInstance( .getNegotiatedLocale();
final DateFormat dateFormatter = DateFormat
.getDateInstance(
DateFormat.MEDIUM, negLocale); DateFormat.MEDIUM, negLocale);
final DateFormat longDateFormatter = DateFormat.getDateInstance( final DateFormat longDateFormatter = DateFormat
.getDateInstance(
DateFormat.LONG, negLocale); DateFormat.LONG, negLocale);
final DateFormat timeFormatter = DateFormat.getTimeInstance( final DateFormat timeFormatter = DateFormat
.getTimeInstance(
DateFormat.SHORT, negLocale); DateFormat.SHORT, negLocale);
element.addAttribute("date", dateFormatter.format(date)); element.addAttribute("date", dateFormatter.format(date));
element.addAttribute("longDate", longDateFormatter.format(date)); element.addAttribute("longDate", longDateFormatter
.format(date));
element.addAttribute("time", timeFormatter.format(date)); element.addAttribute("time", timeFormatter.format(date));
element.addAttribute("monthName", calDate.getDisplayName( element.addAttribute("monthName", calDate
Calendar.MONTH, Calendar.LONG, negLocale)); .getDisplayName(
element.addAttribute("dayName", calDate.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.MONTH, Calendar.LONG,
negLocale));
element.addAttribute("dayName", calDate.getDisplayName(
Calendar.DAY_OF_WEEK,
Calendar.LONG, Calendar.LONG,
negLocale)); negLocale));
element.addAttribute("dayNameShort", calDate.getDisplayName( element.addAttribute("dayNameShort", calDate
Calendar.DAY_OF_WEEK, Calendar.SHORT, negLocale)); .getDisplayName(
Calendar.DAY_OF_WEEK,
Calendar.SHORT, negLocale));
// Quasimodo: END // Quasimodo: END
// jensp ISO date/time in ISO format
final DateFormat isoDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.ROOT);
element
.addAttribute("iso", isoDateFormat.format(date));
} }
} }
} else { } else {
m_element.addAttribute(property.getName(), m_element.addAttribute(property.getName(),
(String) format(obj, path, property, value)); (String) format(obj, path, property,
value));
} }
} }
} }
protected void beginRole(final DomainObject obj, final String path, final Property property) { protected void beginRole(final DomainObject obj, final String path,
final Property property) {
if (m_wrapObjects) { if (m_wrapObjects) {
final Element element = newElement(m_element, property.getName()); final Element element = newElement(m_element, property.getName());
m_elements.push(m_element); m_elements.push(m_element);
@ -393,7 +437,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
} }
protected void endRole(final DomainObject obj, final String path, final Property property) { protected void endRole(final DomainObject obj, final String path,
final Property property) {
if (m_wrapObjects) { if (m_wrapObjects) {
m_element = (Element) m_elements.pop(); m_element = (Element) m_elements.pop();
} }
@ -408,7 +453,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
} }
} }
protected void endAssociation(final DomainObject obj, final String path, final Property property) { protected void endAssociation(final DomainObject obj, final String path,
final Property property) {
if (m_wrapObjects) { if (m_wrapObjects) {
m_element = (Element) m_elements.pop(); m_element = (Element) m_elements.pop();
} }
@ -444,7 +490,8 @@ public class DomainObjectXMLRenderer extends DomainObjectTraversal {
// m_namespaceURI); // m_namespaceURI);
} }
protected Element newElement(final Element parent, final String name, final Element copy) { protected Element newElement(final Element parent, final String name,
final Element copy) {
if (m_namespaceURI == null) { if (m_namespaceURI == null) {
return parent.newChildElement(name, copy); return parent.newChildElement(name, copy);
} else { } else {

View File

@ -18,9 +18,6 @@
package com.arsdigita.navigation; package com.arsdigita.navigation;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.categorization.Categorization;
import com.arsdigita.categorization.Category;
import com.arsdigita.cms.ContentBundle;
import com.arsdigita.cms.ContentItem; import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.dispatcher.ItemResolver; import com.arsdigita.cms.dispatcher.ItemResolver;
import com.arsdigita.cms.dispatcher.SimpleXMLGenerator; import com.arsdigita.cms.dispatcher.SimpleXMLGenerator;
@ -31,7 +28,6 @@ import com.arsdigita.kernel.Party;
import com.arsdigita.kernel.permissions.PermissionDescriptor; import com.arsdigita.kernel.permissions.PermissionDescriptor;
import com.arsdigita.kernel.permissions.PermissionService; import com.arsdigita.kernel.permissions.PermissionService;
import com.arsdigita.kernel.permissions.PrivilegeDescriptor; import com.arsdigita.kernel.permissions.PrivilegeDescriptor;
import com.arsdigita.globalization.GlobalizationHelper;
import com.arsdigita.persistence.DataAssociation; import com.arsdigita.persistence.DataAssociation;
import com.arsdigita.persistence.DataAssociationCursor; import com.arsdigita.persistence.DataAssociationCursor;
import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataCollection;
@ -45,16 +41,19 @@ import com.arsdigita.web.ParameterMap;
import com.arsdigita.web.URL; import com.arsdigita.web.URL;
import com.arsdigita.web.Web; import com.arsdigita.web.Web;
import com.arsdigita.xml.Element; import com.arsdigita.xml.Element;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;
public class DataCollectionRenderer extends LockableImpl { public class DataCollectionRenderer extends LockableImpl {
private static final Logger s_log = Logger.getLogger(DataCollectionRenderer.class); private static final Logger s_log = Logger.getLogger(DataCollectionRenderer.class);
@ -363,6 +362,10 @@ public class DataCollectionRenderer extends LockableImpl {
attribute.addAttribute("monthName", calDate.getDisplayName(Calendar.MONTH, Calendar.LONG, negLocale)); attribute.addAttribute("monthName", calDate.getDisplayName(Calendar.MONTH, Calendar.LONG, negLocale));
// Quasimodo: END // Quasimodo: END
// jensp ISO date/time in ISO format
final DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.ROOT);
attribute.addAttribute("iso", isoDateFormat.format(date));
} }
item.addContent(attribute); item.addContent(attribute);
} else { } else {