- Timestamp:
- 11/23/06 18:21:31 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Jav_CO/com/scenari/m/co/donnee/inclusion/XWriter.java
r7458 r7466 49 49 import com.scenari.m.co.dialog.IHDialog; 50 50 import com.scenari.m.co.donnee.HDonneeUtils; 51 import com.scenari.m.co.donnee.IComputedData;52 51 import com.scenari.m.co.service.IWSDialog; 53 52 import com.scenari.m.co.service.IWService; … … 77 76 public class XWriter extends Writer { 78 77 79 protected static final int STATUT_TEXT = 0; 80 81 protected static final int STATUT_TAGOUV = 1; 82 83 protected static final int STATUT_TAGIN = 2; 84 85 protected static final int STATUT_TAGFERM = 3; 86 87 protected static final char[] TAGOUV_DEFAUT = { '[', '!', '[' }; 88 89 protected static final char[] TAGFERM_DEFAUT = { ']', '!', ']' }; 90 91 /** Writer de destination.*/ 92 protected Writer fWriterDest = null; 93 94 /** Dialog en cours.*/ 95 protected IHDialog fDialog = null; 96 97 /** Owner de cette données.*/ 98 protected Object fOwner = null; 99 100 /** Arguments associés à l'appel de la donnée.*/ 101 protected Object fArguments = null; 102 103 /** Statut correspondant au dernier caractere lu.*/ 104 protected int fStatut = STATUT_TEXT; 105 106 /** Nombre de caractères lus du tag.*/ 107 protected int fPosTagCourant = 0; 108 109 /** Tag d'ouverture d'une inclusion.*/ 110 protected char[] fTagOuv = TAGOUV_DEFAUT; 111 112 /** Tag de fermeture d'une inclusion.*/ 113 protected char[] fTagFerm = TAGFERM_DEFAUT; 114 115 /** Buffer de l'inclusion.*/ 116 protected StringBuffer fBuff = null; 117 118 /** Flag si le flux doit être envoyé dans la dest. */ 119 protected boolean fWriteDest = true; 120 121 /** Flag si la source est XML, donc nécessité de décoder les entitées XML avant de traiter les inclusions. */ 122 protected boolean fSrcIsXml = true; 123 124 /** Stack des if imbriqués. */ 125 protected List fStackCond = null; 126 127 /** 128 * 129 */ 130 public XWriter(Writer pWriterDest, IHDialog pDialog, Object pOwner, Object pArguments, boolean pSrcIsXml) { 131 super(); 132 fWriterDest = pWriterDest; 133 fDialog = pDialog; 134 fOwner = pOwner; 135 fArguments = pArguments; 136 fSrcIsXml = pSrcIsXml; 137 } 138 139 /** 140 * Close the stream, flushing it first. Once a stream has been closed, 141 * further write() or flush() invocations will cause an IOException to be 142 * thrown. Closing a previously-closed stream, however, has no effect. 143 * 144 * @exception IOException If an I/O error occurs 145 */ 146 public void close() throws IOException { 147 148 if (fStatut == STATUT_TAGIN || fStatut == STATUT_TAGFERM) { 149 String vInc = fBuff.toString(); 150 fWriterDest.write(fTagOuv); 151 fWriterDest.write(vInc); 152 if (fStatut == STATUT_TAGFERM) { 153 fWriterDest.write(fTagFerm, 0, fPosTagCourant); 154 } 155 HLogMgr.hPublishException("Terminaison anormale d'une donnée de type 'inclusion-dynamique' dans le dialogue : " + fDialog + ". L'inclusion '" + vInc + "' n'a pu être exécutée."); 156 } 157 158 fWriterDest.close(); 159 } 160 161 /** 162 * Flush the stream. If the stream has saved any characters from the 163 * various write() methods in a buffer, write them immediately to their 164 * intended destination. Then, if that destination is another character or 165 * byte stream, flush it. Thus one flush() invocation will flush all the 166 * buffers in a chain of Writers and OutputStreams. 167 * 168 * @exception IOException If an I/O error occurs 169 */ 170 public void flush() throws IOException { 171 fWriterDest.flush(); 172 } 173 174 /** 175 * Write a portion of an array of characters. 176 * 177 * @param pBuf Array of characters 178 * @param pOffset Offset from which to start writing characters 179 * @param pLength Number of characters to write 180 * 181 * @exception IOException If an I/O error occurs 182 */ 183 public void write(char[] pBuf, int pOffset, int pLength) throws IOException { 184 185 int vEnd = pOffset + pLength; 186 for (int i = pOffset; i < vEnd; i++) { 187 write(pBuf[i]); 188 } 189 190 } 191 192 public void write(int pChar) throws IOException { 193 194 switch (fStatut) { 195 case STATUT_TEXT: { 196 if (pChar == fTagOuv[0]) { 197 if (fTagOuv.length == 1) { 198 //On a trouvé un tag d'ouverture 199 if (fBuff == null) { 200 fBuff = new StringBuffer(64); 201 } 202 fStatut = STATUT_TAGIN; 203 } else { 204 fStatut = STATUT_TAGOUV; 205 fPosTagCourant = 1; 206 } 207 208 } else { 209 if (fWriteDest) fWriterDest.write(pChar); 210 } 211 break; 212 } 213 case STATUT_TAGOUV: { 214 if (pChar == fTagOuv[fPosTagCourant]) { 215 fPosTagCourant++; 216 if (fTagOuv.length == fPosTagCourant) { 217 //On a trouvé un tag d'ouverture 218 if (fBuff == null) { 219 fBuff = new StringBuffer(64); 220 } 221 fStatut = STATUT_TAGIN; 222 } 223 } else { 224 //On n'est pas sur un tag d'ouverture 225 if (fWriteDest) fWriterDest.write(fTagOuv, 0, fPosTagCourant); 226 if (pChar == fTagOuv[0]) { 227 fPosTagCourant = 1; 228 } else { 229 fStatut = STATUT_TEXT; 230 if (fWriteDest) fWriterDest.write((char) pChar); 231 } 232 } 233 break; 234 } 235 case STATUT_TAGIN: { 236 if (pChar == fTagFerm[0]) { 237 if (fTagFerm.length == 1) { 238 //On a trouvé le tag de fermeture 239 xExecuteInclusion(); 240 } else { 241 //On entame peut-être le tag de fermeture 242 fStatut = STATUT_TAGFERM; 243 fPosTagCourant = 1; 244 } 245 } else { 246 fBuff.append((char) pChar); 247 } 248 break; 249 } 250 case STATUT_TAGFERM: { 251 if (pChar == fTagFerm[fPosTagCourant]) { 252 fPosTagCourant++; 253 if (fTagFerm.length == fPosTagCourant) { 254 //On a trouvé le tag de fermeture 255 xExecuteInclusion(); 256 } 257 } else { 258 //On n'est pas encore sur le tag de fermeture 259 fBuff.append(fTagFerm, 0, fPosTagCourant); 260 if (pChar == fTagFerm[0]) { 261 fPosTagCourant = 1; 262 } else { 263 fStatut = STATUT_TAGIN; 264 fBuff.append((char) pChar); 265 } 266 } 267 break; 268 } 269 } 270 } 271 272 public void write(String pStr) throws IOException { 273 int vEnd = pStr.length(); 274 for (int i = 0; i < vEnd; i++) { 275 write(pStr.charAt(i)); 276 } 277 } 278 279 public void write(String pStr, int pOffset, int pLength) throws IOException { 280 int vEnd = Math.min(pOffset + pLength, pStr.length()); 281 for (int i = pOffset; i < vEnd; i++) { 282 write(pStr.charAt(i)); 283 } 284 } 285 286 /** 287 * NOTE : l'atribut "argument" DOIT être en derniere position après les 288 * les attrributs "agent" "service" et/ou "dialogue". 289 * NOTE : si le contenu de l'inclusion est vide, le tag d'ouverture est recopié 290 * dans le résultat (escape du tag d'ouveture). 291 * 292 */ 293 public void xExecuteInclusion() throws IOException { 294 295 fStatut = STATUT_TEXT; 296 297 String vInc; 298 if (fSrcIsXml) { 299 int vLen = fBuff.length(); 300 StringBuffer vBuf = new StringBuffer(vLen); 301 for (int i = 0; i < vLen; i++) { 302 char vCh = fBuff.charAt(i); 303 if (vCh == '&' && i < vLen - 3) { 304 char vCh2 = fBuff.charAt(i + 1); 305 if (vCh2 == 'a') { 306 if (fBuff.charAt(i + 2) == 'm' && fBuff.charAt(i + 3) == 'p' && i < vLen - 4 && fBuff.charAt(i + 4) == ';') { 307 vBuf.append('&'); 308 i = i + 4; 309 continue; 310 } 311 if (fBuff.charAt(i + 2) == 'p' && fBuff.charAt(i + 3) == 'o' && i < vLen - 5 && fBuff.charAt(i + 4) == 's' && fBuff.charAt(i + 5) == ';') { 312 vBuf.append('\''); 313 i = i + 5; 314 continue; 315 } 316 } else if (vCh2 == 'l' && fBuff.charAt(i + 2) == 't' && fBuff.charAt(i + 3) == ';') { 317 vBuf.append('<'); 318 i = i + 3; 319 continue; 320 } else if (vCh2 == 'g'&& fBuff.charAt(i + 2) == 't' && fBuff.charAt(i + 3) == ';') { 321 vBuf.append('>'); 322 i = i + 3; 323 continue; 324 } else if (vCh2 == 'q'&& fBuff.charAt(i + 2) == 'u' && i < vLen - 5 && fBuff.charAt(i + 3) == 'o' && fBuff.charAt(i + 4) == 't' && fBuff.charAt(i + 5) == ';') { 325 vBuf.append('\"'); 326 i = i + 5; 327 continue; 328 } 329 } 330 vBuf.append(vCh); 331 } 332 vInc = vBuf.toString(); 333 } else { 334 vInc = fBuff.toString(); 335 } 336 337 try { 338 if (vInc.equals("")) { 339 fWriterDest.write(fTagOuv); 340 } else if (vInc.equals("endif")) { 341 fWriteDest = true; 342 if (fStackCond != null && fStackCond.size() > 0) { 343 //On supprime le dernier if 344 fStackCond.remove(fStackCond.size() - 1); 345 //Si il existe encore un test faux, on bloque toujours l'écriture. 346 for (int i = 0; i < fStackCond.size(); i++) { 347 Boolean vResultIf = (Boolean) fStackCond.get(i); 348 if (vResultIf == Boolean.FALSE) { 349 fWriteDest = false; 350 break; 351 } 352 } 353 } 354 } else if (vInc.equals("else")) { 355 fWriteDest = true; 356 if (fStackCond != null && fStackCond.size() > 0) { 357 //On supprime le dernier if 358 int vPos = fStackCond.size() - 1; 359 Boolean vNewVal = ((Boolean) fStackCond.get(vPos)).booleanValue() ? Boolean.FALSE : Boolean.TRUE; 360 fStackCond.set(vPos, vNewVal); 361 fWriteDest = vNewVal.booleanValue(); 362 if (fWriteDest) { 363 //Si il existe encore un test faux, on bloque toujours l'écriture. 364 for (int i = 0; i < fStackCond.size(); i++) { 365 Boolean vResultIf = (Boolean) fStackCond.get(i); 366 if (vResultIf == Boolean.FALSE) { 367 fWriteDest = false; 368 break; 369 } 370 } 371 } 372 } 373 } else if (fWriteDest) { 374 375 IWAgent vAgent = null; 376 IWService vService = null; 377 IHDialog vDialog = fDialog; 378 String vArgument = null; 379 380 int vOffset = -1; 381 int vEnd = 0; 382 383 vOffset = vInc.indexOf(";arguments="); 384 if (vOffset > 0) { 385 vArgument = vInc.substring(vOffset + 11); 386 vInc = vInc.substring(0, vOffset); 387 } 388 389 // 'argument=' = OBSOLET A VIRER ! 390 vOffset = vInc.indexOf(";argument="); 391 if (vOffset > 0) { 392 vArgument = vInc.substring(vOffset + 10); 393 vInc = vInc.substring(0, vOffset); 394 HLogMgr.hPublishTrace("La syntaxe 'argument=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'arguments='."); 395 } 396 397 if (vInc.startsWith("agent=")) { 398 vOffset = 6; 399 } else { 400 vOffset = vInc.indexOf(";agent="); 401 if (vOffset >= 0) { 402 vOffset += 7; 403 } 404 } 405 if (vOffset > 0) { 406 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 407 String vUrl = vInc.substring(vOffset, vEnd); 408 if (fOwner instanceof IWAgent) { 409 vAgent = ((IWAgent) fOwner).hGetAgentParRef(vUrl); 410 } else { 411 throw HLogMgr.hNewException("Impossible d'accéder à un agent dans le contexte de service."); 412 } 413 if (vAgent == null) { 414 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun agent."); 415 return; 416 } else if (!(vAgent instanceof IWAgentComputor)) { 417 throw HLogMgr.hNewException("L'url '" + vUrl + "' pointe sur l'agent '" + vAgent + "' qui ne peut produire de résultat"); 418 } 419 } 420 421 if (vInc.startsWith("service=")) { 422 vOffset = 8; 423 } else { 424 vOffset = vInc.indexOf(";service="); 425 if (vOffset >= 0) { 426 vOffset += 9; 427 } 428 } 429 if (vOffset > 0) { 430 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 431 String vUrl = vInc.substring(vOffset, vEnd); 432 vService = ((IWSDialog) fDialog.hGoTo(vUrl)).hGetService(); 433 if (vService == null) { 434 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun service."); 435 return; 436 } else if (!(vService instanceof IWServiceAvecResultat)) { 437 throw HLogMgr.hNewException("L'url '" + vUrl + "' pointe sur le service '" + vService + "' qui ne peut produire de résultat"); 438 } 439 } 440 441 if (vInc.startsWith("dialog=")) { 442 vOffset = 7; 443 } else { 444 vOffset = vInc.indexOf(";dialog="); 445 if (vOffset >= 0) { 446 vOffset += 8; 447 } 448 } 449 450 //'dialogue=' = OBSOLET A VIRER ! 451 // if (vInc.startsWith("dialogue=")) { 452 // vOffset = 9; 453 // HLogMgr.hPublishTrace("La syntaxe 'dialogue=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'dialog='."); 454 // } else { 455 // vOffset = vInc.indexOf(";dialogue="); 456 // if (vOffset >= 0) { 457 // vOffset += 10; 458 // HLogMgr.hPublishTrace("La syntaxe 'dialogue=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'dialog='."); 459 // } 460 // } 461 462 if (vOffset > 0) { 463 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 464 String vUrl = vInc.substring(vOffset, vEnd); 465 vDialog = fDialog.hGoTo(vUrl); 466 if (vDialog == null) { 467 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun dialogue."); 468 return; 469 } 470 } 471 472 if (vInc.startsWith("if;")) { 473 IComputedData vRes = IComputedData.NULL; 474 if (vAgent != null) { 475 vRes = ((IWAgentComputor) vAgent).computeAsData(vDialog, vArgument); 476 } else if (vService != null) { 477 vRes = ((IWServiceAvecResultat) vService).hGetResultat(vDialog, vArgument); 478 } else if (vDialog instanceof IWADialog) { 479 if (!(((IWADialog) vDialog).hGetAgent() instanceof IWAgentComputor)) { 480 throw HLogMgr.hNewException("Le dialogue d'agent '" + vDialog + "' ne peut pas produire de résultat"); 481 } 482 vRes = ((IWAgentComputor) ((IWADialog) vDialog).hGetAgent()).computeAsData(vDialog, vArgument); 483 } else if (vDialog instanceof IWSDialog) { 484 if (!(((IWSDialog) vDialog).hGetService() instanceof IWServiceAvecResultat)) { 485 throw HLogMgr.hNewException("Le dialogue de service '" + vDialog + "' ne peut pas produire de résultat"); 486 } 487 vRes = ((IWServiceAvecResultat) ((IWSDialog) vDialog).hGetService()).hGetResultat(vDialog, vArgument); 488 } 489 if (fStackCond == null) fStackCond = new ArrayList(); 490 if (HDonneeUtils.hGetBooleanEvalTrue(vRes.getString())) { 491 fStackCond.add(Boolean.TRUE); 492 } else { 493 fStackCond.add(Boolean.FALSE); 494 fWriteDest = false; 495 } 496 } else { 497 Writer vWriter = fWriterDest; 498 if (vInc.startsWith("escapeXml;")) { 499 vWriter = new HWriterEscapeXml(vWriter, HWriterEscapeXml.ESCAPE_ATTR); 500 } 501 if (vAgent != null) { 502 ((IWAgentComputor) vAgent).computeAsData(vDialog, vArgument).writeValue(vWriter); 503 } else if (vService != null) { 504 ((IWServiceAvecResultat) vService).hGetResultat(vDialog, vArgument).writeValue(vWriter); 505 } else if (vDialog instanceof IWADialog) { 506 if (!(((IWADialog) vDialog).hGetAgent() instanceof IWAgentComputor)) { 507 throw HLogMgr.hNewException("Le dialogue d'agent '" + vDialog + "' ne peut pas produire de résultat"); 508 } 509 ((IWAgentComputor) ((IWADialog) vDialog).hGetAgent()).computeAsData(vDialog, vArgument).writeValue(vWriter); 510 } else if (vDialog instanceof IWSDialog) { 511 if (!(((IWSDialog) vDialog).hGetService() instanceof IWServiceAvecResultat)) { 512 throw HLogMgr.hNewException("Le dialogue de service '" + vDialog + "' ne peut pas produire de résultat"); 513 } 514 ((IWServiceAvecResultat) ((IWSDialog) vDialog).hGetService()).hGetResultat(vDialog, vArgument).writeValue(vWriter); 515 } 516 } 517 } 518 519 // 520 else { 521 //Cas : fWriteDest == false 522 if (vInc.startsWith("if;")) { 523 //Un père est déjà faux, on ajoute une condition à la stack, la valeur n'a aucune importance. 524 fStackCond.add(Boolean.TRUE); 525 } 526 } 527 528 } catch (IOException e) { 529 throw (IOException) HLogMgr.hAddMessage(e, "Echec à l'insertion d'une inclusion : '" + vInc + "'."); 530 } catch (Exception e) { 531 throw (IOException) HLogMgr.hAddMessage(new IOException("Echec à l'insertion d'une inclusion : '" + vInc + "'."), HLogMgr.hGetMessage(e)); 532 } finally { 533 fBuff.setLength(0); 534 } 535 536 } 78 protected static final int STATUT_TEXT = 0; 79 80 protected static final int STATUT_TAGOUV = 1; 81 82 protected static final int STATUT_TAGIN = 2; 83 84 protected static final int STATUT_TAGFERM = 3; 85 86 protected static final char[] TAGOUV_DEFAUT = { '[', '!', '[' }; 87 88 protected static final char[] TAGFERM_DEFAUT = { ']', '!', ']' }; 89 90 /** Writer de destination.*/ 91 protected Writer fWriterDest = null; 92 93 /** Dialog en cours.*/ 94 protected IHDialog fDialog = null; 95 96 /** Owner de cette données.*/ 97 protected Object fOwner = null; 98 99 /** Arguments associés à l'appel de la donnée.*/ 100 protected Object fArguments = null; 101 102 /** Statut correspondant au dernier caractere lu.*/ 103 protected int fStatut = STATUT_TEXT; 104 105 /** Nombre de caractères lus du tag.*/ 106 protected int fPosTagCourant = 0; 107 108 /** Tag d'ouverture d'une inclusion.*/ 109 protected char[] fTagOuv = TAGOUV_DEFAUT; 110 111 /** Tag de fermeture d'une inclusion.*/ 112 protected char[] fTagFerm = TAGFERM_DEFAUT; 113 114 /** Buffer de l'inclusion.*/ 115 protected StringBuffer fBuff = null; 116 117 /** Flag si le flux doit être envoyé dans la dest. */ 118 protected boolean fWriteDest = true; 119 120 /** Flag si la source est XML, donc nécessité de décoder les entitées XML avant de traiter les inclusions. */ 121 protected boolean fSrcIsXml = true; 122 123 /** Stack des if imbriqués. */ 124 protected List fStackCond = null; 125 126 /** 127 * 128 */ 129 public XWriter(Writer pWriterDest, IHDialog pDialog, Object pOwner, Object pArguments, boolean pSrcIsXml) { 130 super(); 131 fWriterDest = pWriterDest; 132 fDialog = pDialog; 133 fOwner = pOwner; 134 fArguments = pArguments; 135 fSrcIsXml = pSrcIsXml; 136 } 137 138 /** 139 * Close the stream, flushing it first. Once a stream has been closed, 140 * further write() or flush() invocations will cause an IOException to be 141 * thrown. Closing a previously-closed stream, however, has no effect. 142 * 143 * @exception IOException If an I/O error occurs 144 */ 145 public void close() throws IOException { 146 147 if (fStatut == STATUT_TAGIN || fStatut == STATUT_TAGFERM) { 148 String vInc = fBuff.toString(); 149 fWriterDest.write(fTagOuv); 150 fWriterDest.write(vInc); 151 if (fStatut == STATUT_TAGFERM) { 152 fWriterDest.write(fTagFerm, 0, fPosTagCourant); 153 } 154 HLogMgr.hPublishException("Terminaison anormale d'une donnée de type 'inclusion-dynamique' dans le dialogue : " + fDialog + ". L'inclusion '" + vInc + "' n'a pu être exécutée."); 155 } 156 157 fWriterDest.close(); 158 } 159 160 /** 161 * Flush the stream. If the stream has saved any characters from the 162 * various write() methods in a buffer, write them immediately to their 163 * intended destination. Then, if that destination is another character or 164 * byte stream, flush it. Thus one flush() invocation will flush all the 165 * buffers in a chain of Writers and OutputStreams. 166 * 167 * @exception IOException If an I/O error occurs 168 */ 169 public void flush() throws IOException { 170 fWriterDest.flush(); 171 } 172 173 /** 174 * Write a portion of an array of characters. 175 * 176 * @param pBuf Array of characters 177 * @param pOffset Offset from which to start writing characters 178 * @param pLength Number of characters to write 179 * 180 * @exception IOException If an I/O error occurs 181 */ 182 public void write(char[] pBuf, int pOffset, int pLength) throws IOException { 183 184 int vEnd = pOffset + pLength; 185 for (int i = pOffset; i < vEnd; i++) { 186 write(pBuf[i]); 187 } 188 189 } 190 191 public void write(int pChar) throws IOException { 192 193 switch (fStatut) { 194 case STATUT_TEXT: { 195 if (pChar == fTagOuv[0]) { 196 if (fTagOuv.length == 1) { 197 //On a trouvé un tag d'ouverture 198 if (fBuff == null) { 199 fBuff = new StringBuffer(64); 200 } 201 fStatut = STATUT_TAGIN; 202 } else { 203 fStatut = STATUT_TAGOUV; 204 fPosTagCourant = 1; 205 } 206 207 } else { 208 if (fWriteDest) fWriterDest.write(pChar); 209 } 210 break; 211 } 212 case STATUT_TAGOUV: { 213 if (pChar == fTagOuv[fPosTagCourant]) { 214 fPosTagCourant++; 215 if (fTagOuv.length == fPosTagCourant) { 216 //On a trouvé un tag d'ouverture 217 if (fBuff == null) { 218 fBuff = new StringBuffer(64); 219 } 220 fStatut = STATUT_TAGIN; 221 } 222 } else { 223 //On n'est pas sur un tag d'ouverture 224 if (fWriteDest) fWriterDest.write(fTagOuv, 0, fPosTagCourant); 225 if (pChar == fTagOuv[0]) { 226 fPosTagCourant = 1; 227 } else { 228 fStatut = STATUT_TEXT; 229 if (fWriteDest) fWriterDest.write((char) pChar); 230 } 231 } 232 break; 233 } 234 case STATUT_TAGIN: { 235 if (pChar == fTagFerm[0]) { 236 if (fTagFerm.length == 1) { 237 //On a trouvé le tag de fermeture 238 xExecuteInclusion(); 239 } else { 240 //On entame peut-être le tag de fermeture 241 fStatut = STATUT_TAGFERM; 242 fPosTagCourant = 1; 243 } 244 } else { 245 fBuff.append((char) pChar); 246 } 247 break; 248 } 249 case STATUT_TAGFERM: { 250 if (pChar == fTagFerm[fPosTagCourant]) { 251 fPosTagCourant++; 252 if (fTagFerm.length == fPosTagCourant) { 253 //On a trouvé le tag de fermeture 254 xExecuteInclusion(); 255 } 256 } else { 257 //On n'est pas encore sur le tag de fermeture 258 fBuff.append(fTagFerm, 0, fPosTagCourant); 259 if (pChar == fTagFerm[0]) { 260 fPosTagCourant = 1; 261 } else { 262 fStatut = STATUT_TAGIN; 263 fBuff.append((char) pChar); 264 } 265 } 266 break; 267 } 268 } 269 } 270 271 public void write(String pStr) throws IOException { 272 int vEnd = pStr.length(); 273 for (int i = 0; i < vEnd; i++) { 274 write(pStr.charAt(i)); 275 } 276 } 277 278 public void write(String pStr, int pOffset, int pLength) throws IOException { 279 int vEnd = Math.min(pOffset + pLength, pStr.length()); 280 for (int i = pOffset; i < vEnd; i++) { 281 write(pStr.charAt(i)); 282 } 283 } 284 285 /** 286 * NOTE : l'atribut "argument" DOIT être en derniere position après les 287 * les attrributs "agent" "service" et/ou "dialogue". 288 * NOTE : si le contenu de l'inclusion est vide, le tag d'ouverture est recopié 289 * dans le résultat (escape du tag d'ouveture). 290 * 291 */ 292 public void xExecuteInclusion() throws IOException { 293 294 fStatut = STATUT_TEXT; 295 296 String vInc; 297 if (fSrcIsXml) { 298 int vLen = fBuff.length(); 299 StringBuffer vBuf = new StringBuffer(vLen); 300 for (int i = 0; i < vLen; i++) { 301 char vCh = fBuff.charAt(i); 302 if (vCh == '&' && i < vLen - 3) { 303 char vCh2 = fBuff.charAt(i + 1); 304 if (vCh2 == 'a') { 305 if (fBuff.charAt(i + 2) == 'm' && fBuff.charAt(i + 3) == 'p' && i < vLen - 4 && fBuff.charAt(i + 4) == ';') { 306 vBuf.append('&'); 307 i = i + 4; 308 continue; 309 } 310 if (fBuff.charAt(i + 2) == 'p' && fBuff.charAt(i + 3) == 'o' && i < vLen - 5 && fBuff.charAt(i + 4) == 's' && fBuff.charAt(i + 5) == ';') { 311 vBuf.append('\''); 312 i = i + 5; 313 continue; 314 } 315 } else if (vCh2 == 'l' && fBuff.charAt(i + 2) == 't' && fBuff.charAt(i + 3) == ';') { 316 vBuf.append('<'); 317 i = i + 3; 318 continue; 319 } else if (vCh2 == 'g' && fBuff.charAt(i + 2) == 't' && fBuff.charAt(i + 3) == ';') { 320 vBuf.append('>'); 321 i = i + 3; 322 continue; 323 } else if (vCh2 == 'q' && fBuff.charAt(i + 2) == 'u' && i < vLen - 5 && fBuff.charAt(i + 3) == 'o' && fBuff.charAt(i + 4) == 't' && fBuff.charAt(i + 5) == ';') { 324 vBuf.append('\"'); 325 i = i + 5; 326 continue; 327 } 328 } 329 vBuf.append(vCh); 330 } 331 vInc = vBuf.toString(); 332 } else { 333 vInc = fBuff.toString(); 334 } 335 336 try { 337 if (vInc.equals("")) { 338 fWriterDest.write(fTagOuv); 339 } else if (vInc.equals("endif")) { 340 fWriteDest = true; 341 if (fStackCond != null && fStackCond.size() > 0) { 342 //On supprime le dernier if 343 fStackCond.remove(fStackCond.size() - 1); 344 //Si il existe encore un test faux, on bloque toujours l'écriture. 345 for (int i = 0; i < fStackCond.size(); i++) { 346 Boolean vResultIf = (Boolean) fStackCond.get(i); 347 if (vResultIf == Boolean.FALSE) { 348 fWriteDest = false; 349 break; 350 } 351 } 352 } 353 } else if (vInc.equals("else")) { 354 fWriteDest = true; 355 if (fStackCond != null && fStackCond.size() > 0) { 356 //On supprime le dernier if 357 int vPos = fStackCond.size() - 1; 358 Boolean vNewVal = ((Boolean) fStackCond.get(vPos)).booleanValue() ? Boolean.FALSE : Boolean.TRUE; 359 fStackCond.set(vPos, vNewVal); 360 fWriteDest = vNewVal.booleanValue(); 361 if (fWriteDest) { 362 //Si il existe encore un test faux, on bloque toujours l'écriture. 363 for (int i = 0; i < fStackCond.size(); i++) { 364 Boolean vResultIf = (Boolean) fStackCond.get(i); 365 if (vResultIf == Boolean.FALSE) { 366 fWriteDest = false; 367 break; 368 } 369 } 370 } 371 } 372 } else if (fWriteDest) { 373 374 IWAgent vAgent = null; 375 IWService vService = null; 376 IHDialog vDialog = fDialog; 377 String vArgument = null; 378 379 int vOffset = -1; 380 int vEnd = 0; 381 382 vOffset = vInc.indexOf(";arguments="); 383 if (vOffset > 0) { 384 vArgument = vInc.substring(vOffset + 11); 385 vInc = vInc.substring(0, vOffset); 386 } 387 388 // 'argument=' = OBSOLET A VIRER ! 389 vOffset = vInc.indexOf(";argument="); 390 if (vOffset > 0) { 391 vArgument = vInc.substring(vOffset + 10); 392 vInc = vInc.substring(0, vOffset); 393 HLogMgr.hPublishTrace("La syntaxe 'argument=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'arguments='."); 394 } 395 396 if (vInc.startsWith("agent=")) { 397 vOffset = 6; 398 } else { 399 vOffset = vInc.indexOf(";agent="); 400 if (vOffset >= 0) { 401 vOffset += 7; 402 } 403 } 404 if (vOffset > 0) { 405 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 406 String vUrl = vInc.substring(vOffset, vEnd); 407 if (fOwner instanceof IWAgent) { 408 vAgent = ((IWAgent) fOwner).hGetAgentParRef(vUrl); 409 } else { 410 throw HLogMgr.hNewException("Impossible d'accéder à un agent dans le contexte de service."); 411 } 412 if (vAgent == null) { 413 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun agent."); 414 return; 415 } else if (!(vAgent instanceof IWAgentComputor)) { throw HLogMgr.hNewException("L'url '" + vUrl + "' pointe sur l'agent '" + vAgent + "' qui ne peut produire de résultat"); } 416 } 417 418 if (vInc.startsWith("service=")) { 419 vOffset = 8; 420 } else { 421 vOffset = vInc.indexOf(";service="); 422 if (vOffset >= 0) { 423 vOffset += 9; 424 } 425 } 426 if (vOffset > 0) { 427 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 428 String vUrl = vInc.substring(vOffset, vEnd); 429 vService = ((IWSDialog) fDialog.hGoTo(vUrl)).hGetService(); 430 if (vService == null) { 431 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun service."); 432 return; 433 } else if (!(vService instanceof IWServiceAvecResultat)) { throw HLogMgr.hNewException("L'url '" + vUrl + "' pointe sur le service '" + vService + "' qui ne peut produire de résultat"); } 434 } 435 436 if (vInc.startsWith("dialog=")) { 437 vOffset = 7; 438 } else { 439 vOffset = vInc.indexOf(";dialog="); 440 if (vOffset >= 0) { 441 vOffset += 8; 442 } 443 } 444 445 //'dialogue=' = OBSOLET A VIRER ! 446 // if (vInc.startsWith("dialogue=")) { 447 // vOffset = 9; 448 // HLogMgr.hPublishTrace("La syntaxe 'dialogue=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'dialog='."); 449 // } else { 450 // vOffset = vInc.indexOf(";dialogue="); 451 // if (vOffset >= 0) { 452 // vOffset += 10; 453 // HLogMgr.hPublishTrace("La syntaxe 'dialogue=' dans une inclusion dynamique est obsolete et doit etre remplacée par 'dialog='."); 454 // } 455 // } 456 457 if (vOffset > 0) { 458 vEnd = Math.max(vInc.indexOf(';', vOffset), vInc.length()); 459 String vUrl = vInc.substring(vOffset, vEnd); 460 vDialog = fDialog.hGoTo(vUrl); 461 if (vDialog == null) { 462 //throw HLogMgr.hNewException("L'url '" + vUrl + "' ne pointe sur aucun dialogue."); 463 return; 464 } 465 } 466 467 if (vInc.startsWith("if;")) { 468 String vRes = ""; 469 if (vAgent != null) { 470 vRes = ((IWAgentComputor) vAgent).computeAsString(vDialog, vArgument); 471 } else if (vService != null) { 472 vRes = ((IWServiceAvecResultat) vService).hGetResultat(vDialog, vArgument).getString(); 473 } else if (vDialog instanceof IWADialog) { 474 if (!(((IWADialog) vDialog).hGetAgent() instanceof IWAgentComputor)) { throw HLogMgr.hNewException("Le dialogue d'agent '" + vDialog + "' ne peut pas produire de résultat"); } 475 vRes = ((IWAgentComputor) ((IWADialog) vDialog).hGetAgent()).computeAsString(vDialog, vArgument); 476 } else if (vDialog instanceof IWSDialog) { 477 if (!(((IWSDialog) vDialog).hGetService() instanceof IWServiceAvecResultat)) { throw HLogMgr.hNewException("Le dialogue de service '" + vDialog + "' ne peut pas produire de résultat"); } 478 vRes = ((IWServiceAvecResultat) ((IWSDialog) vDialog).hGetService()).hGetResultat(vDialog, vArgument).getString(); 479 } 480 if (fStackCond == null) fStackCond = new ArrayList(); 481 if (HDonneeUtils.hGetBooleanEvalTrue(vRes)) { 482 fStackCond.add(Boolean.TRUE); 483 } else { 484 fStackCond.add(Boolean.FALSE); 485 fWriteDest = false; 486 } 487 } else { 488 Writer vWriter = fWriterDest; 489 if (vInc.startsWith("escapeXml;")) { 490 vWriter = new HWriterEscapeXml(vWriter, HWriterEscapeXml.ESCAPE_ATTR); 491 } 492 if (vAgent != null) { 493 ((IWAgentComputor) vAgent).computeAsData(vDialog, vArgument).writeValue(vWriter); 494 } else if (vService != null) { 495 ((IWServiceAvecResultat) vService).hGetResultat(vDialog, vArgument).writeValue(vWriter); 496 } else if (vDialog instanceof IWADialog) { 497 if (!(((IWADialog) vDialog).hGetAgent() instanceof IWAgentComputor)) { throw HLogMgr.hNewException("Le dialogue d'agent '" + vDialog + "' ne peut pas produire de résultat"); } 498 ((IWAgentComputor) ((IWADialog) vDialog).hGetAgent()).computeAsData(vDialog, vArgument).writeValue(vWriter); 499 } else if (vDialog instanceof IWSDialog) { 500 if (!(((IWSDialog) vDialog).hGetService() instanceof IWServiceAvecResultat)) { throw HLogMgr.hNewException("Le dialogue de service '" + vDialog + "' ne peut pas produire de résultat"); } 501 ((IWServiceAvecResultat) ((IWSDialog) vDialog).hGetService()).hGetResultat(vDialog, vArgument).writeValue(vWriter); 502 } 503 } 504 } 505 506 // 507 else { 508 //Cas : fWriteDest == false 509 if (vInc.startsWith("if;")) { 510 //Un père est déjà faux, on ajoute une condition à la stack, la valeur n'a aucune importance. 511 fStackCond.add(Boolean.TRUE); 512 } 513 } 514 515 } catch (IOException e) { 516 throw (IOException) HLogMgr.hAddMessage(e, "Echec à l'insertion d'une inclusion : '" + vInc + "'."); 517 } catch (Exception e) { 518 throw (IOException) HLogMgr.hAddMessage(new IOException("Echec à l'insertion d'une inclusion : '" + vInc + "'."), HLogMgr.hGetMessage(e)); 519 } finally { 520 fBuff.setLength(0); 521 } 522 523 } 537 524 }
Note: See TracChangeset
for help on using the changeset viewer.