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 |
|
---|
29 | import java.util.Stack;
|
---|
30 |
|
---|
31 | public class GS2MacroResolver
|
---|
32 | 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 | super();
|
---|
42 | coll_db = db;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public GS2MacroResolver() {
|
---|
46 | super();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void setDB(SimpleCollectionDatabase db) {
|
---|
50 | this.coll_db = db;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | public String resolve(String text, String lang, String scope,
|
---|
55 | String doc_oid) {
|
---|
56 | if (text == null || text.equals("")) return text;
|
---|
57 | if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
|
---|
58 | if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
|
---|
59 | DBInfo node_info = null;
|
---|
60 | DBInfo root_info = null;
|
---|
61 | boolean new_lang = false;
|
---|
62 | if (this.lang == null || !this.lang.equals(lang) ) {
|
---|
63 | new_lang = true;
|
---|
64 | this.lang = lang;
|
---|
65 | }
|
---|
66 |
|
---|
67 | Stack macros = new Stack();//ArrayList macros;
|
---|
68 | if (scope.equals(SCOPE_TEXT)) {
|
---|
69 | macros.addAll(text_macros);
|
---|
70 | } else {
|
---|
71 | macros.addAll(metadata_macros);
|
---|
72 | }
|
---|
73 | //for (int i=0; i<macros.size(); i++) {
|
---|
74 | while(!macros.empty()) {
|
---|
75 | String new_text = null;
|
---|
76 | Macro m = (Macro)macros.pop();//.get(i);
|
---|
77 | switch (m.type) {
|
---|
78 | case TYPE_DICT:
|
---|
79 | if (m.text==null || new_lang) {
|
---|
80 | Dictionary dict = new Dictionary(m.bundle, lang);
|
---|
81 | m.text = dict.get(m.key, null);
|
---|
82 | }
|
---|
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
|
---|
86 | text = StringUtils.replace(text, m.macro, m.text);
|
---|
87 | break;
|
---|
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
|
---|
92 | //if (text.matches("(?s).*"+m.macro+".*")) {
|
---|
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+".*")) {
|
---|
100 | if (m.resolve) {
|
---|
101 | new_text = this.resolve(m.text, lang, scope, doc_oid);
|
---|
102 | } else {
|
---|
103 | new_text = m.text;
|
---|
104 | }
|
---|
105 | text = StringUtils.replace(text, m.macro, new_text);
|
---|
106 | if (m.macro.endsWith("\\\\")){ // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
|
---|
107 |
|
---|
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 | }
|
---|
122 | }
|
---|
123 | break;
|
---|
124 | case TYPE_META:
|
---|
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+".*")) {
|
---|
129 | if (node_info == null) {
|
---|
130 | node_info = coll_db.getInfo(doc_oid);
|
---|
131 | if (node_info == null) {
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | new_text = node_info.getInfo(m.text);
|
---|
136 | if (new_text == null || new_text.equals("")) {
|
---|
137 | // try the root node
|
---|
138 | if (root_info == null && !OID.isTop(doc_oid)) {
|
---|
139 | root_info = coll_db.getInfo(OID.getTop(doc_oid));
|
---|
140 | }
|
---|
141 | if (root_info == null) break;
|
---|
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);
|
---|
147 | }
|
---|
148 | text = StringUtils.replace(text, m.macro, new_text);
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | break;
|
---|
154 | } // switch
|
---|
155 |
|
---|
156 | }
|
---|
157 | return text;
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|