home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
HTMLUtils.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
8KB
|
289 lines
/*
* @(#)HTMLUtils.java 1.3 97/10/01
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.text.html;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
import com.sun.java.swing.*;
/*
* A class of general purpose HTML utilities
*
* @author Jill Nakata
* @version 1.3 10/01/97
*/
class HTMLUtils implements HTMLDefs {
/**
* Converts a type Color to a hex string
* in the format "#RRGGBB"
*/
static public String colorToHex(Color color) throws HTMLException {
String colorstr = new String("#");
// Red
String str = Integer.toHexString(color.getRed());
if (str.length() > 2)
throw new HTMLException(HTMLError.COLOR_CONVERSION,
"invalid red value");
else if (str.length() < 2)
colorstr += "0" + str;
else
colorstr += str;
// Green
str = Integer.toHexString(color.getGreen());
if (str.length() > 2)
throw new HTMLException(HTMLError.COLOR_CONVERSION,
"invalid green value");
else if (str.length() < 2)
colorstr += "0" + str;
else
colorstr += str;
// Blue
str = Integer.toHexString(color.getBlue());
if (str.length() > 2)
throw new HTMLException(HTMLError.COLOR_CONVERSION,
"invalid green value");
else if (str.length() < 2)
colorstr += "0" + str;
else
colorstr += str;
return colorstr;
}
/**
* Determine if an end </TAG> is needed
* for the given tag.
*/
static public boolean endTagNeeded(String tag) {
// These should not have an end </TAGNAME>.
if (tag.equals(HTMLDefs.BASE) ||
tag.equals(HTMLDefs.ISINDEX) ||
tag.equals(HTMLDefs.LINK) ||
tag.equals(HTMLDefs.META))
return false;
// These require </TAGNAME>.
else if (tag.equals(HTMLDefs.BODY) ||
tag.equals(HTMLDefs.HEAD) ||
tag.equals(HTMLDefs.SCRIPT) ||
tag.equals(HTMLDefs.STYLE) ||
tag.equals(HTMLDefs.TITLE) ||
tag.equals(HTMLDefs.P) ||
tag.equals(HTMLDefs.PRE) ||
tag.equals(HTMLDefs.CODE) ||
tag.equals(HTMLDefs.CENTER) ||
tag.equals(HTMLDefs.DIV) ||
tag.equals(HTMLDefs.BLOCKQUOTE) ||
tag.equals(HTMLDefs.KBD) ||
tag.equals(HTMLDefs.B) ||
tag.equals(HTMLDefs.I) ||
tag.equals(HTMLDefs.U) ||
tag.equals(HTMLDefs.CITE) ||
tag.equals(HTMLDefs.BIG) ||
tag.equals(HTMLDefs.SMALL) ||
tag.equals(HTMLDefs.DFN) ||
tag.equals(HTMLDefs.EM) ||
tag.equals(HTMLDefs.SAMP) ||
tag.equals(HTMLDefs.STRIKE) ||
tag.equals(HTMLDefs.STRONG) ||
tag.equals(HTMLDefs.SUB) ||
tag.equals(HTMLDefs.SUP) ||
tag.equals(HTMLDefs.TT) ||
tag.equals(HTMLDefs.VAR))
return true;
else
return true;
}
/**
* Convert a "#FFFFFF" hex string to a Color
*/
public static final Color hexToColor(String value) throws HTMLException
{
if (value.length() != 7) {
throw new HTMLException(HTMLError.COLOR_CONVERSION,
"invalid hex color string length");
}
else if (value.startsWith("#")) {
String str = "0x" + value.substring(1, value.length());
Color c = Color.decode(value);
return c;
}
return null;
}
/**
* Convert a 2 position hex number "NN" string into an integer
*/
static final int stringToHex(String str) throws HTMLException
{
if (str.length() != 2)
throw new HTMLException(HTMLError.HEX_CONVERSION,
"invalid hex string" + str);
int pos1 = Character.digit(str.charAt(0), 16) * 16;
int pos0 = Character.digit(str.charAt(1), 16);
return(pos0 + pos1);
}
/**
* Convert a color string "RED" or "#NNNNNN" to a Color.
* Note: This will only convert the HTML3.2 colors strings
* or string of length 7
* otherwise, it will throw an HTMLException.
*/
static public final Color stringToColor(String str)
throws HTMLException {
Color color;
if (str.charAt(0) == '#')
color = hexToColor(str);
else if (str.equalsIgnoreCase("Black"))
color = hexToColor("#000000");
else if(str.equalsIgnoreCase("Silver"))
color = hexToColor("#C0C0C0");
else if(str.equalsIgnoreCase("Gray"))
color = hexToColor("#808080");
else if(str.equalsIgnoreCase("White"))
color = hexToColor("#FFFFFF");
else if(str.equalsIgnoreCase("Maroon"))
color = hexToColor("#800000");
else if(str.equalsIgnoreCase("Red"))
color = hexToColor("#FF0000");
else if(str.equalsIgnoreCase("Purple"))
color = hexToColor("#800080");
else if(str.equalsIgnoreCase("Fuchsia"))
color = hexToColor("#FF00FF");
else if(str.equalsIgnoreCase("Green"))
color = hexToColor("#008000");
else if(str.equalsIgnoreCase("Lime"))
color = hexToColor("#00FF00");
else if(str.equalsIgnoreCase("Olive"))
color = hexToColor("#808000");
else if(str.equalsIgnoreCase("Yellow"))
color = hexToColor("#FFFF00");
else if(str.equalsIgnoreCase("Navy"))
color = hexToColor("#000080");
else if(str.equalsIgnoreCase("Blue"))
color = hexToColor("#0000FF");
else if(str.equalsIgnoreCase("Teal"))
color = hexToColor("#008080");
else if(str.equalsIgnoreCase("Aqua"))
color = hexToColor("#00FFFF");
else
throw new HTMLException(HTMLError.COLOR_CONVERSION,
"invalid HTML color string: " + str);
return color;
}
/**
* Remove begin and end quotes and return string:
* "xxxxx" returns xxxxx.
*/
static public String removeSurroundingQuotes(String s) {
String result = new String(s);
if (s.startsWith("\"") && s.endsWith("\"")) {
result = new String(s.substring(1, s.length()-1));
}
return result;
}
/**
* Return the directory where images reside.
*/
/*
static public String imageDirectory() {
String dir;
// "./images/htmleditor/"
if (HTMLResource.jdtBase == null)
dir = "." + File.separator + HTMLDefs.IMAGES + File.separator +
HTMLDefs.HTMLEDITOR_BASE + File.separator;
// Check if ended with "/" or File.separator.
// "/opt/SUNWjdt/.../images/htmleditor/
else if (HTMLResource.jdtBase.endsWith(File.separator))
dir = HTMLResource.jdtBase + HTMLDefs.IMAGES + File.separator +
HTMLDefs.HTMLEDITOR_BASE + File.separator;
// "/opt/SUNWjdt/.../images/htmleditor/
else
dir = HTMLResource.jdtBase + File.separator + HTMLDefs.IMAGES +
File.separator + HTMLDefs.HTMLEDITOR_BASE + File.separator;
return dir;
}
*/
public static ImageIcon loadImage(Component c, String imageName){
Toolkit toolkit = c.getToolkit();
if(toolkit == null)
return null;
URL url = null;
try {
url = new URL(imageName);
InputStream is = (url.openConnection()).getInputStream();
is.close();
}catch (MalformedURLException e){
File f = new File(imageName);
if(!f.canRead())
return null;
}catch (IOException io){
return null;
}
Image image;
if(url != null)
image = toolkit.getImage(url);
else
image = toolkit.getImage(imageName);
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
System.out.println(e);
return null;
}
ImageIcon ig = new ImageIcon(image);
if (ig.getIconWidth() == -1 || ig.getIconHeight() == -1)
return null;
else
return ig;
}
}