added a little spike of a little std.dom API to make the W3C DOM API a little kooler
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module" module-name="stdlib" exported="" />
|
||||||
|
<orderEntry type="module" module-name="testlib" exported="" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package stdlib.testall;
|
||||||
|
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
import test.dom.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public class DomTestAllTest {
|
||||||
|
public static TestSuite suite() {
|
||||||
|
return new TestSuite(DomTest.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package stdlib.testall;
|
||||||
|
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
import test.collections.*;
|
||||||
|
import test.standard.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public class TestAll {
|
||||||
|
public static TestSuite suite() {
|
||||||
|
TestSuite suite = new TestSuite(GetOrElseTest.class, test.stdlib.issues.StdLibIssuesTest.class, StandardCollectionTest.class, CollectionTest.class, IoTest.class, ListTest.class, MapTest.class, SetTest.class, OldStdlibTest.class);
|
||||||
|
suite.addTest(testDslExample.namespace.getSuite());
|
||||||
|
return suite;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,6 +68,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/runtests/runtests.iml" filepath="$PROJECT_DIR$/runtests/runtests.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/stdlib/stdlib.iml" filepath="$PROJECT_DIR$/stdlib/stdlib.iml" />
|
<module fileurl="file://$PROJECT_DIR$/stdlib/stdlib.iml" filepath="$PROJECT_DIR$/stdlib/stdlib.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/testlib/testlib.iml" filepath="$PROJECT_DIR$/testlib/testlib.iml" />
|
<module fileurl="file://$PROJECT_DIR$/testlib/testlib.iml" filepath="$PROJECT_DIR$/testlib/testlib.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package std.dom
|
||||||
|
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import javax.xml.parsers.DocumentBuilder
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory
|
||||||
|
import std.getOrElse
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
|
||||||
|
var Element.id : String
|
||||||
|
get() = this.getAttribute("id").getOrElse("")
|
||||||
|
set(value) {
|
||||||
|
this.setAttribute("id", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
var Element.style : String
|
||||||
|
get() = this.getAttribute("style").getOrElse("")
|
||||||
|
set(value) {
|
||||||
|
this.setAttribute("style", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
var Element.cssClass : String
|
||||||
|
get() = this.getAttribute("class").getOrElse("")
|
||||||
|
set(value) {
|
||||||
|
this.setAttribute("class", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Syntax sugar
|
||||||
|
|
||||||
|
inline fun Node.plus(child: Node?): Node {
|
||||||
|
if (child != null) {
|
||||||
|
this.appendChild(child)
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* JVM specific API implementations using JAXB and so forth which would not be used when compiling to JS
|
||||||
|
*/
|
||||||
|
package std.dom
|
||||||
|
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import javax.xml.parsers.DocumentBuilder
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory
|
||||||
|
import javax.xml.transform.Transformer
|
||||||
|
import javax.xml.transform.TransformerFactory
|
||||||
|
import javax.xml.transform.Source
|
||||||
|
import javax.xml.transform.dom.DOMSource
|
||||||
|
import javax.xml.transform.stream.StreamResult
|
||||||
|
|
||||||
|
import java.io.StringWriter
|
||||||
|
import javax.xml.transform.OutputKeys
|
||||||
|
|
||||||
|
fun createDocument(builder: DocumentBuilder): Document {
|
||||||
|
return builder.newDocument().sure()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createDocument(builderFactory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance().sure()): Document {
|
||||||
|
return createDocument(builderFactory.newDocumentBuilder().sure())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createTransformer(source: Source? = null, factory: TransformerFactory = TransformerFactory.newInstance().sure()): Transformer {
|
||||||
|
val transformer = if (source != null) {
|
||||||
|
factory.newTransformer(source)
|
||||||
|
} else {
|
||||||
|
factory.newTransformer()
|
||||||
|
}
|
||||||
|
return transformer.sure()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Node.toXmlString(xmlDeclaration: Boolean = this is Document): String {
|
||||||
|
return nodeToXmlString(this, xmlDeclaration)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fun Document.toXmlString(xmlDeclaration: Boolean = true): String {
|
||||||
|
return nodeToXmlString(this, xmlDeclaration)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
fun nodeToXmlString(node: Node, xmlDeclaration: Boolean): String {
|
||||||
|
val transformer = createTransformer()
|
||||||
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, if (xmlDeclaration) "no" else "yes")
|
||||||
|
val buffer = StringWriter()
|
||||||
|
transformer.transform(DOMSource(node), StreamResult(buffer))
|
||||||
|
return buffer.toString().sure()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package test.dom
|
||||||
|
|
||||||
|
import std.*
|
||||||
|
import std.dom.*
|
||||||
|
import stdhack.test.TestSupport
|
||||||
|
import stdhack.test.*
|
||||||
|
import stdhack.test.assertNull
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import stdhack.test.assertEquals
|
||||||
|
import std.dom.toXmlString
|
||||||
|
|
||||||
|
class DomTest() : TestSupport() {
|
||||||
|
|
||||||
|
fun testCreateDocument() {
|
||||||
|
var doc = createDocument()
|
||||||
|
assertNotNull(doc, "Should have created a document")
|
||||||
|
|
||||||
|
val e = doc.createElement("foo").sure()
|
||||||
|
assertCssClass(e, "")
|
||||||
|
|
||||||
|
// now lets update the cssClass property
|
||||||
|
e.cssClass = "foo"
|
||||||
|
assertCssClass(e, "foo")
|
||||||
|
|
||||||
|
// now using the attribute directly
|
||||||
|
e.setAttribute("class", "bar")
|
||||||
|
assertCssClass(e, "bar")
|
||||||
|
|
||||||
|
doc += e
|
||||||
|
println("document ${doc.toXmlString()}")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun assertCssClass(e: Element, value: String?): Unit {
|
||||||
|
val cl = e.cssClass
|
||||||
|
val cl2 = e.getAttribute("class")
|
||||||
|
println("element ${e.toXmlString()} has cssClass `${cl}` class attr `${cl2}`")
|
||||||
|
|
||||||
|
assertEquals(value, cl, "value of element.cssClass")
|
||||||
|
assertEquals(value, cl2, "value of element.getAttribute(\"class\")")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user