01 /*
02 * LaTeXTranslator.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 21st August 2006
11 */
12
13 package gate.yam.translate;
14
15 import java.util.*;
16 import java.io.*;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19
20 import gate.util.*;
21 import gate.yam.parse.*;
22
23 /**
24 * This class provides translation of YAM into LaTeX.
25 * @author Hamish Cunningham
26 */
27 public class LaTeXTranslator
28 extends AbstractTranslator implements LaTeXConstants
29 {
30 /** Construction. */
31 public LaTeXTranslator() {
32 controlsMap = new HashMap();
33 constantsMap = new HashMap();
34 predicatesMap = new HashMap();
35 constantsTable = getConstantsTable();
36 predicatesTable = getPredicatesTable();
37 translatorType = this.getClass().getSimpleName().replace("Translator", "");
38
39 // populate the constants map
40 for(int i = 0; i < constantsTable.length; i++) {
41 String[] outputStrings = new String[3];
42 outputStrings[0] = constantsTable[i][CONSTANTNAME];
43 outputStrings[1] = constantsTable[i][CONSTANTSTART];
44 outputStrings[2] = constantsTable[i][CONSTANTEND];
45 constantsMap.put(constantsTable[i][CONSTANTNAME], outputStrings);
46 }
47
48 // populate the predicates map
49 for(int i = 0; i < predicatesTable.length; i++) {
50 String predicateName = (String) predicatesTable[i][0];
51 String[] attributeStrings = (String[]) predicatesTable[i][1];
52 predicatesMap.put(predicateName, attributeStrings);
53 }
54 } // LaTeXTranslator()
55
56 /** Get the path to the preamble resource. */
57 public String getPreamblePath() { return "/gate/yam/translate/preamble.tex"; }
58
59 /** Array mapping node type name to start/end strings. */
60 public String[][] getConstantsTable() { return latexConstantsTable; }
61
62 /** Array mapping predicate type name to attributes. */
63 public Object[][] getPredicatesTable() { return latexPredicatesTable; }
64
65 /** Process URLs and Anchors. */
66 public void processURLs(SimpleNode node) throws GateException {
67 // input URI and HREF elements and output pattern
68 String latexHrefPattern = getConstantStart("linkPattern");
69 String latexAnchorPattern = getConstantStart("Anchor");
70 String linkTargetUrl = node.getStart();
71 String linkText = node.getBody();
72
73 log.debug(
74 //System.out.println(
75 "linkTargetUrl=" + linkTargetUrl + "; linkText=" + linkText +
76 "; latexHrefPattern=" + latexHrefPattern +
77 "; latexAnchorPattern=" + latexAnchorPattern
78 );
79
80 // do a regsub on the pattern and print
81 String link = "";
82 if(node instanceof ASTAnchor) {
83 link = latexAnchorPattern.replace("_X_", linkTargetUrl);
84 } else if(linkTargetUrl != null) { // URL, but no point linking if no target
85 if(linkText == null) linkText = "";
86 linkText = linkText.replace("#", "\\#"); // escape # in link text
87 link = latexHrefPattern.replace(
88 "_X_", linkTargetUrl
89 ).replace("_Y_", linkText);
90 }
91 log.debug("link=" + link);
92 pr(link);
93 } // processURLs(SimpleNode)
94
95 } // LaTeXTranslator
|