import scala.io.Codec import collection.mutable.{HashMap, MultiMap, Set} import scala.xml.{NodeSeq, PrettyPrinter} import java.util.Calendar import java.text.SimpleDateFormat import java.io.FileOutputStream import java.nio.channels.Channels import TermwikiParser._ object TermwikiExporter extends App { val usage = """ Usage: dumpfile """ type OptionMap = Map[Symbol, String] override def main(args: Array[String]) { if (args.length == 0) println(usage) val arglist = args.toList def nextOption(map : OptionMap, list: List[String]): OptionMap = { def isSwitch(s : String) = (s(0) == '-') list match { case Nil => map case string :: opt2 :: tail if isSwitch(opt2) => nextOption(map ++ Map('infile -> string), list.tail) case string :: Nil => nextOption(map ++ Map('infile -> string), list.tail) case option :: tail => println("Unknown option "+option) sys.exit(1) } } val options = nextOption(Map(),arglist) process(options) } private def process(map: OptionMap) = { implicit val codec = Codec.UTF8 val timeFormat = new SimpleDateFormat("yyyy-MM-dd") val timestamp = timeFormat.format(Calendar.getInstance().getTime()) val file = Option(map).get('infile) val iterator = TermwikiParser.iterator(file) var termcenter: Set[NodeSeq] = Set() val termsLang = new HashMap[String, Set[NodeSeq]] with MultiMap[String, NodeSeq] iterator.foreach { term => termcenter += term.toXml term.entries foreach {e => termsLang.addBinding(e.language, e.toXmlInLang(term.title))} } iterator.close() save(termcenter, "terms/termcenter.xml", "null", timestamp) termsLang foreach {l => save(l._2, "terms/terms-" + l._1 + ".xml", l._1, timestamp)} } private def save(nodes: Set[NodeSeq], fileName: String, lang: String, timestamp: String) = { val pp = new PrettyPrinter(80, 2) val fos = new FileOutputStream(fileName) val writer = Channels.newWriter(fos.getChannel(), "UTF-8") try { writer.write("\n") nodes foreach { x => writer.write(pp.formatNodes(x)) } writer.write("") } finally { writer.close() } } }