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

Last change on this file since 36592 was 36592, checked in by kjdon, 20 months ago

modified Dictionary, so that if a lang fragment starts with [PENDING], then don't use it. This is to allow the addition of old strings which will help the translators, but which are too out of date to display in the interface. OTher classes use of Dictionary modified so that they go through getTextString, or createDictionaryAndGetString, thereby benifitting from the pending stuff

  • 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 m.text = Dictionary.createDictionaryAndGetString(m.bundle, this.class_loader, m.key, lang, null);
116 // }
117 // we assume that dictionary entries will contain no macros
118 // otherwise we can't cache the answer because it might be
119 // document specific
120 text = StringUtils.replace(text, m.macro, m.text);}
121 break;
122 case TYPE_TEXT:
123 // make sure we resolve any macros in the text
124 // the (?s) treats the string as a single line, cos .
125 // doesn't necessarily match line breaks
126 //if (text.matches("(?s).*"+m.macro+".*")) {
127
128 /*
129 * Pattern p_text = Pattern.compile(".*" + m.macro +
130 * ".*",Pattern.DOTALL); Matcher match_text =
131 * p_text.matcher(text);
132 */
133
134 // sm252
135 // String.contains is far faster than regex!
136 if (text.contains(m.macro))
137 { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
138 if (m.resolve)
139 {
140 new_text = this.resolve(m.text, lang, scope, doc_oid);
141 }
142 else
143 {
144 new_text = m.text;
145 }
146 text = StringUtils.replace(text, m.macro, new_text);
147 if (m.macro.endsWith("\\\\"))
148 { // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
149
150 Matcher m_slash = p_back_slash.matcher(text);
151 String clean_str = "";
152 int s = 0;
153 while (m_slash.find())
154 {
155 if (!text.substring(m_slash.end() - 2, m_slash.end() - 1).equals("\\"))
156 {
157 clean_str = clean_str + text.substring(s, m_slash.end() - 1); // it matches ", so get a substring before "
158 }
159 else
160 {
161 clean_str = clean_str + text.substring(s, m_slash.end() - 2);// it matches \", so get a substring before \
162 }
163 s = m_slash.end();// get the index of the last match
164 clean_str = clean_str + "\"";
165 }
166 text = clean_str + text.substring(s, text.length());
167 }
168 }
169 break;
170 case TYPE_META:
171 //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
172 //Matcher match = p.matcher(text);
173 // sm252
174 if (text.contains(m.macro))
175 { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
176 if (node_info == null)
177 {
178 node_info = coll_db.getInfo(doc_oid);
179 if (node_info == null)
180 {
181 break;
182 }
183 }
184 new_text = node_info.getInfo(m.text);
185 if (new_text == null || new_text.equals(""))
186 {
187 // try the root node
188 if (root_info == null && !OID.isTop(doc_oid))
189 {
190 root_info = coll_db.getInfo(OID.getTop(doc_oid));
191 }
192 if (root_info == null)
193 break;
194 new_text = root_info.getInfo(m.text);
195 }
196 if (new_text != null)
197 {
198 if (m.resolve)
199 {
200 new_text = this.resolve(new_text, lang, scope, doc_oid);
201 }
202 text = StringUtils.replace(text, m.macro, new_text);
203 }
204 }
205
206 break;
207 } // switch
208 }
209 return text;
210
211 }
212
213}
Note: See TracBrowser for help on using the repository browser.