mbien commented on code in PR #161:
URL: https://github.com/apache/roller/pull/161#discussion_r3940131333


##########
app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomWriter.java:
##########
@@ -0,0 +1,235 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.weblogger.webservices.atomprotocol;
+
+import static 
org.apache.roller.weblogger.webservices.atomprotocol.AtomConstants.APP_NS;
+import static 
org.apache.roller.weblogger.webservices.atomprotocol.AtomConstants.ATOM_NS;
+
+import java.io.OutputStream;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * Serializes the AtomPub wire model ({@link AtomEntry}, {@link AtomFeed},
+ * {@link AtomServiceDoc}) to XML using the JDK StAX API. Replaces the ROME and
+ * Propono serialization the AtomPub server previously relied on.
+ *
+ * <p>Atom entries and feeds are written with the Atom namespace as the default
+ * namespace (so atom elements are unprefixed) and the APP namespace bound to 
the
+ * {@code app} prefix. Service documents use the reverse: APP as the default
+ * namespace and {@code atom} for atom elements.
+ */
+public class AtomWriter {
+
+    private static final DateTimeFormatter RFC3339 =
+            
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);
+
+    private final XMLOutputFactory factory = XMLOutputFactory.newInstance();
+
+    static String formatDate(Date date) {
+        return date == null ? null : RFC3339.format(date.toInstant());
+    }
+
+    public void writeEntry(OutputStream out, AtomEntry entry) throws 
AtomException {
+        try {
+            XMLStreamWriter w = factory.createXMLStreamWriter(out, "UTF-8");
+            w.writeStartDocument("UTF-8", "1.0");
+            w.setDefaultNamespace(ATOM_NS);
+            w.setPrefix("app", APP_NS);
+            w.writeStartElement(ATOM_NS, "entry");
+            w.writeDefaultNamespace(ATOM_NS);
+            w.writeNamespace("app", APP_NS);
+            writeEntryBody(w, entry);
+            w.writeEndElement();
+            w.writeEndDocument();
+            w.flush();
+            w.close();
+        } catch (XMLStreamException ex) {
+            throw new AtomException("Error serializing Atom entry", ex);
+        }
+    }
+
+    public void writeFeed(OutputStream out, AtomFeed feed) throws 
AtomException {
+        try {
+            XMLStreamWriter w = factory.createXMLStreamWriter(out, "UTF-8");
+            w.writeStartDocument("UTF-8", "1.0");
+            w.setDefaultNamespace(ATOM_NS);
+            w.setPrefix("app", APP_NS);
+            w.writeStartElement(ATOM_NS, "feed");
+            w.writeDefaultNamespace(ATOM_NS);
+            w.writeNamespace("app", APP_NS);
+            writeAtomText(w, "id", feed.getId());
+            writeAtomText(w, "title", feed.getTitle());
+            writeAtomText(w, "updated", formatDate(feed.getUpdated()));
+            for (AtomLink link : feed.getLinks()) {
+                writeLink(w, link);
+            }
+            for (AtomEntry entry : feed.getEntries()) {
+                w.writeStartElement(ATOM_NS, "entry");
+                writeEntryBody(w, entry);
+                w.writeEndElement();
+            }
+            w.writeEndElement();
+            w.writeEndDocument();
+            w.flush();
+            w.close();

Review Comment:
   just a nitpick: assuming this was generated by a LLM loop -> would be good 
to check if it knows what java lang level to use. Since try-with-resource 
blocks etc would reduce the noise a bit.
   
   But this can be also done afterwards with IDEs and refactoring actions. Not 
necessarily worth to spend cyber credits for rewrites.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to