View Javadoc

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