001 /*
002 * HtmlTranslator.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham 17th April 2006
011 */
012
013 package gate.yam.translate;
014
015 import java.util.*;
016 import java.io.IOException;
017 import java.net.URI;
018 import java.net.URISyntaxException;
019 import org.springframework.web.util.HtmlUtils;
020
021 import gate.util.GateException;
022 import gate.util.GateRuntimeException;
023 import gate.yam.parse.*;
024 import gate.util.Files;
025
026 /**
027 * This class provides translation of YAM into XHTML.
028 * @author Hamish Cunningham
029 */
030 public class HtmlTranslator extends AbstractTranslator implements HtmlConstants
031 {
032 /** Construction. */
033 public HtmlTranslator() {
034 controlsMap = new HashMap();
035 constantsMap = new HashMap();
036 predicatesMap = new HashMap();
037 constantsTable = getConstantsTable();
038 predicatesTable = getPredicatesTable();
039 translatorType = this.getClass().getSimpleName().replace("Translator", "");
040
041 // populate the constants map
042 for(int i = 0; i < constantsTable.length; i++) {
043 String[] outputStrings = new String[3];
044 outputStrings[0] = constantsTable[i][CONSTANTNAME];
045 outputStrings[1] = constantsTable[i][CONSTANTSTART];
046 outputStrings[2] = constantsTable[i][CONSTANTEND];
047 constantsMap.put(constantsTable[i][CONSTANTNAME], outputStrings);
048 }
049
050 // populate the predicates map
051 for(int i = 0; i < predicatesTable.length; i++) {
052 String predicateName = (String) predicatesTable[i][0];
053 String[] attributeStrings = (String[]) predicatesTable[i][1];
054 predicatesMap.put(predicateName, attributeStrings);
055 }
056 } // HtmlTranslator()
057
058 /** Get the path to the preamble resource. */
059 public String getPreamblePath() {
060 return "/gate/yam/translate/preamble.xhtml";
061 }
062
063 /** Array mapping node type name to start/end strings. */
064 public String[][] getConstantsTable() { return htmlConstantsTable; }
065
066 /** Array mapping predicate type name to attributes. */
067 public Object[][] getPredicatesTable() { return htmlPredicatesTable; }
068
069 /** Process URLs and Anchors. */
070 public void processURLs(SimpleNode node) throws GateException {
071 // start is the url; body is the text
072 String markupStart = getConstantStart(node.getId());
073 String nodeStart = node.getStart();
074
075 // escape needs to be only done in HtmlTranslator
076 // escape any markup in the URL body
077 String nodeBody = HtmlUtils.htmlEscape(node.getBody());
078 if(nodeStart != null) {
079 String link =
080 markupStart.replace("_X_", nodeStart).replace("_Y_", "cow-url");
081
082 // if the link is a URL (not anchor) and points at a local file
083 if(node.getId() == YamParserTreeConstants.JJTURL) {
084 try {
085 String urlText = nodeStart;
086 URI uri = new URI(urlText);
087 //log.debug("path = " + uri.getPath());
088
089 if(
090 ! urlText.startsWith("#") && // ref to an anchor
091 ! uri.isAbsolute() && // external reference
092 ! urlText.matches("/g8/.*") && // ref to controllers
093 ! urlText.matches("/userguide(?:/.*)?") && // ref to ug
094 ! urlText.matches("/download(?:/.*)?") && // ref to download area
095 ! urlText.matches("/help(?:/.*)?") && // help area
096 ! ioHandler.existsInContext(uri.getPath()) // local non-existent
097 ) { // point at the create uri
098 urlText = ioHandler.getCreatePageUrl();
099 link = markupStart.replace("_X_", urlText + nodeStart + "?m=1")
100 .replace("_Y_", "cow-non-existant-url");
101 }
102 } catch(URISyntaxException e) {
103 // throw new GateException(e);
104 }
105 }
106
107 //log.debug(link);
108 pr(link);
109 }
110 if(nodeBody != null) {
111 pr(nodeBody);
112 }
113 pr(getConstantEnd(node.getId()));
114 } // processURLs(SimpleNode)
115
116 /** Construct the preamble/header for the document. */
117 @Override
118 String makePreamble(String title) {
119 String header = null;
120 try {
121 header = Files.getResourceAsString(getPreamblePath());
122 } catch(IOException e) {
123 // should never happen
124 throw new GateRuntimeException(
125 "couldn't find resource file " + getPreamblePath() + ": " + e
126 );
127 }
128
129 return header.replace("___TITLE___", title.replaceAll("<[^>]*>",""));
130 } // makePreamble(String)
131
132 } // HtmlTranslator
|