/* net_msgs.java Copyright 1998 Kyle R. Burton This is free software; you can redistribute it and/or modify it under the tearms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this software; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ /** * * file: net_msgs.java * * description: utility class for getting message text from a URL. One of the * purposes of this is to remove language specific elements from Java applications. * * revision list: * Thu Apr 30 09:23:32 EDT 1998 KRB - created file. * */ import java.util.*; import java.net.*; import java.io.*; public class net_msgs extends Hashtable { public boolean show_diagnostics = false; String msg_url = null; public net_msgs() { super(); } public net_msgs( String msg_url ) throws MalformedURLException, IOException { super(); init_from_url(msg_url); } public boolean init_from_url( String msg_url ) throws MalformedURLException, IOException { this.msg_url = new String(msg_url); return init_from_url(new URL(msg_url)); } public String get_msg_url() { return new String(msg_url); } public Object get( Object key ) { return containsKey(key) ? super.get(key) : key; } public String get( String key ) { return containsKey(key) ? (String) super.get(key) : key; } public boolean init_from_url( URL msg_url ) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(msg_url.openStream()) ); return init(br); } public boolean init( BufferedReader br ) throws IOException { StreamTokenizer stok = new StreamTokenizer(br); stok.commentChar('#'); stok.eolIsSignificant(true); stok.quoteChar('"'); stok.wordChars('_','_'); int type; boolean more_tokens = true, got_key = false; String key = new String(); String value = new String(); while( more_tokens ) { type = stok.nextToken(); if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) parsed token: " + stok.toString() ); } switch( type ) { case StreamTokenizer.TT_EOF: if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) type is TT_EOF." ); } more_tokens = false; break; case StreamTokenizer.TT_EOL: if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) type is TT_EOL." ); } got_key = false; break; case StreamTokenizer.TT_NUMBER: case StreamTokenizer.TT_WORD: value = stok.sval; if( StreamTokenizer.TT_NUMBER == stok.ttype ) { if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) type is TT_NUMBER." ); } value = java.lang.Double.toString(stok.nval); } else { if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) type is TT_WORD." ); } } if( null == value ) { break; } if( got_key ) { got_key = false; if( show_diagnostics ) { System.out.println("net_msgs::init() setting: " + key + " = " + value ); } if( null != key && null != value ) { put(key,value); } } else { key = value; got_key = true; } break; default: // unknown type...ther's probably a quoted string in sval if( show_diagnostics ) { System.out.println("net_msgs::init_from_url(URL) type is indeterminate." ); } if( got_key ) { got_key = false; if( show_diagnostics ) { System.out.println("net_msgs::init() setting: " + key + " = " + stok.sval ); } if( null != key && null != stok.sval ) { put(key,stok.sval); } } else { key = stok.sval; got_key = true; } break; } } return true; } }