/* * rico2edm * Copyright (C) 2021 UB Basel * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ package ch.memobase.rico2edm.utils import java.io.ByteArrayOutputStream import java.util.zip.Deflater import java.util.{Properties, HashMap => JHashMap} import scala.language.reflectiveCalls object Helper { private var isoLanguageCodes: Option[JHashMap[String,String]] = None def compress(data: Array[Byte]): Array[Byte] = { val deflater = new Deflater() deflater.setInput(data) val outputStream = new ByteArrayOutputStream(data.length) deflater.finish() val buffer = new Array[Byte](1024) while ( { !deflater.finished }) { val count = deflater.deflate(buffer) // returns the generated code... index outputStream.write(buffer, 0, count) } outputStream.close() outputStream.toByteArray } def initEnrichementMapping(props: Properties):Unit = { val isoCodes = new JHashMap[String,String]() using(io.Source.fromFile(props.get(Keys.LANGUAGE_ISO_CODE).toString)) { source => for (line <- source.getLines) { val temp = line.split(",").map(_.trim) isoCodes.put(temp(0),temp(1)) } } isoLanguageCodes = Some(isoCodes) } //noinspection ScalaStyle private def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B = try { f(resource) } finally { resource.close() } def getLanguageCode(wikiId:String):Option[String] = { isoLanguageCodes match { case Some(langkeys) if langkeys.containsKey(wikiId) => Some(langkeys.get(wikiId)) case Some(langkeys) if !langkeys.containsKey(wikiId) => None case None => None } } }