001: /*
002: * Copyright (C) 2000-2004 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=BTE
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017:
018: package com.Ostermiller.bte;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: class IncludeElement extends Element {
024:
025: private IncludeElement() {
026: super ();
027: }
028:
029: private Template template = null;
030:
031: IncludeElement(Token t) throws CompileException, IOException {
032: super (t);
033: TagToken tt;
034: TagLexer tl = new TagLexer(new ByteArrayInputStream(t
035: .getContents().getBytes()));
036: while ((tt = tl.next_token()) != null) {
037: if (tt.id == TagToken.NAME) {
038: if (tt.contents.equalsIgnoreCase("name")) {
039: if ((tt = tl.next_token()) != null
040: && tt.id == TagToken.VALUE) {
041: name = unescape(tt.contents);
042: } else {
043: throw new CompileException(
044: "Line "
045: + t.getLineNumber()
046: + ": include attribute 'name' has no value");
047: }
048:
049: } else {
050: throw new CompileException("Line "
051: + t.getLineNumber()
052: + ": include has unknown attribute: '"
053: + tt.contents + "'\n");
054: }
055: }
056: }
057: if (name == null) {
058: throw new CompileException("Line " + t.getLineNumber()
059: + ": include has no name attribute.");
060: }
061: }
062:
063: boolean resolve(Hashtable defaults, Hashtable included,
064: int onDefault, int onNoResolve) throws ResolveException {
065: template = null;
066: if (included.containsKey(name)) {
067: template = (Template) (included.get(name));
068: } else if (defaults.containsKey(name)) {
069: template = (Template) (defaults.get(name));
070: String s = "Line " + token.getLineNumber() + ": include '"
071: + name + "' could not be resolved.";
072: switch (onDefault) {
073: case Element.RESOLVE_OK:
074: break;
075: case Element.RESOLVE_ERROR:
076: throw new ResolveException(s);
077: default:
078: warnings.add(s + " Using default.");
079: break;
080: }
081: } else {
082: String s = "Line " + token.getLineNumber() + ": include '"
083: + name + "' could not be resolved.";
084: switch (onNoResolve) {
085: case Element.RESOLVE_OK:
086: break;
087: case Element.RESOLVE_WARNING:
088: warnings.add(s);
089: break;
090: default:
091: throw new ResolveException(s);
092: }
093: return (false);
094: }
095: return (true);
096: }
097:
098: void print(Writer w) throws IOException {
099: if (template != null) {
100: template.print(w);
101: }
102: super.print(w);
103: }
104: }
|