raimund@259: /* raimund@259: * Copyright (c) 2011 by Intevation GmbH, Germany raimund@259: * raimund@259: * This file is part of MXD2map. raimund@259: * raimund@259: * This program is free software under the LGPL (>=v2.1) raimund@259: * Read the file LICENCE.txt coming with the software for details raimund@259: * or visit http://www.gnu.org/licenses/ if it does not exist. raimund@259: * raimund@259: * MXD2map has been developed on behalf of the raimund@259: * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg raimund@259: * by Intevation GmbH. raimund@259: * raimund@259: * Authors: raimund@259: * Raimund Renkert raimund@259: * Bjoern Schilberg raimund@259: * Stephan Holl raimund@259: */ raimund@259: raimund@259: package de.intevation.mxd.utils; raimund@259: raimund@259: import org.apache.log4j.Logger; raimund@259: raimund@259: import edu.umn.gis.mapscript.mapObj; raimund@259: raimund@259: /** raimund@259: * MapScript utilities. raimund@259: * This utility class provides some useful functions when working with the raimund@259: * MapScript Java API. raimund@259: * raimund@259: * @author Raimund Renkert raimund@259: */ raimund@259: public class MapScriptUtils raimund@259: { raimund@259: /** raimund@259: * The Logger. raimund@259: */ raimund@259: private static final Logger logger = Logger.getLogger(MapScriptUtils.class); raimund@259: raimund@259: raimund@259: /** raimund@259: * This function is a wrapper for mapObj.getMetaData. raimund@259: * The mapObj.getMetaData raises a not catchable error if the metadata key raimund@259: * can not be found in the map. To avoid this, use this function instead. raimund@259: * raimund@259: * @param map The map object. raimund@259: * @param key The metadata key. raimund@259: * raimund@259: * @return Empty string if the key was not found, else the value. raimund@259: */ raimund@259: public String getMetaData (mapObj map, String key) { raimund@259: logger.debug("getMetaData(mapObj, String)"); raimund@259: String meta = map.getFirstMetaDataKey(); raimund@259: if(meta.equals("")) { raimund@259: return ""; raimund@259: } raimund@259: while(meta != null && !meta.equals(key)) { raimund@259: meta = map.getNextMetaDataKey(meta); raimund@259: } raimund@259: if(meta == null || meta.equals("")) { raimund@259: return ""; raimund@259: } raimund@259: else { raimund@259: return map.getMetaData(meta); raimund@259: } raimund@259: } raimund@259: raimund@259: raimund@259: /** raimund@259: * Removes special characters and whitespaces. raimund@259: * raimund@259: * @param s The input string. raimund@259: * raimund@259: * @return The input string without special chars. raimund@259: */ raimund@259: public String removeSpecialChars (String s) { raimund@259: if(s.equals("")) { raimund@259: return ""; raimund@259: } raimund@259: raimund@259: String tmp = s.trim(); raimund@259: tmp = tmp.replace(" ", ""); raimund@259: tmp = tmp.replace("/", ""); raimund@259: tmp = tmp.replace(",", ""); raimund@259: tmp = tmp.replace("#", ""); raimund@259: tmp = tmp.replace("+", ""); raimund@259: tmp = tmp.replace("~", ""); raimund@259: tmp = tmp.replace("(", ""); raimund@259: tmp = tmp.replace(")", ""); raimund@259: tmp = tmp.replace("{", ""); raimund@259: tmp = tmp.replace("}", ""); raimund@259: tmp = tmp.replace("[", ""); raimund@259: tmp = tmp.replace("]", ""); raimund@259: tmp = tmp.replace("<", ""); raimund@259: tmp = tmp.replace(">", ""); raimund@259: tmp = tmp.replace("*", ""); raimund@259: tmp = tmp.replace("\\\\", ""); raimund@259: tmp = tmp.replace("^", ""); raimund@259: if(!s.equals(tmp)) { raimund@259: logger.info("Renamed \"" + s +"\" to \"" + tmp +"\"."); raimund@259: } raimund@259: return tmp; raimund@259: } raimund@259: raimund@259: raimund@259: /** raimund@259: * Replaces german umlauts and removes leading and trailing whitespaces. raimund@259: * raimund@259: * @param s The input string. raimund@259: * raimund@259: * @return The string without german umlauts. raimund@259: */ raimund@259: public String replaceUmlauts (String s) { raimund@259: if (s.equals("")) { raimund@259: return ""; raimund@259: } raimund@259: String tmp = s.trim(); aheinecke@333: tmp = tmp.replace ("\u00f6", "oe"); aheinecke@333: tmp = tmp.replace ("\u00e4", "ae"); aheinecke@333: tmp = tmp.replace ("\u00fc", "ue"); aheinecke@333: tmp = tmp.replace ("\u00df", "ss"); raimund@259: if(!s.equals(tmp)) { raimund@259: logger.info("Renamed \"" + s + "\" to \"" + tmp + "\"."); raimund@259: } raimund@259: return tmp; raimund@259: } raimund@259: } raimund@259: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :