You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2023/03/07 22:47:58 UTC

[GitHub] [maven] gnodet opened a new pull request, #1043: Fix callee closing streams and readers instead of callers

gnodet opened a new pull request, #1043:
URL: https://github.com/apache/maven/pull/1043

   For compatibility reason, the Xpp3DomBuilder will close those
   
   Following this checklist to help us incorporate your
   contribution quickly and easily:
   
    - [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MNG) filed
          for the change (usually before you start working on it).  Trivial changes like typos do not
          require a JIRA issue. Your pull request should address just this issue, without
          pulling in other changes.
    - [ ] Each commit in the pull request should have a meaningful subject line and body.
    - [ ] Format the pull request title like `[MNG-XXX] SUMMARY`,
          where you replace `MNG-XXX` and `SUMMARY` with the appropriate JIRA issue.
    - [ ] Also format the first line of the commit message like `[MNG-XXX] SUMMARY`.
          Best practice is to use the JIRA issue title in both the pull request title and in the first line of the commit message.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [ ] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will
          be performed on your pull request automatically.
    - [ ] You have run the [Core IT][core-its] successfully.
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [ ] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [ ] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   [core-its]: https://maven.apache.org/core-its/core-it-suite/
   


-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "michael-o (via GitHub)" <gi...@apache.org>.
michael-o commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1128707959


