Skip to content

Commit

Permalink
fix DocHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
duantihua committed Apr 15, 2023
1 parent 5f3de14 commit 1bb303f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
35 changes: 18 additions & 17 deletions docx/src/main/scala/org/beangle/doc/docx/DocHelper.scala
Expand Up @@ -18,6 +18,7 @@
package org.beangle.doc.docx

import org.apache.poi.xwpf.usermodel.XWPFDocument
import org.beangle.commons.lang.Strings

import java.io.ByteArrayOutputStream
import java.net.URL
Expand All @@ -27,21 +28,14 @@ object DocHelper {
def toDoc(url: URL, data: collection.Map[String, String]): Array[Byte] = {
val templateIs = url.openStream()
val doc = new XWPFDocument(templateIs)
import scala.jdk.javaapi.CollectionConverters._
import scala.jdk.javaapi.CollectionConverters.*

for (p <- asScala(doc.getParagraphs)) {
val runs = p.getRuns
if (runs != null) {
for (r <- asScala(runs)) {
var text = r.getText(0)
if (text != null) {
data foreach { case (k, v) =>
if (text.contains("${" + k + "}")) {
text = text.replace("${" + k + "}", v)
}
}
r.setText(text, 0)
}
val text = r.getText(0)
if (text != null && text.contains("${")) r.setText(replace(text, data), 0)
}
}
}
Expand All @@ -51,13 +45,8 @@ object DocHelper {
for (cell <- asScala(row.getTableCells)) {
for (p <- asScala(cell.getParagraphs)) {
for (r <- asScala(p.getRuns)) {
var text = r.getText(0)
if (text != null) {
data.find { case (k, v) => text.contains("${" + k + "}") } foreach { e =>
text = text.replace("${" + e._1 + "}", e._2)
r.setText(text, 0)
}
}
val text = r.getText(0)
if (text != null && text.contains("${")) r.setText(replace(text, data), 0)
}
}
}
Expand All @@ -68,4 +57,16 @@ object DocHelper {
templateIs.close()
bos.toByteArray
}

private[docx] def replace(template: String, data: collection.Map[String, String]): String = {
var text = template
while (text.contains("${")) {
val k = Strings.substringBetween(text, "${", "}").trim()
val v = data.getOrElse(k, "")
val begin = text.indexOf("${")
val end = text.indexOf("}") + 1
text = Strings.replace(text, text.substring(begin, end), v)
}
text
}
}
39 changes: 39 additions & 0 deletions docx/src/test/scala/org/beangle/doc/docx/DocHelperTest.scala
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2005, The Beangle Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.beangle.doc.docx

import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

class DocHelperTest extends AnyFunSpec with Matchers {
val data = Map("param1" -> "value1", "param2" -> "value2")

describe("DocHelper replace function") {
it("replace normal") {
DocHelper.replace("${param1} is fine", data) should equal("value1 is fine")
}

it("replace normal multiple") {
DocHelper.replace("${param1} != ${param2}", data) should equal("value1 != value2")
}

it("replace expression with space") {
DocHelper.replace("${param1 } != ${ param2}", data) should equal("value1 != value2")
}
}
}
2 changes: 1 addition & 1 deletion project/plugin.sbt
@@ -1,3 +1,3 @@
addSbtPlugin("org.beangle.parent" % "sbt-beangle-parent" % "0.8.0")
addSbtPlugin("org.beangle.parent" % "sbt-beangle-parent" % "0.8.4")
addSbtPlugin("org.beangle.build" % "sbt-beangle-build" % "0.0.11")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")

0 comments on commit 1bb303f

Please sign in to comment.