source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/MacroResolver.java@ 25300

Last change on this file since 25300 was 25300, checked in by kjdon, 12 years ago

a little bit of tidying up. adding a few more string constants to GSParams

  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 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, String library_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 if (library_name!=null){
56 addMacro(TYPE_TEXT, "_libraryname_", library_name, SCOPE_ALL, false);
57 }
58 }
59
60 public void addMacro(int type, String macro, String text_or_metadata, String scope, boolean resolve) {
61 Macro m = new Macro();
62 m.type = type;
63 m.macro = macro;
64 m.text = text_or_metadata;
65 m.resolve = resolve;
66 addMacro(m, scope);
67 }
68
69 public void addMacro(int type, String macro, String bundle, String key, String scope, boolean resolve) {
70 Macro m = new Macro();
71 m.type = type;
72 m.macro = macro;
73 m.bundle = bundle;
74 m.key = key;
75 m.resolve = resolve;
76 addMacro(m, scope);
77 }
78
79 public void addMacros(Element replace_list_elem) {
80 NodeList replaces = replace_list_elem.getElementsByTagName(GSXML.REPLACE_ELEM);
81 for (int i=0; i<replaces.getLength(); i++) {
82 Element e = (Element)replaces.item(i);
83 String scope = e.getAttribute("scope");
84 if (scope.equals("")) {
85 scope = SCOPE_ALL;
86 }
87 boolean resolve = true;
88 String resolve_str = e.getAttribute("resolve");
89 if (resolve_str.equals("false")) {
90 resolve = false;
91 }
92 String from = e.getAttribute("macro");
93 String to = e.getAttribute("text");
94 if (!to.equals("")) {
95 addMacro(TYPE_TEXT, from, to, scope, resolve);
96 } else {
97 String meta = e.getAttribute("metadata");
98 if (!meta.equals("")) {
99 addMacro(TYPE_META, from, meta, scope, resolve);
100 } else {
101 String key = e.getAttribute("key");
102 String bundle = e.getAttribute("bundle");
103 addMacro(TYPE_DICT, from, bundle, key, scope, resolve);
104 }
105 }
106 }
107
108 }
109
110 protected void addMacro(Macro m, String scope) {
111
112 if (scope.equals(SCOPE_TEXT)) {
113 this.text_macros.add(m);
114 }
115 else if (scope.equals(SCOPE_META)) {
116 this.metadata_macros.add(m);
117 }
118 else if (scope.equals(SCOPE_ALL)) {
119 this.text_macros.add(m);
120 this.metadata_macros.add(m);
121
122 }
123
124 }
125
126 abstract public String resolve(String text, String lang, String scope,
127 String doc_oid);
128
129 protected class Macro {
130
131 /** type of replacement: TEXT, METADATA, DICTIONARY */
132 public int type;
133 /** the macro to replace */
134 public String macro = null;
135 /** If text type, holds the text to replace with. If metadata type, holds the metadata name */
136 public String text = null;
137 /** If dictionary type, holds the key to look up in the dictionary */
138 public String key = null;
139 /** If dictionary type, holds the resource bundle name */
140 public String bundle = null;
141 /** If true, will try to resolve macros in the replacement text */
142 public boolean resolve = true;
143
144 }
145
146}
Note: See TracBrowser for help on using the repository browser.