001: package org.drools.decisiontable.parser.xls;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.drools.decisiontable.parser.SheetListener;
025:
026: /**
027: * Reads an Excel sheet as key-value properties.
028: *
029: * Treats the first non-empty cell on a row as a key and any subsequent
030: * non-empty cell as a value. Any cells defined after the second cell are
031: * ignored as comments.
032: *
033: * Could be easily adapted to accept multiple values per key but the semantics
034: * were kept in line with Properties.
035: *
036: * @author <a href="mailto:shaun.addison@gmail.com"> Shaun Addison </a>
037: *
038: */
039: public class PropertiesSheetListener implements SheetListener {
040:
041: private static final String EMPTY_STRING = "";
042:
043: private final Map _rowProperties = new HashMap();
044:
045: private final Properties _properties = new Properties();
046:
047: /**
048: * Return the key value pairs. If this is called before the sheet is
049: * finished, then it will build the properties map with what is known.
050: * Subsequent calls will update the properties map.
051: *
052: * @return properties
053: */
054: public Properties getProperties() {
055: finishSheet(); // MN allows this to be called before the sheet is
056: // finished, as
057: // some properties are used whilst the sheet is being parsed.
058: return this ._properties;
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see my.hssf.util.SheetListener#startSheet(java.lang.String)
065: */
066: public void startSheet(final String name) {
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see my.hssf.util.SheetListener#finishSheet()
073: */
074: public void finishSheet() {
075: for (final Iterator iter = this ._rowProperties.keySet()
076: .iterator(); iter.hasNext();) {
077: final String[] keyValue = (String[]) this ._rowProperties
078: .get(iter.next());
079: this ._properties.setProperty(keyValue[0], keyValue[1]);
080: }
081: }
082:
083: /**
084: * Enter a new row. This is ignored.
085: *
086: * @param rowNumber
087: * The row number.
088: * @param columns
089: * The Colum number.
090: */
091: public void newRow(final int rowNumber, final int columns) {
092: // nothing to do.
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see my.hssf.util.SheetListener#newCell(int, int, java.lang.String)
099: */
100: public void newCell(final int row, final int column,
101: final String value, final int mergedColStart) {
102: if (emptyCellValue(value)) {
103: return;
104: }
105: final Integer rowInt = new Integer(row);
106: if (this ._rowProperties.containsKey(rowInt)) {
107: final String[] keyValue = (String[]) this ._rowProperties
108: .get(rowInt);
109:
110: if (keyValue[1] == PropertiesSheetListener.EMPTY_STRING) {
111: keyValue[1] = value;
112: }
113: } else {
114: final String[] keyValue = { value,
115: PropertiesSheetListener.EMPTY_STRING };
116: this ._rowProperties.put(rowInt, keyValue);
117: }
118: }
119:
120: private boolean emptyCellValue(final String value) {
121: return value == null || value.trim().equals("");
122: }
123:
124: }
|