001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: OperIterate.java 3626 2006-12-12 03:13:32Z lzheng $
023: */
024: package com.bostechcorp.cbesb.common.trn.compiler;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: import com.bostechcorp.cbesb.common.util.TabbingPrintWriter;
031:
032: public class OperIterate extends OperBuiltin {
033: // parameters for iterate operation
034: protected String context;
035:
036: public OperIterate(String name) {
037: super (name);
038: this .builtinType = OpType.BUILTIN_OP_ITERATE;
039: }
040:
041: public String getContext() {
042: return context;
043: }
044:
045: public void setContext(String context) {
046: this .context = context;
047: }
048:
049: @Override
050: public void handleProperty(String propertyName, String propertyValue) {
051:
052: if (propertyName.equals("context"))
053: this .context = propertyValue;
054:
055: }
056:
057: @Override
058: public void startCode(TransformerCompiler compiler) {
059:
060: TabbingPrintWriter javaWriter = compiler.javaWriter;
061: javaWriter.println("// start iterate ");
062: if (this .sources.length > 0) {
063: /*
064: * there is a source to loop through
065: */
066: int addressIndex = this .sources[0].getAddressIndex();
067: javaWriter
068: .println("for ("
069: + this .context
070: + ".setSourceNode( dataAddresses["
071: + addressIndex
072: + "].findSourceNode(sourceTree)); "
073: + this .context
074: + ".getSourceNode() != null; "
075: + this .context
076: + ".setSourceNode( AbstractRuntimeAddress.findNextElement("
077: + this .context + ".getSourceNode()))) {");
078: javaWriter.addTab();
079: } else {
080: /*
081: * iterates with only a target are encapsulated by an if(true) {
082: */
083: javaWriter.println("if (true) {");
084: javaWriter.addTab();
085: }
086: if (this .targets.length > 0) {
087: /*
088: * Associate the target address with the context.
089: * Auto-increment the target if necessary.
090: */
091: javaWriter.println("if(" + this .context
092: + ".getTargetAddress()==null){");
093: javaWriter.addTab();
094: javaWriter.println(this .context
095: + ".setTargetAddress(dataAddresses["
096: + this .targets[0].getAddressIndex() + "]);");
097: javaWriter.delTab();
098: javaWriter.println("}else{");
099: javaWriter.addTab();
100: javaWriter.println(this .context
101: + ".setTargetAddress(dataAddresses["
102: + this .targets[0].getAddressIndex() + "]);");
103: javaWriter.println(this .context + ".targetIncrement();");
104: javaWriter.delTab();
105: javaWriter.println("}");
106:
107: }
108: invalidateChildContexts(this .context, compiler.contextList,
109: javaWriter);
110: }
111:
112: /*
113: * Invalidate all contexts that depend on this one
114: */
115: private void invalidateChildContexts(String parent,
116: HashMap<String, String> contextList,
117: TabbingPrintWriter javaWriter) {
118: Set contextSet = contextList.keySet();
119: for (Iterator i = contextSet.iterator(); i.hasNext();) {
120: String varName = (String) i.next();
121: String this ContextParent = contextList.get(varName);
122: if (parent.equals(this ContextParent)) {
123: /*
124: * found a context with this one as the parent
125: */
126: javaWriter.println(varName + ".invalidate();");
127: invalidateChildContexts(varName, contextList,
128: javaWriter);
129: }
130: }
131: }
132:
133: public void endCode(TabbingPrintWriter javaWriter) {
134: javaWriter.delTab();
135: javaWriter.println("} // end iterate ");
136: }
137:
138: }
|