You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by hi...@apache.org on 2011/01/06 20:31:31 UTC

svn commit: r1056022 - in /ant/ivy/core/trunk/src/java/org/apache/ivy/osgi: obr/xml/OBRXMLParser.java util/DelegetingHandler.java

Author: hibou
Date: Thu Jan  6 19:31:30 2011
New Revision: 1056022

URL: http://svn.apache.org/viewvc?rev=1056022&view=rev
Log:
Make the delegating handler more reusable by making the child handler not tied to the parent.

Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/obr/xml/OBRXMLParser.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/util/DelegetingHandler.java

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/obr/xml/OBRXMLParser.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/obr/xml/OBRXMLParser.java?rev=1056022&r1=1056021&r2=1056022&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/obr/xml/OBRXMLParser.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/obr/xml/OBRXMLParser.java Thu Jan  6 19:31:30 2011
@@ -96,13 +96,17 @@ public class OBRXMLParser {
         return handler.repo;
     }
 
-    private static class RepositoryHandler extends DelegetingHandler/* <DelegetingHandler<?>> */{
+    private static class RepositoryHandler extends DelegetingHandler {
 
         BundleRepoDescriptor repo;
 
         public RepositoryHandler() {
-            super(REPOSITORY, null);
-            new ResourceHandler(this);
+            super(REPOSITORY);
+            addChild(new ResourceHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    repo.addBundle(((ResourceHandler) child).bundleInfo);
+                }
+            });
         }
 
         protected void handleAttributes(Attributes atts) {
@@ -123,18 +127,65 @@ public class OBRXMLParser {
         }
     }
 
-    private static class ResourceHandler extends DelegetingHandler/* <RepositoryHandler> */{
+    private static class ResourceHandler extends DelegetingHandler {
 
         BundleInfo bundleInfo;
 
-        public ResourceHandler(RepositoryHandler repositoryHandler) {
-            super(RESOURCE, repositoryHandler);
-            new ResourceDescriptionHandler(this);
-            new ResourceDocumentationHandler(this);
-            new ResourceLicenseHandler(this);
-            new ResourceSizeHandler(this);
-            new CapabilityHandler(this);
-            new RequireHandler(this);
+        public ResourceHandler() {
+            super(RESOURCE);
+            addChild(new ResourceDescriptionHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    bundleInfo.setDescription(((ResourceDescriptionHandler) child)
+                            .getBufferedChars().trim());
+                }
+            });
+            addChild(new ResourceDocumentationHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    bundleInfo.setDocumentation(((ResourceDocumentationHandler) child)
+                            .getBufferedChars().trim());
+                }
+            });
+            addChild(new ResourceLicenseHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    bundleInfo.setLicense(((ResourceLicenseHandler) child).getBufferedChars()
+                            .trim());
+                }
+            });
+            addChild(new ResourceSizeHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    String size = ((ResourceSizeHandler) child).getBufferedChars().trim();
+                    try {
+                        bundleInfo.setSize(Integer.valueOf(size));
+                    } catch (NumberFormatException e) {
+                        printWarning(child,
+                            "Invalid size for the bundle" + bundleInfo.getSymbolicName() + ": "
+                                    + size + ". This size is then ignored.");
+                    }
+                }
+            });
+            addChild(new CapabilityHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+
+                    try {
+                        CapabilityAdapter.adapt(bundleInfo, ((CapabilityHandler) child).capability);
+                    } catch (ParseException e) {
+                        skipResourceOnError(child, "Invalid capability: " + e.getMessage());
+                    }
+                }
+            });
+            addChild(new RequireHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    try {
+                        RequirementAdapter.adapt(bundleInfo, ((RequireHandler) child).requirement);
+                    } catch (UnsupportedFilterException e) {
+                        skipResourceOnError(child, "Unsupported requirement filter: "
+                                + ((RequireHandler) child).filter + " (" + e.getMessage() + ")");
+                    } catch (ParseException e) {
+                        skipResourceOnError(child,
+                            "Error in the requirement filter on the bundle: " + e.getMessage());
+                    }
+                }
+            });
         }
 
         protected void handleAttributes(Attributes atts) throws SAXException {
@@ -166,74 +217,71 @@ public class OBRXMLParser {
             bundleInfo.setId(atts.getValue(RESOURCE_ID));
         }
 
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            ((RepositoryHandler) getParent()).repo.addBundle(bundleInfo);
-        }
-
     }
 
