From dee125eda4df31f63df88250088a01f6280f8e4d Mon Sep 17 00:00:00 2001 From: Claus Nagel Date: Sat, 9 Mar 2024 18:31:16 +0100 Subject: [PATCH] avoid unnecessary conversion to OffsetDateTime in TextContent --- .../org/xmlobjects/annotation/XMLElement.java | 1 + .../org/xmlobjects/builder/ObjectBuilder.java | 8 +- src/main/java/org/xmlobjects/model/Child.java | 1 + .../serializer/ObjectSerializer.java | 12 +- .../org/xmlobjects/stream/BuildResult.java | 2 +- .../xmlobjects/stream/XMLWriterFactory.java | 2 +- .../java/org/xmlobjects/util/Properties.java | 2 +- .../util/xml/DepthXMLStreamReader.java | 2 +- .../java/org/xmlobjects/xml/TextContent.java | 161 ++++++++++-------- 9 files changed, 107 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/xmlobjects/annotation/XMLElement.java b/src/main/java/org/xmlobjects/annotation/XMLElement.java index 8033103..37753f4 100644 --- a/src/main/java/org/xmlobjects/annotation/XMLElement.java +++ b/src/main/java/org/xmlobjects/annotation/XMLElement.java @@ -27,5 +27,6 @@ @Retention(value = RetentionPolicy.RUNTIME) public @interface XMLElement { String name(); + String namespaceURI() default XMLConstants.NULL_NS_URI; } diff --git a/src/main/java/org/xmlobjects/builder/ObjectBuilder.java b/src/main/java/org/xmlobjects/builder/ObjectBuilder.java index 28377ba..22615c1 100644 --- a/src/main/java/org/xmlobjects/builder/ObjectBuilder.java +++ b/src/main/java/org/xmlobjects/builder/ObjectBuilder.java @@ -29,6 +29,10 @@ @IndexSubclasses public interface ObjectBuilder { T createObject(QName name, Object parent) throws ObjectBuildException; - default void initializeObject(T object, QName name, Attributes attributes, XMLReader reader) throws ObjectBuildException, XMLReadException { } - default void buildChildObject(T object, QName name, Attributes attributes, XMLReader reader) throws ObjectBuildException, XMLReadException { } + + default void initializeObject(T object, QName name, Attributes attributes, XMLReader reader) throws ObjectBuildException, XMLReadException { + } + + default void buildChildObject(T object, QName name, Attributes attributes, XMLReader reader) throws ObjectBuildException, XMLReadException { + } } diff --git a/src/main/java/org/xmlobjects/model/Child.java b/src/main/java/org/xmlobjects/model/Child.java index b23ebf9..de8f57f 100644 --- a/src/main/java/org/xmlobjects/model/Child.java +++ b/src/main/java/org/xmlobjects/model/Child.java @@ -21,6 +21,7 @@ public interface Child { Child getParent(); + void setParent(Child parent); default T getParent(Class type) { diff --git a/src/main/java/org/xmlobjects/serializer/ObjectSerializer.java b/src/main/java/org/xmlobjects/serializer/ObjectSerializer.java index 28ba44f..0970c0f 100644 --- a/src/main/java/org/xmlobjects/serializer/ObjectSerializer.java +++ b/src/main/java/org/xmlobjects/serializer/ObjectSerializer.java @@ -27,7 +27,13 @@ @IndexSubclasses public interface ObjectSerializer { - default Element createElement(T object, Namespaces namespaces) throws ObjectSerializeException { return null; } - default void initializeElement(Element element, T object, Namespaces namespaces, XMLWriter writer) throws ObjectSerializeException, XMLWriteException { } - default void writeChildElements(T object, Namespaces namespaces, XMLWriter writer) throws ObjectSerializeException, XMLWriteException { } + default Element createElement(T object, Namespaces namespaces) throws ObjectSerializeException { + return null; + } + + default void initializeElement(Element element, T object, Namespaces namespaces, XMLWriter writer) throws ObjectSerializeException, XMLWriteException { + } + + default void writeChildElements(T object, Namespaces namespaces, XMLWriter writer) throws ObjectSerializeException, XMLWriteException { + } } diff --git a/src/main/java/org/xmlobjects/stream/BuildResult.java b/src/main/java/org/xmlobjects/stream/BuildResult.java index 5a71ee7..c6c65fa 100644 --- a/src/main/java/org/xmlobjects/stream/BuildResult.java +++ b/src/main/java/org/xmlobjects/stream/BuildResult.java @@ -71,7 +71,7 @@ public void ifDOMElement(Consumer action) { } } - public Element getDOMElement(){ + public Element getDOMElement() { return element; } } diff --git a/src/main/java/org/xmlobjects/stream/XMLWriterFactory.java b/src/main/java/org/xmlobjects/stream/XMLWriterFactory.java index 4a42eb3..1ce79d4 100644 --- a/src/main/java/org/xmlobjects/stream/XMLWriterFactory.java +++ b/src/main/java/org/xmlobjects/stream/XMLWriterFactory.java @@ -134,7 +134,7 @@ public XMLWriter createWriter(SAXWriter saxWriter) { xmlWriter.setProperties(properties); return xmlWriter; } - + public XMLWriter createWriter(ContentHandler contentHandler) { XMLWriter xmlWriter = new XMLWriter(xmlObjects, new SAXOutputHandler(contentHandler)); xmlWriter.setProperties(properties); diff --git a/src/main/java/org/xmlobjects/util/Properties.java b/src/main/java/org/xmlobjects/util/Properties.java index f9ed69a..0644567 100644 --- a/src/main/java/org/xmlobjects/util/Properties.java +++ b/src/main/java/org/xmlobjects/util/Properties.java @@ -57,7 +57,7 @@ public boolean getAndCompare(String name, Object expectedValue) { public T getOrDefault(String name, Class type, Supplier supplier) { T value = get(name, type); - return value != null ? value : supplier.get(); + return value != null ? value : supplier.get(); } public T getOrSet(String name, Class type, Supplier supplier) { diff --git a/src/main/java/org/xmlobjects/util/xml/DepthXMLStreamReader.java b/src/main/java/org/xmlobjects/util/xml/DepthXMLStreamReader.java index 8d0e3d0..eb8ad39 100644 --- a/src/main/java/org/xmlobjects/util/xml/DepthXMLStreamReader.java +++ b/src/main/java/org/xmlobjects/util/xml/DepthXMLStreamReader.java @@ -131,7 +131,7 @@ public int next() throws XMLStreamException { @Override public void require(int type, String namespaceURI, String localName) throws XMLStreamException { - reader.require(type,namespaceURI,localName); + reader.require(type, namespaceURI, localName); } @Override diff --git a/src/main/java/org/xmlobjects/xml/TextContent.java b/src/main/java/org/xmlobjects/xml/TextContent.java index f2df183..d8aa6cf 100644 --- a/src/main/java/org/xmlobjects/xml/TextContent.java +++ b/src/main/java/org/xmlobjects/xml/TextContent.java @@ -37,7 +37,8 @@ public class TextContent { private static boolean WITH_TIME_OFFSET = true; private static boolean WITH_DATE_OFFSET = false; - private enum Fields { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, NANO, TIMEZONE } + private enum Fields {YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, NANO, TIMEZONE} + private static final EnumSet DATE_TIME_FIELDS = EnumSet.allOf(Fields.class); private static final EnumSet TIME_FIELDS = EnumSet.of(Fields.HOUR, Fields.MINUTE, Fields.SECOND, Fields.NANO, Fields.TIMEZONE); private static final EnumSet DATE_FIELDS = EnumSet.of(Fields.YEAR, Fields.MONTH, Fields.DAY, Fields.TIMEZONE); @@ -557,11 +558,11 @@ public void ifDurationList(Consumer> action) { } public OffsetDateTime getAsDateTime() { - return getAsOffsetDateTime("dateTime"); + return toOffsetDateTime(getAsCalender("dateTime")); } public boolean isDateTime() { - return getAsDateTime() != null; + return getAsCalender("dateTime") != null; } public void ifDateTime(Consumer action) { @@ -572,11 +573,11 @@ public void ifDateTime(Consumer action) { } public List getAsDateTimeList() { - return getAsOffsetDateTimeList("dateTime"); + return toOffsetDateTimeList(getAsCalenderList("dateTime")); } public boolean isDateTimeList() { - return getAsDateTimeList() != null; + return getAsCalenderList("dateTime") != null; } public void ifDateTimeList(Consumer> action) { @@ -587,11 +588,11 @@ public void ifDateTimeList(Consumer> action) { } public OffsetDateTime getAsTime() { - return getAsOffsetDateTime("time"); + return toOffsetDateTime(getAsCalender("time")); } public boolean isTime() { - return getAsTime() != null; + return getAsCalender("time") != null; } public void ifTime(Consumer action) { @@ -602,11 +603,11 @@ public void ifTime(Consumer action) { } public List getAsTimeList() { - return getAsOffsetDateTimeList("time"); + return toOffsetDateTimeList(getAsCalenderList("time")); } public boolean isTimeList() { - return getAsTimeList() != null; + return getAsCalenderList("time") != null; } public void ifTimeList(Consumer> action) { @@ -617,11 +618,11 @@ public void ifTimeList(Consumer> action) { } public OffsetDateTime getAsDate() { - return getAsOffsetDateTime("date"); + return toOffsetDateTime(getAsCalender("date")); } public boolean isDate() { - return getAsDate() != null; + return getAsCalender("date") != null; } public void ifDate(Consumer action) { @@ -632,11 +633,11 @@ public void ifDate(Consumer action) { } public List getAsDateList() { - return getAsOffsetDateTimeList("date"); + return toOffsetDateTimeList(getAsCalenderList("date")); } public boolean isDateList() { - return getAsDateList() != null; + return getAsCalenderList("date") != null; } public void ifDateList(Consumer> action) { @@ -647,11 +648,11 @@ public void ifDateList(Consumer> action) { } public OffsetDateTime getAsGYearMonth() { - return getAsOffsetDateTime("gYearMonth"); + return toOffsetDateTime(getAsCalender("gYearMonth")); } public boolean isGYearMonth() { - return getAsGYearMonth() != null; + return getAsCalender("gYearMonth") != null; } public void ifGYearMonth(Consumer action) { @@ -662,11 +663,11 @@ public void ifGYearMonth(Consumer action) { } public List getAsGYearMonthList() { - return getAsOffsetDateTimeList("gYearMonth"); + return toOffsetDateTimeList(getAsCalenderList("gYearMonth")); } public boolean isGYearMonthList() { - return getAsGYearMonthList() != null; + return getAsCalenderList("gYearMonth") != null; } public void ifGYearMonthList(Consumer> action) { @@ -677,11 +678,11 @@ public void ifGYearMonthList(Consumer> action) { } public OffsetDateTime getAsGMonthDay() { - return getAsOffsetDateTime("gMonthDay"); + return toOffsetDateTime(getAsCalender("gMonthDay")); } public boolean isGMonthDay() { - return getAsGMonthDay() != null; + return getAsCalender("gMonthDay") != null; } public void ifGMonthDay(Consumer action) { @@ -692,11 +693,11 @@ public void ifGMonthDay(Consumer action) { } public List getAsGMonthDayList() { - return getAsOffsetDateTimeList("gMonthDay"); + return toOffsetDateTimeList(getAsCalenderList("gMonthDay")); } public boolean isGMonthDayList() { - return getAsGMonthDayList() != null; + return getAsCalenderList("gMonthDay") != null; } public void ifGMonthDayList(Consumer> action) { @@ -707,11 +708,11 @@ public void ifGMonthDayList(Consumer> action) { } public OffsetDateTime getAsGDay() { - return getAsOffsetDateTime("gDay"); + return toOffsetDateTime(getAsCalender("gDay")); } public boolean isGDay() { - return getAsGDay() != null; + return getAsCalender("gDay") != null; } public void ifGDay(Consumer action) { @@ -722,11 +723,11 @@ public void ifGDay(Consumer action) { } public List getAsGDayList() { - return getAsOffsetDateTimeList("gDay"); + return toOffsetDateTimeList(getAsCalenderList("gDay")); } public boolean isGDayList() { - return getAsGDayList() != null; + return getAsCalenderList("gDay") != null; } public void ifGDayList(Consumer> action) { @@ -737,11 +738,11 @@ public void ifGDayList(Consumer> action) { } public OffsetDateTime getAsGMonth() { - return getAsOffsetDateTime("gMonth"); + return toOffsetDateTime(getAsCalender("gMonth")); } public boolean isGMonth() { - return getAsGMonth() != null; + return getAsCalender("gMonth") != null; } public void ifGMonth(Consumer action) { @@ -752,11 +753,11 @@ public void ifGMonth(Consumer action) { } public List getAsGMonthList() { - return getAsOffsetDateTimeList("gMonth"); + return toOffsetDateTimeList(getAsCalenderList("gMonth")); } public boolean isGMonthList() { - return getAsGMonthList() != null; + return getAsCalenderList("gMonth") != null; } public void ifGMonthList(Consumer> action) { @@ -767,11 +768,11 @@ public void ifGMonthList(Consumer> action) { } public OffsetDateTime getAsGYear() { - return getAsOffsetDateTime("gYear"); + return toOffsetDateTime(getAsCalender("gYear")); } public boolean isGYear() { - return getAsGYear() != null; + return getAsCalender("gYear") != null; } public void ifGYear(Consumer action) { @@ -782,11 +783,11 @@ public void ifGYear(Consumer action) { } public List getAsGYearList() { - return getAsOffsetDateTimeList("gYear"); + return toOffsetDateTimeList(getAsCalenderList("gYear")); } public boolean isGYearList() { - return getAsGYearList() != null; + return getAsCalenderList("gYear") != null; } public void ifGYearList(Consumer> action) { @@ -804,25 +805,29 @@ public TextContent withZoneOffsetProvider(Function zo return this; } - private OffsetDateTime getAsOffsetDateTime(String localName) { + private T setValue(T value) { + this.value = value; + return value; + } + + private XMLGregorianCalendar getAsCalender(String localName) { if (value instanceof XMLGregorianCalendar calendar && calendar.getXMLSchemaType().getLocalPart().equals(localName)) { - return toOffsetDateTime((XMLGregorianCalendar) value); + return calendar; } else if (!isEmpty() && !trimmedContent().isEmpty()) { - XMLGregorianCalendar calendar = setValue(toCalendar(trimmedContent(), localName)); - return calendar != null ? toOffsetDateTime(calendar) : null; + return setValue(toCalendar(trimmedContent(), localName)); } else { return setValue(null); } } @SuppressWarnings("unchecked") - private List getAsOffsetDateTimeList(String localName) { + private List getAsCalenderList(String localName) { List calendars; if (value instanceof List list && list.stream().allMatch(v -> v instanceof XMLGregorianCalendar calendar && calendar.getXMLSchemaType().getLocalPart().equals(localName))) { - calendars = (List) value; + return (List) list; } else if (tokenizeContent() != 0) { calendars = new ArrayList<>(tokenizedContent.length); for (String token : tokenizedContent) { @@ -834,22 +839,10 @@ private List getAsOffsetDateTimeList(String localName) { } } - setValue(calendars); + return setValue(calendars); } else { return setValue(null); } - - List result = new ArrayList<>(calendars.size()); - for (XMLGregorianCalendar calendar : calendars) { - result.add(toOffsetDateTime(calendar)); - } - - return result; - } - - private T setValue(T value) { - this.value = value; - return value; } private boolean isListOfType(Object value, Class type) { @@ -884,29 +877,47 @@ private XMLGregorianCalendar toCalendar(String value, String localName) { } private OffsetDateTime toOffsetDateTime(XMLGregorianCalendar calendar) { - int day = calendar.getDay(); - int month = calendar.getMonth(); - int year = calendar.getYear(); - int hour = calendar.getHour(); - int minute = calendar.getMinute(); - int second = calendar.getSecond(); - int offset = calendar.getTimezone(); - - BigDecimal fractional = calendar.getFractionalSecond(); - int nano = fractional != null ? (int) (fractional.doubleValue() * 1e+9) : DatatypeConstants.FIELD_UNDEFINED; - - LocalDateTime dateTime = LocalDateTime.of( - year != DatatypeConstants.FIELD_UNDEFINED ? year : 0, - month != DatatypeConstants.FIELD_UNDEFINED ? month : 1, - day != DatatypeConstants.FIELD_UNDEFINED ? day : 1, - hour != DatatypeConstants.FIELD_UNDEFINED ? hour : 0, - minute != DatatypeConstants.FIELD_UNDEFINED ? minute : 0, - second != DatatypeConstants.FIELD_UNDEFINED ? second : 0, - nano != DatatypeConstants.FIELD_UNDEFINED ? nano : 0); - - return OffsetDateTime.of(dateTime, offset != DatatypeConstants.FIELD_UNDEFINED ? - ZoneOffset.ofTotalSeconds(offset * 60) : - zoneOffsetProvider.apply(dateTime)); + if (calendar != null) { + int day = calendar.getDay(); + int month = calendar.getMonth(); + int year = calendar.getYear(); + int hour = calendar.getHour(); + int minute = calendar.getMinute(); + int second = calendar.getSecond(); + int offset = calendar.getTimezone(); + + BigDecimal fractional = calendar.getFractionalSecond(); + int nano = fractional != null ? + (int) (fractional.doubleValue() * 1e+9) : + DatatypeConstants.FIELD_UNDEFINED; + + LocalDateTime dateTime = LocalDateTime.of( + year != DatatypeConstants.FIELD_UNDEFINED ? year : 0, + month != DatatypeConstants.FIELD_UNDEFINED ? month : 1, + day != DatatypeConstants.FIELD_UNDEFINED ? day : 1, + hour != DatatypeConstants.FIELD_UNDEFINED ? hour : 0, + minute != DatatypeConstants.FIELD_UNDEFINED ? minute : 0, + second != DatatypeConstants.FIELD_UNDEFINED ? second : 0, + nano != DatatypeConstants.FIELD_UNDEFINED ? nano : 0); + + return OffsetDateTime.of(dateTime, offset != DatatypeConstants.FIELD_UNDEFINED ? + ZoneOffset.ofTotalSeconds(offset * 60) : + zoneOffsetProvider.apply(dateTime)); + } else { + return null; + } + } + + private List toOffsetDateTimeList(List calenders) { + if (calenders != null) { + List dateTimes = new ArrayList<>(calenders.size()); + calenders.stream() + .map(this::toOffsetDateTime) + .forEach(dateTimes::add); + return dateTimes; + } else { + return null; + } } private static TextContent ofObject(Object content) {