1 31 32 package org.objectweb.proactive.core.xml.handler; 33 34 import org.objectweb.proactive.core.xml.io.Attributes; 35 36 44 public abstract class AbstractUnmarshallerDecorator implements UnmarshallerHandler { 45 46 47 private java.util.HashMap handlersMap; 48 private int elementCounter = 0; 49 private UnmarshallerHandler currentActiveHandler; 50 private boolean lenient; 51 52 53 57 public AbstractUnmarshallerDecorator(boolean lenient) { 58 handlersMap = new java.util.HashMap (); 59 this.lenient = lenient; 60 } 61 62 public AbstractUnmarshallerDecorator() { 63 this(true); 64 } 65 66 67 71 public void addHandler(String elementName, UnmarshallerHandler handler) { 72 handlersMap.put(elementName, handler); 73 } 74 75 76 80 82 83 87 88 public void startElement(String name, Attributes attributes) throws org.xml.sax.SAXException { 89 elementCounter++; 91 if (currentActiveHandler == null) { 92 currentActiveHandler = getHandler(name); 94 if (currentActiveHandler == null) { 95 if (lenient) { 96 currentActiveHandler = new NullUnmarshallerHandler(); 97 } else { 98 throw new org.xml.sax.SAXException ("Cannot find an handler registered for element "+name); 99 } 100 } 101 currentActiveHandler.startContextElement(name, attributes); 102 } else { 103 currentActiveHandler.startElement(name, attributes); 104 } 105 } 106 107 108 public void endElement(String name) throws org.xml.sax.SAXException { 109 checkActiveHandler(); 111 elementCounter--; 112 if (elementCounter == 0) { 113 notifyEndActiveHandler(name, currentActiveHandler); 116 currentActiveHandler = null; 117 } else { 118 currentActiveHandler.endElement(name); 119 } 120 } 121 122 123 public void readValue(String value) throws org.xml.sax.SAXException { 124 if (currentActiveHandler != null) currentActiveHandler.readValue(value); 126 } 127 128 129 public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException { 130 checkActiveHandler(); 132 currentActiveHandler.startPrefixMapping(prefix, uri); 133 } 134 135 136 public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException { 137 checkActiveHandler(); 138 currentActiveHandler.endPrefixMapping(prefix); 139 } 140 141 142 143 147 protected void checkActiveHandler() throws org.xml.sax.SAXException { 148 if (currentActiveHandler == null) throw new org.xml.sax.SAXException ("No handler is currently defined"); 149 } 150 151 152 protected abstract void notifyEndActiveHandler(String name, UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException ; 153 154 155 protected boolean checkNonEmpty(String s) { 156 return (s != null) && (s.length() > 0); 157 } 158 159 160 protected UnmarshallerHandler getHandler(String elementName) { 161 Object o = handlersMap.get(elementName); 162 if (o == null) return null; 163 return (UnmarshallerHandler) o; 164 } 165 166 167 171 172 176 private class NullUnmarshallerHandler extends BasicUnmarshaller { 177 public void startElement(String name, Attributes attributes) throws org.xml.sax.SAXException { 178 } 180 public void startContextElement(String name, Attributes attributes) throws org.xml.sax.SAXException { 181 } 183 public Object getResultObject() throws org.xml.sax.SAXException { 184 return null; 185 } 186 } 187 } | Popular Tags |