##########
maven-xml-impl/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java:
##########
@@ -62,8 +64,10 @@ public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserExc
      */
     public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        return new Xpp3Dom(
-                XmlNodeBuilder.build(reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        try (Reader closeMe = reader) {
+            return new Xpp3Dom(XmlNodeBuilder.build(
+                    reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        }
     }

Review Comment:
   These two hunks I don't understand.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] gnodet commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1128725418


##########
maven-xml-impl/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java:
##########
@@ -62,8 +64,10 @@ public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserExc
      */
     public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        return new Xpp3Dom(
-                XmlNodeBuilder.build(reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        try (Reader closeMe = reader) {
+            return new Xpp3Dom(XmlNodeBuilder.build(
+                    reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        }
     }

Review Comment:
   The `XmlNodeBuilder` is new in maven 4, so we can change it the way we want.  The `Xpp3DomBuilder` comes from `plexus-utils` but now delegates to the the new `XmlNodeBuilder`.  In order to preserve compatibility, I'd rather keep the previous behaviour for this class.
   
   Fwiw, my plan is to release 4.0.0-alpha-5, and work on the plexus-xml / plexus-utils 4.0 split, which can then later be used in maven 4.0.0-alpha-6 to get rid of the redefined plexus classes in maven. 
   
   Does that make more sense ?



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] gnodet commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1129437408


##########
maven-xml-impl/src/main/java/org/apache/maven/internal/xml/XmlNodeBuilder.java:
##########
@@ -84,14 +81,9 @@ public static XmlNodeImpl build(Reader reader, boolean trim) throws XmlPullParse
      */
     public static XmlNodeImpl build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        try (Reader closeMe = reader) {
-            final XmlPullParser parser = new MXParser();
-            parser.setInput(reader);
-
-            final XmlNodeImpl node = build(parser, trim, locationBuilder);
-
-            return node;
-        }
+        XmlPullParser parser = new MXParser();

Review Comment:
   > Unless very specially coded the XML parser will consume the entire stream. It does not stop at the end of the first document.
   > I'm not sure about calling setInput multiple times. It can perhaps read from different streams in sequence, but not from the same stream twice, which will be fully consumed by a single parse.
   
   Note that MXParser is a pull parser, so it does not consume anything unless asked.  The one that asks is that `XmlNodeBuilder`.  The loop is [here](https://github.com/apache/maven/blob/4e098a3205e261ce8bc7fdfe30fa6f46928424d6/maven-xml-impl/src/main/java/org/apache/maven/internal/xml/XmlNodeBuilder.java#L124) and the parsing ends as soon as the  `END_DOCUMENT` token is received.   In a usual scenario, the stream can't really be reused, but it's not because the parser consume the entire stream, but rather because the parser buffers the data and consumes more that what is needed.  Forcing an unbuffered reader leads to a different behaviour.
   
   Fwiw, the following test passes:
   ```
       @Test
       public void testReadMultiDoc() throws Exception {
           String doc = "<?xml version='1.0'?><doc><child>foo</child></doc>";
           StringReader r = new StringReader(doc + doc) {
               @Override
               public int read(char[] cbuf, int off, int len) throws IOException {
                   return super.read(cbuf, off, 1);
               }
           };
           XmlNode node1 = XmlNodeBuilder.build(r);
           XmlNode node2 = XmlNodeBuilder.build(r);
           assertEquals(node1, node2);
       }
   ```
   
   Though I do agree, this may not be a common use case, or one that we do run into with maven.  Another similar use case with streams would be a `ZipInputStream` where the stream can be fully consumed, but should not be closed.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] elharo commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1128780743


##########
maven-xml-impl/src/main/java/org/apache/maven/internal/xml/XmlNodeBuilder.java:
##########
@@ -84,14 +81,9 @@ public static XmlNodeImpl build(Reader reader, boolean trim) throws XmlPullParse
      */
     public static XmlNodeImpl build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        try (Reader closeMe = reader) {
-            final XmlPullParser parser = new MXParser();
-            parser.setInput(reader);
-
-            final XmlNodeImpl node = build(parser, trim, locationBuilder);
-
-            return node;
-        }
+        XmlPullParser parser = new MXParser();

Review Comment:
   since this is a public method this is a potentially behavior breaking change, and could affect code outside this repo.
   
   I'm also not certain this is a good idea. An XML parser is going to process the entire stream. They're won't be anything left to read from, and closing twice isn't usually a problem whereas forgetting to close a file can be.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] gnodet commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1129178097


##########
maven-xml-impl/src/main/java/org/apache/maven/internal/xml/XmlNodeBuilder.java:
##########
@@ -84,14 +81,9 @@ public static XmlNodeImpl build(Reader reader, boolean trim) throws XmlPullParse
      */
     public static XmlNodeImpl build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        try (Reader closeMe = reader) {
-            final XmlPullParser parser = new MXParser();
-            parser.setInput(reader);
-
-            final XmlNodeImpl node = build(parser, trim, locationBuilder);
-
-            return node;
-        }
+        XmlPullParser parser = new MXParser();

Review Comment:
   The xml parser does not need to consume the whole stream.  This is definitely the common use case, but we can think of continuously reading xml pieces from a socket or any other input stream.  The `XmlNodeBuilder` / `MXParser` should support that when being called with `setInput(...)` multiple times : it should reset its internal state and be able to read again from the given stream.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] gnodet commented on pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #1043:
URL: https://github.com/apache/maven/pull/1043#issuecomment-1459625925

   > I don't think every PR needs a JIRA, bu since this one changes behavior of a public method it's probably worth one and wider discussion.
   
   @elharo Fwiw, `XmlNodeBuilder` has been introduced in the latest 4.0.0-alpha-4 release, and I would not consider it _public api_, not is it supposed to become part of a public api.  The _public api_ for new maven 4 stuff is `org.apache.maven.api.*` and that one is in the `org.apache.maven.`*`internal`*`.xml` package.


-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "michael-o (via GitHub)" <gi...@apache.org>.
michael-o commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1128733052


##########
maven-xml-impl/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java:
##########
@@ -62,8 +64,10 @@ public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserExc
      */
     public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        return new Xpp3Dom(
-                XmlNodeBuilder.build(reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        try (Reader closeMe = reader) {
+            return new Xpp3Dom(XmlNodeBuilder.build(
+                    reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
+        }
     }

Review Comment:
   Yes, absolutely!



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] elharo commented on a diff in pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #1043:
URL: https://github.com/apache/maven/pull/1043#discussion_r1129389367


##########
maven-xml-impl/src/main/java/org/apache/maven/internal/xml/XmlNodeBuilder.java:
##########
@@ -84,14 +81,9 @@ public static XmlNodeImpl build(Reader reader, boolean trim) throws XmlPullParse
      */
     public static XmlNodeImpl build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
             throws XmlPullParserException, IOException {
-        try (Reader closeMe = reader) {
-            final XmlPullParser parser = new MXParser();
-            parser.setInput(reader);
-
-            final XmlNodeImpl node = build(parser, trim, locationBuilder);
-
-            return node;
-        }
+        XmlPullParser parser = new MXParser();

Review Comment:
   Unless very specially coded the XML parser will consume the entire stream. It does not stop at the end of the first document. 
   
   I'm not sure about calling setInput multiple times. It can perhaps read from different streams in sequence, but not from the same stream twice, which will be fully consumed by a single parse. 



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] gnodet merged pull request #1043: Fix callee closing streams and readers instead of callers

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #1043:
URL: https://github.com/apache/maven/pull/1043


-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org