Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java diff -u lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java:1.1 lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java:1.2 --- lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java:1.1 Fri Oct 21 14:27:23 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java Thu Oct 27 12:28:10 2005 @@ -23,9 +23,9 @@ * Created: 11 octobre 2005 20:15:20 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2005/10/21 14:27:23 $ + * Last update: $Date: 2005/10/27 12:28:10 $ * by : $Author: bpoussin $ */ @@ -48,6 +48,32 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private Log log = LogFactory.getLog(MatrixFactory.class); + + static { + // on essai d'enregistrer le converter XML + try { + org.codelutin.xml.XMLConverterFactory.addConverter(MatrixND.class, + new MatrixNDXMLConverter()); + log.info("Converter XML pour MatrixND ajoute"); + + // on essai d'enregistrer le converter JDBC + // le JDBC depend du XML + try { + org.codelutin.topia.persistence.jdbctransformer.JDBCTransformerFactory + .addConverter(MatrixND.class, new MatrixNDJDBCConverter()); + log.info("Converter JDBC pour MatrixND ajoute"); + } catch (Throwable eee) { + log.info("librairie topia non presente. Import/Export JDBC impossible"); + log.debug("L'exception etait", eee); + } + + } catch (Throwable eee) { + log.info("librairie lutinxml non presente. Import/Export XML impossible"); + log.debug("L'exception etait", eee); + } + + } + /** Valeur par defaut si aucun type de Vector n'est donné */ static protected Class defaultVectorClass = FloatVector.class; Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixXMLDelegator.java diff -u lutinmatrix/src/java/org/codelutin/math/matrix/MatrixXMLDelegator.java:1.3 lutinmatrix/src/java/org/codelutin/math/matrix/MatrixXMLDelegator.java:1.4 --- lutinmatrix/src/java/org/codelutin/math/matrix/MatrixXMLDelegator.java:1.3 Thu Oct 20 20:58:04 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixXMLDelegator.java Thu Oct 27 12:28:10 2005 @@ -23,9 +23,9 @@ * Created: 5 nov. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Mise a jour: $Date: 2005/10/20 20:58:04 $ + * Mise a jour: $Date: 2005/10/27 12:28:10 $ * par : $Author: bpoussin $ */ @@ -41,6 +41,9 @@ import org.codelutin.xml.XMLEncoderDecoderDelegatorVersioned; import org.codelutin.util.ArrayUtil; +/** +* @deprecated use MatrixNDXMLConverter +*/ public class MatrixXMLDelegator extends XMLEncoderDecoderDelegatorVersioned { // MatrixXMLDelegator /** version d'encodage de la matrice */ Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDJDBCConverter.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDJDBCConverter.java:1.1 --- /dev/null Thu Oct 27 12:28:15 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDJDBCConverter.java Thu Oct 27 12:28:10 2005 @@ -0,0 +1,132 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixNDJDBCConverter.java + * + * Created: 27 octobre 2005 01:10:12 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/27 12:28:10 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +import java.io.StringWriter; +import java.lang.reflect.Method; +import org.apache.commons.beanutils.MethodUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.persistence.jdbctransformer.JDBCTransformer; +import org.codelutin.topia.TopiaContext; +import org.codelutin.util.ArrayUtil; +import org.codelutin.util.FormatConverter; +import org.codelutin.util.FormatConverterFactory; +import org.codelutin.util.FormatMap; +import org.codelutin.util.FormatMap.Format; +import org.codelutin.xml.XMLConverter; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.XMLWriter; + +public class MatrixNDJDBCConverter implements JDBCTransformer { // MatrixNDJDBCConverter + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(MatrixNDJDBCConverter.class); + + static private OutputFormat XML_OUTPUT_FORMAT = new OutputFormat(" ", true); + + public Class getJavaType(){ + return String.class; + } + + public Object convert(FormatConverterFactory factory, + Format format, FormatMap values, Object ... args){ + Object result = null; + + if (values.containsKey(format)){ + result = values.get(format); + } else { + Element resultXML = (Element)values.convert(factory, XMLConverter.FORMAT_XML, args); + StringWriter resultString = new StringWriter(); + XMLWriter out = new XMLWriter(resultString, XML_OUTPUT_FORMAT); + result = resultString.toString(); + } + // on stocke la valeur trouver pour les prochaines fois + values.put(format, result); + + return result; + } + + public Object unconvert(FormatConverterFactory factory, + Format format, FormatMap values, Object ... args) { + Object result = null; + + if (!values.containsKey(format) && !values.containsKey(FORMAT_JAVA)) { + throw new IllegalArgumentException("Aucun valeur disponible"); + } + + // si on a deja la representation Java on la retourne tout de suite + if (values.containsKey(FORMAT_JAVA)) { + result = values.get(FORMAT_JAVA); + } else { + String resultString = (String)values.get(format); + Element resultXML = null; + try { + resultXML = DocumentHelper.parseText(resultString). getRootElement(); + } catch (DocumentException eee) { + throw new IllegalArgumentException("JDBC format don't represent XML String"); + } + values.put(XMLConverter.FORMAT_XML, resultXML); + result = factory.unconvert(XMLConverter.FORMAT_XML, values, args); + if (result != null) { + TopiaContext context = ArrayUtil.search(args, TopiaContext.class); + if (context == null) { + throw new IllegalArgumentException("TopiaContext must be in args to convert id to TopiaEntity"); + } else { + if(context != null){ + // s'il a une methode setContext(TopiaContext) + // alors on l'appelle avec le context qui est en parametre + Method m = MethodUtils.getMatchingAccessibleMethod( + result.getClass(), "setContext", new Class[]{context.getClass()}); + if(m != null){ + try{ + m.invoke(result, context); + }catch(Exception eee){ + log.warn("Can't set context on object", eee); + } + } + } + } + } + } + // on stocke la valeur trouver pour les prochaines fois + values.put(FORMAT_JAVA, result); + + return result; + } + + +} // MatrixNDJDBCConverter + Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDXMLConverter.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDXMLConverter.java:1.1 --- /dev/null Thu Oct 27 12:28:15 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixNDXMLConverter.java Thu Oct 27 12:28:10 2005 @@ -0,0 +1,247 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixNDXMLConverter.java + * + * Created: 27 octobre 2005 00:40:56 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/27 12:28:10 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.ArrayUtil; +import org.codelutin.util.FormatConverter; +import org.codelutin.util.FormatConverterFactory; +import org.codelutin.util.FormatMap; +import org.codelutin.util.FormatMap.Format; +import org.codelutin.xml.XMLConverter; +import org.codelutin.xml.XMLConverterVersioned; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; + +/** +* Pour utiliser cette classe vous avez besoin de la librairie lutinxml. +* Mais tant que vous utilisez les matrices sans la partie XML, vous n'etes +* pas obligé d'avoir lutinxml present. +*/ +public class MatrixNDXMLConverter extends XMLConverterVersioned { // MatrixNDXMLConverter + + private static final String VERSION = "1"; + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(MatrixNDXMLConverter.class); + + public String getVersion() { + return VERSION; + } + + public Element encode(FormatConverterFactory factory, + Format format, FormatMap values, Object ... args){ + MatrixND matrix = (MatrixND)values.convert(factory, FormatConverter.FORMAT_JAVA); + + Element result = DocumentHelper.createElement("matrix"); + + if (matrix == null) { + result.addAttribute("null", "true"); + } else { + + double defaultValue = matrix.getMaxOccurence(); + + result.addAttribute("defaultValue", ""+defaultValue); + result.addAttribute("name", matrix.getName()); + result.addAttribute("dimensions", MatrixHelper.coordinatesToString(matrix.getDim())); + result.addAttribute("type", matrix.getClass().getName()); + + // ecriture des noms des dimensions + for(int i=0; i 0){ + Element semXML = (Element)childs.get(0); + String className = semXML.attributeValue("xmlconverter_type"); + Class clazz = Class.forName(className); + FormatMap semValue = new FormatMap(clazz); + semValue.put(XMLConverter.FORMAT_XML, semXML); + Object o = factory.unconvert(FORMAT_JAVA, semValue, args); + // on remet l'objet a la bonne place + String indexString = obj.attributeValue("index"); + int index = Integer.parseInt(indexString); + semantics.set(index, o); + } + } + result.setSemantics(dim, semantics); + } + + //on remet toutes les valeurs dans la matrice + for(Iterator i=root.elementIterator("element"); i.hasNext();){ + Element elem = (Element)i.next(); + + String [] path = elem.attributeValue("path").split(","); + int [] dim = ArrayUtil.asIntArray(path); + + String v = elem.attributeValue("value"); + result.setValue(dim, Double.parseDouble(v)); + } + } + return result; + } + +} // MatrixNDXMLConverter +