Olabusayo Kilo created DAFFODIL-3042:
----------------------------------------

             Summary: Add XML style Formatter
                 Key: DAFFODIL-3042
                 URL: https://issues.apache.org/jira/browse/DAFFODIL-3042
             Project: Daffodil
          Issue Type: Improvement
          Components: Clean Ups
    Affects Versions: 3.11.0
            Reporter: Olabusayo Kilo


Our TDML and XSD files are often full of inconsistences that the IDEs try to 
fix polluting the change set. These inconsistences should be fixed using an XML 
Formatter. We couldn't find a sbt xml formatter plugin , but 
scala.xml.PrettyPrinter might be enough for our purposes with a custom task 
suggested by Gemini. 

{code:scala}
import scala.xml.PrettyPrinter
import sbt.io.IO
 
// Define a custom task key for formatting XML
val formatXml = taskKey[Unit]("Formats XML files in the project.")
 
formatXml := {
  val log = streams.value.log
  val files = (baseDirectory.value ** "*.xml").get ++ (baseDirectory.value ** 
"*.xsd").get ++ (baseDirectory.value ** "*.tdml").get
  if (files.isEmpty) {
    log.info("No XML files found to format.")
  } else {
    log.info(s"Formatting ${files.size} XML files...")
    val printer = new PrettyPrinter(80, 2, true) // Max width 80, indent 2 
spaces, minimize empty ex <foo />
    files.foreach { file =>
      try {
        val xmlText = IO.read(file)
        val formattedXml = printer.format(scala.xml.XML.loadString(xmlText))
        IO.write(file, formattedXml)
        log.info(s"Formatted: ${file.getName}")
      } catch {
        case e: Exception =>
          log.error(s"Failed to format ${file.getName}: ${e.getMessage}")
      }
    }
  }
}
{code}

There was also this maven plugin but sbt and mvn don't play nice

 https://github.com/hazendaz/xml-format-maven-plugin



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to