FileDocCategorySizeDatePackage
ImageSizer.javaAPI DocExample4027Sat Sep 09 20:33:08 BST 2000None

ImageSizer

public class ImageSizer extends HTMLEditorKit$ParserCallback

Fields Summary
private Writer
out
private URL
base
Constructors Summary
public ImageSizer(Writer out, URL base)

    this.out = out;
    this.base = base;
  
Methods Summary
public voidhandleComment(char[] text, int position)

 
    
    try {
      out.write("<!-- ");
      out.write(text);
      out.write(" -->");
      out.flush();
    }
    catch (IOException e) {
      System.err.println(e);
    }
    
  
public voidhandleEndTag(javax.swing.text.html.HTML$Tag tag, int position)

    try {    
      out.write("</" + tag + ">");
      if (tag.breaksFlow()) out.write("\r\n");
      out.flush();
    }
    catch (IOException e) {
      System.err.println(e);
    }
  
public voidhandleSimpleTag(javax.swing.text.html.HTML$Tag tag, javax.swing.text.MutableAttributeSet attributes, int position)

    try {
      out.write("<" + tag);
      this.writeAttributes(tag, attributes);
      out.write(">");
    }
    catch (IOException e) {
      System.err.println(e);
      e.printStackTrace();
    }
  
public voidhandleStartTag(javax.swing.text.html.HTML$Tag tag, javax.swing.text.MutableAttributeSet attributes, int position)

    try {  
      out.write("<" + tag);
      this.writeAttributes(tag, attributes);
      out.write(">");
      out.flush();
    }
    catch (IOException e) {
      System.err.println(e);
      e.printStackTrace();
    }
    
  
public voidhandleText(char[] text, int position)

 
    
    try { 
      out.write(text);
      out.flush();
    }
    catch (IOException e) {
      System.err.println(e);
      e.printStackTrace();
    }
    
  
public static voidmain(java.lang.String[] args)

 
    
    for (int i = 0; i < args.length; i++) { 
      
      // The ParserGetter class is from Chapter 8
      ParserGetter kit = new ParserGetter();
      HTMLEditorKit.Parser parser = kit.getParser();
    
      try {
        URL u = new URL(args[0]);
        InputStream in = u.openStream();
        InputStreamReader r = new InputStreamReader(in);
        HTMLEditorKit.ParserCallback callback 
         = new ImageSizer(new OutputStreamWriter(System.out), u);
        parser.parse(r, callback, false);
      }
      catch (IOException e) {
        System.err.println(e); 
        e.printStackTrace();
      }
      
    } 
    
  
private voidwriteAttributes(javax.swing.text.html.HTML$Tag tag, javax.swing.text.AttributeSet attributes)

    
    Enumeration e = attributes.getAttributeNames();
    while (e.hasMoreElements()) {
      Object name = e.nextElement();
      String value = (String) attributes.getAttribute(name);
      out.write(" " + name + "=\"" + value + "\"");
    }
    // for the IMG tag we may have to add HEIGHT and WIDTH attributes
    if (tag == HTML.Tag.IMG) { 
      try {
        if (attributes.getAttribute(HTML.Attribute.HEIGHT) == null
         || attributes.getAttribute(HTML.Attribute.WIDTH) == null) {
           URL u = new URL(base, (String) attributes.getAttribute(HTML.Attribute.SRC));
           Image img = Toolkit.getDefaultToolkit().getImage(u);
           Component temp = new Label();
           MediaTracker tracker = new MediaTracker(temp);
           tracker.addImage(img, 1);
           try {
             tracker.waitForID(1);
             if (attributes.getAttribute(HTML.Attribute.WIDTH) == null) {
               out.write(" WIDTH=\"" + img.getWidth(temp) + "\"");
             }
             if (attributes.getAttribute(HTML.Attribute.HEIGHT) == null) {
               out.write(" HEIGHT=\"" + img.getHeight(temp) + "\"");
             } 
           }
           catch (InterruptedException ex) {
           }
        }
      }
      catch (MalformedURLException ex) {
        // SRC attribute is malformed
      }
    }