Last active
December 9, 2021 06:03
-
-
Save chumpa/19870444b51b26ab6b9a507671bdf0ea to your computer and use it in GitHub Desktop.
XmlMemoires
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.util.slurpersupport.GPathResult | |
import groovy.xml.StreamingMarkupBuilder | |
import groovy.xml.XmlUtil | |
import groovy.xml.streamingmarkupsupport.BaseMarkupBuilder | |
import org.junit.Assert | |
import org.junit.Before | |
import org.junit.Test | |
import org.xmlunit.builder.DiffBuilder | |
import org.xmlunit.builder.Input | |
import org.xmlunit.diff.Diff | |
/** | |
* Это шпаргалка по обработке XML с учётом билдеров и тд. Можно в гист. | |
*/ | |
class XmlMemories { | |
XmlSlurper xp = new XmlSlurper(false, true) | |
String xmlIn = """<root a="1" b="2" c="русскиеБуковыъ"> | |
<n1>1</n1> | |
<n1>1</n1> | |
<n2>2</n2> | |
<rus>Русскiя буквы</rus> | |
</root> | |
""" | |
GPathResult root = xp.parseText(xmlIn) | |
BaseMarkupBuilder.Document binding = null | |
@Before | |
void init() { | |
assert root.name() == "root" | |
} | |
void copy0__1(String name, GPathResult content, Closure<String> z = { String x -> x }) { | |
assert binding!=null | |
int sz = content.size() | |
if (sz > 0) this.binding."$name"(z.call(content.text())) | |
} | |
@Test | |
void "доступы и выборки"() { | |
Assert.assertEquals("112Русскiя буквы", root.toString()) | |
Assert.assertEquals("русскиеБуковыъ", [email protected]()) | |
Assert.assertEquals("11", root.n1.text()) //два текста склеены | |
Assert.assertEquals("1", root.n1[0].text()) //0-based | |
Assert.assertEquals("1", root.n1[1].text()) | |
Assert.assertEquals("", root.n1[2].text()) //элемента по номеру нет и вместо него текст "" | |
Assert.assertEquals(2, root.n1.size()) | |
Assert.assertEquals("", root.n3.text()) // элемента нет вообще и вместо него текст "" | |
Assert.assertNotEquals(null, root.n3.text()) // элемента нет вообще и для него текст != null | |
println("доступы и выборки - OK") | |
} | |
static Diff makeDiff(String xmlExpected, String xmlActual) { | |
return DiffBuilder.compare(Input.fromString(xmlExpected)) | |
.withTest(Input.fromString(xmlActual)) | |
.ignoreWhitespace() | |
.checkForSimilar() | |
.build() | |
} | |
static Diff makeDiff(String xmlExpected, Writable xmlActual) { | |
return makeDiff(xmlExpected, XmlUtil.serialize(xmlActual)) | |
} | |
void dc40(String num) { | |
assert binding!=null | |
binding."EDI_DC40"(MANDT: num) { | |
} | |
} | |
@Test | |
void "построение через StreamingMarkupBuilder"() { | |
Writable wr = new StreamingMarkupBuilder().bind { bnd -> | |
this.binding = bnd | |
"wr"("Русскiя буквы") | |
} as Writable | |
assert !makeDiff("<wr>Русскiя буквы</wr>", wr).hasDifferences() | |
wr = new StreamingMarkupBuilder().bind { bnd -> | |
this.binding = bnd | |
bnd.root { | |
dc40("100") | |
copy0__1("N1", root.n1) // скопирует <N1>11</N1>, т.е. текст от двух элементов | |
copy0__1("N2", root.n2) // скопирует | |
copy0__1("N3", root.n3) // не скопирует -- нечего | |
copy0__1("RUS", root.rus) // скопирует | |
} | |
} as Writable | |
assert !makeDiff("""<root><EDI_DC40 MANDT="100"/><N1>11</N1><N2>2</N2><RUS>Русскiя буквы</RUS></root>""", wr).hasDifferences() | |
println("построение через StreamingMarkupBuilder - OK") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment