Dec 10

In the past developers have used log4j, and Java’s built in logging framework api. Commons logging (JCL) has also been used as a facade to allow easy implementation switching.

Now we also have logback, and the slf4j facade.

So the question is what should we be using to implement logging in our Java application?

Firstly using a facade is recommended. It allows you to far more easily switch the underlying logging implementation without affecting your application code. JCL suffered from a variety of classloader issues that made it troublesome to use. SLF4J has none of these issues and works well as a logging facade and is currently our facade of choice.

For the logging implementation we have long used log4j and it does a good job of providing simple logging capabilities. Our preferred logging framework for new projects however is logback. It includes many improvements over log4j such as:

  • faster
  • more flexible configurations
  • filters
  • implements SLF4J natively

written by objects \\ tags: , , , , , ,

Dec 03

We often need to parse text and have vaious classes at our disposal to help us. For example String.split() is often used to break up a string on a given delimiter.

But what if we want to break up a paragraph of text into sentences. We could split on the period (.) character but this will not work because the period character may occur within the sentence. Different languages may also use a different character to mark the end of a sentence.

The BreakIterator class solves this problem for us providing implementations for breaking a string into sentences, words, lines or characters. The following example shows it’s usage for breaking a paragraph into sentences.

BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(text);
int index = 0;
while (bi.next() != BreakIterator.DONE) {
    String sentence = text.substring(index, bi.current());
    System.out.println("Sentence: " + sentence);
    index = bi.current();
}

written by objects \\ tags: , , , ,

Dec 01

The following just puts together everything we’ve covered on POI so far.

The following is a simple utility method to read an Excel file (XLS or XLSX) and write the reference and contents of each cell to a writer of your choice. So you could use it to dump stdout, or a file.

public static void dump(File excel, PrintWriter out) throws IOException, InvalidFormatException {
	FileInputStream in = new FileInputStream(excel);
	Workbook workbook = WorkbookFactory.create(in);

	int nsheets = workbook.getNumberOfSheets();
	for (int i = 0; i < nsheets; i++) {
		Sheet sheet = workbook.getSheetAt(i);
		for (Row row : sheet) {
			for (Cell cell : row) {
				String cellValue = null;

				switch (cell.getCellType()) {
					case Cell.CELL_TYPE_STRING:
						cellValue = cell.getStringCellValue();
						break;

					case Cell.CELL_TYPE_FORMULA:
						cellValue = cell.getCellFormula();
						break;

					case Cell.CELL_TYPE_NUMERIC:
						if (DateUtil.isCellDateFormatted(cell)) {
							cellValue = 
								cell.getDateCellValue().toString();
						} else {
							cellValue = Double.toString(
								cell.getNumericCellValue());
						}
						break;

					case Cell.CELL_TYPE_BLANK:
						cellValue = "";
						break;

					case Cell.CELL_TYPE_BOOLEAN:
						cellValue = Boolean
								.toString(cell.getBooleanCellValue());
						break;
				}
				CellReference cellReference = new CellReference(
						row.getRowNum(), cell.getColumnIndex());
				out.println(cellReference.formatAsString()+"="+cellValue);
			}
		}
	}
	in.close();
}

written by objects \\ tags: , , , , , , , , , ,