org/objectweb/proactive/core/xml/handler/AbstractUnmarshallerDecorator.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     // -- CONSTRUCTORS -----------------------------------------------
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     // -- PUBLIC METHODS -----------------------------------------------
00065     //
00066     public void addHandler(String elementName, UnmarshallerHandler handler) {
00067         handlersMap.put(elementName, handler);
00068     }
00069 
00070     //
00071     // -- implements UnmarshallerHandler ------------------------------------------------------
00072     //  
00073     // left abstract 
00074     //
00075     // -- implements XMLHandler ------------------------------------------------------
00076     //  
00077     public void startElement(String name, Attributes attributes)
00078         throws org.xml.sax.SAXException {
00079         //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" startElement="+name);
00080         elementCounter++;
00081         if (currentActiveHandler == null) {
00082             // we look for an handler able to handle the tag
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         //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" endElement="+name+"  elementCounter="+elementCounter);
00101         checkActiveHandler();
00102         elementCounter--;
00103         if (elementCounter == 0) {
00104             // the element that triggered the currentActiveHandler is closed, we set
00105             // the handler to null
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         //System.out.println("AbstractCompositeUnmarshaller "+this.getClass().getName()+" readValue="+value);
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         //      System.out.println("prefix "+prefix+" uri "+uri);
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     // -- PROTECTED METHODS ------------------------------------------------------
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     // -- PRIVATE METHODS ------------------------------------------------------
00159     //
00160     //
00161     // -- INNER CLASSES ------------------------------------------------------
00162     //
00163     private class NullUnmarshallerHandler extends BasicUnmarshaller {
00164         public void startElement(String name, Attributes attributes)
00165             throws org.xml.sax.SAXException {
00166             //System.out.println(name+"  ignored");
00167         }
00168 
00169         public void startContextElement(String name, Attributes attributes)
00170             throws org.xml.sax.SAXException {
00171             //System.out.println(name+"  ignored");
00172         }
00173 
00174         public Object getResultObject() throws org.xml.sax.SAXException {
00175             return null;
00176         }
00177     }
00178 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1