1 7 package org.jboss.xb.binding.metadata.unmarshalling; 8 9 import javax.xml.namespace.QName ; 10 import java.util.LinkedList ; 11 12 16 public interface BindingCursor 17 { 18 void startElement(String namespaceURI, String localName); 19 20 void endElement(String namespaceURI, String localName); 21 22 Object getElementBinding(); 23 24 Object 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 stack = new LinkedList (); 42 43 public BindingCursorImpl(DocumentBinding docBinding) 44 { 45 this.docBinding = docBinding; 46 } 47 48 public void startElement(String namespaceURI, String 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 (namespaceURI, localName)); 60 } 61 stack.addLast(elBinding); 62 } 63 64 public void endElement(String namespaceURI, String localName) 65 { 66 stack.removeLast(); 67 } 68 69 public Object getElementBinding() 70 { 71 return (BasicElementBinding)stack.getLast(); 72 } 73 74 public Object 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 namespaceURI, String localName) 90 { 91 } 92 93 public void endElement(String namespaceURI, String localName) 94 { 95 } 96 97 public Object getElementBinding() 98 { 99 return null; 100 } 101 102 public Object getParentElementBinding() 103 { 104 return null; 105 } 106 } 107 } 108 } 109 | Popular Tags |