Try to deal with encoding
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jxl / src / main / java / org / argeo / server / jxl / dao / JxlDaoSupport.java
index 0488d519815896da54ccde15f204a2860e12bb02..fecd7f2911bfd0b1bd0439d021536110bf874b0a 100644 (file)
@@ -9,10 +9,14 @@ import java.util.Map;
 import java.util.TreeMap;
 
 import jxl.Cell;
+import jxl.CellType;
 import jxl.FormulaCell;
 import jxl.JXLException;
+import jxl.LabelCell;
+import jxl.NumberCell;
 import jxl.Sheet;
 import jxl.Workbook;
+import jxl.WorkbookSettings;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
@@ -35,6 +39,7 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
 
        private ClassLoader classLoader = getClass().getClassLoader();
        private ApplicationContext applicationContext;
+       private List<Class<?>> additionalClasses = new ArrayList<Class<?>>();
 
        private Map<Class<?>, Map<Object, Object>> model = new HashMap<Class<?>, Map<Object, Object>>();
 
@@ -44,38 +49,54 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
 
        private List<Resource> workbooks = new ArrayList<Resource>();
 
+       private Integer charset = 0;
+
        public void afterPropertiesSet() throws Exception {
+               init();
+       }
+
+       public void init() {
+               // used to resolve inner references
+               Map<String, List<Object>> tempRefs = new HashMap<String, List<Object>>();
+
+               List<Reference> references = new ArrayList<Reference>();
+
                for (Resource res : workbooks) {
                        InputStream in = null;
                        try {
                                in = res.getInputStream();
-                               load(in);
+                               load(in, references, tempRefs);
+                       } catch (Exception e) {
+                               throw new ArgeoServerException("Cannot load stream", e);
                        } finally {
                                IOUtils.closeQuietly(in);
                        }
                }
 
+               // Inject references
+               for (Reference ref : references) {
+                       injectReference(ref, tempRefs);
+               }
+               if (log.isDebugEnabled())
+                       log.debug(references.size() + " references linked");
        }
 
-       public void load(InputStream in) {
-               try {
-                       // used to resolve inner references
-                       Map<String, List<Object>> tempRefs = new HashMap<String, List<Object>>();
-
-                       List<Reference> references = new ArrayList<Reference>();
-
-                       Workbook workbook = Workbook.getWorkbook(in);
+       public List<Class<?>> getSupportedClasses() {
+               List<Class<?>> res = new ArrayList<Class<?>>();
+               res.addAll(additionalClasses);
+               res.addAll(model.keySet());
+               return res;
+       }
 
+       public void load(InputStream in, List<Reference> references,
+                       Map<String, List<Object>> tempRefs) {
+               try {
+                       WorkbookSettings workbookSettings = new WorkbookSettings();
+                       workbookSettings.setCharacterSet(charset);
+                       Workbook workbook = Workbook.getWorkbook(in, workbookSettings);
                        for (Sheet sheet : workbook.getSheets()) {
                                loadSheet(sheet, references, tempRefs);
                        }
-
-                       for (Reference ref : references) {
-                               injectReference(ref, tempRefs);
-                       }
-                       if (log.isDebugEnabled())
-                               log.debug(references.size() + " references linked");
-
                } catch (Exception e) {
                        throw new ArgeoServerException("Cannot load workbook", e);
                }
@@ -130,6 +151,10 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
                if (cell instanceof FormulaCell) {
                        String formula = ((FormulaCell) cell).getFormula();
                        int index = formula.indexOf('!');
+                       if (index < 0)
+                               throw new ArgeoServerException("Cannot interpret formula "
+                                               + formula);
+                       ;
                        String targetSheet = formula.substring(0, index);
                        // assume no double letters!!
                        String targetRowStr = formula.substring(index + 2);
@@ -145,6 +170,17 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
                                                + ", targetRow=" + targetRow);
                } else {
                        String contents = cell.getContents();
+
+//                     if (cell.getType() == CellType.LABEL) {
+//                             LabelCell lc = (LabelCell) cell;
+//                             contents = lc.getString();
+//                     } else if (cell.getType() == CellType.NUMBER) {
+//                             NumberCell nc = (NumberCell) cell;
+//                             contents = new Double(nc.getValue()).toString();
+//                     } else {
+//                             contents = cell.getContents();
+//                     }
+
                        if (propertyName.equals(keyProperty)
                                        && !StringUtils.hasText(contents)) {
                                // auto allocate key column if empty
@@ -304,6 +340,18 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
                this.classLoader = classLoader;
        }
 
+       public List<Class<?>> getAdditionalClasses() {
+               return additionalClasses;
+       }
+
+       public void setAdditionalClasses(List<Class<?>> additionalClasses) {
+               this.additionalClasses = additionalClasses;
+       }
+
+       public void setCharset(Integer charset) {
+               this.charset = charset;
+       }
+
        public static class Reference {
                private Object object;
                private String property;