1 16 17 package org.apache.ws.jaxme.pm.ino.api4j; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.xml.bind.JAXBException; 24 25 import org.apache.ws.jaxme.pm.ino.InoObject; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.Locator ; 28 import org.xml.sax.SAXException ; 29 30 import com.softwareag.tamino.db.api.objectModel.sax.TSAXElement; 31 import com.softwareag.tamino.db.api.objectModel.sax.TSAXElementDefaultHandler; 32 33 34 37 public class ElementDefaultHandler extends TSAXElementDefaultHandler { 38 static class Data { 39 int level = 0; 40 boolean inDocument = false; 41 boolean iHaveCreatedStartDocument = false; 42 final List result = new ArrayList (); 43 } 44 static ThreadLocal data = new ThreadLocal (); 45 static void initData() { 46 data.set(new Data()); 47 } 48 static void resetData() { 49 data.set(null); 50 } 51 52 private Data getData() { 53 return (Data) data.get(); 54 } 55 56 public ElementDefaultHandler() { 57 } 58 59 public TSAXElement getFirstElement() { 60 List result = getData().result; 61 if (result.size() > 0) { 62 return new TJMElement((InoObject) result.get(0)); 63 } else { 64 return null; 65 } 66 } 67 68 public Iterator getElementIterator() { 69 return new Iterator (){ 70 Iterator inner = getData().result.iterator(); 71 public void remove() { 72 inner.remove(); 73 } 74 public boolean hasNext() { 75 return inner.hasNext(); 76 } 77 public Object next() { 78 return new TJMElement((InoObject) inner.next()); 79 } 80 }; 81 } 82 83 public void startDocument() throws SAXException { 84 Data d = getData(); 85 d.iHaveCreatedStartDocument = false; 86 d.inDocument = true; 87 DocumentDefaultHandler.getUnmarshallerHandler().startDocument(); 88 } 89 90 public void endDocument() throws SAXException { 91 Data d = getData(); 92 if (d.inDocument) { 93 DocumentDefaultHandler.getUnmarshallerHandler().endDocument(); 94 try { 95 d.result.add(DocumentDefaultHandler.getUnmarshallerHandler().getResult()); 96 } catch (JAXBException e) { 97 throw new SAXException (e); 98 } 99 d.inDocument = false; 100 d.iHaveCreatedStartDocument = false; 101 } 102 } 103 104 public void startElement(String pNamespaceURI, String pLocalName, String pQName, 105 Attributes pAttr) throws SAXException { 106 Data d = getData(); 107 if (d.level++ == 0 && !d.inDocument) { 108 startDocument(); 109 d.iHaveCreatedStartDocument = true; 110 } 111 DocumentDefaultHandler.getUnmarshallerHandler().startElement(pNamespaceURI, pLocalName, pQName, pAttr); 112 } 113 114 public void endElement(String pNamespaceURI, String pLocalName, String pQName) 115 throws SAXException { 116 Data d = getData(); 117 DocumentDefaultHandler.getUnmarshallerHandler().endElement(pNamespaceURI, pLocalName, pQName); 118 if (--d.level == 0 && d.iHaveCreatedStartDocument) { 119 endDocument(); 120 } 121 } 122 123 public void characters(char[] pBuffer, int pOffset, int pLen) throws SAXException { 124 Data d = getData(); 125 if (d.inDocument) { 126 DocumentDefaultHandler.getUnmarshallerHandler().characters(pBuffer, pOffset, pLen); 127 } 128 } 129 130 public void ignorableWhitespace(char[] pBuffer, int pOffset, int pLen) throws SAXException { 131 Data d = getData(); 132 if (d.inDocument) { 133 DocumentDefaultHandler.getUnmarshallerHandler().ignorableWhitespace(pBuffer, pOffset, pLen); 134 } 135 } 136 137 public void processingInstruction(String pTarget, String pData) throws SAXException { 138 Data d = getData(); 139 if (d.inDocument) { 140 DocumentDefaultHandler.getUnmarshallerHandler().processingInstruction(pTarget, pData); 141 } 142 } 143 144 public void skippedEntity(String pEntity) throws SAXException { 145 Data d = getData(); 146 if (d.inDocument) { 147 DocumentDefaultHandler.getUnmarshallerHandler().skippedEntity(pEntity); 148 } 149 } 150 151 public void endPrefixMapping(String pPrefix) throws SAXException { 152 Data d = getData(); 153 if (d.inDocument) { 154 DocumentDefaultHandler.getUnmarshallerHandler().endPrefixMapping(pPrefix); 155 } 156 } 157 158 public void startPrefixMapping(String pPrefix, String pNamespaceURI) throws SAXException { 159 Data d = getData(); 160 if (d.inDocument) { 161 DocumentDefaultHandler.getUnmarshallerHandler().startPrefixMapping(pPrefix, pNamespaceURI); 162 } 163 } 164 165 public void setDocumentLocator(Locator pLocator) { 166 Data d = getData(); 167 if (d.inDocument) { 168 DocumentDefaultHandler.getUnmarshallerHandler().setDocumentLocator(pLocator); 169 } 170 } 171 } 172 | Popular Tags |