Skip to content

Commit

Permalink
remove prettifyXml
Browse files Browse the repository at this point in the history
  • Loading branch information
yffrankwang committed Jun 16, 2023
1 parent 81ba075 commit a15aaad
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 62 deletions.
47 changes: 0 additions & 47 deletions panda-core/src/main/java/panda/lang/Texts.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,53 +876,6 @@ public static String ellipsiz(String str, int len, String ellipsis) {
}
return str;
}

// -----------------------------------------------------------------------
private static class XmlPattern {
static Pattern p0 = Pattern.compile(".+</\\w[^>]*>$");
static Pattern p1 = Pattern.compile("^</\\w*>$");
static Pattern p2 = Pattern.compile("^<\\w[^>]*[^/]>.*$");
static Pattern p3 = Pattern.compile("(>)(<)(/*)");
}

/**
* prettify xml
*
* @param xml xml string
* @return prettified xml
*/
public static String prettifyXml(String xml) {
StringBuilder fmt = new StringBuilder();

xml = XmlPattern.p3.matcher(xml).replaceAll("$1\n$2$3");

String[] xs = Strings.split(xml, Strings.CRLF);

int pad = 0;
for (int i = 0; i < xs.length; i++) {
String node = xs[i];
int indent = 0;
if (XmlPattern.p0.matcher(node).matches()) {
indent = 0;
}
else if (XmlPattern.p1.matcher(node).matches()) {
if (pad != 0) {
pad -= 1;
}
}
else if (XmlPattern.p2.matcher(node).matches()) {
indent = 1;
}
else {
indent = 0;
}

fmt.append(Strings.leftPad(Strings.EMPTY, pad * 2, ' ')).append(node).append(Strings.CRLF);
pad += indent;
}

return fmt.toString();
}

// -----------------------------------------------------------------------
/**
Expand Down
12 changes: 0 additions & 12 deletions panda-core/src/test/java/panda/lang/TextsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,4 @@ public void testEllipsiz() throws Exception {
assertEquals(".", Texts.ellipsiz("あいうえお", 1));
assertEquals("", Texts.ellipsiz("あいうえお", 0));
}

// -----------------------------------------------------------------------
/**
* test method: prettifyXml
* @throws Exception if an error occurs
*/
@Test
public void testPrettifyXml() throws Exception {
assertEquals("<a>1</a>\r\n<b>2</b>\r\n", Texts.prettifyXml("<a>1</a><b>2</b>"));
//assertEquals("<a>\r\n <b>2</b>\r\n</a>\r\n", Texts.prettifyXml("<a><b>2</b></a>"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</div>
<#include "/action-alert.ftl"/>

<@p.form id="eleval" method="post" theme="simple" action="+/exec.xml" onsubmit="return elEvaluate();">
<@p.form id="eleval" method="post" theme="simple" action="+/exec.json" onsubmit="return elEvaluate();">
<table width="100%">
<tr><td>Expression: (Ctrl+Enter to evaluate)</td></tr>
<tr><td><@p.textarea name="expr" rows="8" cssStyle="width:100%" enterfire="#eleval_submit"/></td></tr>
Expand Down Expand Up @@ -40,9 +40,9 @@
url: $o.attr('action'),
method: 'post',
data: $o.serializeArray(),
dataType: 'text',
dataType: 'json',
success: function(data, ts, xhr) {
elSetResult(data.prettifyXml());
elSetResult(JSON.stringify(data, null, ' '));
},
error: function(xhr, ts, err) {
elSetResult(err + '\r\n' + xhr.responseText);
Expand Down

0 comments on commit a15aaad

Please sign in to comment.