1 package org.oddjob.designer.arooa; 2 3 import java.util.Stack ; 4 5 import org.apache.log4j.Logger; 6 import org.oddjob.arooa.ArooaHandler; 7 import org.oddjob.arooa.ArooaContext; 8 import org.xml.sax.Attributes ; 9 import org.xml.sax.SAXParseException ; 10 11 14 public class DualHandler extends ArooaHandler { 15 private static final Logger logger = Logger.getLogger(DualHandler.class); 16 17 private final Stack handlers1 = new Stack (); 18 private final Stack handlers2 = new Stack (); 19 20 private boolean handler1OK = true; 21 private boolean handler2OK = true; 22 23 public DualHandler(ArooaHandler handler1, ArooaHandler handler2) { 24 this.handlers1.push(handler1); 25 this.handlers2.push(handler2); 26 } 27 28 public boolean isHandler1OK() { 29 return handler1OK; 30 } 31 32 public boolean isHandler2OK() { 33 return handler2OK; 34 } 35 36 public void onStartElement(String uri, String tag, String qname, 37 Attributes attrs, 38 ArooaContext context) 39 throws SAXParseException { 40 if (handler1OK) { 41 ArooaHandler handler1 = (ArooaHandler) handlers1.peek(); 42 try { 43 handler1.onStartElement(uri, tag, qname, attrs, context); 44 } 45 catch (Throwable t) { 46 logger.debug("Handler1, (" + handler1 + "), start element [" + tag + "] failed.", t); 47 handler1OK = false; 48 } 49 } 50 if (handler2OK) { 51 ArooaHandler handler2 = (ArooaHandler) handlers2.peek(); 52 try { 53 handler2.onStartElement(uri, tag, qname, attrs, context); 54 } 55 catch (Throwable t) { 56 logger.debug("Handler2 (" + handler2 + "), start element [" + tag + "] failed.", t); 57 handler2OK = false; 58 } 59 } 60 } 61 62 public ArooaHandler onStartChild(String uri, String tag, String qname, 63 Attributes attrs, 64 ArooaContext context) 65 throws SAXParseException { 66 if (handler1OK) { 67 ArooaHandler handler1 = (ArooaHandler) handlers1.peek(); 68 try { 69 ArooaHandler nextHandler = handler1.onStartChild(uri, tag, qname, attrs, context); 70 handlers1.push(nextHandler); 71 } 72 catch (Throwable t) { 73 logger.debug("Handler1, start child [" + tag + "] failed.", t); 74 handler1OK = false; 75 } 76 } 77 if (handler2OK) { 78 ArooaHandler handler2 = (ArooaHandler) handlers2.peek(); 79 try { 80 ArooaHandler nextHandler = handler2.onStartChild(uri, tag, qname, attrs, context); 81 handlers2.push(nextHandler); 82 } 83 catch (Throwable t) { 84 logger.debug("Handler2, start child [" + tag + "] failed.", t); 85 handler2OK = false; 86 } 87 } 88 return this; 89 } 90 91 public void onEndChild(String uri, String tag, String qname, 92 ArooaContext context) 93 throws SAXParseException { 94 if (handler1OK) { 95 handlers1.pop(); 96 ArooaHandler handler1 = (ArooaHandler) handlers1.peek(); 97 try { 98 handler1.onEndChild(uri, tag, qname, context); 99 } 100 catch (Throwable t) { 101 logger.debug("Handler1, end child [" + tag + "] failed.", t); 102 handler1OK = false; 103 } 104 } 105 if (handler2OK) { 106 handlers2.pop(); 107 ArooaHandler handler2 = (ArooaHandler) handlers2.peek(); 108 try { 109 handler2.onEndChild(uri, tag, qname, context); 110 } 111 catch (Throwable t) { 112 logger.debug("Handler2, end child [" + tag + "] failed.", t); 113 handler2OK = false; 114 } 115 } 116 } 117 118 public void onEndElement(String uri, String tag, 119 ArooaContext context) { 120 if (handler1OK) { 121 ArooaHandler handler1 = (ArooaHandler) handlers1.peek(); 122 try { 123 handler1.onEndElement(uri, tag, context); 124 } 125 catch (Throwable t) { 126 logger.debug("Handler1, end element [" + tag + "] failed.", t); 127 handler1OK = false; 128 } 129 } 130 if (handler2OK) { 131 ArooaHandler handler2 = (ArooaHandler) handlers2.peek(); 132 try { 133 handler2.onEndElement(uri, tag, context); 134 } 135 catch (Throwable t) { 136 logger.debug("Handler2, end element [" + tag + "] failed.", t); 137 handler2OK = false; 138 } 139 } 140 } 141 142 public void characters(char[] buf, int start, int count, ArooaContext context) 143 throws SAXParseException { 144 if (handler1OK) { 145 ArooaHandler handler1 = (ArooaHandler) handlers1.peek(); 146 try { 147 handler1.characters(buf, start, count, context); 148 } 149 catch (Throwable t) { 150 logger.debug("Handler1 (" + handler1 + "), characters [" + new String (buf, start, count) + "] failed.", t); 151 handler1OK = false; 152 } 153 } 154 if (handler2OK) { 155 ArooaHandler handler2 = (ArooaHandler) handlers2.peek(); 156 try { 157 handler2.characters(buf, start, count, context); 158 } 159 catch (Throwable t) { 160 logger.debug("Handler2 (" + handler2 + "), characters [" + new String (buf, start, count) + "] failed.", t); 161 handler2OK = false; 162 } 163 } 164 } 165 166 } 167 168 | Popular Tags |