Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did a little framework that lets you define a layout in a spring file :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt; &lt;beans&gt; &lt;bean id="merchantNumber" class="com.xxx.io.Field"&gt; &lt;property name="name"&gt;&lt;value&gt;merchantNumber&lt;/value&gt;&lt;/property&gt; &lt;property name="length"&gt;&lt;value&gt;8&lt;/value&gt;&lt;/property&gt; &lt;property name="scale"&gt;&lt;value&gt;0&lt;/value&gt;&lt;/property&gt; &lt;property name="type"&gt;&lt;value&gt;7&lt;/value&gt;&lt;/property&gt; &lt;property name="index"&gt;&lt;value&gt;1&lt;/value&gt;&lt;/property&gt; &lt;/bean&gt; &lt;bean id="merchantName" class="com.xxx.io.Field"&gt; &lt;property name="name"&gt;&lt;value&gt;merchantName&lt;/value&gt;&lt;/property&gt; &lt;property name="length"&gt;&lt;value&gt;40&lt;/value&gt;&lt;/property&gt; &lt;property name="scale"&gt;&lt;value&gt;0&lt;/value&gt;&lt;/property&gt; &lt;property name="type"&gt;&lt;value&gt;2&lt;/value&gt;&lt;/property&gt; &lt;property name="index"&gt;&lt;value&gt;2&lt;/value&gt;&lt;/property&gt; &lt;/bean&gt; </code></pre> <p> </p> <pre><code>&lt;bean id="marchandLayout" class="com.xxx.MarchandLayout" &gt; &lt;property name="fields"&gt; &lt;bean class="java.util.HashMap"&gt; &lt;constructor-arg&gt; &lt;map&gt; &lt;entry&gt; &lt;key&gt;&lt;value&gt;merchantNumber&lt;/value&gt;&lt;/key&gt; &lt;ref bean="merchantNumber"/&gt; &lt;/entry&gt; &lt;entry&gt; &lt;key&gt;&lt;value&gt;merchantName&lt;/value&gt;&lt;/key&gt; &lt;ref bean="merchantName"/&gt; &lt;/entry&gt; </code></pre> <p>From then on you create a class that creates a FileExport (which writes in a fileOutputStream) using the recordLayout defined in Spring.</p> <p>I've also added a functionnality that reads a sequential file and stores the values in the layout attributes.</p> <p>If you're interested, I will send you the source code.</p> <p>hope it helps.</p> <p>RecordLayout Class : package com.xxx.io;</p> <pre><code>import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.TreeSet; public class RecordLayout { private HashMap fields; private static final int SEVENTY = 70; /** * Constructeur */ public RecordLayout() { fields = new HashMap(); } /** * @param aField * le champ à ajouter dans le HashMap */ protected void addField(final Field aField) { fields.put(aField.getName(), aField); } /** * Methode qui retourne un objet de type Field en passant en parametre le * nom du champ. * * @param aFieldName * le nom de l'objet à récuperer * @return un objet de type Field * @throws FieldException * si le Field n'est pas trouvable dans le HashMap, on retourne * une exception */ public Field getField(final String aFieldName) throws FieldException { Field field = (Field) fields.get(aFieldName); if (field == null) { throw new FieldException("Field " + aFieldName + " n'existe pas dans le layout"); } return field; } /** * méthode qui retourne un iterator contenant une collection d'objet de type * Field en fonction de l'ordre basé sur le champ de l'index de l'objet * * @see Field.getIndex() * @see Field.compareTo() * @return un iterator */ protected Iterator getOrderedFieldIterator() { TreeSet structureTreeSet = new TreeSet(fields.values()); return structureTreeSet.iterator(); } /** * Retourne la valeur de l'objet Field * * @param aFieldName * le nom du champ à recherché. * @return retourne l'objet qui contient la valeur. String, BigDecimal * @throws FieldException * si le champ n'existe pas, une exception est lancée. */ public Object getValue(final String aFieldName) throws FieldException { return getField(aFieldName).getValue(); } /** * Cette méthode assigne une valeur à un objet. * * @param aName * le nom de l'objet, le Field * @param aValue * la valeur à assigner au Field * @throws FieldException * si le Field n'existe pas. */ public void setValue(final String aName, final Object aValue) throws FieldException { getField(aName).setValue(aValue); } /** * Cette méthode prend la valeur des champs, en fonction de l'ordre des * champs et parcours les valeurs et les mets dans un stream. * * @return un stream contenant les données des objets comme dans un layout * de fichier plat. * @throws Exception * si une exception arrive lors de la transformation */ public ByteArrayOutputStream toByteArray() throws Exception { ByteArrayOutputStream anOutputStream; try { anOutputStream = new ByteArrayOutputStream(SEVENTY); toStream(anOutputStream); } catch (IOException e) { throw new Exception("Unexpected at the toByte Creation "); } return anOutputStream; } /** * Méthode qui itère sur la collection et qui appelle la transformation des * valeurs objets en objets String * * @param anOutputStream * le stream contenant les valeurs * @throws FieldException * Si une erreur survient au niveau des champs * @throws IOException * si une erreur survient au niveau du stream. */ public void toStream(final OutputStream anOutputStream) throws FieldException, IOException { Iterator iterator; iterator = getOrderedFieldIterator(); while (iterator.hasNext()) { Field field = (Field) iterator.next(); field.toStream(anOutputStream); } } /** * Méthode qui prend un inputStream et qui crée les objets * * @param message * le contenu du stream * @throws FieldException * si une erreur de Field survient * @throws IOException * si une erreur survient au niveau du stream. */ public void buildFromStream(final InputStream message) throws FieldException, IOException { Iterator iterator; iterator = getOrderedFieldIterator(); while (iterator.hasNext()) { ((Field) iterator.next()).initializeFrom(message); } } /** * @see java.lang.Object#toString() * @return String rprésentation */ public String toString() { StringBuffer sb = new StringBuffer(); Iterator it = getOrderedFieldIterator(); while (it.hasNext()) { Field f = (Field) it.next(); sb.append(f.getName() + " = [" + f.getValue() + "]\n"); } return sb.toString(); } /** * @return le HashMap contenant les champs */ public HashMap getFields() { return fields; } /** * @param map * à setter */ public void setFields(final HashMap map) { fields = map; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload