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

Last change on this file since 14532 was 14532, checked in by qq6, 17 years ago

updated replacing macros

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