[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.*;
|
---|
| 28 |
|
---|
| 29 | import java.util.Stack;
|
---|
| 30 |
|
---|
[8962] | 31 | public class GS2MacroResolver
|
---|
[14532] | 32 | extends MacroResolver
|
---|
[8962] | 33 | {
|
---|
[8573] | 34 |
|
---|
[15324] | 35 | protected SimpleCollectionDatabase coll_db = null;
|
---|
[23791] | 36 |
|
---|
| 37 | private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \"
|
---|
[8616] | 38 |
|
---|
[8573] | 39 | // need to make it not add macros if they are already present
|
---|
[15324] | 40 | public GS2MacroResolver(SimpleCollectionDatabase db) {
|
---|
[11264] | 41 | super();
|
---|
[15324] | 42 | coll_db = db;
|
---|
[8573] | 43 | }
|
---|
| 44 |
|
---|
[15756] | 45 | public GS2MacroResolver() {
|
---|
| 46 | super();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public void setDB(SimpleCollectionDatabase db) {
|
---|
| 50 | this.coll_db = db;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[8573] | 53 |
|
---|
[11264] | 54 | public String resolve(String text, String lang, String scope,
|
---|
[8962] | 55 | String doc_oid) {
|
---|
[11264] | 56 | if (text == null || text.equals("")) return text;
|
---|
[8616] | 57 | if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
|
---|
| 58 | if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
|
---|
[8962] | 59 | DBInfo node_info = null;
|
---|
[8643] | 60 | DBInfo root_info = null;
|
---|
[8573] | 61 | boolean new_lang = false;
|
---|
[8616] | 62 | if (this.lang == null || !this.lang.equals(lang) ) {
|
---|
[8573] | 63 | new_lang = true;
|
---|
| 64 | this.lang = lang;
|
---|
[8616] | 65 | }
|
---|
| 66 |
|
---|
[23798] | 67 | Stack macros = new Stack();//ArrayList macros;
|
---|
[8616] | 68 | if (scope.equals(SCOPE_TEXT)) {
|
---|
[23798] | 69 | macros.addAll(text_macros);
|
---|
[8573] | 70 | } else {
|
---|
[23798] | 71 | macros.addAll(metadata_macros);
|
---|
[8573] | 72 | }
|
---|
[23791] | 73 | //for (int i=0; i<macros.size(); i++) {
|
---|
| 74 | while(!macros.empty()) {
|
---|
[11264] | 75 | String new_text = null;
|
---|
[23791] | 76 | Macro m = (Macro)macros.pop();//.get(i);
|
---|
[8616] | 77 | switch (m.type) {
|
---|
| 78 | case TYPE_DICT:
|
---|
[8573] | 79 | if (m.text==null || new_lang) {
|
---|
| 80 | Dictionary dict = new Dictionary(m.bundle, lang);
|
---|
| 81 | m.text = dict.get(m.key, null);
|
---|
| 82 | }
|
---|
[11264] | 83 | // we assume that dictionary entries will contain no macros
|
---|
| 84 | // otherwise we can't cache the answer because it might be
|
---|
| 85 | // document specific
|
---|
[23791] | 86 | text = StringUtils.replace(text, m.macro, m.text);
|
---|
[8616] | 87 | break;
|
---|
[11264] | 88 | case TYPE_TEXT:
|
---|
| 89 | // make sure we resolve any macros in the text
|
---|
| 90 | // the (?s) treats the string as a single line, cos .
|
---|
| 91 | // doesn't necessarily match line breaks
|
---|
[14532] | 92 | //if (text.matches("(?s).*"+m.macro+".*")) {
|
---|
[23791] | 93 |
|
---|
| 94 | /*Pattern p_text = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
|
---|
| 95 | Matcher match_text = p_text.matcher(text);*/
|
---|
| 96 |
|
---|
| 97 | // sm252
|
---|
| 98 | // String.contains is far faster than regex!
|
---|
| 99 | if (text.contains(m.macro)) { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
|
---|
[11264] | 100 | if (m.resolve) {
|
---|
| 101 | new_text = this.resolve(m.text, lang, scope, doc_oid);
|
---|
| 102 | } else {
|
---|
| 103 | new_text = m.text;
|
---|
| 104 | }
|
---|
[23791] | 105 | text = StringUtils.replace(text, m.macro, new_text);
|
---|
[14532] | 106 | if (m.macro.endsWith("\\\\")){ // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
|
---|
[23791] | 107 |
|
---|
[14532] | 108 | Matcher m_slash = p_back_slash.matcher(text);
|
---|
| 109 | String clean_str = "";
|
---|
| 110 | int s=0;
|
---|
| 111 | while (m_slash.find()){
|
---|
| 112 | if (!text.substring(m_slash.end()-2,m_slash.end()-1).equals("\\")){
|
---|
| 113 | clean_str = clean_str + text.substring(s,m_slash.end()-1); // it matches ", so get a substring before "
|
---|
| 114 | }else{
|
---|
| 115 | clean_str = clean_str + text.substring(s,m_slash.end()-2);// it matches \", so get a substring before \
|
---|
| 116 | }
|
---|
| 117 | s = m_slash.end();// get the index of the last match
|
---|
| 118 | clean_str = clean_str + "\"";
|
---|
| 119 | }
|
---|
| 120 | text = clean_str + text.substring(s,text.length());
|
---|
| 121 | }
|
---|
[11264] | 122 | }
|
---|
| 123 | break;
|
---|
[8616] | 124 | case TYPE_META:
|
---|
[23791] | 125 | //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
|
---|
| 126 | //Matcher match = p.matcher(text);
|
---|
| 127 | // sm252
|
---|
| 128 | if (text.contains(m.macro)) { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
|
---|
[8962] | 129 | if (node_info == null) {
|
---|
[15324] | 130 | node_info = coll_db.getInfo(doc_oid);
|
---|
[8962] | 131 | if (node_info == null) {
|
---|
[8827] | 132 | break;
|
---|
| 133 | }
|
---|
[8616] | 134 | }
|
---|
[11264] | 135 | new_text = node_info.getInfo(m.text);
|
---|
| 136 | if (new_text == null || new_text.equals("")) {
|
---|
[8827] | 137 | // try the root node
|
---|
| 138 | if (root_info == null && !OID.isTop(doc_oid)) {
|
---|
[15324] | 139 | root_info = coll_db.getInfo(OID.getTop(doc_oid));
|
---|
[8827] | 140 | }
|
---|
| 141 | if (root_info == null) break;
|
---|
[11264] | 142 | new_text = root_info.getInfo(m.text);
|
---|
| 143 | }
|
---|
| 144 | if (new_text != null) {
|
---|
| 145 | if (m.resolve) {
|
---|
| 146 | new_text = this.resolve(new_text, lang, scope, doc_oid);
|
---|
[8827] | 147 | }
|
---|
[23791] | 148 | text = StringUtils.replace(text, m.macro, new_text);
|
---|
[8643] | 149 | }
|
---|
[11264] | 150 |
|
---|
[8643] | 151 | }
|
---|
[8827] | 152 |
|
---|
[8616] | 153 | break;
|
---|
[8643] | 154 | } // switch
|
---|
| 155 |
|
---|
[8573] | 156 | }
|
---|
| 157 | return text;
|
---|
| 158 |
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | }
|
---|