-    private static class ResourceDescriptionHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class ResourceDescriptionHandler extends DelegetingHandler {
 
-        public ResourceDescriptionHandler(ResourceHandler resourceHandler) {
-            super("description", resourceHandler);
+        public ResourceDescriptionHandler() {
+            super("description");
             setBufferingChar(true);
         }
 
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            ((ResourceHandler) getParent()).bundleInfo.setDescription(getBufferedChars().trim());
-        }
     }
 
-    private static class ResourceDocumentationHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class ResourceDocumentationHandler extends DelegetingHandler {
 
-        public ResourceDocumentationHandler(ResourceHandler resourceHandler) {
-            super("documentation", resourceHandler);
+        public ResourceDocumentationHandler() {
+            super("documentation");
             setBufferingChar(true);
         }
-
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            ((ResourceHandler) getParent()).bundleInfo.setDocumentation(getBufferedChars().trim());
-        }
     }
 
-    private static class ResourceLicenseHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class ResourceLicenseHandler extends DelegetingHandler {
 
-        public ResourceLicenseHandler(ResourceHandler resourceHandler) {
-            super("license", resourceHandler);
+        public ResourceLicenseHandler() {
+            super("license");
             setBufferingChar(true);
         }
 
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            ((ResourceHandler) getParent()).bundleInfo.setLicense(getBufferedChars().trim());
-        }
     }
 
-    private static class ResourceSizeHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class ResourceSizeHandler extends DelegetingHandler {
 
-        public ResourceSizeHandler(ResourceHandler resourceHandler) {
-            super("size", resourceHandler);
+        public ResourceSizeHandler() {
+            super("size");
             setBufferingChar(true);
         }
-
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            String size = getBufferedChars().trim();
-            try {
-                ((ResourceHandler) getParent()).bundleInfo.setSize(Integer.valueOf(size));
-            } catch (NumberFormatException e) {
-                printWarning(this, "Invalid size for the bundle"
-                        + ((ResourceHandler) getParent()).bundleInfo.getSymbolicName() + ": "
-                        + size + ". This size is then ignored.");
-            }
-        }
     }
 
-    private static class CapabilityHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class CapabilityHandler extends DelegetingHandler {
 
         Capability capability;
 
-        public CapabilityHandler(ResourceHandler resourceHandler) {
-            super(CAPABILITY, resourceHandler);
-            new CapabilityPropertyHandler(this);
+        public CapabilityHandler() {
+            super(CAPABILITY);
+            addChild(new CapabilityPropertyHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    String name = ((CapabilityPropertyHandler) child).name;
+                    if (name == null) {
+                        skipResourceOnError(
+                            child,
+                            "Capability property with no name on a capability "
+                                    + capability.getName());
+                        return;
+                    }
+                    String value = ((CapabilityPropertyHandler) child).value;
+                    if (value == null) {
+                        skipResourceOnError(
+                            child,
+                            "Capability property with no value on a capability "
+                                    + capability.getName());
+                        return;
+                    }
+                    String type = ((CapabilityPropertyHandler) child).type;
+
+                    capability.addProperty(name, value, type);
+                }
+            });
         }
 
         protected void handleAttributes(Attributes atts) throws SAXException {
@@ -246,48 +294,35 @@ public class OBRXMLParser {
             capability = new Capability(name);
         }
 
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            try {
-                CapabilityAdapter.adapt(((ResourceHandler) getParent()).bundleInfo, capability);
-            } catch (ParseException e) {
-                skipResourceOnError(this, "Invalid capability: " + e.getMessage());
-            }
-        }
     }
 
