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

Last change on this file since 25635 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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