[jdom-interest] [jdom-announce] jsp and XMLOutputter
Alex Chaffee
guru at edamame.stinky.com
Thu Oct 26 11:28:57 PDT 2000
On Wed, Oct 25, 2000 at 05:26:58PM -0400, Sibon Barman wrote:
> Is there any standard stylesheet I can use to convert the XML to display in
> HTML format?
> Sibon.
Run it through a method like my htmlescape() (attached) to convert < to < etc.
- A
---
Alex Chaffee mailto:alex at jguru.com
jGuru - Java News and FAQs http://www.jguru.com/alex/
Creator of Gamelan http://www.gamelan.com/
Founder of Purple Technology http://www.purpletech.com/
Curator of Stinky Art Collective http://www.stinky.com/
-------------- next part --------------
/*
* ====================================================================
* Copyright (c) 1995-1999 Purple Technology, Inc. All rights
* reserved.
*
* PLAIN LANGUAGE LICENSE: Do whatever you like with this code, free
* of charge, just give credit where credit is due. If you improve it,
* please send your improvements to server at purpletech.com. Check
* http://www.purpletech.com/server/ for the latest version and news.
*
* LEGAL LANGUAGE LICENSE: Redistribution and use in source and binary
* forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. The names of the authors and the names "Purple Technology,"
* "Purple Server" and "Purple Chat" must not be used to endorse or
* promote products derived from this software without prior written
* permission. For written permission, please contact
* server at purpletech.com.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND PURPLE TECHNOLOGY ``AS
* IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHORS OR PURPLE TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
**/
package com.purpletech.util;
import java.io.*;
import java.util.*;
public class Utils {
/**
* fills the left side of a number with zeros <br>
* e.g. zerofill(14, 3) -> "014" <br>
* e.g. zerofill(187, 6) -> "000014"
* Note: doesn't work with negative numbers
**/
public static String zerofill(int x, int d) {
String s = "";
switch (d) {
case 7:
if (x<1000000) s += "0";
case 6:
if (x<100000) s += "0";
case 5:
if (x<10000) s += "0";
case 4:
if (x<1000) s += "0";
case 3:
if (x<100) s += "0";
case 2:
if (x<10) s += "0";
}
return s+x;
}
public static void printIndent(PrintWriter out, int indent) {
out.print(indent(indent));
}
public static String indent(int indent) {
switch (indent) {
case 8:
return(" ");
case 7:
return(" ");
case 6:
return(" ");
case 5:
return(" ");
case 4:
return(" ");
case 3:
return(" ");
case 2:
return(" ");
case 1:
return(" ");
default:
StringBuffer buf = new StringBuffer();
for (int i=0; i<indent; ++i) { buf.append(" "); }
return buf.toString();
}
}
public static boolean isWhitespace(String s) {
for (int i=0; i<s.length(); ++i) {
if (!Character.isWhitespace(s.charAt(i))) return false;
}
return true;
}
public static class ExecInfo {
public int exit;
public String stdout;
public String stderr;
}
/**
* Wrapper for Runtime.exec. Takes input as a String. Times out
* after sleep msec. Returns an object containing exit value,
* standard output, and error output.
* @param sleep msec to wait before terminating process
**/
public static ExecInfo exec(String command, String input, long sleep) throws IOException {
Process process = null;
ExecInfo info = new ExecInfo();
try {
Alarm a = null;
if (sleep>0) {
a = new Alarm(Thread.currentThread(), sleep);
a.start();
}
process = Runtime.getRuntime().exec(command);
if (input != null) {
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.print(input);
pw.close();
}
info.stdout = IOUtils.readStream(process.getInputStream());
info.stderr = IOUtils.readStream(process.getErrorStream());
process.waitFor();
if (a!=null) a.stop = true;
}
catch (InterruptedIOException iioe) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
catch (InterruptedException ie) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
finally {
if (process != null)
process.destroy();
}
info.exit = process.exitValue();
return info;
}
/**
* Turn "Now is the time for all good men" into "Now is the time for..."
* @param max maximum length of result string
**/
public static String abbreviate(String s, int max) {
if (s.length() < max) return s;
// todo: break into words
return s.substring(0, max-3) + "...";
}
/**
* pad or truncate
**/
public static String pad(String s, int length) {
if (s.length() < length) return s + indent(length - s.length());
else return s.substring(0,length);
}
/**
* returns the part of the second string from where it's different
* from the first <p>
* strdiff("i am a machine", "i am a robot") -> "robot"
*
**/
public static String strdiff(String s1, String s2) {
int i;
for (i=0; i<s1.length() && i<s2.length(); ++i) {
if (s1.charAt(i) != s2.charAt(i)) {
break;
}
}
if (i<s2.length())
return s2.substring(i);
return "";
}
/**
* count the number of occurences of ch inside s
**/
public static int count(String s, char ch) {
int c=0;
for (int i=0; i<s.length(); ++i) {
if (s.charAt(i) == ch) c++;
}
return c;
}
/**
* Replace all occurences of target inside source with replacement.
* E.g. replace("fee fie fo fum", "f", "gr") -> "gree grie gro grum"
**/
public static String replace(String source, String target, String replacement)
{
// could use a regular expression, but this keeps it portable
StringBuffer result = new StringBuffer(source.length());
int i = 0, j = 0;
int len = source.length();
while (i < len) {
j = source.indexOf(target, i);
if (j == -1) {
result.append( source.substring(i,len) );
break;
}
else {
result.append( source.substring(i,j) );
result.append( replacement );
i = j + target.length();
}
}
return result.toString();
}
// see http://hotwired.lycos.com/webmonkey/reference/special_characters/
static Object[][] entities = {
{"#39", new Integer(39)}, // ' - apostrophe
{"quot", new Integer(34)}, // " - double-quote
{"amp", new Integer(38)}, // & - ampersand
{"lt", new Integer(60)}, // < - less-than
{"gt", new Integer(62)}, // > - greater-than
{"nbsp", new Integer(160)}, // non-breaking space
{"copy", new Integer(169)}, // ? - copyright
{"reg", new Integer(174)}, // ? - registered trademark
{"Agrave", new Integer(192)}, // ? - uppercase A, grave accent
{"Aacute", new Integer(193)}, // ? - uppercase A, acute accent
{"Acirc", new Integer(194)}, // ? - uppercase A, circumflex accent
{"Atilde", new Integer(195)}, // ? - uppercase A, tilde
{"Auml", new Integer(196)}, // ? - uppercase A, umlaut
{"Aring", new Integer(197)}, // ? - uppercase A, ring
{"AElig", new Integer(198)}, // ? - uppercase AE
{"Ccedil", new Integer(199)}, // ? - uppercase C, cedilla
{"Egrave", new Integer(200)}, // ? - uppercase E, grave accent
{"Eacute", new Integer(201)}, // ? - uppercase E, acute accent
{"Ecirc", new Integer(202)}, // ? - uppercase E, circumflex accent
{"Euml", new Integer(203)}, // ? - uppercase E, umlaut
{"Igrave", new Integer(204)}, // ? - uppercase I, grave accent
{"Iacute", new Integer(205)}, // ? - uppercase I, acute accent
{"Icirc", new Integer(206)}, // ? - uppercase I, circumflex accent
{"Iuml", new Integer(207)}, // ? - uppercase I, umlaut
{"ETH", new Integer(208)}, // ? - uppercase Eth, Icelandic
{"Ntilde", new Integer(209)}, // ? - uppercase N, tilde
{"Ograve", new Integer(210)}, // ? - uppercase O, grave accent
{"Oacute", new Integer(211)}, // ? - uppercase O, acute accent
{"Ocirc", new Integer(212)}, // ? - uppercase O, circumflex accent
{"Otilde", new Integer(213)}, // ? - uppercase O, tilde
{"Ouml", new Integer(214)}, // ? - uppercase O, umlaut
{"Oslash", new Integer(216)}, // ? - uppercase O, slash
{"Ugrave", new Integer(217)}, // ? - uppercase U, grave accent
{"Uacute", new Integer(218)}, // ? - uppercase U, acute accent
{"Ucirc", new Integer(219)}, // ? - uppercase U, circumflex accent
{"Uuml", new Integer(220)}, // ? - uppercase U, umlaut
{"Yacute", new Integer(221)}, // ? - uppercase Y, acute accent
{"THORN", new Integer(222)}, // ? - uppercase THORN, Icelandic
{"szlig", new Integer(223)}, // ? - lowercase sharps, German
{"agrave", new Integer(224)}, // ? - lowercase a, grave accent
{"aacute", new Integer(225)}, // ? - lowercase a, acute accent
{"acirc", new Integer(226)}, // ? - lowercase a, circumflex accent
{"atilde", new Integer(227)}, // ? - lowercase a, tilde
{"auml", new Integer(228)}, // ? - lowercase a, umlaut
{"aring", new Integer(229)}, // ? - lowercase a, ring
{"aelig", new Integer(230)}, // ? - lowercase ae
{"ccedil", new Integer(231)}, // ? - lowercase c, cedilla
{"egrave", new Integer(232)}, // ? - lowercase e, grave accent
{"eacute", new Integer(233)}, // ? - lowercase e, acute accent
{"ecirc", new Integer(234)}, // ? - lowercase e, circumflex accent
{"euml", new Integer(235)}, // ? - lowercase e, umlaut
{"igrave", new Integer(236)}, // ? - lowercase i, grave accent
{"iacute", new Integer(237)}, // ? - lowercase i, acute accent
{"icirc", new Integer(238)}, // ? - lowercase i, circumflex accent
{"iuml", new Integer(239)}, // ? - lowercase i, umlaut
{"eth", new Integer(240)}, // ? - lowercase eth, Icelandic
{"ntilde", new Integer(241)}, // ? - lowercase n, tilde
{"ograve", new Integer(242)}, // ? - lowercase o, grave accent
{"oacute", new Integer(243)}, // ? - lowercase o, acute accent
{"ocirc", new Integer(244)}, // ? - lowercase o, circumflex accent
{"otilde", new Integer(245)}, // ? - lowercase o, tilde
{"ouml", new Integer(246)}, // ? - lowercase o, umlaut
{"oslash", new Integer(248)}, // ? - lowercase o, slash
{"ugrave", new Integer(249)}, // ? - lowercase u, grave accent
{"uacute", new Integer(250)}, // ? - lowercase u, acute accent
{"ucirc", new Integer(251)}, // ? - lowercase u, circumflex accent
{"uuml", new Integer(252)}, // ? - lowercase u, umlaut
{"yacute", new Integer(253)}, // ? - lowercase y, acute accent
{"thorn", new Integer(254)}, // ? - lowercase thorn, Icelandic
{"yuml", new Integer(255)}, // ? - lowercase y, umlaut
{"euro", new Integer(8364)}, // Euro symbol
};
static Map e2i = new HashMap();
static Map i2e = new HashMap();
static {
for (int i=0; i<entities.length; ++i) {
e2i.put(entities[i][0], entities[i][1]);
i2e.put(entities[i][1], entities[i][0]);
}
}
/**
* Turns funky characters into HTML entity equivalents<p>
* e.g. <tt>"bread" & "butter"</tt> => <tt>&quot;bread&quot; &amp; &quot;butter&quot;</tt>.
* Update: supports nearly all HTML entities, including funky accents. See the source code for more detail.
**/
public static String htmlescape(String s1)
{
StringBuffer buf = new StringBuffer();
int i;
for (i=0; i<s1.length(); ++i) {
char ch = s1.charAt(i);
String entity = (String)i2e.get( new Integer((int)ch) );
if (entity == null) {
if (((int)ch) > 128) {
buf.append("&#" + ((int)ch) + ";");
}
else {
buf.append(ch);
}
}
else {
buf.append("&" + entity + ";");
}
}
return buf.toString();
}
/**
* Reverses htmlescape.
* @see htmlescape
**/
public static String htmlunescape(String s1) {
StringBuffer buf = new StringBuffer();
int i;
for (i=0; i<s1.length(); ++i) {
char ch = s1.charAt(i);
if (ch == '&') {
int semi = s1.indexOf(';', i+i);
if (semi == -1) {
buf.append(ch);
continue;
}
String entity = s1.substring(i+1, semi-1);
Integer iso;
if (entity.charAt(0) == '#') {
iso = new Integer(entity.substring(1));
}
else {
iso = (Integer)e2i.get(entity);
}
if (iso == null) {
buf.append("&" + entity + ";");
}
else {
buf.append((char)(iso.intValue()));
}
}
else {
buf.append(ch);
}
}
return buf.toString();
}
/**
* capitalize the first character of s
**/
public static String capitalize(String s) {
return s.substring(0,1).toUpperCase() + s.substring(1);
}
/**
* turn String s into a plural noun (doing the right thing with
* "story" -> "stories" and "mess" -> "messes")
**/
public static String pluralize(String s) {
if (s.endsWith("y"))
return s.substring(0, s.length()-1) + "ies";
else if (s.endsWith("s"))
return s + "es";
else
return s + "s";
}
public static String getStackTrace(Throwable t) {
StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
t.printStackTrace(p);
p.close();
return s.toString();
}
public static void sleep(long msec) {
try {
Thread.sleep(msec);
}
catch (InterruptedException ie) {}
}
} // class Utils
More information about the jdom-interest
mailing list