1 | /*
|
---|
2 | * GS2MacroResolver.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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import org.greenstone.gdbm.GDBMWrapper;
|
---|
23 | import org.greenstone.gdbm.DBInfo;
|
---|
24 |
|
---|
25 | public class GS2MacroResolver
|
---|
26 | extends MacroResolver
|
---|
27 | {
|
---|
28 |
|
---|
29 | protected GDBMWrapper gdbm_src = null;
|
---|
30 |
|
---|
31 | // need to make it not add macros if they are already present
|
---|
32 | public GS2MacroResolver(GDBMWrapper gdbm) {
|
---|
33 | gdbm_src = gdbm;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | public String resolve(String text, String lang, String scope,
|
---|
38 | String doc_oid) {
|
---|
39 | //System.err.println("resolving macros for "+text);
|
---|
40 | if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
|
---|
41 | if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
|
---|
42 |
|
---|
43 | DBInfo node_info = null;
|
---|
44 | DBInfo root_info = null;
|
---|
45 | boolean new_lang = false;
|
---|
46 | if (this.lang == null || !this.lang.equals(lang) ) {
|
---|
47 | new_lang = true;
|
---|
48 | this.lang = lang;
|
---|
49 | }
|
---|
50 |
|
---|
51 | ArrayList macros;
|
---|
52 | if (scope.equals(SCOPE_TEXT)) {
|
---|
53 | macros = text_macros;
|
---|
54 | } else {
|
---|
55 | macros = metadata_macros;
|
---|
56 | }
|
---|
57 |
|
---|
58 | for (int i=0; i<macros.size(); i++) {
|
---|
59 | Macro m = (Macro)macros.get(i);
|
---|
60 | switch (m.type) {
|
---|
61 | case TYPE_DICT:
|
---|
62 | if (m.text==null || new_lang) {
|
---|
63 | Dictionary dict = new Dictionary(m.bundle, lang);
|
---|
64 | m.text = dict.get(m.key, null);
|
---|
65 | }
|
---|
66 | // now drop through to text case
|
---|
67 | case TYPE_TEXT:
|
---|
68 | text = text.replaceAll(m.macro, m.text);
|
---|
69 | break;
|
---|
70 | case TYPE_META:
|
---|
71 | if (text.indexOf(m.macro) != -1) {
|
---|
72 | if (node_info == null) {
|
---|
73 | node_info = gdbm_src.getInfo(doc_oid);
|
---|
74 | if (node_info == null) {
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | String value = node_info.getInfo(m.text);
|
---|
80 | if (value != null) {
|
---|
81 | text = text.replaceAll(m.macro, value);
|
---|
82 | } else {
|
---|
83 | // try the root node
|
---|
84 | if (root_info == null && !OID.isTop(doc_oid)) {
|
---|
85 | root_info = gdbm_src.getInfo(OID.getTop(doc_oid));
|
---|
86 | }
|
---|
87 | if (root_info == null) break;
|
---|
88 | value = root_info.getInfo(m.text);
|
---|
89 | if (value != null) {
|
---|
90 | text = text.replaceAll(m.macro, value);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | break;
|
---|
96 | } // switch
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | return text;
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|