1 22 23 package org.xquark.schema; 24 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 28 public class SchemaContext { 29 private static final String RCSRevision = "$Revision: 1.1 $"; 30 private static final String RCSName = "$Name: $"; 31 32 private LinkedList contextStack = new LinkedList (); 33 private SchemaManager manager; 34 private SchemaLocator locator; 35 36 public SchemaContext(SchemaManager manager) { 37 this(manager, manager); 38 } 39 40 public SchemaContext(SchemaManager manager, SchemaLocator locator) { 41 this.manager = manager; 42 this.locator = locator; 43 } 44 45 public ElementDeclaration getElementDeclaration (String namespace, String localName) 46 { 47 if (namespace != null && namespace.length() == 0) namespace = null; 48 ElementDeclaration decl = null; 49 50 if (!contextStack.isEmpty() && contextStack.getFirst() != null) 51 { 52 decl = (ElementDeclaration)contextStack.getFirst(); 53 decl = decl.getType().getElementDeclaration(namespace, localName); 54 } 55 if (decl == null) 58 { 59 Schema schema = null; 60 try 61 { 62 schema = manager.loadSchema(locator, namespace); 63 } 64 catch ( Exception e ) 65 { 66 } 69 if (schema == null) 70 return null; 71 decl = schema.getElementDeclaration(localName); 72 } 73 return decl; 74 } 75 76 public AttributeDeclaration getAttributeDeclaration(String namespace, String localName) 77 { 78 if (namespace != null && namespace.length() == 0) namespace = null; 79 if (contextStack.isEmpty() || contextStack.getFirst() == null) { 80 Schema schema = null; 81 try { 82 schema = manager.loadSchema(locator, namespace); 83 } 84 catch ( Exception e ) { 85 throw new RuntimeException (e.getMessage()); 86 } 87 if (schema == null) return null; 88 AttributeDeclaration decl = schema.getAttributeDeclaration(localName); 89 return decl; 90 } else { 91 ElementDeclaration decl = (ElementDeclaration)contextStack.getFirst(); 92 return decl.getType().getAttributeDeclaration(namespace, localName); 93 } 94 } 95 96 public boolean isEmpty() { 97 return contextStack.isEmpty(); 98 } 99 100 public Iterator iterator() { 101 return contextStack.iterator(); 102 } 103 104 public void push(ElementDeclaration decl) { 105 contextStack.addFirst(decl); 106 } 107 108 public ElementDeclaration top() { 109 if (contextStack.isEmpty()) return null; 110 return (ElementDeclaration)contextStack.getFirst(); 111 } 112 113 public ElementDeclaration pop() { 114 if (contextStack.isEmpty()) return null; 115 return (ElementDeclaration)contextStack.removeFirst(); 116 } 117 } 118 | Popular Tags |