source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GS2MacroResolver.java@ 28965

Last change on this file since 28965 was 26196, checked in by kjdon, 12 years ago

use a class loader for the dictionary to allow a colleciton to have a resource bundle in its resources folder. Need to read from the dictionary each time, without caching, otherwise may end up with the wrong language being cached. Thi scan happen when you ask for a metadata value to be resolved. the lang is new, so reload the dictionary entry. But when ask for a text to be resolved, now the lang does not look new, but the last time the text fragment was looked up in the dictionary it might have been a different language. - not all dictionary lookups are done each time, so can get out of date. So now, do a new lookup each time, but only do it if the macro is in the text

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 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;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24import java.net.URLDecoder;
25
26// Apache Commons
27import org.apache.commons.lang3.*;
28import org.greenstone.gsdl3.util.MacroResolver.Macro;
29
30import java.util.Stack;
31
32public class GS2MacroResolver extends MacroResolver
33{
34
35 protected SimpleCollectionDatabase coll_db = null;
36 protected ClassLoader class_loader = null;
37 private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \"
38
39 // need to make it not add macros if they are already present
40 public GS2MacroResolver(SimpleCollectionDatabase db, ClassLoader class_loader)
41 {
42 super();
43 coll_db = db;
44 this.class_loader = class_loader;
45 }
46
47 public GS2MacroResolver()
48 {
49 super();
50 }
51
52 public void setDB(SimpleCollectionDatabase db)
53 {
54 this.coll_db = db;
55 }
56 public void setClassLoader(ClassLoader class_loader) {
57 this.class_loader = class_loader;
58 }
59
60 public String resolve(String text, String lang, String scope, String doc_oid)
61 {
62 if (text == null || text.equals(""))
63 return text;
64 if (scope.equals(SCOPE_TEXT) && text_macros.size() == 0)
65 return text;
66 if (scope.equals(SCOPE_META) && metadata_macros.size() == 0)
67 return text;
68 DBInfo node_info = null;
69 DBInfo root_info = null;
70 boolean new_lang = false;
71 if (this.lang == null || !this.lang.equals(lang))
72 {
73 new_lang = true;
74 this.lang = lang;
75 }
76
77 Stack<Macro> macros = new Stack<Macro>();//ArrayList macros;
78 if (scope.equals(SCOPE_TEXT))
79 {
80 macros.addAll(text_macros);
81
82 //Two helpful runtime macros
83 Macro docIDMacro = new Macro();
84 docIDMacro.macro = "[DocOID]";
85 docIDMacro.text = doc_oid;
86 docIDMacro.type = TYPE_TEXT;
87
88 Macro docTopIDMacro = new Macro();
89 docTopIDMacro.macro = "[DocTopOID]";
90 docTopIDMacro.text = OID.getTop(doc_oid);
91 docTopIDMacro.type = TYPE_TEXT;
92
93 macros.add(docIDMacro);
94 macros.add(docTopIDMacro);
95 }
96 else
97 {
98 macros.addAll(metadata_macros);
99 }
100
101 //for (int i=0; i<macros.size(); i++) {
102 while (!macros.empty())
103 {
104 String new_text = null;
105 Macro m = macros.pop();//.get(i);
106 switch (m.type)
107 {
108 case TYPE_DICT:
109 if (text.contains(m.macro))
110
111 {
112 // if we change the lang, then do a metadata resolve, then a text resolve, the lang hasn't changed, but the text might be leftover from the last language.
113 // if (m.text == null || new_lang)
114 // {
115 Dictionary dict = new Dictionary(m.bundle, lang, this.class_loader);
116 m.text = dict.get(m.key, null);
117 // }
118 // we assume that dictionary entries will contain no macros
119 // otherwise we can't cache the answer because it might be
120 // document specific
121 text = StringUtils.replace(text, m.macro, m.text);}
122 break;
123 case TYPE_TEXT:
124 // make sure we resolve any macros in the text
125 // the (?s) treats the string as a single line, cos .
126 // doesn't necessarily match line breaks
127 //if (text.matches("(?s).*"+m.macro+".*")) {
128
129 /*
130 * Pattern p_text = Pattern.compile(".*" + m.macro +
131 * ".*",Pattern.DOTALL); Matcher match_text =
132 * p_text.matcher(text);
133 */
134
135 // sm252
136 // String.contains is far faster than regex!
137 if (text.contains(m.macro))
138 { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
139 if (m.resolve)
140 {
141 new_text = this.resolve(m.text, lang, scope, doc_oid);
142 }
143 else
144 {
145 new_text = m.text;
146 }
147 text = StringUtils.replace(text, m.macro, new_text);
148 if (m.macro.endsWith("\\\\"))
149 { // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
150
151 Matcher m_slash = p_back_slash.matcher(text);
152 String clean_str = "";
153 int s = 0;
154 while (m_slash.find())
155 {
156 if (!text.substring(m_slash.end() - 2, m_slash.end() - 1).equals("\\"))
157 {
158 clean_str = clean_str + text.substring(s, m_slash.end() - 1); // it matches ", so get a substring before "
159 }
160 else
161 {
162 clean_str = clean_str + text.substring(s, m_slash.end() - 2);// it matches \", so get a substring before \
163 }
164 s = m_slash.end();// get the index of the last match
165 clean_str = clean_str + "\"";
166 }
167 text = clean_str + text.substring(s, text.length());
168 }
169 }
170 break;
171 case TYPE_META:
172 //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
173 //Matcher match = p.matcher(text);
174 // sm252
175 if (text.contains(m.macro))
176 { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
177 if (node_info == null)
178 {
179 node_info = coll_db.getInfo(doc_oid);
180 if (node_info == null)
181 {
182 break;
183 }
184 }
185 new_text = node_info.getInfo(m.text);
186 if (new_text == null || new_text.equals(""))
187 {
188 // try the root node
189 if (root_info == null && !OID.isTop(doc_oid))
190 {
191 root_info = coll_db.getInfo(OID.getTop(doc_oid));
192 }
193 if (root_info == null)
194 break;
195 new_text = root_info.getInfo(m.text);
196 }
197 if (new_text != null)
198 {
199 if (m.resolve)
200 {
201 new_text = this.resolve(new_text, lang, scope, doc_oid);
202 }
203 text = StringUtils.replace(text, m.macro, new_text);
204 }
205 }
206
207 break;
208 } // switch
209 }
210 return text;
211
212 }
213
214}
Note: See TracBrowser for help on using the repository browser.