import com.ms.xml.ParseException; import com.ms.xml.Document; import com.ms.xml.Element; import java.util.Enumeration; import java.awt.*; import java.net.*; import java.applet.Applet; public class textbrowser extends Applet{ static String filename; Button button1; static String displayStrings[] = new String[100]; static int displayAttributes[] = new int[100]; static int numberDisplayLines = 0; public static void main(String args[]) { textbrowserFrame frame = new textbrowserFrame("The textbrowser application"); frame.show(); frame.hide(); frame.resize(320, 320); textbrowser applet = new textbrowser(); frame.add("Center", applet); applet.init(); applet.start(); frame.show(); } public void init(){ button1 = new Button("Browse"); add(button1); } public boolean action (Event e, Object o){ URL url = null; filename = "file:////c://xml//textbrowser//textbrowser.xml"; try { url = new URL(filename); } catch (MalformedURLException e1) { System.out.println("Cannot create URL for: " + filename); System.exit(0); } Document d = new Document(); try { d.load(url); } catch (ParseException e3) { d.reportError(e3, System.out); } if (d != null) { doTree(d.getRoot(), 0); repaint(); } return true; } void doTree(Element elem, int doBold) { Enumeration enum = elem.getChildren(); while (enum.hasMoreElements()) { Element elem2 = (Element)enum.nextElement(); if(elem2.getType() != com.ms.xml.Element.ELEMENT){ displayStrings[numberDisplayLines] = elem2.getText(); displayAttributes[numberDisplayLines++] = doBold; } else{ int doBoldOld = doBold; if(elem2.getTagName().equals("BOLD")){ doBold = 1; } if(elem2.getTagName().equals("PLAIN")){ doBold = 0; } doTree(elem2, doBold); doBold = doBoldOld; } } } public void paint(Graphics g) { int y = 0; for(int index = 0; index < numberDisplayLines; index++){ Font font; int style = Font.PLAIN; if(displayAttributes[index] == 1){ style = Font.BOLD; } font = new Font("Roman", style, 12); g.setFont(font); FontMetrics fontmetrics = getFontMetrics(getFont()); y += fontmetrics.getHeight(); g.drawString(displayStrings[index], 0, y); } } } class textbrowserFrame extends Frame { public textbrowserFrame(String str) { super (str); } public boolean handleEvent(Event evt) { switch (evt.id) { case Event.WINDOW_DESTROY: dispose(); System.exit(0); return true; default: return super.handleEvent(evt); } } }