source: greenstone3/branches/customizingGreenstone3/src/java/org/greenstone/gsdl3/util/GS2MacroResolver.java@ 15787

Last change on this file since 15787 was 15787, checked in by oranfry, 16 years ago

updating from trunk: brought in trunk changes from r15191 to r15785

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