Home | Documentation | Foundry | Examples | Demo | Download | Feedback | Books
Examples: stripping out html tags

Source:
import jregex.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Html extends Applet{
   private static final String space=" ";
   private static final String tag="<.*?>";
   private static final Pattern delimiters=new Pattern(space+"|"+tag);
   
   private static final Insets insets=new Insets(6,6,6,6);
   Label lEnter=new Label("Enter html here:");
   TextArea input=new TextArea(6,40);
   Label lResult=new Label("Result:");
   TextArea display=new TextArea(6,40);
   Button bStrip=new Button("Strip out tags");
   
   public void init(){
      setBackground(Color.white);
      GridBagLayout gb=new GridBagLayout();
      setLayout(gb);
      GridBagConstraints gbc=new GridBagConstraints();
      gbc.insets=new Insets(2,4,2,4);
      gbc.weightx=1;
      gbc.weighty=1;
      
      gbc.gridwidth=gbc.REMAINDER;
      gbc.anchor=gbc.WEST;
      gb.setConstraints(lEnter,gbc);
      gbc.anchor=gbc.CENTER;
      add(lEnter);
      gb.setConstraints(input,gbc);
      add(input);
      
      gbc.gridwidth=1;
      gbc.anchor=gbc.WEST;
      gb.setConstraints(lResult,gbc);
      gbc.anchor=gbc.CENTER;
      add(lResult);
      gbc.gridwidth=gbc.REMAINDER;
      gb.setConstraints(bStrip,gbc);
      add(bStrip);
      
      gb.setConstraints(display,gbc);
      add(display);
      
      display.setEditable(false);
      
      input.addTextListener(new TextListener(){
         public void textValueChanged(TextEvent e){
            display.setText(null);
         }
      });
      
      bStrip.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            display.setText(strip(input.getText()));
         }
      });
      
      input.setText(getParameter("text"));
   }
   
   public Insets getInsets(){
      return insets;
   }
   
   private static String strip(String s){
      StringBuffer sb=new StringBuffer();
      RETokenizer tok=delimiters.tokenizer(s);
      while(tok.hasMore()){
         sb.append(tok.nextToken());
         sb.append(" ");
      }
      return sb.toString();
   }
}
                  

Home | Documentation | Foundry | Examples | Demo | Download | Feedback | Books
Copyright 2000-2002 S. A. Samokhodkin