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