forked from akka/alpakka
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the serialization of the ParseEvent from xml-parser
- Loading branch information
Showing
6 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
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
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
31 changes: 31 additions & 0 deletions
31
xml/src/main/scala/akka/stream/alpakka/xml/javadsl/XmlWriting.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.xml.javadsl | ||
|
||
import java.nio.charset.{Charset, StandardCharsets} | ||
|
||
import akka.NotUsed | ||
import akka.stream.alpakka.xml.ParseEvent | ||
import akka.stream.alpakka.xml.Xml.StreamingXmlWriter | ||
import akka.stream.scaladsl.Flow | ||
import akka.util.ByteString | ||
|
||
object XmlWriting { | ||
|
||
/** | ||
* Writer Flow that takes a stream of XML events similar to SAX and write ByteStrings. | ||
* encoding UTF-8 | ||
*/ | ||
def writer(): akka.stream.javadsl.Flow[ParseEvent, ByteString, NotUsed] = | ||
Flow.fromGraph(new StreamingXmlWriter(StandardCharsets.UTF_8)).asJava | ||
|
||
/** | ||
* Writer Flow that takes a stream of XML events similar to SAX and write ByteStrings. | ||
* @param strEncoding encoding of the stream | ||
*/ | ||
def writer(charset: Charset): akka.stream.javadsl.Flow[ParseEvent, ByteString, NotUsed] = | ||
Flow.fromGraph(new StreamingXmlWriter(charset)).asJava | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
xml/src/main/scala/akka/stream/alpakka/xml/scaladsl/XmlWriting.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.xml.scaladsl | ||
|
||
import java.nio.charset.{Charset, StandardCharsets} | ||
|
||
import akka.NotUsed | ||
import akka.stream.alpakka.xml.ParseEvent | ||
import akka.stream.alpakka.xml.Xml.StreamingXmlWriter | ||
import akka.stream.scaladsl.Flow | ||
import akka.util.ByteString | ||
|
||
object XmlWriting { | ||
|
||
/** | ||
* Writer Flow that takes a stream of XML events similar to SAX and write ByteStrings. | ||
* @param charset charset of encoding | ||
*/ | ||
def writer(charset: Charset): Flow[ParseEvent, ByteString, NotUsed] = | ||
Flow.fromGraph(new StreamingXmlWriter(charset)) | ||
|
||
/** | ||
* Writer Flow that takes a stream of XML events similar to SAX and write ByteStrings. | ||
* encoding UTF-8 | ||
*/ | ||
val writer: Flow[ParseEvent, ByteString, NotUsed] = writer(StandardCharsets.UTF_8) | ||
} |
78 changes: 78 additions & 0 deletions
78
xml/src/test/java/akka/stream/alpakka/xml/javadsl/XmlWritingTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.xml.javadsl; | ||
|
||
import akka.actor.ActorSystem; | ||
import akka.stream.ActorMaterializer; | ||
import akka.stream.Materializer; | ||
import akka.stream.alpakka.xml.*; | ||
import akka.stream.javadsl.Flow; | ||
import akka.stream.javadsl.Keep; | ||
import akka.stream.javadsl.Sink; | ||
import akka.stream.javadsl.Source; | ||
import akka.testkit.JavaTestKit; | ||
import akka.util.ByteString; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class XmlWritingTest { | ||
private static ActorSystem system; | ||
private static Materializer materializer; | ||
|
||
@Test | ||
public void xmlWriter() throws InterruptedException, ExecutionException, TimeoutException { | ||
|
||
// #writer | ||
final Sink<ParseEvent, CompletionStage<String>> write = Flow.of(ParseEvent.class) | ||
.via(XmlWriting.writer()) | ||
.map((ByteString bs) -> bs.utf8String()) | ||
.toMat(Sink.fold("", (acc, el) -> acc + el), Keep.right()); | ||
// #writer | ||
|
||
// #writer-usage | ||
final String doc = "<?xml version='1.0' encoding='UTF-8'?><doc><elem>elem1</elem><elem>elem2</elem></doc>"; | ||
final List<ParseEvent> docList= new ArrayList<ParseEvent>(); | ||
docList.add(StartDocument.getInstance()); | ||
docList.add(StartElement.create("doc", Collections.emptyMap())); | ||
docList.add(StartElement.create("elem", Collections.emptyMap())); | ||
docList.add(Characters.create("elem1")); | ||
docList.add(EndElement.create("elem")); | ||
docList.add(StartElement.create("elem", Collections.emptyMap())); | ||
docList.add(Characters.create("elem2")); | ||
docList.add(EndElement.create("elem")); | ||
docList.add(EndElement.create("doc")); | ||
docList.add(EndDocument.getInstance()); | ||
|
||
|
||
final CompletionStage<String> resultStage = Source.from(docList).runWith(write, materializer); | ||
// #writer-usage | ||
|
||
resultStage.thenAccept((str) -> { | ||
assertEquals(doc,str); | ||
}).toCompletableFuture().get(5, TimeUnit.SECONDS); | ||
} | ||
|
||
@BeforeClass | ||
public static void setup() throws Exception { | ||
system = ActorSystem.create(); | ||
materializer = ActorMaterializer.create(system); | ||
} | ||
|
||
@AfterClass | ||
public static void teardown() throws Exception { | ||
JavaTestKit.shutdownActorSystem(system); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
xml/src/test/scala/akka/stream/alpakka/xml/scaladsl/XmlWritingTest.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.xml.scaladsl | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.alpakka.xml._ | ||
import akka.stream.scaladsl.{Flow, Keep, Sink, Source} | ||
import org.scalatest.concurrent.PatienceConfiguration.Timeout | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec} | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
|
||
class XmlWritingTest extends WordSpec with Matchers with BeforeAndAfterAll with ScalaFutures { | ||
implicit val system = ActorSystem("Test") | ||
implicit val mat = ActorMaterializer() | ||
|
||
// #writer | ||
val writer: Sink[ParseEvent, Future[String]] = Flow[ParseEvent] | ||
.via(XmlWriting.writer) | ||
.map[String](_.utf8String) | ||
.toMat(Sink.fold[String, String]("")((t, u) => t + u))(Keep.right) | ||
// #writer | ||
|
||
"XML Writer" must { | ||
|
||
"properly write simple XML" in { | ||
// #writer-usage | ||
val listEl: List[ParseEvent] = List( | ||
StartDocument, | ||
StartElement("doc", Map.empty), | ||
StartElement("elem", Map.empty), | ||
Characters("elem1"), | ||
EndElement("elem"), | ||
StartElement("elem", Map.empty), | ||
Characters("elem2"), | ||
EndElement("elem"), | ||
EndElement("doc"), | ||
EndDocument | ||
) | ||
|
||
val doc = "<?xml version='1.0' encoding='UTF-8'?><doc><elem>elem1</elem><elem>elem2</elem></doc>" | ||
val resultFuture: Future[String] = Source.fromIterator[ParseEvent](() => listEl.iterator).runWith(writer) | ||
// #writer-usage | ||
|
||
resultFuture.futureValue(Timeout(20.seconds)) should ===(doc) | ||
} | ||
|
||
"properly process a comment" in { | ||
val doc = "<?xml version='1.0' encoding='UTF-8'?><doc><!--comment--></doc>" | ||
val listEl = List( | ||
StartDocument, | ||
StartElement("doc", Map.empty), | ||
Comment("comment"), | ||
EndElement("doc"), | ||
EndDocument | ||
) | ||
|
||
val resultFuture: Future[String] = Source.fromIterator[ParseEvent](() => listEl.iterator).runWith(writer) | ||
|
||
resultFuture.futureValue(Timeout(20.seconds)) should ===(doc) | ||
} | ||
|
||
"properly process parse instructions" in { | ||
val doc = """<?xml version='1.0' encoding='UTF-8'?><?target content?><doc/>""" | ||
val listEl = List( | ||
StartDocument, | ||
ProcessingInstruction(Some("target"), Some("content")), | ||
StartElement("doc", Map.empty), | ||
EndElement("doc"), | ||
EndDocument | ||
) | ||
|
||
val resultFuture: Future[String] = Source.fromIterator[ParseEvent](() => listEl.iterator).runWith(writer) | ||
|
||
resultFuture.futureValue(Timeout(20.seconds)) should ===(doc) | ||
|
||
} | ||
|
||
"properly process attributes" in { | ||
val doc = | ||
"""<?xml version='1.0' encoding='UTF-8'?><doc good="yes"><elem nice="yes" very="true">elem1</elem></doc>""" | ||
val listEl = List( | ||
StartDocument, | ||
StartElement("doc", Map("good" -> "yes")), | ||
StartElement("elem", Map("nice" -> "yes", "very" -> "true")), | ||
Characters("elem1"), | ||
EndElement("elem"), | ||
EndElement("doc"), | ||
EndDocument | ||
) | ||
|
||
val resultFuture: Future[String] = Source.fromIterator[ParseEvent](() => listEl.iterator).runWith(writer) | ||
|
||
resultFuture.futureValue(Timeout(3.seconds)) should ===(doc) | ||
} | ||
|
||
"properly process CData blocks" in { | ||
val doc = """<?xml version='1.0' encoding='UTF-8'?><doc><![CDATA[<not>even</valid>]]></doc>""" | ||
|
||
val listEl = List( | ||
StartDocument, | ||
StartElement("doc", Map.empty), | ||
CData("<not>even</valid>"), | ||
EndElement("doc"), | ||
EndDocument | ||
) | ||
val resultFuture: Future[String] = Source.fromIterator[ParseEvent](() => listEl.iterator).runWith(writer) | ||
|
||
resultFuture.futureValue(Timeout(3.seconds)) should ===(doc) | ||
} | ||
|
||
} | ||
|
||
override protected def afterAll(): Unit = system.terminate() | ||
} |