KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > SchemaContext


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 public class SchemaContext {
29 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
30 private static final String JavaDoc RCSName = "$Name: $";
31
32   private LinkedList JavaDoc contextStack = new LinkedList JavaDoc();
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 JavaDoc namespace, String JavaDoc 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       // search in top level declaration if at the root or substitution group or wildcard
56
// TO DO : check that this is a substitution group or wildcard
57
if (decl == null)
58       {
59           Schema schema = null;
60           try
61           {
62               schema = manager.loadSchema(locator, namespace);
63           }
64           catch ( Exception JavaDoc e )
65           {
66       //e.printStackTrace(System.out);
67
//throw new RuntimeException(e.getMessage());
68
}
69           if (schema == null)
70               return null;
71           decl = schema.getElementDeclaration(localName);
72       }
73       return decl;
74   }
75   
76   public AttributeDeclaration getAttributeDeclaration(String JavaDoc namespace, String JavaDoc 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 JavaDoc e ) {
85                 throw new RuntimeException JavaDoc(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 JavaDoc 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