1: /* 2: * assoc3.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.io.*; 16: import java.util.*; 17: 18: class assoc3 { 19: 20: Hashtable h = new Hashtable(); 21: String name = null ; 22: String subject = null ; 23: int points = 0 ; 24: 25: assoc3(String filename){ 26: int ret; 27: try { 28: FileInputStream in = new FileInputStream(filename); 29: StreamTokenizer st = new StreamTokenizer(in); 30: while( ( ret=st.nextToken() ) != st.TT_EOF ){ 31: name = st.sval ; 32: ret=st.nextToken(); 33: subject = st.sval ; 34: ret=st.nextToken(); 35: points = (int)st.nval ; 36: System.out.println(name + ": " + subject + ": " + points ); 37: register(name,subject,points); 38: } 39: } catch ( Exception e){ 40: System.out.println(e); 41: } 42: } 43: 44: void register(String name ,String sbj, int p ){ 45: Vector v1 = null; 46: Vector v2 = null; 47: 48: if ( ( v1 = (Vector)h.get(name)) == null ){ 49: v1 = new Vector(); 50: } 51: v1.addElement(new Integer(p)); 52: h.put(name, v1) ; 53: 54: if ( ( v2 = (Vector)h.get(sbj)) == null ){ 55: v2 = new Vector(); 56: } 57: v2.addElement(new Integer(p)); 58: h.put(sbj, v2) ; 59: } 60: 61: void show(){ 62: String key=null; 63: Vector v = null ; 64: System.out.println( "--------------------- " ); 65: for( Enumeration e = h.keys(); e.hasMoreElements(); ){ 66: key=(String)e.nextElement(); 67: v = (Vector)h.get(key) ; 68: System.out.println( key + ": " + v.toString() + " --> " + avg(v) ); 69: } 70: } 71: 72: float avg(Vector v){ 73: int n = v.size() ; 74: int sum = 0 ; 75: for( int i = 0 ; i < n ; i++){ 76: sum += ((Integer)v.elementAt(i)).intValue(); 77: } 78: return ( float )( sum/n ); 79: } 80: 81: public static void main(String argv[]){ 82: assoc3 as = new assoc3( argv[0] ); 83: as.show(); 84: } 85: }
1: /* 2: * robo.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.io.*; 16: import java.net.*; 17: import java.util.*; 18: 19: class Robo { 20: static final int TAG_A = 1 ; 21: static final int TAG_IMG = 2 ; 22: 23: URL src_url = null ; 24: BufferedInputStream in = null ; 25: ByteArrayOutputStream out = null ; 26: 27: String buf; 28: String tag; 29: String text; 30: String url; 31: String base; 32: String file; 33: String refbase; 34: 35: static Hashtable hash = new Hashtable() ; 36: static Vector pictures = new Vector() ; 37: static Stack stack = new Stack(); 38: Vector tmpToReverse = new Vector() ; 39: 40: void setUrl(String address){ 41: try { 42: src_url = new URL(address); 43: base = src_url.getProtocol() +"://"+src_url.getHost(); 44: file = src_url.getFile(); 45: url=base+file ; 46: hash.put(url,"x"); 47: // System.out.println("-----------------------------------"); 48: // System.out.println(url); 49: int n = file.lastIndexOf('/'); 50: if ( n== -1 ) { 51: refbase = base ; 52: } else { 53: file = file.substring(0,n); 54: refbase = base + file + "/" ; 55: } 56: } catch (Exception e ){ 57: System.err.println("setHost :" + e); 58: } 59: } 60: 61: 62: void readPage(String address){ 63: try { 64: setUrl(address) ; 65: System.out.println(url); 66: 67: HtmlParser hp = new HtmlParser(address); 68: int ret ; 69: 70: while( (ret = hp.nextToken()) != hp.EOF ){ 71: if ( ret == hp.TEXT ) continue ; 72: tag = hp.tag ; 73: if ( tag.startsWith("A ") || tag.startsWith("a ") ) { 74: searchKey("href"); 75: continue ; 76: } 77: if ( tag.startsWith("IMG ") || tag.startsWith("img ") ) { 78: searchKey("src"); 79: continue ; 80: } 81: } 82: } catch (Exception e ){ 83: System.err.println("readPage :" + e); 84: } 85: } 86: 87: void searchKey(String key){ 88: StringTokenizer st = new StringTokenizer(tag,"= tn"); 89: 90: while (st.hasMoreTokens()) { 91: if ( st.nextToken().equalsIgnoreCase(key) ){ 92: add( toFullURL( st.nextToken() ) ); 93: return ; 94: } 95: } 96: } 97: 98: String strip( String a ){ 99: int ref = a.lastIndexOf('#'); 100: if ( ref != -1) a = a.substring(0,ref); 101: int first = a.indexOf('"'); 102: if ( first == -1 ) return a ; 103: int second = a.indexOf('"',first+1); 104: if ( second == -1 ) return a.substring(1) ; 105: return a.substring(first+1,second); 106: } 107: 108: public String toFullURL( String a ){ 109: a = strip(a); 110: if ( a.startsWith("http:") ){ 111: return a ; 112: } else if ( a.startsWith("/") ){ 113: return base + a ; 114: } else { 115: return refbase + a ; 116: } 117: } 118: 119: public void add(String a){ 120: if ( hash.containsKey(a) ) return ; 121: hash.put(a,"x"); 122: if ( a.endsWith(".html") ) { 123: tmpToReverse.addElement(a); 124: } else if ( a.endsWith(".gif") || 125: a.endsWith(".jpg") || 126: a.endsWith(".jpeg") ){ 127: pictures.addElement(a); 128: } 129: } 130: 131: Robo(String p){ 132: this( p, false); 133: } 134: 135: Robo(String p, boolean serverlimit ){ 136: String page = p ; 137: String top = null ; 138: 139: setUrl(p) ; 140: if ( serverlimit ){ 141: top = base + "/" ; 142: } else { 143: int i = p.lastIndexOf('/'); 144: top = p.substring(0,i+1); 145: } 146: 147: try { 148: while(true){ 149: tmpToReverse = new Vector() ; 150: readPage(page) ; 151: //System.out.println("-----------------------------------"); 152: for( int i=tmpToReverse.size()-1 ; i>=0 ; i--){ 153: // System.out.println(i + " : "+ tmpToReverse.elementAt(i) ); 154: stack.push( tmpToReverse.elementAt(i) ); 155: } 156: while( !(page=(String)stack.pop()).startsWith(top) ) ; 157: } 158: } catch (EmptyStackException e){ 159: System.out.println("Successfully finished !!"); 160: } catch ( Exception e ){ 161: System.out.println("Robo: Something wrong :" + e ); 162: } 163: 164: System.out.println("==================================="); 165: for(Enumeration e = pictures.elements() ; e.hasMoreElements(); ){ 166: System.out.println(e.nextElement() ); 167: } 168: } 169: 170: public static void main(String argv[]){ 171: Robo robo = new Robo(argv[0]); 172: } 173: }