KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > metadata > unmarshalling > BindingCursor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xb.binding.metadata.unmarshalling;
8
9 import javax.xml.namespace.QName JavaDoc;
10 import java.util.LinkedList JavaDoc;
11
12 /**
13  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
14  * @version <tt>$Revision: 1.1.2.1 $</tt>
15  */

16 public interface BindingCursor
17 {
18    void startElement(String JavaDoc namespaceURI, String JavaDoc localName);
19
20    void endElement(String JavaDoc namespaceURI, String JavaDoc localName);
21
22    Object JavaDoc getElementBinding();
23
24    Object JavaDoc getParentElementBinding();
25
26    class Factory
27    {
28       private Factory()
29       {
30       }
31
32       public static BindingCursor newCursor(DocumentBinding doc)
33       {
34          return doc == null ? NoopBindingCursor.INSTANCE : new BindingCursorImpl(doc);
35       }
36
37       private static class BindingCursorImpl
38          implements BindingCursor
39       {
40          private final DocumentBinding docBinding;
41          private final LinkedList JavaDoc stack = new LinkedList JavaDoc();
42
43          public BindingCursorImpl(DocumentBinding docBinding)
44          {
45             this.docBinding = docBinding;
46          }
47
48          public void startElement(String JavaDoc namespaceURI, String JavaDoc localName)
49          {
50             BasicElementBinding elBinding;
51             if(stack.isEmpty())
52             {
53                NamespaceBinding nsBinding = docBinding.getNamespace(namespaceURI);
54                elBinding = nsBinding.getTopElement(localName);
55             }
56             else
57             {
58                elBinding = (BasicElementBinding)stack.getLast();
59                elBinding = elBinding.getElement(new QName JavaDoc(namespaceURI, localName));
60             }
61             stack.addLast(elBinding);
62          }
63
64          public void endElement(String JavaDoc namespaceURI, String JavaDoc localName)
65          {
66             stack.removeLast();
67          }
68
69          public Object JavaDoc getElementBinding()
70          {
71             return (BasicElementBinding)stack.getLast();
72          }
73
74          public Object JavaDoc getParentElementBinding()
75          {
76             return stack.size() - 2 >= 0 ? (BasicElementBinding)stack.get(stack.size() - 2) : null;
77          }
78       }
79
80       private static class NoopBindingCursor
81          implements BindingCursor
82       {
83          static final BindingCursor INSTANCE = new NoopBindingCursor();
84
85          private NoopBindingCursor()
86          {
87          }
88
89          public void startElement(String JavaDoc namespaceURI, String JavaDoc localName)
90          {
91          }
92
93          public void endElement(String JavaDoc namespaceURI, String JavaDoc localName)
94          {
95          }
96
97          public Object JavaDoc getElementBinding()
98          {
99             return null;
100          }
101
102          public Object JavaDoc getParentElementBinding()
103          {
104             return null;
105          }
106       }
107    }
108 }
109
Popular Tags