1 package org.ejen; 22 23 import org.ejen.util.XSLUtil; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 import javax.xml.transform.dom.DOMSource ; 27 import org.apache.xalan.transformer.TransformerImpl; 28 import org.apache.xalan.processor.StylesheetHandler; 29 30 35 public abstract class EjenChildNode implements EjenConstants { 36 37 38 private static final EjenContextsStack _ejenContextsStack = new EjenContextsStack(); 39 40 41 private static EjenListener _ejenListener = null; 42 43 44 protected static String _messageIndent = ""; 45 46 50 private int _state = STATE_IDLE; 51 52 56 public abstract String nodeName(); 57 58 62 public Properties getAttributes() { 63 return new Properties (); 64 } 65 66 70 public Vector getChildren() { 71 return new Vector (); 72 } 73 74 78 public void check() { 79 _state = STATE_CHECK; 80 sendStateEvent(); 81 } 82 83 87 public void beforeProcess() { 88 _state = STATE_BEFORE_PROCESS; 89 sendStateEvent(); 90 } 91 92 96 public void process() { 97 _state = STATE_PROCESS; 98 sendStateEvent(); 99 } 100 101 105 public void afterProcess() { 106 _state = STATE_AFTER_PROCESS; 107 sendStateEvent(); 108 } 109 110 115 public final void idle() { 116 _state = STATE_IDLE; 117 sendStateEvent(); 118 } 119 120 124 public final int getState() { 125 return _state; 126 } 127 128 132 public static final void setListener(EjenListener ejenListener) { 133 _ejenListener = ejenListener; 134 } 135 136 140 public static final EjenListener getListener() { 141 return _ejenListener; 142 } 143 144 148 public final void sendStateEvent() { 149 if (_ejenListener != null) { 150 _ejenListener.stateChanged(new EjenEvent(this, STATES[_state], 151 MSG_VERBOSE)); 152 } 153 } 154 155 161 public final void sendMessageEvent(String msg) { 162 sendMessageEvent(msg, MSG_INFO); 163 } 164 165 171 public final void sendMessageEvent(String msg, int level) { 172 if (_ejenListener != null) { 173 _ejenListener.nodeMessageSent(new EjenEvent(this, 174 _messageIndent + msg, level)); 175 } 176 } 177 178 185 public static final void sendXSLMessageEvent(String msg) { 186 sendXSLMessageEvent(msg, MSG_INFO); 187 } 188 189 197 public static final void sendXSLMessageEvent(String msg, int level) { 198 if (_ejenListener != null) { 199 _ejenListener.xslMessageSent(new EjenEvent("(xsl)", 200 _messageIndent + "[xsl] " + msg, level)); 201 } 202 } 203 204 209 public final String toString() { 210 return nodeName() + " " + getAttributes(); 211 } 212 213 218 protected final Object getFromContext(String name) { 219 return _ejenContextsStack.peekContext().get(name); 220 } 221 222 229 protected final Object getFromContext(String name, int offset) { 230 return _ejenContextsStack.peekContext(_ejenContextsStack.size() + offset - 1).get(name); 231 } 232 233 238 protected final Object getFromGlobalContext(String name) { 239 return _ejenContextsStack.globalGet(name); 240 } 241 242 249 protected final Object putInContext(String name, Object value) { 250 return _ejenContextsStack.peekContext().put(name, value); 251 } 252 253 259 protected final Object putInGlobalContext(String name, Object value) { 260 return _ejenContextsStack.globalPut(name, value); 261 } 262 263 268 protected final EjenContext popContext() { 269 return _ejenContextsStack.popContext(); 270 } 271 272 277 protected final EjenContext pushContext(EjenContext ejenContext) { 278 return _ejenContextsStack.pushContext(ejenContext); 279 } 280 281 285 protected final EjenContext cloneContext() { 286 if (!_ejenContextsStack.isEmpty()) { 287 return new EjenContext(_ejenContextsStack.peekContext()); 288 } else { 289 return new EjenContext(); 290 } 291 } 292 293 299 protected final String evaluateAVT(String avt) { 300 TransformerImpl ti = null; 301 302 try { 303 ti = (TransformerImpl) (getFromGlobalContext(CTX_TRANSFORMER_IMPL)); 304 } catch (Exception e) { 305 throw new EjenException(this, null, e); 306 } 307 if (ti == null) { 308 throw new EjenException(this, 309 "no '" + CTX_TRANSFORMER_IMPL + "' in global context"); 310 } 311 312 return evaluateAVT(ti, avt); 313 } 314 315 323 protected final String evaluateAVT(TransformerImpl ti, String avt) { 324 StylesheetHandler sh = null; 325 DOMSource src = null; 326 327 try { 328 src = (DOMSource ) (getFromGlobalContext(CTX_DOM_SOURCE)); 329 sh = (StylesheetHandler) (getFromGlobalContext(CTX_STYLESHEET_HANDLER)); 330 } catch (Exception e) { 331 throw new EjenException(this, null, e); 332 } 333 if (sh == null) { 334 throw new EjenException(this, 335 "no '" + CTX_STYLESHEET_HANDLER + "' in global context"); 336 } 337 if (src == null) { 338 throw new EjenException(this, 339 "no '" + CTX_DOM_SOURCE + "' in global context"); 340 } 341 try { 342 String s = XSLUtil.evaluateAttribute(sh, ti.getXPathContext(), 343 src.getNode(), avt); 344 345 if (!s.equals(avt)) { 346 sendMessageEvent("Resolved '" + avt + "' into '" + s + "'"); 347 } 348 return s; 349 } catch (Exception e) { 350 throw new EjenException(this, null, e); 351 } 352 } 353 } 354 | Popular Tags |