Hi all, Attached patch contains most of the funcionality discussed here: http://nagoya.apache.org/eyebrowse/ReadMsg?listId=15&msgNo=20440
Nicola, please, review it and let me know your opinion. Comments welcome... Regards Tomek Pik [EMAIL PROTECTED]
Index: org/apache/commons/morphos/ObjectFlavor.java =================================================================== RCS file: /home/cvspublic/jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/ObjectFlavor.java,v retrieving revision 1.1 diff -u -r1.1 ObjectFlavor.java --- org/apache/commons/morphos/ObjectFlavor.java 25 Jul 2002 13:18:10 -0000 1.1 +++ org/apache/commons/morphos/ObjectFlavor.java 30 Dec 2002 23:45:35 -0000 @@ -84,6 +84,7 @@ * * @param dataType The mime type * @param dataForm The format in which the data is (object, file, stream, etc) + * @throws IllegalArgumentException if one (or both) of agumentets is +<code>null</code> */ public ObjectFlavor (String dataType, String dataForm) { setDataType(dataType); @@ -98,10 +99,29 @@ setDataForm(""); } + /** + * Sets data type of this flavor. + * + * Data type cannot be <code>null</code>. + * + * @param dataType The mime type + * @throws IllegalArgumentException if <code>dataType</code> + * is <code>null</code>. + */ void setDataType(String dataType){ this.dataType = dataType; } + /** + * Sets data form of this flavor. + * + * Data form cannot be <code>null</code>. + * + * @param dataForm The format in which the data is + * (object, file, stream, etc) + * @throws IllegalArgumentException if <code>dataForm</code> + * is <code>null</code>. + */ void setDataForm(String dataForm){ this.dataForm = dataForm; } @@ -113,7 +133,26 @@ String getdataForm(){ return dataForm; } - + + /** + * @see Object#equals(java.lang.Object) + */ + public boolean equals(Object o) { + try { + ObjectFlavor other = (ObjectFlavor) o; + return (other.getdataType().equals(dataType) && + other.getdataForm().equals(dataForm)); + } catch (ClassCastException cce) { + return false; + } + } + + /** + * @see Object#hashCode() + */ + public int hashCode() { + return (dataType + "->" + dataForm).hashCode(); + } } Index: org/apache/commons/morphos/factories/BasicMorpherFactory.java =================================================================== RCS file: /home/cvspublic/jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/factories/BasicMorpherFactory.java,v retrieving revision 1.2 diff -u -r1.2 BasicMorpherFactory.java --- org/apache/commons/morphos/factories/BasicMorpherFactory.java 29 Aug 2002 07:07:59 -0000 1.2 +++ org/apache/commons/morphos/factories/BasicMorpherFactory.java 30 Dec 2002 23:45:43 +-0000 @@ -64,7 +64,12 @@ import org.apache.commons.morphos.Morpher; import org.apache.commons.morphos.MorpherFactory; import org.apache.commons.morphos.ObjectFlavor; +import org.apache.commons.morphos.helpers.FlavorPair; + import java.util.HashMap; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; /** * @@ -73,47 +78,107 @@ * @version $Revision: 1.2 $ $Date: 2002/08/29 07:07:59 $ */ public class BasicMorpherFactory implements MorpherFactory { - private HashMap knownMorphers; - - public BasicMorpherFactory(){ - super(); - knownMorphers=new HashMap(); - // FIXME Make configurable - knownMorphers.put("gnumeric2xls","org.apache.commons.morphos.morphers.poi.HSSFMorpher"); - knownMorphers.put("gnumeric2poi","org.apache.commons.morphos.morphers.poi.HSSFMorpher"); - knownMorphers.put("xslt","org.apache.commons.morphos.morphers.xml.XSLTMorpher"); - knownMorphers.put("poi2xls","org.apache.commons.morphos.morphers.poi.POIFSMorpher"); - knownMorphers.put("is2sax","org.apache.commons.morphos.morphers.dataform.InputSource2SAXMorpher"); - knownMorphers.put("stream2sax","org.apache.commons.morphos.morphers.dataform.Stream2SAXMorpher"); - } - - public Morpher getMorpher(ObjectFlavor inputFlavor, ObjectFlavor outputFlavor){ - throw new UnsupportedOperationException("implement me :-)"); - } - - public Morpher getMorpher(String name){ - Morpher morpher=null; - String clazzName = (String)knownMorphers.get(name); - - if(clazzName!=null){ - try{ - morpher = (Morpher)Class.forName(clazzName).newInstance(); - } catch(Exception e){ - e.printStackTrace(); - } - } - return morpher; - } -} - - - - - + private Map knownMorphers; + private Map inputOutputMapping; + public BasicMorpherFactory() { + super(); + knownMorphers = new HashMap(); + inputOutputMapping = new HashMap(); + registerKnownMorphers(); + } + /** + * @see MorpherFactory#getMorpher(ObjectFlavor, ObjectFlavor) + */ + public Morpher getMorpher(ObjectFlavor inputFlavor, ObjectFlavor outputFlavor) { + Map outputs = (Map) inputOutputMapping.get(inputFlavor); + if (outputs == null) { + return null; + } + String morpherName = (String) outputs.get(outputFlavor); + if (morpherName == null) { + return null; + } + return getMorpher(morpherName); + } + /** + * @see MorpherFactory#getMorpher(String) + */ + public Morpher getMorpher(String name) { + Morpher morpher = null; + String clazzName = (String) knownMorphers.get(name); + + if (clazzName != null) { + try { + morpher = (Morpher) Class.forName(clazzName).newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return morpher; + } + /** + * Register <em>Morpher</em> with given class and input/output flavors. + * + * @param String name name of the <em>Morpher</em> + * @param String className name of Morpher class + * @param flavorPairs <code>Collection</code> of {@link FlavorPair}s + * supported by Morpher + */ + public synchronized void registerMorpher( + String name, String className, Collection flavorPairs) { + knownMorphers.put(name, className); + Iterator pairsIterator = flavorPairs.iterator(); + while (pairsIterator.hasNext()) { + FlavorPair pair = (FlavorPair) pairsIterator.next(); + ObjectFlavor input = pair.getInputFlavor(); + ObjectFlavor output = pair.getOutputFlavor(); + Map outputs = (Map) inputOutputMapping.get(input); + if (outputs == null) { + outputs = new HashMap(); + inputOutputMapping.put(input, outputs); + } + outputs.put(output, name); + } + } + /** + * Register <em>Morpher</em> with given class and input/output flavors. + * + * @param String name name of the <em>Morpher</em> + * @param String className name of Morpher class + * @param input input flavor supported by <em>Morpher</em> + * @param output output flavor supproted by <em>Morpher</em> + */ + public synchronized void registerMorpher(String name, String className, + ObjectFlavor input, ObjectFlavor output) { + knownMorphers.put(name, className); + Map outputs = (Map) inputOutputMapping.get(input); + if (outputs == null) { + outputs = new HashMap(); + inputOutputMapping.put(input, outputs); + } + outputs.put(output, name); + } + private void registerKnownMorphers() { + // FIXME Make configurable + knownMorphers.put("gnumeric2xls", +"org.apache.commons.morphos.morphers.poi.HSSFMorpher"); + knownMorphers.put("gnumeric2poi", +"org.apache.commons.morphos.morphers.poi.HSSFMorpher"); + knownMorphers.put("xslt", +"org.apache.commons.morphos.morphers.xml.XSLTMorpher"); + knownMorphers.put("poi2xls", +"org.apache.commons.morphos.morphers.poi.POIFSMorpher"); + //knownMorphers.put("is2sax", +"org.apache.commons.morphos.morphers.dataform.InputSource2SAXMorpher"); + //knownMorphers.put("stream2sax", +"org.apache.commons.morphos.morphers.dataform.Stream2SAXMorpher"); + + registerMorpher( + "stream2sax", +org.apache.commons.morphos.morphers.dataform.Stream2SAXMorpher.class.getName(), + new ObjectFlavor("text/xml", "stream/unix"), new +ObjectFlavor("text/xml", "object/sax")); + registerMorpher( + "is2sax", +org.apache.commons.morphos.morphers.dataform.InputSource2SAXMorpher.class.getName(), + new ObjectFlavor("text/xml", "stream/sax"), new ObjectFlavor("text/xml", +"object/sax")); + } +} Index: org/apache/commons/morphos/helpers/FlavorPair.java =================================================================== RCS file: org/apache/commons/morphos/helpers/FlavorPair.java diff -N org/apache/commons/morphos/helpers/FlavorPair.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org/apache/commons/morphos/helpers/FlavorPair.java 30 Dec 2002 23:45:36 -0000 @@ -0,0 +1,101 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact [EMAIL PROTECTED] + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.commons.morphos.helpers; + +import org.apache.commons.morphos.ObjectFlavor; + +/** + * Pair of {@link ObjectFlavor} instances. + * + * Represents possible morphing. + * + * @author Tomasz Pik + * @version $Revision$ + */ +public class FlavorPair { + + private ObjectFlavor input; + private ObjectFlavor output; + + /** + * @param input flavor of possible Morpher input + * @param output flavor of possible Morpher output + */ + public FlavorPair(ObjectFlavor input, ObjectFlavor output) { + this.input = input; + this.output = output; + } + + /** + * @@return output flavor of pair. + */ + public ObjectFlavor getOutputFlavor() { + return output; + } + + /** + * @return input flavor of pair. + */ + public ObjectFlavor getInputFlavor() { + return input; + } +}
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>