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