Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty 12 : XmlAppendable use Charset, not String #8609

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;

Expand All @@ -30,31 +33,12 @@ public class XmlAppendable
private final Stack<String> _tags = new Stack<>();
private String _space = "";

public XmlAppendable(OutputStream out, String encoding) throws IOException
public XmlAppendable(OutputStream out) throws IOException
{
this(new OutputStreamWriter(out, encoding), encoding);
}

public XmlAppendable(Appendable out) throws IOException
{
this(out, 2);
}

public XmlAppendable(Appendable out, String encoding) throws IOException
{
this(out, 2, encoding);
}

public XmlAppendable(Appendable out, int indent) throws IOException
{
this(out, indent, "utf-8");
}

public XmlAppendable(Appendable out, int indent, String encoding) throws IOException
{
_out = out;
_indent = indent;
_out.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\"?>\n");
Charset utf8 = StandardCharsets.UTF_8;
_out = new OutputStreamWriter(out, utf8);
_indent = 2;
_out.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
}

public XmlAppendable openTag(String tag, Map<String, String> attributes) throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.eclipse.jetty.xml;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -25,8 +27,8 @@ public class XmlAppendableTest
@Test
public void test() throws Exception
{
StringBuilder b = new StringBuilder();
XmlAppendable out = new XmlAppendable(b);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XmlAppendable out = new XmlAppendable(outputStream);
Map<String, String> attr = new LinkedHashMap<>();

out.openTag("test");
Expand All @@ -45,22 +47,26 @@ public void test() throws Exception
out.closeTag();

String expected =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<test>\n" +
" <tag/>\n" +
" <tag name=\"attr value\" noval=\"\" quotes=\"&apos;&quot;\"/>\n" +
" <tag name=\"attr value\" noval=\"\" quotes=\"&apos;&quot;\">content</tag>\n" +
" <level1>\n" +
" <tag>content</tag>\n" +
" <tag>content</tag>\n" +
" </level1>\n" +
" <level1 name=\"attr value\" noval=\"\" quotes=\"&apos;&quot;\">\n" +
" <level2>\n" +
" <tag>content</tag>\n" +
" <tag>content</tag>\n" +
" </level2>\n" +
" </level1>\n" +
"</test>\n";
assertEquals(expected, b.toString());
"""
<?xml version="1.0" encoding="utf-8"?>
<test>
<tag/>
<tag name="attr value" noval="" quotes="&apos;&quot;"/>
<tag name="attr value" noval="" quotes="&apos;&quot;">content</tag>
<level1>
<tag>content</tag>
<tag>content</tag>
</level1>
<level1 name="attr value" noval="" quotes="&apos;&quot;">
<level2>
<tag>content</tag>
<tag>content</tag>
</level2>
</level1>
</test>
""";

String result = outputStream.toString(StandardCharsets.UTF_8);
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
webappAttr.put("metadata-complete", Boolean.toString(context.getMetaData().isMetaDataComplete()));
webappAttr.put("version", major + "." + minor);

XmlAppendable out = new XmlAppendable(stream, "UTF-8");
XmlAppendable out = new XmlAppendable(stream);
out.openTag("web-app", webappAttr);
if (context.getDisplayName() != null)
out.tag("display-name", context.getDisplayName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
webappAttr.put("metadata-complete", Boolean.toString(context.getMetaData().isMetaDataComplete()));
webappAttr.put("version", major + "." + minor);

XmlAppendable out = new XmlAppendable(stream, "UTF-8");
XmlAppendable out = new XmlAppendable(stream);
out.openTag("web-app", webappAttr);
if (context.getDisplayName() != null)
out.tag("display-name", context.getDisplayName());
Expand Down