01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.repository.query;
17:
18: import org.outerj.daisy.repository.Document;
19: import org.outerj.daisy.repository.Version;
20:
21: import java.util.List;
22: import java.util.ArrayList;
23:
24: public final class EvaluationContext {
25: private List<ContextDocumentEntry> contextDocs = new ArrayList<ContextDocumentEntry>();
26:
27: public Document getContextDocument() {
28: return getContextDocument(1);
29: }
30:
31: public Version getContextVersion() {
32: return getContextVersion(1);
33: }
34:
35: public Document getContextDocument(int position) {
36: int actualPos = contextDocs.size() - position; // position specifies index from the end of the list
37: if (actualPos >= 0 && actualPos < contextDocs.size())
38: return contextDocs.get(actualPos).getDocument();
39: else
40: return null;
41: }
42:
43: public Version getContextVersion(int position) {
44: int actualPos = contextDocs.size() - position; // position specifies index from the end of the list
45: if (actualPos >= 0 && actualPos < contextDocs.size())
46: return contextDocs.get(actualPos).getVersion();
47: else
48: return null;
49: }
50:
51: /**
52: * Pushes a context document on the stack of context documents.
53: * The version is allowed to be null.
54: */
55: public void pushContextDocument(Document document, Version version) {
56: contextDocs.add(new ContextDocumentEntry(document, version));
57: }
58:
59: /**
60: * Removes a context document (the latest pushed one) from the stack of context documents.
61: */
62: public void popContextDocument() {
63: if (contextDocs.size() > 0)
64: contextDocs.remove(contextDocs.size() - 1);
65: else
66: throw new RuntimeException(
67: "EvaluationContext: stack of context documents is empty");
68: }
69:
70: /**
71: * Sets the context document. This removes any previously pushed context
72: * documents from the stack, thus after calling this method the context
73: * document stack contains just one entry. The version is allowed to be null.
74: */
75: public void setContextDocument(Document document, Version version) {
76: contextDocs.clear();
77: pushContextDocument(document, version);
78: }
79:
80: private final class ContextDocumentEntry {
81: private Document document;
82: private Version version;
83:
84: public ContextDocumentEntry(Document document, Version version) {
85: this .document = document;
86: this .version = version;
87: }
88:
89: public Document getDocument() {
90: return document;
91: }
92:
93: public Version getVersion() {
94: return version;
95: }
96: }
97: }
|