Java tutorial
/***************************************************************************** Copyright 2007 BizBundle.org 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. ***************************************************************************/ package org.bizbundles.reverse; import jxl.CellView; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.bizbundles.support.FileNameHelper; import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; import java.util.Enumeration; import java.util.List; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; /** * Class that will reverse resoruce bundles into excel spreadsheets. * use -ea to enable assertion checks * * @author Steve Roach */ public class ResourcesToExcel { private Log logger = LogFactory.getLog(ResourcesToExcel.class); private static final String KEY = "Key"; private static final String VALUE = "Value"; private static final String ACTIVE = "Active?"; private static final String ACTIVE_INDCATOR = "Y"; public ResourcesToExcel() { } /** * This method will reverse resource bundle files into an excel spreadsheeet. * the spreadsheet will be created with the baseName parameter and the curnet date and time appended to it in 24 hour format. * There will be n number of tabs, one for each resource(property) file found. * * @param pathToResources Directory where all the resources files are located * @param baseName The basename for the resource bundles */ public void generate(final File pathToResources, final String baseName) { assert pathToResources != null : "null file path entered"; assert pathToResources.exists() : "file does not exists."; assert pathToResources.isDirectory() : "File is not a directory"; assert pathToResources.canRead(); assert baseName != null || baseName.length() > 0 : "The base name cannot be empty."; File files[] = pathToResources.listFiles(new FilenameFilter() { public boolean accept(File file, String string) { return string.endsWith("properties") && string.startsWith(baseName); } }); assert null != files && files.length > 0 : "There were no resources found with the given baseName " + baseName; WritableWorkbook workbook = null; try { WritableCellFormat wrapFormat = new WritableCellFormat(); wrapFormat.setWrap(true); CellView cellView = new CellView(); cellView.setFormat(wrapFormat); WorkbookSettings settings = new WorkbookSettings(); settings.setGCDisabled(true); workbook = Workbook.createWorkbook(new File( pathToResources + File.separator + baseName + FileNameHelper.getDateTimeString() + ".xls"), settings); int col1MaxSize = 10; int col2MaxSize = 10; for (File file : files) { if (file.isFile()) { String filename = file.getName(); if (logger.isDebugEnabled()) { logger.debug("Procesing File " + file.getAbsoluteFile()); } int i = 0; WritableFont timesboldfont = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, true); WritableCellFormat timesBoldFormat = new WritableCellFormat(timesboldfont); WritableSheet sheet = workbook.createSheet(filename, i); sheet.getColumnView(1).setSize(60); sheet.addCell(new Label(0, 0, KEY, timesBoldFormat)); sheet.addCell(new Label(1, 0, VALUE, timesBoldFormat)); sheet.addCell(new Label(2, 0, ACTIVE, timesBoldFormat)); int row = 1; ResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(file)); Enumeration enums = bundle.getKeys(); while (enums.hasMoreElements()) { String key = (String) enums.nextElement(); String val = bundle.getString(key); col1MaxSize = key.length() > col1MaxSize ? key.length() : col1MaxSize; col2MaxSize = val.length() > col1MaxSize ? val.length() : col1MaxSize; sheet.addCell(new Label(0, row, key)); sheet.addCell(new Label(1, row, val, wrapFormat)); sheet.addCell(new Label(2, row++, ACTIVE_INDCATOR)); } sheet.setColumnView(1, col2MaxSize + 4); sheet.setColumnView(0, col1MaxSize + 4); } } workbook.write(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != workbook) { try { workbook.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * This method will reverse resource bundles into excel spreadsheeets. * Each spreadsheet will be created with each baseName in the List parameter * and the current date and time appended to it in 24 hour format. * There will be n number of tabs, one for each resource(property) file found per base name. * * @param pathToResources Directory where all the resources files are located * @param baseNames The basename for the resource bundles */ public void generate(final File pathToResources, final List<String> baseNames) { assert baseNames != null && baseNames.size() > 0 : "The list of basenames is empty"; for (String baseName : baseNames) { generate(pathToResources, baseName); } } }