00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.xml.handler;
00032
00033 import org.objectweb.proactive.core.xml.io.Attributes;
00034
00035
00044 public abstract class AbstractUnmarshallerDecorator
00045 implements UnmarshallerHandler {
00046 private java.util.HashMap handlersMap;
00047 private int elementCounter = 0;
00048 private UnmarshallerHandler currentActiveHandler;
00049 private boolean lenient;
00050
00051
00052
00053
00054 public AbstractUnmarshallerDecorator(boolean lenient) {
00055 handlersMap = new java.util.HashMap();
00056 this.lenient = lenient;
00057 }
00058
00059 public AbstractUnmarshallerDecorator() {
00060 this(true);
00061 }
00062
00063
00064
00065
00066 public void addHandler(String elementName, UnmarshallerHandler handler) {
00067 handlersMap.put(elementName, handler);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077 public void startElement(String name, Attributes attributes)
00078 throws org.xml.sax.SAXException {
00079
00080 elementCounter++;
00081 if (currentActiveHandler == null) {
00082
00083 currentActiveHandler = getHandler(name);
00084 if (currentActiveHandler == null) {
00085 if (lenient) {
00086 currentActiveHandler = new NullUnmarshallerHandler();
00087 } else {
00088 throw new org.xml.sax.SAXException(
00089 "Cannot find an handler registered for element " +
00090 name);
00091 }
00092 }
00093 currentActiveHandler.startContextElement(name, attributes);
00094 } else {
00095 currentActiveHandler.startElement(name, attributes);
00096 }
00097 }
00098
00099 public void endElement(String name) throws org.xml.sax.SAXException {
00100
00101 checkActiveHandler();
00102 elementCounter--;
00103 if (elementCounter == 0) {
00104
00105
00106 notifyEndActiveHandler(name, currentActiveHandler);
00107 currentActiveHandler = null;
00108 } else {
00109 currentActiveHandler.endElement(name);
00110 }
00111 }
00112
00113 public void readValue(String value) throws org.xml.sax.SAXException {
00114
00115 if (currentActiveHandler != null) {
00116 currentActiveHandler.readValue(value);
00117 }
00118 }
00119
00120 public void startPrefixMapping(String prefix, String uri)
00121 throws org.xml.sax.SAXException {
00122
00123 checkActiveHandler();
00124 currentActiveHandler.startPrefixMapping(prefix, uri);
00125 }
00126
00127 public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException {
00128 checkActiveHandler();
00129 currentActiveHandler.endPrefixMapping(prefix);
00130 }
00131
00132
00133
00134
00135 protected void checkActiveHandler() throws org.xml.sax.SAXException {
00136 if (currentActiveHandler == null) {
00137 throw new org.xml.sax.SAXException(
00138 "No handler is currently defined");
00139 }
00140 }
00141
00142 protected abstract void notifyEndActiveHandler(String name,
00143 UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException;
00144
00145 protected boolean checkNonEmpty(String s) {
00146 return (s != null) && (s.length() > 0);
00147 }
00148
00149 protected UnmarshallerHandler getHandler(String elementName) {
00150 Object o = handlersMap.get(elementName);
00151 if (o == null) {
00152 return null;
00153 }
00154 return (UnmarshallerHandler) o;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 private class NullUnmarshallerHandler extends BasicUnmarshaller {
00164 public void startElement(String name, Attributes attributes)
00165 throws org.xml.sax.SAXException {
00166
00167 }
00168
00169 public void startContextElement(String name, Attributes attributes)
00170 throws org.xml.sax.SAXException {
00171
00172 }
00173
00174 public Object getResultObject() throws org.xml.sax.SAXException {
00175 return null;
00176 }
00177 }
00178 }