001: /*
002: * Created on Nov 4, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl;
008:
009: import java.util.HashMap;
010:
011: import org.jdom.Attribute;
012: import org.jdom.Document;
013: import org.jdom.Text;
014:
015: import org.jaxen.jdom.JDOMXPath;
016: import org.jdom.Element;
017: import org.xdev.base.core.BaseSaxBuilder;
018: import org.xdev.base.core.compiler.AXCompiler;
019: import org.xdev.base.log.LoggerWriter;
020: import org.xdev.base.resource.IDataSource;
021:
022: /**
023: * @author AYegorov
024: *
025: * To change the template for this generated type comment go to
026: * Window>Preferences>Java>Code Generation>Code and Comments
027: */
028: public final class XText extends XSSLReturn {
029: public static final String TRANSACTION_TEXT_TAG_NAME = "text";
030:
031: private Object returnValue = null;
032:
033: /**
034: * @param id
035: */
036: public XText(String id) {
037: super (id);
038: // TODO Auto-generated constructor stub
039: }
040:
041: /**
042: * @param id
043: * @param properties
044: */
045: public XText(String id, HashMap properties) {
046: super (id, properties);
047: }
048:
049: /* (non-Javadoc)
050: * @see org.xdev.base.transaction.AbstractTransactionResult#set()
051: */
052: protected void set() throws Exception {
053: if (this .getCount() > 0) {
054: XSSLReturn r = (XSSLReturn) this .getElement(0);
055:
056: if (r.execute(this )) {
057:
058: Object obj = r.getObjectValue();
059:
060: this .setReturnedText(obj != null ? obj.toString() : "");
061: }
062: }
063: }
064:
065: public void setReturnedText(String text) {
066: this .returnValue = text;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.xdev.base.transaction.AbstractTransactionResult#getValue()
071: */
072: public Object getObjectValue() {
073: StringBuffer buffer = new StringBuffer();
074:
075: try {
076: String key = (String) this .returnValue;
077:
078: XText.compileText(key, buffer, AXCompiler.getInstance(),
079: this );
080:
081: } catch (Exception ex) {
082: this .logError(ex);
083:
084: buffer.setLength(0);
085:
086: buffer.append(this .returnValue);
087: }
088:
089: return buffer.toString();
090: }
091:
092: public static void parseText(String response, StringBuffer result,
093: AXCompiler ap) throws Exception {
094: String keyword = null;
095:
096: String before = null;
097:
098: while (response != null && response.indexOf("{!/") != -1
099: && response.indexOf("}") != -1) {
100:
101: before = response.substring(0, response.indexOf("{!/"));
102:
103: result.append(before);
104:
105: keyword = response.substring(response.indexOf("{!/") + 3,
106: response.indexOf("}"));
107:
108: response = response.substring(response.indexOf("}") + 1,
109: response.length());
110:
111: result.append(XText.getResolvedResults(keyword, ap));
112: }
113:
114: result.append(response);
115: }
116:
117: public static void compileText(String response,
118: StringBuffer result, AXCompiler ap, XSSLAction parent)
119: throws Exception {
120: String keyword = null;
121:
122: String before = null;
123:
124: StringBuffer temp = new StringBuffer();
125:
126: BaseSaxBuilder builder = new BaseSaxBuilder();
127:
128: Element root = null;
129:
130: Document doc = null;
131:
132: XSSLAction action = null;
133:
134: Object obj = null;
135:
136: while (response != null && response.indexOf("[@") != -1
137: && response.indexOf("@]") != -1) {
138:
139: before = response.substring(0, response.indexOf("[@"));
140:
141: result.append(before);
142:
143: keyword = response.substring(response.indexOf("[@") + 2,
144: response.indexOf("@]"));
145:
146: parseText(keyword, temp, ap);
147:
148: LoggerWriter.log("Building source xml: " + temp);
149:
150: AXCompiler compiler = AXCompiler.getInstance();
151:
152: doc = builder.build(temp.toString(), AXCompiler.getTypes());
153:
154: root = doc.getRootElement();
155:
156: action = (XSSLAction) compiler.getCompiledComponent(parent
157: .getConfig(), root, parent, (XSSLAction) parent
158: .getRoot(), parent.getRulePath());
159:
160: action.setConfig(parent.getConfig());
161:
162: if (action.execute(parent) && action instanceof XSSLReturn) {
163:
164: obj = ((XSSLReturn) action).getObjectValue();
165:
166: XText.compileText(obj.toString(), result, ap, action);
167: }
168:
169: response = response.substring(response.indexOf("@]") + 2,
170: response.length());
171: }
172:
173: parseText(response, result, ap);
174: }
175:
176: public static Object getResolvedResults(String keyword,
177: AXCompiler ap) throws Exception {
178: Element root = null;
179:
180: Object obj = null;
181:
182: String ds = keyword.substring(0, keyword.indexOf("/"));
183:
184: IDataSource source = ap.getDataSource(ds);
185:
186: Document doc = (Document) source.getConnection();
187:
188: root = doc.getRootElement();
189:
190: if (root == null) {
191: root = AXCompiler.BASE_ROOT;
192: }
193:
194: JDOMXPath xpath = new JDOMXPath(keyword.substring(keyword
195: .indexOf("/"), keyword.length()));
196:
197: obj = xpath.selectSingleNode(root);
198:
199: return XText.resolveXmlElement(obj);
200: }
201:
202: public static Object resolveXmlElement(Object obj) {
203: if (obj instanceof Element) {
204: obj = ((Element) obj).getTextTrim();
205: } else if (obj instanceof Attribute) {
206: obj = ((Attribute) obj).getValue();
207: } else if (obj instanceof Text) {
208: obj = ((Text) obj).getTextTrim();
209: }
210:
211: return obj;
212: }
213:
214: /* (non-Javadoc)
215: * @see org.xdev.base.transaction.AbstractTransaction#rollback()
216: */
217: public void rollback() throws Exception {
218: }
219:
220: /* (non-Javadoc)
221: * @see org.xdev.base.transaction.AbstractTransaction#commit()
222: */
223: public void commit() {
224: }
225:
226: /* (non-Javadoc)
227: * @see org.xdev.base.transaction.AbstractTransaction#setTagName()
228: */
229: protected String setTagName() {
230: // TODO Auto-generated method stub
231: return XText.TRANSACTION_TEXT_TAG_NAME;
232: }
233:
234: public Object clone() {
235: XText xtxt = (XText) super .clone();
236:
237: xtxt.setReturnedText((String) this.returnValue);
238:
239: return xtxt;
240: }
241: }
|