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

Last change on this file since 8962 was 8962, checked in by kjdon, 19 years ago

made a base class which GS2MAcroREsolver inherits

  • Property svn:keywords set to Author Date Id Revision
File size: 3.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 public void addMacro(int type, String macro, String text_or_metadata, String scope) {
48 Macro m = new Macro();
49 m.type = type;
50 m.macro = macro;
51 m.text = text_or_metadata;
52 addMacro(m, scope);
53 }
54
55 public void addMacro(int type, String macro, String bundle, String key, String scope) {
56 Macro m = new Macro();
57 m.type = type;
58 m.macro = macro;
59 m.bundle = bundle;
60 m.key = key;
61 addMacro(m, scope);
62 }
63
64 public void addMacros(Element replace_list_elem) {
65 NodeList replaces = replace_list_elem.getElementsByTagName("replace");
66 for (int i=0; i<replaces.getLength(); i++) {
67 Element e = (Element)replaces.item(i);
68 String scope = e.getAttribute("scope");
69 if (scope.equals("")) {
70 scope = SCOPE_ALL;
71 }
72 String from = e.getAttribute("macro");
73 String to = e.getAttribute("text");
74 if (!to.equals("")) {
75 addMacro(TYPE_TEXT, from, to, scope);
76 } else {
77 String meta = e.getAttribute("metadata");
78 if (!meta.equals("")) {
79 addMacro(TYPE_META, from, meta, scope);
80 } else {
81 String key = e.getAttribute("key");
82 String bundle = e.getAttribute("bundle");
83 addMacro(TYPE_DICT, from, bundle, key, scope);
84 }
85 }
86 }
87
88 }
89
90 protected void addMacro(Macro m, String scope) {
91
92 if (scope.equals(SCOPE_TEXT)) {
93 this.text_macros.add(m);
94 }
95 else if (scope.equals(SCOPE_META)) {
96 this.metadata_macros.add(m);
97 }
98 else if (scope.equals(SCOPE_ALL)) {
99 this.text_macros.add(m);
100 this.metadata_macros.add(m);
101
102 }
103
104 }
105
106 abstract public String resolve(String text, String lang, String scope,
107 String doc_oid);
108
109 protected class Macro {
110
111 public int type;
112 public String macro = null;
113 public String text = null;
114 public String key = null;
115 public String bundle = null;
116
117 }
118
119}
Note: See TracBrowser for help on using the repository browser.