[8962] | 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 | */
|
---|
[8573] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import java.util.ArrayList;
|
---|
[14532] | 22 | import java.util.regex.Matcher;
|
---|
| 23 | import java.util.regex.Pattern;
|
---|
| 24 | import java.net.URLDecoder;
|
---|
[8573] | 25 |
|
---|
[23791] | 26 | // Apache Commons
|
---|
| 27 | import org.apache.commons.lang3.*;
|
---|
[25635] | 28 | import org.greenstone.gsdl3.util.MacroResolver.Macro;
|
---|
[23791] | 29 |
|
---|
| 30 | import java.util.Stack;
|
---|
| 31 |
|
---|
[25671] | 32 | public class GS2MacroResolver extends MacroResolver
|
---|
[8962] | 33 | {
|
---|
[8573] | 34 |
|
---|
[25671] | 35 | protected SimpleCollectionDatabase coll_db = null;
|
---|
[26196] | 36 | protected ClassLoader class_loader = null;
|
---|
[25671] | 37 | private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \"
|
---|
[8573] | 38 |
|
---|
[25671] | 39 | // need to make it not add macros if they are already present
|
---|
[26196] | 40 | public GS2MacroResolver(SimpleCollectionDatabase db, ClassLoader class_loader)
|
---|
[25671] | 41 | {
|
---|
| 42 | super();
|
---|
| 43 | coll_db = db;
|
---|
[26196] | 44 | this.class_loader = class_loader;
|
---|
[25671] | 45 | }
|
---|
[15756] | 46 |
|
---|
[25671] | 47 | public GS2MacroResolver()
|
---|
| 48 | {
|
---|
| 49 | super();
|
---|
| 50 | }
|
---|
[15756] | 51 |
|
---|
[25671] | 52 | public void setDB(SimpleCollectionDatabase db)
|
---|
| 53 | {
|
---|
| 54 | this.coll_db = db;
|
---|
| 55 | }
|
---|
[26196] | 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)
|
---|
[25671] | 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;
|
---|
[8573] | 75 | }
|
---|
[23791] | 76 |
|
---|
[25671] | 77 | Stack<Macro> macros = new Stack<Macro>();//ArrayList macros;
|
---|
| 78 | if (scope.equals(SCOPE_TEXT))
|
---|
| 79 | {
|
---|
| 80 | macros.addAll(text_macros);
|
---|
[25672] | 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);
|
---|
[11264] | 95 | }
|
---|
[25671] | 96 | else
|
---|
| 97 | {
|
---|
| 98 | macros.addAll(metadata_macros);
|
---|
[8643] | 99 | }
|
---|
[25672] | 100 |
|
---|
[25671] | 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:
|
---|
[26196] | 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 | // }
|
---|
[25671] | 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
|
---|
[26196] | 121 | text = StringUtils.replace(text, m.macro, m.text);}
|
---|
[25671] | 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 | {
|
---|
[26196] | 141 | new_text = this.resolve(m.text, lang, scope, doc_oid);
|
---|
[25671] | 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 | {
|
---|
[26196] | 201 | new_text = this.resolve(new_text, lang, scope, doc_oid);
|
---|
[25671] | 202 | }
|
---|
| 203 | text = StringUtils.replace(text, m.macro, new_text);
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | break;
|
---|
| 208 | } // switch
|
---|
| 209 | }
|
---|
| 210 | return text;
|
---|
| 211 |
|
---|
[8573] | 212 | }
|
---|
| 213 |
|
---|
| 214 | }
|
---|