View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.webapp;
15  
16  import java.lang.reflect.Method;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import org.eclipse.jetty.xml.XmlParser;
22  
23  /**
24   * IterativeDescriptorProcessor
25   *
26   *
27   */
28  public abstract class IterativeDescriptorProcessor implements DescriptorProcessor
29  {
30      public static final Class<?>[] __signature = new Class[]{WebAppContext.class, Descriptor.class, XmlParser.Node.class};
31      protected Map<String, Method> _visitors = new HashMap<String, Method>();
32      public abstract void start(WebAppContext context, Descriptor descriptor);
33      public abstract void end(WebAppContext context, Descriptor descriptor);
34  
35      /**
36       * Register a method to be called back when visiting the node with the given name.
37       * The method must exist on a subclass of this class, and must have the signature:
38       * public void method (Descriptor descriptor, XmlParser.Node node)
39       * @param nodeName
40       * @param m
41       */
42      public void registerVisitor(String nodeName, Method m)
43      {
44          _visitors.put(nodeName, m);
45      }
46  
47      
48      /** 
49       * {@inheritDoc}
50       */
51      public void process(WebAppContext context, Descriptor descriptor)
52      throws Exception
53      {
54          if (descriptor == null)
55              return;
56  
57          start(context,descriptor);
58  
59          XmlParser.Node root = descriptor.getRoot();
60          Iterator iter = root.iterator();
61          XmlParser.Node node = null;
62          while (iter.hasNext())
63          {
64              Object o = iter.next();
65              if (!(o instanceof XmlParser.Node)) continue;
66              node = (XmlParser.Node) o;
67              visit(context, descriptor, node);
68          }
69  
70          end(context,descriptor);
71      }
72  
73  
74      protected void visit (WebAppContext context, Descriptor descriptor, XmlParser.Node node)
75      throws Exception
76      {
77          String name = node.getTag();
78          Method m =  _visitors.get(name);
79          if (m != null)
80              m.invoke(this, new Object[]{context, descriptor, node});
81      }
82  }