source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/MacroResolver.java@ 11263

Last change on this file since 11263 was 11263, checked in by kjdon, 18 years ago

added a new field to macros: resolve. if true, will try to resolve macros (recursively) in the replacement value. also added setSAiteDetails() method, which sets up two default macros, _httpsite_ and _clustername_

  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1/*
2 * MacroResolver.java
3 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.util.ArrayList;
22
23import org.w3c.dom.Element;
24import org.w3c.dom.NodeList;
25
26
27abstract public class MacroResolver {
28
29 public static final String SCOPE_TEXT = "text";
30 public static final String SCOPE_META = "metadata";
31 public static final String SCOPE_ALL = "all";
32
33 public static final int TYPE_TEXT = 0;
34 public static final int TYPE_META = 1;
35 public static final int TYPE_DICT = 2;
36
37 protected ArrayList text_macros = null;
38 protected ArrayList metadata_macros = null;
39
40 String lang = null;
41
42 public MacroResolver() {
43 this.text_macros = new ArrayList();
44 this.metadata_macros = new ArrayList();
45 }
46
47
48 public void setSiteDetails(String site_address, String cluster_name) {
49 if (site_address != null) {
50 addMacro(TYPE_TEXT, "_httpsite_", site_address, SCOPE_ALL, false);
51 }
52 if (cluster_name != null) {
53 addMacro(TYPE_TEXT, "_clustername_", cluster_name, SCOPE_ALL, false);
54 }
55 }
56
57 public void addMacro(int type, String macro, String text_or_metadata, String scope, boolean resolve) {
58 Macro m = new Macro();
59 m.type = type;
60 m.macro = macro;
61 m.text = text_or_metadata;
62 m.resolve = resolve;
63 addMacro(m, scope);
64 }
65
66 public void addMacro(int type, String macro, String bundle, String key, String scope, boolean resolve) {
67 Macro m = new Macro();
68 m.type = type;
69 m.macro = macro;
70 m.bundle = bundle;
71 m.key = key;
72 m.resolve = resolve;
73 addMacro(m, scope);
74 }
75
76 public void addMacros(Element replace_list_elem) {
77 NodeList replaces = replace_list_elem.getElementsByTagName("replace");
78 for (int i=0; i<replaces.getLength(); i++) {
79 Element e = (Element)replaces.item(i);
80 String scope = e.getAttribute("scope");
81 if (scope.equals("")) {
82 scope = SCOPE_ALL;
83 }
84 boolean resolve = true;
85 String resolve_str = e.getAttribute("resolve");
86 if (resolve_str.equals("false")) {
87 resolve = false;
88 }
89 String from = e.getAttribute("macro");
90 String to = e.getAttribute("text");
91 if (!to.equals("")) {
92 addMacro(TYPE_TEXT, from, to, scope, resolve);
93 } else {
94 String meta = e.getAttribute("metadata");
95 if (!meta.equals("")) {
96 addMacro(TYPE_META, from, meta, scope, resolve);
97 } else {
98 String key = e.getAttribute("key");
99 String bundle = e.getAttribute("bundle");
100 addMacro(TYPE_DICT, from, bundle, key, scope, resolve);
101 }
102 }
103 }
104
105 }
106
107 protected void addMacro(Macro m, String scope) {
108
109 if (scope.equals(SCOPE_TEXT)) {
110 this.text_macros.add(m);
111 }
112 else if (scope.equals(SCOPE_META)) {
113 this.metadata_macros.add(m);
114 }
115 else if (scope.equals(SCOPE_ALL)) {
116 this.text_macros.add(m);
117 this.metadata_macros.add(m);
118
119 }
120
121 }
122
123 abstract public String resolve(String text, String lang, String scope,
124 String doc_oid);
125
126 protected class Macro {
127
128 /** type of replacement: TEXT, METADATA, DICTIONARY */
129 public int type;
130 /** the macro to replace */
131 public String macro = null;
132 /** If text type, holds the text to replace with. If metadata type, holds the metadata name */
133 public String text = null;
134 /** If dictionary type, holds the key to look up in the dictionary */
135 public String key = null;
136 /** If dictionary type, holds the resource bundle name */
137 public String bundle = null;
138 /** If true, will try to resolve macros in the replacement text */
139 public boolean resolve = true;
140
141 }
142
143}
Note: See TracBrowser for help on using the repository browser.