J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | /* The contents of this file are subject to the Sun Public License |
| 28 | * Version 1.0 (the "License"); you may not use this file except in |
| 29 | * compliance with the License. A copy of the License is available at |
| 30 | * http://www.sun.com/, and in the file LICENSE.html in the |
| 31 | * doc directory. |
| 32 | * |
| 33 | * The Original Code is HAT. The Initial Developer of the |
| 34 | * Original Code is Bill Foote, with contributions from others |
| 35 | * at JavaSoft/Sun. Portions created by Bill Foote and others |
| 36 | * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved. |
| 37 | * |
| 38 | * In addition to the formal license, I ask that you don't |
| 39 | * change the history or donations files without permission. |
| 40 | */ |
| 41 | |
| 42 | package com.sun.tools.hat.internal.util; |
| 43 | import java.util.*; |
| 44 | |
| 45 | /** |
| 46 | * Miscellaneous functions I couldn't think of a good place to put. |
| 47 | * |
| 48 | * @author Bill Foote |
| 49 | */ |
| 50 | |
| 51 | |
| 52 | public class Misc { |
| 53 | |
| 54 | private static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 55 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 56 | |
| 57 | public final static String toHex(int addr) { |
| 58 | char[] buf = new char[8]; |
| 59 | int i = 0; |
| 60 | for (int s = 28; s >= 0; s -= 4) { |
| 61 | buf[i++] = digits[(addr >> s) & 0xf]; |
| 62 | } |
| 63 | return "0x" + new String(buf); |
| 64 | } |
| 65 | |
| 66 | public final static String toHex(long addr) { |
| 67 | return "0x" + Long.toHexString(addr); |
| 68 | } |
| 69 | |
| 70 | public final static long parseHex(String value) { |
| 71 | long result = 0; |
| 72 | if (value.length() < 2 || value.charAt(0) != '0' || |
| 73 | value.charAt(1) != 'x') { |
| 74 | return -1L; |
| 75 | } |
| 76 | for(int i = 2; i < value.length(); i++) { |
| 77 | result *= 16; |
| 78 | char ch = value.charAt(i); |
| 79 | if (ch >= '0' && ch <= '9') { |
| 80 | result += (ch - '0'); |
| 81 | } else if (ch >= 'a' && ch <= 'f') { |
| 82 | result += (ch - 'a') + 10; |
| 83 | } else if (ch >= 'A' && ch <= 'F') { |
| 84 | result += (ch - 'A') + 10; |
| 85 | } else { |
| 86 | throw new NumberFormatException("" + ch |
| 87 | + " is not a valid hex digit"); |
| 88 | } |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | public static String encodeHtml(String str) { |
| 94 | final int len = str.length(); |
| 95 | StringBuffer buf = new StringBuffer(); |
| 96 | for (int i = 0; i < len; i++) { |
| 97 | char ch = str.charAt(i); |
| 98 | if (ch == '<') { |
| 99 | buf.append("<"); |
| 100 | } else if (ch == '>') { |
| 101 | buf.append(">"); |
| 102 | } else if (ch == '"') { |
| 103 | buf.append("""); |
| 104 | } else if (ch == '\'') { |
| 105 | buf.append("'"); |
| 106 | } else if (ch == '&') { |
| 107 | buf.append("&"); |
| 108 | } else if (ch < ' ') { |
| 109 | buf.append("&#" + Integer.toString(ch) + ";"); |
| 110 | } else { |
| 111 | int c = (ch & 0xFFFF); |
| 112 | if (c > 127) { |
| 113 | buf.append("&#" + Integer.toString(c) + ";"); |
| 114 | } else { |
| 115 | buf.append(ch); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return buf.toString(); |
| 120 | } |
| 121 | } |