Java tutorial
//package com.java2s; /* * Copyright (C) 2014 University of Washington * * Originally developed by Dobility, Inc. (as part of SurveyCTO) * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; public class Main { private static final String COLUMN_SEPARATOR = ","; private static final String FALLBACK_COLUMN_SEPARATOR = " "; /** * We could simple return new String(displayColumns + "," + valueColumn) but we want to handle the cases * where the displayColumns (valueColumn) contain more commas than needed, in the middle, start or end. * * @param valueColumn single string to appear first. * @param displayColumns comma-separated string * @return A {@link java.util.LinkedHashMap} that contains the SQL columns as keys, and the CSV columns as values */ public static LinkedHashMap<String, String> createMapWithDisplayingColumns(String valueColumn, String displayColumns) { valueColumn = valueColumn.trim(); LinkedHashMap<String, String> columns = new LinkedHashMap<String, String>(); columns.put(toSafeColumnName(valueColumn), valueColumn); if (displayColumns != null && displayColumns.trim().length() > 0) { displayColumns = displayColumns.trim(); List<String> commaSplitParts = splitTrimmed(displayColumns, COLUMN_SEPARATOR, FALLBACK_COLUMN_SEPARATOR); for (String commaSplitPart : commaSplitParts) { columns.put(toSafeColumnName(commaSplitPart), commaSplitPart); } } return columns; } public static String toSafeColumnName(String columnName, Map<String, String> cache) { String cachedName = cache.get(columnName); if (cachedName == null) { String safeColumnName = toSafeColumnName(columnName); cache.put(columnName, safeColumnName); return safeColumnName; } else { return cachedName; } } public static String toSafeColumnName(String columnName) { // SCTO-567 - begin all column names with "c_" to avoid possible conflicts with // reserved keywords; also, escape any potentially-illegal characters return "c_" + columnName.trim().replaceAll("[^A-Za-z0-9_]", "_").toLowerCase(Locale.ENGLISH); } protected static List<String> splitTrimmed(String displayColumns, String separator, String fallbackSeparator) { List<String> commaSplitParts = splitTrimmed(displayColumns, separator); // SCTO-584: Fall back to a space-separated list if (commaSplitParts.size() == 1 && displayColumns.contains(fallbackSeparator)) { commaSplitParts = splitTrimmed(displayColumns, fallbackSeparator); } return commaSplitParts; } protected static List<String> splitTrimmed(String text, String separator) { List<String> parts = new ArrayList<String>(); StringTokenizer st = new StringTokenizer(text, separator); while (st.hasMoreTokens()) { String token = st.nextToken().trim(); if (token.length() > 0) { parts.add(token); } } return parts; } }