|
[Fudaa-cvs-commit] fudaa_devel/dodico/src/org/fudaa/dodico/telemac/io TelemacCasWriter.java,NONE,1.1
From: <de...@us...> - 2003-05-19 14:04:05
|
Update of /cvsroot/fudaa/fudaa_devel/dodico/src/org/fudaa/dodico/telemac/io In directory sc8-pr-cvs1:/tmp/cvs-serv13570/telemac/io Added Files: TelemacCasWriter.java Log Message: TelemacDicoFactory: permet d'automatiser la creation des classes dico Support pour les fichiers cas et ajout d'un test --- NEW FILE: TelemacCasWriter.java --- /* * @file TelemacCasWriter.java * @creation 18 avr. 2003 * @modification $Date: 2003/05/19 14:04:02 $ * @license GNU General Public License 2 * @copyright (c)1998-2001 CETMEF 2 bd Gambetta F-60231 Compiegne * @mail de...@fu... */ package org.fudaa.dodico.telemac.io; import java.io.IOException; import java.text.DateFormat; import java.util.Calendar; import java.util.Set; import java.util.StringTokenizer; import com.memoire.fu.FuLib; import org.fudaa.dodico.commun.DodicoLib; import org.fudaa.dodico.dico.DicoEntite; import org.fudaa.dodico.fichiers.FileWriterCharSimpleAbstract; /** * @author deniger * @version $Id: TelemacCasWriter.java,v 1.1 2003/05/19 14:04:02 deniger Exp $ */ public class TelemacCasWriter extends FileWriterCharSimpleAbstract { TelemacCasFileFormat fileFormat_; TelemacCasInterface interface_; String commentSep_; String enteteFormat_; String enteteRubrique_; char com_; /** * @param _ft */ public TelemacCasWriter(TelemacCasFileFormat _ft) { fileFormat_ = _ft; com_ = fileFormat_.getCommentChar(); commentSep_ = com_ + "---------------------------------------------------------------------" + lineSep_; enteteFormat_ = commentSep_ + com_ + " TELEMAC-2D Version ${TELEMAC_VERSION} ${DATE}" + lineSep_ + com_ + " ${TITLE}" + lineSep_ + commentSep_; enteteRubrique_ = lineSep_ + commentSep_ + com_ + " ${RUBRIQUE}" + lineSep_ + commentSep_; } /** * @see org.fudaa.dodico.tr.TrWriterInterface#write() */ public void write() { if (interface_ == null) { analyze_.addTranslateError("Les données sont nulles"); return; } if (out_ == null) { analyze_.addTranslateError("Le flux de sortie est nul."); return; } //Hashtable entitesValues = interface_.getInputs(); Set rest = interface_.getTrEntiteSet(); String[] rubriques = fileFormat_.getDico().getRubriques(); try { String dd = null; DicoEntite entNom = fileFormat_.getTitreEntite(); if (entNom != null) { dd = interface_.getValue(entNom); } if (dd == null) dd = "nom inconnu"; writeEntete(dd); if ((rubriques == null) || (rubriques.length == 0)) { DicoEntite[] ents = new DicoEntite[rest.size()]; rest.toArray(ents); writeRubrique(DodicoLib.geti18n("Inconnu"), ents); rest.clear(); } int n = rubriques.length; for (int i = 0; i < n; i++) { if(rubriques[i]==null) System.err.println("ub nuLL"); DicoEntite[] entToWrite = DicoEntite.getAndRemoveEntite(rubriques[i], rest); writeRubrique(rubriques[i], entToWrite); } } catch (IOException _e) { manageException(_e, analyze_); } } /** * * @param _entValues * @throws IOException */ private void writeEntete(String _nomEtude) throws IOException { String entete = FuLib.replace(enteteFormat_, "${TITLE}", _nomEtude); entete = FuLib.replace(entete, "${TELEMAC_VERSION}", fileFormat_.getVersion()); entete = FuLib.replace(entete, "${DATE}", DateFormat.getDateInstance().format(Calendar.getInstance().getTime())); String r=""; String com=""+fileFormat_.getCommentChar(); for(StringTokenizer tk=new StringTokenizer(entete,lineSep_);tk.hasMoreTokens();) { r+=formatToMaxChar(tk.nextToken(), com)+lineSep_; } writeln(r); } private void writeRubrique(String _rubrique, DicoEntite[] _ent) throws IOException { if ((_ent == null) || (_ent.length == 0)) { return; } int entiteNomMaxLength = geEntiteNomtMaxLength(_ent) + 1; String entete = FuLib.replace(enteteRubrique_, "${RUBRIQUE}", _rubrique); writeln(); out_.write(entete); int n = _ent.length; DicoEntite ent; int maxChar = fileFormat_.getMaxCharPerLine(); for (int i = 0; i < n; i++) { ent = _ent[i]; if (!fileFormat_.getDico().isKey(ent)) { analyze_.addWarning("Entité inconnue pour ce format" + " " + ent.getNom(), -1); } else { String value = interface_.getValue(ent); if (ent.containsStringDataType()) { value = FuLib.replace(value, "'", "''"); value = "'" + value + "'"; } String initNom = ent.getNom(); String nom = DodicoLib.adjustSize(entiteNomMaxLength, initNom); if (nom.length() + value.length() + 1 > maxChar) { if (initNom.length() + value.length() + 1 <= maxChar) { writeln(initNom + "=" + value); } else { if (initNom.length() + 1 > maxChar) { System.err.println("Les noms des parametres ne doivent pas dépasser 72 char"); writeln(fileFormat_.getCommentChar() + DodicoLib.geti18n("Ce parametres depasse 72 caractères")); } writeln(initNom + "="); if (value.length() > maxChar) { value = formatToMaxChar(value); } writeln(value); } } else { writeln(nom + "=" + value); } } } } /** * Renvoie une chaine de caractere respectant le nb de caractere max par * ligne. Un separateur de ligne ( et pas de prefixe) est ajoute. */ private String formatToMaxChar(String _init) { return formatToMaxChar(_init,""); } /** * Renvoie une chaine de caractere respectant le nb de caractere max par * ligne. Un separateur de ligne (et un prefixe optionnel) est ajoute. */ private String formatToMaxChar(String _init,String _pref) { int maxChar = fileFormat_.getMaxCharPerLine()-_pref.length(); StringBuffer valueFinal = new StringBuffer(_init); int vfLength = valueFinal.length(); int lnLength = lineSep_.length()+_pref.length(); for (int j = maxChar-1; j < vfLength; j += (maxChar + lnLength)) { valueFinal.insert(j - lnLength, lineSep_+_pref); } return valueFinal.toString(); } /** * Renvoie la taille max des noms des entites. */ private int geEntiteNomtMaxLength(DicoEntite[] _ent) { int r = 0; int n = _ent.length - 1; int taille = 0; for (int i = n; i >= 0; i--) { taille = _ent[i].getNom().length(); if (taille > r) r = taille; } return r; } /** * @param un objet de type TelemacCasInterface * @see org.fudaa.dodico.tr.TrWriterInterface#setInterface(java.lang.Object) */ public void setInterface(Object _interface) { setTelemacCasInterface((TelemacCasInterface) _interface); } public void setTelemacCasInterface(TelemacCasInterface _interface) { interface_ = _interface; } } |