1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import org.greenstone.gdbm.GDBMWrapper;
|
---|
5 | import org.greenstone.gdbm.DBInfo;
|
---|
6 |
|
---|
7 | public class GS2MacroResolver {
|
---|
8 |
|
---|
9 | public static final String SCOPE_TEXT = "text";
|
---|
10 | public static final String SCOPE_META = "metadata";
|
---|
11 | public static final String SCOPE_ALL = "all";
|
---|
12 |
|
---|
13 | public static final int TYPE_TEXT = 0;
|
---|
14 | public static final int TYPE_META = 1;
|
---|
15 | public static final int TYPE_DICT = 2;
|
---|
16 |
|
---|
17 | protected ArrayList text_macros = null;
|
---|
18 | protected ArrayList metadata_macros = null;
|
---|
19 | protected GDBMWrapper gdbm_src = null;
|
---|
20 |
|
---|
21 | String lang = null;
|
---|
22 | // need to make it not add macros if they are already present
|
---|
23 | public GS2MacroResolver(GDBMWrapper wrapper) {
|
---|
24 | this.text_macros = new ArrayList();
|
---|
25 | this.metadata_macros = new ArrayList();
|
---|
26 | this.gdbm_src = wrapper;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void addMacro(int type, String macro, String text_or_metadata, String scope) {
|
---|
30 | Macro m = new Macro();
|
---|
31 | m.type = type;
|
---|
32 | m.macro = macro;
|
---|
33 | m.text = text_or_metadata;
|
---|
34 | addMacro(m, scope);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void addMacro(int type, String macro, String bundle, String key, String scope) {
|
---|
38 | Macro m = new Macro();
|
---|
39 | m.type = type;
|
---|
40 | m.macro = macro;
|
---|
41 | m.bundle = bundle;
|
---|
42 | m.key = key;
|
---|
43 | addMacro(m, scope);
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected void addMacro(Macro m, String scope) {
|
---|
47 |
|
---|
48 | if (scope.equals(SCOPE_TEXT)) {
|
---|
49 | this.text_macros.add(m);
|
---|
50 | }
|
---|
51 | else if (scope.equals(SCOPE_META)) {
|
---|
52 | this.metadata_macros.add(m);
|
---|
53 | }
|
---|
54 | else if (scope.equals(SCOPE_ALL)) {
|
---|
55 | this.text_macros.add(m);
|
---|
56 | this.metadata_macros.add(m);
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | public String resolve(String text, String lang, String scope, String doc_oid, DBInfo info) {
|
---|
63 | //System.err.println("resolving macros for "+text);
|
---|
64 | if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
|
---|
65 | if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
|
---|
66 |
|
---|
67 | DBInfo root_info = null;
|
---|
68 | boolean new_lang = false;
|
---|
69 | if (this.lang == null || !this.lang.equals(lang) ) {
|
---|
70 | new_lang = true;
|
---|
71 | this.lang = lang;
|
---|
72 | }
|
---|
73 |
|
---|
74 | ArrayList macros;
|
---|
75 | if (scope.equals(SCOPE_TEXT)) {
|
---|
76 | macros = text_macros;
|
---|
77 | } else {
|
---|
78 | macros = metadata_macros;
|
---|
79 | }
|
---|
80 |
|
---|
81 | for (int i=0; i<macros.size(); i++) {
|
---|
82 | Macro m = (Macro)macros.get(i);
|
---|
83 | switch (m.type) {
|
---|
84 | case TYPE_DICT:
|
---|
85 | if (m.text==null || new_lang) {
|
---|
86 | Dictionary dict = new Dictionary(m.bundle, lang);
|
---|
87 | m.text = dict.get(m.key, null);
|
---|
88 | }
|
---|
89 | // now drop through to text case
|
---|
90 | case TYPE_TEXT:
|
---|
91 | text = text.replaceAll(m.macro, m.text);
|
---|
92 | break;
|
---|
93 | case TYPE_META:
|
---|
94 | if (text.indexOf(m.macro) != -1) {
|
---|
95 | if (info == null) {
|
---|
96 | info = gdbm_src.getInfo(doc_oid);
|
---|
97 | if (info == null) {
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | String value = info.getInfo(m.text);
|
---|
102 | if (value != null) {
|
---|
103 | text = text.replaceAll(m.macro, value);
|
---|
104 | } else {
|
---|
105 | // try the root node
|
---|
106 | if (root_info == null && !OID.isTop(doc_oid)) {
|
---|
107 | System.err.println("top="+OID.getTop(doc_oid));
|
---|
108 | root_info = gdbm_src.getInfo(OID.getTop(doc_oid));
|
---|
109 | }
|
---|
110 | if (root_info == null) break;
|
---|
111 | value = root_info.getInfo(m.text);
|
---|
112 | if (value != null) {
|
---|
113 | text = text.replaceAll(m.macro, value);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | break;
|
---|
119 | } // switch
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | return text;
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | private class Macro {
|
---|
131 |
|
---|
132 | public int type;
|
---|
133 | public String macro = null;
|
---|
134 | public String text = null;
|
---|
135 | public String key = null;
|
---|
136 | public String bundle = null;
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | }
|
---|