01 /*
02 * Google.java
03 * Copyright (c) 1998-2008, The University of Sheffield.
04 *
05 * This code is from the GATE project (http://gate.ac.uk/) and is free
06 * software licenced under the GNU General Public License version 3. It is
07 * distributed without any warranty. For more details see COPYING.txt in the
08 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
09 *
10 * Hamish Cunningham, 9th September 2009
11 */
12
13 package gate.yam.plugins;
14
15 import java.util.*;
16 import java.io.*;
17 import gate.yam.YamPlugin;
18 import gate.yam.translate.AbstractTranslator;
19 import gate.util.Files;
20
21 /**
22 * Google plugin for YAM.
23 * @author Hamish Cunningham
24 */
25 public class Google implements YamPlugin {
26 /**
27 * A YAM plugin that allows site search forms (and maybe other stuff).
28 *
29 * @param m a map of arguments from the YAM source
30 * @param t the translator that is converting the YAM document
31 * @param target the target output language
32 * @return the translation for insertion at the plugin call location
33 *
34 * An example call:
35 * %google(siteip=gate.ac.uk, inurl=gate/doc, blurb=Search Google:)
36 */
37 public String translate(Map m, AbstractTranslator t, String target) {
38
39 // set up the resources file to use dependent on the o/p type
40 String templateName = "/gate/yam/plugins/google-template.";
41 if(target.equals("Html")) { // html output
42 templateName = templateName + "html";
43 } else if(target.equals("LaTeX")) { // latex output
44 templateName = templateName + "tex";
45 }
46
47 // do the munge
48 if(target.equals("Html") || target.equals("LaTeX")) {
49 String template = null;
50 String blurb = null;
51 try {
52 template = Files.getResourceAsString(templateName);
53 } catch(IOException e) {
54 return "YAM Google plugin resource file not found";
55 }
56 for(String key : (Set<String>) m.keySet()) {
57 String theKey = "__" + key + "__";
58 template = template.replaceAll(theKey, (String) m.get(key));
59 if(key.equals("blurb"))
60 blurb = (String) m.get(key);
61 }
62
63 // if no inurl was specified, delete that line
64 template = template.replaceAll(".*input.*__inurl__.*", "");
65
66 // if we got a blurb, replace that line
67 if(blurb != null)
68 template = template.replaceAll(".*Search .a href.*", blurb);
69
70 return template;
71 }
72
73 // catchall for other output types
74 return m.toString();
75 } // String translate(Map, AbstractTranslator, String)
76
77 } // Google
|