-    private static class CapabilityPropertyHandler extends DelegetingHandler/* <CapabilityHandler> */{
+    private static class CapabilityPropertyHandler extends DelegetingHandler {
+
+        String name;
 
-        public CapabilityPropertyHandler(CapabilityHandler capabilityHandler) {
-            super(CAPABILITY_PROPERTY, capabilityHandler);
+        String value;
+
+        String type;
+
+        public CapabilityPropertyHandler() {
+            super(CAPABILITY_PROPERTY);
         }
 
         protected void handleAttributes(Attributes atts) throws SAXException {
-            String name = atts.getValue(CAPABILITY_PROPERTY_NAME);
-            if (name == null) {
-                skipResourceOnError(this, "Capability property with no name on a capability "
-                        + ((CapabilityHandler) getParent()).capability.getName());
-                return;
-            }
-            String value = atts.getValue(CAPABILITY_PROPERTY_VALUE);
-            if (value == null) {
-                skipResourceOnError(this, "Capability property with no value on a capability "
-                        + ((CapabilityHandler) getParent()).capability.getName());
-                return;
-            }
-            String type = atts.getValue(CAPABILITY_PROPERTY_TYPE);
-
-            ((CapabilityHandler) getParent()).capability.addProperty(name, value, type);
+            name = atts.getValue(CAPABILITY_PROPERTY_NAME);
+            value = atts.getValue(CAPABILITY_PROPERTY_VALUE);
+            type = atts.getValue(CAPABILITY_PROPERTY_TYPE);
         }
     }
 
-    private static class RequireHandler extends DelegetingHandler/* <ResourceHandler> */{
+    private static class RequireHandler extends DelegetingHandler {
 
         private Requirement requirement;
 
         private RequirementFilter filter;
 
-        public RequireHandler(ResourceHandler resourceHandler) {
-            super(REQUIRE, resourceHandler);
+        public RequireHandler() {
+            super(REQUIRE);
         }
 
         protected void handleAttributes(Attributes atts) throws SAXException {
@@ -346,17 +381,6 @@ public class OBRXMLParser {
             }
         }
 
-        protected void doEndElement(String uri, String localName, String name) throws SAXException {
-            try {
-                RequirementAdapter.adapt(((ResourceHandler) getParent()).bundleInfo, requirement);
-            } catch (UnsupportedFilterException e) {
-                skipResourceOnError(this,
-                    "Unsupported requirement filter: " + filter + " (" + e.getMessage() + ")");
-            } catch (ParseException e) {
-                skipResourceOnError(this,
-                    "Error in the requirement filter on the bundle: " + e.getMessage());
-            }
-        }
     }
 
     private static Boolean parseBoolean(Attributes atts, String name) throws ParseException {

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/util/DelegetingHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/util/DelegetingHandler.java?rev=1056022&r1=1056021&r2=1056022&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/util/DelegetingHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/util/DelegetingHandler.java Thu Jan  6 19:31:30 2011
@@ -30,18 +30,15 @@ import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
 
-public class DelegetingHandler/* <P extends DelegetingHandler<?>> */ extends DefaultHandler implements DTDHandler,
-        ContentHandler, ErrorHandler {
+public class DelegetingHandler extends DefaultHandler implements DTDHandler, ContentHandler, ErrorHandler {
 
     private DelegetingHandler/* <?> */delegate = null;
 
-    private/* P */DelegetingHandler parent;
+    private DelegetingHandler/* <?> */parent;
 
-    private final Map/* <String, DelegetingHandler<?>> */mapping = new HashMap/*
-                                                                               * <String,
-                                                                               * DelegetingHandler
-                                                                               * <?>>
-                                                                               */();
+    private final Map/* <String, DelegetingHandler<?>> */saxHandlerMapping = new HashMap();
+
+    private final Map/* <String, ChildElementHandler<?>> */childHandlerMapping = new HashMap();
 
     private final String tagName;
 
@@ -55,20 +52,22 @@ public class DelegetingHandler/* <P exte
 
     private Locator locator;
 
-    public DelegetingHandler(String name, /* P */DelegetingHandler parent) {
+    public DelegetingHandler(String name) {
         this.tagName = name;
-        this.parent = parent;
-        if (parent != null) {
-            parent.mapping.put(name, this);
-        }
         charBuffer.setLength(0);
     }
 
+    protected void addChild(DelegetingHandler saxHandler, ChildElementHandler elementHandler) {
+        saxHandlerMapping.put(saxHandler.getName(), saxHandler);
+        childHandlerMapping.put(saxHandler.getName(), elementHandler);
+        saxHandler.parent = this;
+    }
+
     public String getName() {
         return tagName;
     }
 
-    public/* P */DelegetingHandler getParent() {
+    public DelegetingHandler getParent() {
         return parent;
     }
 
@@ -86,7 +85,7 @@ public class DelegetingHandler/* <P exte
 
     public void setDocumentLocator(Locator locator) {
         this.locator = locator;
-        Iterator itHandler = mapping.values().iterator();
+        Iterator itHandler = saxHandlerMapping.values().iterator();
         while (itHandler.hasNext()) {
             DelegetingHandler/* <?> */subHandler = (DelegetingHandler) itHandler.next();
             subHandler.setDocumentLocator(locator);
@@ -99,7 +98,7 @@ public class DelegetingHandler/* <P exte
 
     public void skip() {
         skip = true;
-        Iterator itHandler = mapping.values().iterator();
+        Iterator itHandler = saxHandlerMapping.values().iterator();
         while (itHandler.hasNext()) {
             DelegetingHandler/* <?> */subHandler = (DelegetingHandler) itHandler.next();
             subHandler.stopDelegating();
@@ -111,7 +110,7 @@ public class DelegetingHandler/* <P exte
         skip = false;
         started = false;
         charBuffer.setLength(0);
-        Iterator itHandler = mapping.values().iterator();
+        Iterator itHandler = saxHandlerMapping.values().iterator();
         while (itHandler.hasNext()) {
             DelegetingHandler/* <?> */subHandler = (DelegetingHandler) itHandler.next();
             subHandler.stopDelegating();
@@ -176,7 +175,7 @@ public class DelegetingHandler/* <P exte
                     return;
                 }
                 // time now to delegate for a new element
-                delegate = (DelegetingHandler) mapping.get(localName);
+                delegate = (DelegetingHandler) saxHandlerMapping.get(localName);
                 if (delegate != null) {
                     delegate.startElement(uri, localName, n, atts);
                 }
@@ -206,8 +205,17 @@ public class DelegetingHandler/* <P exte
 
     public final void endElement(String uri, String localName, String n) throws SAXException {
         if (delegate != null) {
+            DelegetingHandler savedDelegate = delegate;
             // we are already delegating, let's continue
             delegate.endElement(uri, localName, n);
+            if (delegate == null) {
+                // we just stopped delegating, it means that the child has ended
+                ChildElementHandler childHandler = (ChildElementHandler) childHandlerMapping
+                        .get(localName);
+                if (childHandler != null) {
+                    childHandler.childHanlded(savedDelegate);
+                }
+            }
         } else {
             if (!skip) {
                 doEndElement(uri, localName, n);
@@ -226,6 +234,12 @@ public class DelegetingHandler/* <P exte
         // by default do nothing
     }
 
+    public static interface ChildElementHandler/* <DH extends DelegatingHandler> */{
+
+        public void childHanlded(/* DH */DelegetingHandler child);
+
+    }
+
     public final void characters(char[] ch, int start, int length) throws SAXException {
         if (skip) {
             return;