Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.export.writer; import java.util.Collection; import java.util.ResourceBundle; import java.util.TreeMap; import org.apache.commons.lang.StringUtils; /** * The Class ExportWriterBase. * * @author David Harrison */ public abstract class ExportWriterBase implements ExportWriter { /** The keys. */ private ResourceBundle keys; /** The type. */ private String type = ""; /** The Constant ADDRESSFIELD_COUNT. */ protected static final int ADDRESSFIELD_COUNT = 8; /** * Sets the keys. * * @param keysBundle the new keys bundle reference */ protected final void setKeys(final String keysBundle) { this.keys = ResourceBundle.getBundle(keysBundle); } /** * Gets the keys. * * @return the keys */ protected final ResourceBundle getKeys() { return this.keys; } /** * Sets the type. * * @param typeVal the new type value */ protected final void setType(final String typeVal) { this.type = typeVal; } /** * Gets the type. * * @return the type */ protected final String getType() { return this.type; } /** * Checks if the field is an address. * * @param fieldName the field name * @return true, if is address */ protected final boolean isAddress(final String fieldName) { boolean isAddress = false; if (StringUtils.equalsIgnoreCase(fieldName, "Address (mail merge)") || StringUtils.equalsIgnoreCase(fieldName, "Street Address (mail merge)")) { isAddress = true; } return isAddress; } /** * Gets the formatted address header. * * @return the formatted address header */ protected final String getFormattedAddressHeader() { final StringBuffer header = new StringBuffer(); for (int i = 1; i < (ADDRESSFIELD_COUNT + 1); i++) { header.append("Address"); header.append(i); if (i < ADDRESSFIELD_COUNT) { header.append(keys.getString("HEADERITEM_SUFFIX")); header.append(keys.getString("ITEM_DIVIDER")); header.append(keys.getString("HEADERITEM_PREFIX")); } } return header.toString(); } /** * Gets the formatted address field. * * @param fieldVal the field val * * @return the formatted address field */ protected final String getFormattedAddressField(final String fieldVal) { String field = fieldVal; final String divider = keys.getString("ITEM_SUFFIX") + keys.getString("ITEM_DIVIDER") + keys.getString("ITEM_PREFIX"); int extraLines = ADDRESSFIELD_COUNT - 1; if (StringUtils.isNotBlank(field)) { extraLines = extraLines - ((field.length() - field.toLowerCase().replaceAll("<br />", "").length()) / (ADDRESSFIELD_COUNT - 2)); field = StringUtils.replace(field, "<br />", divider); } for (int x = 0; x < extraLines; x++) { field += divider; } return field; } /** * Builds the export table. * * @param headings the headings * @param values the values * @return the string */ protected final String buildExportTable(final Collection<String> headings, final TreeMap<Integer, Collection<String>> values) { final StringBuffer table = new StringBuffer(); final StringBuffer header = new StringBuffer(); for (String heading : headings) { if (header.length() > 0) { header.append(this.getKeys().getString("ITEM_DIVIDER")); } header.append(this.getKeys().getString("HEADERITEM_PREFIX")); header.append(heading); header.append(this.getKeys().getString("HEADERITEM_SUFFIX")); } if (StringUtils.isNotBlank(header.toString())) { table.append(this.getKeys().getString("LIST_BEGINNING")); table.append(this.getKeys().getString("ROW_BEGINNING")); table.append(header.toString()); table.append(this.getKeys().getString("ROW_END")); } final StringBuffer content = new StringBuffer(); for (Integer index : values.keySet()) { Collection<String> row = values.get(index); content.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer fieldValue = new StringBuffer(); for (String field : row) { if (fieldValue.length() > 0) { fieldValue.append(this.getKeys().getString("ITEM_DIVIDER")); } fieldValue.append(this.getKeys().getString("ITEM_PREFIX")); fieldValue.append(field); fieldValue.append(this.getKeys().getString("ITEM_SUFFIX")); } content.append(fieldValue.toString()); content.append(this.getKeys().getString("ROW_END")); } if (StringUtils.isNotBlank(content.toString())) { table.append(content.toString()); } if (StringUtils.isNotBlank(header.toString())) { table.append(this.getKeys().getString("LIST_END")); } return table.toString(); } }