source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GS2MacroResolver.java@ 11264

Last change on this file since 11264 was 11264, checked in by kjdon, 18 years ago

recursively resolve the replacement values if resolve is set to true

  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import java.util.ArrayList;
22
23public 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 super();
32 gdbm_src = gdbm;
33 }
34
35
36 public String resolve(String text, String lang, String scope,
37 String doc_oid) {
38 if (text == null || text.equals("")) return text;
39 if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
40 if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
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 for (int i=0; i<macros.size(); i++) {
56 String new_text = null;
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 // we assume that dictionary entries will contain no macros
65 // otherwise we can't cache the answer because it might be
66 // document specific
67 text = text.replaceAll(m.macro, m.text);
68 break;
69 case TYPE_TEXT:
70 // make sure we resolve any macros in the text
71 // the (?s) treats the string as a single line, cos .
72 // doesn't necessarily match line breaks
73 if (text.matches("(?s).*"+m.macro+".*")) {
74 if (m.resolve) {
75 new_text = this.resolve(m.text, lang, scope, doc_oid);
76 } else {
77 new_text = m.text;
78 }
79 text = text.replaceAll(m.macro, new_text);
80 }
81 break;
82 case TYPE_META:
83 if (text.matches(".*"+m.macro+".*")) {
84 if (node_info == null) {
85 node_info = gdbm_src.getInfo(doc_oid);
86 if (node_info == null) {
87 break;
88 }
89 }
90 new_text = node_info.getInfo(m.text);
91 if (new_text == null || new_text.equals("")) {
92 // try the root node
93 if (root_info == null && !OID.isTop(doc_oid)) {
94 root_info = gdbm_src.getInfo(OID.getTop(doc_oid));
95 }
96 if (root_info == null) break;
97 new_text = root_info.getInfo(m.text);
98 }
99 if (new_text != null) {
100 if (m.resolve) {
101 new_text = this.resolve(new_text, lang, scope, doc_oid);
102 }
103 text = text.replaceAll(m.macro, new_text);
104 }
105
106 }
107
108 break;
109 } // switch
110
111 }
112 return text;
113
114 }
115
116}
Note: See TracBrowser for help on using the repository browser.