Added boilerplate and a class comment and traded in tabs for four

spaces.  A unittest and an example would be great, but this is a
start.
This commit is contained in:
dchandler 2005-07-13 06:19:27 +00:00
parent 8ccd68789a
commit bef1d1b625

View file

@ -1,225 +1,255 @@
/*
The contents of this file are subject to the THDL Open Community License
Version 1.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License on the THDL web site
(http://www.thdl.org/).
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific terms governing rights and limitations under the
License.
The Initial Developer of this software is the Tibetan and Himalayan Digital
Library (THDL). Portions created by the THDL are Copyright 2001-2005 THDL.
All Rights Reserved.
Contributor(s): ______________________________________.
*/
package org.thdl.tib.text; package org.thdl.tib.text;
import java.util.StringTokenizer; import java.util.StringTokenizer;
/** A class that can create HTML that uses the Tibetan Machine Web
* font, which is really ten font files. This is intended to be used
* by Xalan XSLT to convert an XML document that uses Wylie into HTML
* that uses TMW. Or something like that -- I, David Chandler,
* didn't write this class, Edward Garrett did. But now that
* EWTS->TMW conversion is best done by org.thdl.tib.text.ttt, this
* class is deprecated.
* @author Edward Garrett, Tibetan and Himalayan Digital Library
* @deprecated
*/
public class TibetanHTML { public class TibetanHTML {
static String[] styleNames = static String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"}; {"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
public static String getStyles(String fontSize) { /** Returns CSS needed if you use other functions in this class.
return ".tmw {font: "+fontSize+"pt TibetanMachineWeb}\n"+ * @param fontSize the base-10 integral font size. */
".tmw1 {font: "+fontSize+"pt TibetanMachineWeb1}\n"+ public static String getStyles(String fontSize) {
".tmw2 {font: "+fontSize+"pt TibetanMachineWeb2}\n"+ return ".tmw {font: "+fontSize+"pt TibetanMachineWeb}\n"+
".tmw3 {font: "+fontSize+"pt TibetanMachineWeb3}\n"+ ".tmw1 {font: "+fontSize+"pt TibetanMachineWeb1}\n"+
".tmw4 {font: "+fontSize+"pt TibetanMachineWeb4}\n"+ ".tmw2 {font: "+fontSize+"pt TibetanMachineWeb2}\n"+
".tmw5 {font: "+fontSize+"pt TibetanMachineWeb5}\n"+ ".tmw3 {font: "+fontSize+"pt TibetanMachineWeb3}\n"+
".tmw6 {font: "+fontSize+"pt TibetanMachineWeb6}\n"+ ".tmw4 {font: "+fontSize+"pt TibetanMachineWeb4}\n"+
".tmw7 {font: "+fontSize+"pt TibetanMachineWeb7}\n"+ ".tmw5 {font: "+fontSize+"pt TibetanMachineWeb5}\n"+
".tmw8 {font: "+fontSize+"pt TibetanMachineWeb8}\n"+ ".tmw6 {font: "+fontSize+"pt TibetanMachineWeb6}\n"+
".tmw9 {font: "+fontSize+"pt TibetanMachineWeb9}\n"; ".tmw7 {font: "+fontSize+"pt TibetanMachineWeb7}\n"+
} ".tmw8 {font: "+fontSize+"pt TibetanMachineWeb8}\n"+
".tmw9 {font: "+fontSize+"pt TibetanMachineWeb9}\n";
}
public static String getHTMLX(String wylie) { public static String getHTMLX(String wylie) {
try { try {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) { for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken(); String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) { if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr/>"); buffer.append("<wbr/>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_"))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_"))); // hard-coded EWTS
buffer.append("<wbr/>"); buffer.append("<wbr/>");
} }
else else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next)));
} }
return buffer.toString(); return buffer.toString();
} catch (InvalidWylieException ive) { } catch (InvalidWylieException ive) {
return ""; return "";
} }
} }
public static String getHTMLX(DuffData[] duffData) { public static String getHTMLX(DuffData[] duffData) {
String[] styleNames = String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"}; {"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
StringBuffer htmlBuffer = new StringBuffer(); StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>"); htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) { for (int i=0; i<duffData.length; i++) {
char[] c = duffData[i].text.toCharArray(); char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) { for (int k=0; k<c.length; k++) {
htmlBuffer.append("<span class=\""); htmlBuffer.append("<span class=\"");
htmlBuffer.append(styleNames[duffData[i].font-1]); htmlBuffer.append(styleNames[duffData[i].font-1]);
htmlBuffer.append("\">"); htmlBuffer.append("\">");
if (c[k] > 32 && c[k] < 127) { //ie if it's not formatting if (c[k] > 32 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) { switch (c[k]) {
case '"': case '"':
htmlBuffer.append("&quot;"); htmlBuffer.append("&quot;");
break; break;
case '<': case '<':
htmlBuffer.append("&lt;"); htmlBuffer.append("&lt;");
break; break;
case '>': case '>':
htmlBuffer.append("&gt;"); htmlBuffer.append("&gt;");
break; break;
case '&': case '&':
htmlBuffer.append("&amp;"); htmlBuffer.append("&amp;");
break; break;
default: default:
htmlBuffer.append(c[k]); htmlBuffer.append(c[k]);
break; break;
} }
htmlBuffer.append("</span>"); htmlBuffer.append("</span>");
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot); String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot);
if (TibetanMachineWeb.isWyliePunc(wylie)) if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr/>"); htmlBuffer.append("<wbr/>");
} else { } else {
htmlBuffer.append("</span><br/>"); htmlBuffer.append("</span><br/>");
} }
} }
} }
htmlBuffer.append("</nobr>"); htmlBuffer.append("</nobr>");
return htmlBuffer.toString(); return htmlBuffer.toString();
} }
public static String getIndentedHTML(String wylie) { public static String getIndentedHTML(String wylie) {
return getHTML("_" + wylie); return getHTML("_" + wylie); // hard-coded EWTS
} }
public static String getHTML(String wylie) { public static String getHTML(String wylie) {
try { try {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) { for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken(); String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) { if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr/>"); buffer.append("<wbr/>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_"))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_"))); // hard-coded EWTS
buffer.append("<wbr/>"); buffer.append("<wbr/>");
} }
else else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next)));
} }
return buffer.toString(); return buffer.toString();
} catch (InvalidWylieException ive) { } catch (InvalidWylieException ive) {
return ""; return "";
} }
} }
public static String getHTML(DuffData[] duffData) { public static String getHTML(DuffData[] duffData) {
String[] styleNames = String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"}; {"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
StringBuffer htmlBuffer = new StringBuffer(); StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>"); htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) { for (int i=0; i<duffData.length; i++) {
htmlBuffer.append("<span class=\""); htmlBuffer.append("<span class=\"");
htmlBuffer.append(styleNames[duffData[i].font-1]); htmlBuffer.append(styleNames[duffData[i].font-1]);
htmlBuffer.append("\">"); htmlBuffer.append("\">");
char[] c = duffData[i].text.toCharArray(); char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) { for (int k=0; k<c.length; k++) {
if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) { switch (c[k]) {
case '"': case '"':
htmlBuffer.append("&quot;"); htmlBuffer.append("&quot;");
break; break;
case '<': case '<':
htmlBuffer.append("&lt;"); htmlBuffer.append("&lt;");
break; break;
case '>': case '>':
htmlBuffer.append("&gt;"); htmlBuffer.append("&gt;");
break; break;
case '&': case '&':
htmlBuffer.append("&amp;"); htmlBuffer.append("&amp;");
break; break;
default: default:
htmlBuffer.append(c[k]); htmlBuffer.append(c[k]);
break; break;
} }
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot); String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot);
if (TibetanMachineWeb.isWyliePunc(wylie)) if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr/>"); htmlBuffer.append("<wbr/>");
} else { } else {
htmlBuffer.append("<br/>"); htmlBuffer.append("<br/>");
} }
} }
htmlBuffer.append("</span>"); htmlBuffer.append("</span>");
} }
htmlBuffer.append("</nobr>"); htmlBuffer.append("</nobr>");
return htmlBuffer.toString(); return htmlBuffer.toString();
} }
public static String getHTMLforJava(String wylie) { public static String getHTMLforJava(String wylie) {
//differences: //differences:
// as of 1.4.1, anyway, browser built into java does not accept <wbr/> and <br/>, // as of 1.4.1, anyway, browser built into java does not accept <wbr/> and <br/>,
// only <wbr> and <br> // only <wbr> and <br>
try { try {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) { for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken(); String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) { if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr>"); buffer.append("<wbr>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_"))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS("_")));
buffer.append("<wbr>"); buffer.append("<wbr>");
} }
else else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next))); buffer.append(getHTML(TibTextUtils.getTibetanMachineWebForEWTS(next)));
} }
return buffer.toString(); return buffer.toString();
} catch (InvalidWylieException ive) { } catch (InvalidWylieException ive) {
return ""; return "";
} }
} }
public static String getHTMLforJava(DuffData[] duffData) { public static String getHTMLforJava(DuffData[] duffData) {
String[] fontNames = { String[] fontNames = {
"TibetanMachineWeb","TibetanMachineWeb1", "TibetanMachineWeb2", "TibetanMachineWeb","TibetanMachineWeb1", "TibetanMachineWeb2",
"TibetanMachineWeb3","TibetanMachineWeb4","TibetanMachineWeb5", "TibetanMachineWeb3","TibetanMachineWeb4","TibetanMachineWeb5",
"TibetanMachineWeb6","TibetanMachineWeb7","TibetanMachineWeb8", "TibetanMachineWeb6","TibetanMachineWeb7","TibetanMachineWeb8",
"TibetanMachineWeb9"}; "TibetanMachineWeb9"};
StringBuffer htmlBuffer = new StringBuffer(); StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>"); htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) { for (int i=0; i<duffData.length; i++) {
htmlBuffer.append("<font size=\"36\" face=\""); htmlBuffer.append("<font size=\"36\" face=\"");
htmlBuffer.append(fontNames[duffData[i].font-1]); htmlBuffer.append(fontNames[duffData[i].font-1]);
htmlBuffer.append("\">"); htmlBuffer.append("\">");
char[] c = duffData[i].text.toCharArray(); char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) { for (int k=0; k<c.length; k++) {
if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) { switch (c[k]) {
case '"': case '"':
htmlBuffer.append("&quot;"); htmlBuffer.append("&quot;");
break; break;
case '<': case '<':
htmlBuffer.append("&lt;"); htmlBuffer.append("&lt;");
break; break;
case '>': case '>':
htmlBuffer.append("&gt;"); htmlBuffer.append("&gt;");
break; break;
case '&': case '&':
htmlBuffer.append("&amp;"); htmlBuffer.append("&amp;");
break; break;
default: default:
htmlBuffer.append(c[k]); htmlBuffer.append(c[k]);
break; break;
} }
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot); String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k], TibTextUtils.weDoNotCareIfThereIsCorrespondingWylieOrNot);
if (TibetanMachineWeb.isWyliePunc(wylie)) if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr>"); htmlBuffer.append("<wbr>");
} else { } else {
htmlBuffer.append("<br>"); htmlBuffer.append("<br>");
} }
} }
htmlBuffer.append("</font>"); htmlBuffer.append("</font>");
} }
htmlBuffer.append("</nobr>"); htmlBuffer.append("</nobr>");
return htmlBuffer.toString(); return htmlBuffer.toString();
} }
} }