home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-10-31 | 12.7 KB | 411 lines |
- //******************************************************************************
- // XMLViewer.java: Applet
- //
- //******************************************************************************
- import com.ms.xml.parser.*;
- import com.ms.xml.om.Document;
-
- import java.io.*;
- import java.net.*;
- import java.applet.*;
- import java.awt.*;
- import java.awt.Choice;
-
- import XMLViewerFrame;
- import XMLTreeView;
- import Scroller;
-
- //==============================================================================
- class ParserThread extends Thread
- {
- Applet owner;
- URL url;
- String action;
- String results;
- Document doc;
- String buffer;
- boolean parse;
-
- public ParserThread(Applet owner, URL url, boolean parse, String action)
- {
- this.owner = owner;
- this.url = url;
- this.action = action;
- this.parse = parse;
- }
-
- public void run() {
- if (parse)
- parse();
- else
- load();
-
- owner.handleEvent(new Event(owner,Event.ACTION_EVENT,action));
- }
-
- void parse()
- {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- PrintStream o = new PrintStream(out);
- try {
- try {
- doc = new Document();
- doc.load(url);
- o.println("Done.");
- } catch (ParseException e) {
- doc.reportError(e, out);
- }
- } catch (ParseException e){
- o.println(e.toString());
- }
- results = out.toString();
- }
-
- void load()
- {
- buffer = new String();
- try
- {
- InputStream i = url.openStream();
- int rc = 0;
- byte b[] = new byte[8192];
- while (rc >= 0)
- {
- rc = i.read(b);
- if( rc > 0 )
- {
- buffer = buffer + new String(b);
- b = new byte[8192];
- }
- }
- i.close();
- results = "Done";
- } catch (IOException e) {
- results = e.toString();
- }
- }
-
- public String getResults()
- {
- return results;
- }
-
- public Document getDocument()
- {
- return doc;
- }
-
- public String getBuffer()
- {
- return buffer;
- }
- }
-
-
- //==============================================================================
- // Main Class for applet XMLViewer
- //
- //==============================================================================
- public class XMLViewer extends Applet
- {
- String input;
-
- // STANDALONE APPLICATION SUPPORT:
- // m_fStandAlone will be set to true if applet is run standalone
- //--------------------------------------------------------------------------
- private boolean m_fStandAlone = false;
-
- // STANDALONE APPLICATION SUPPORT
- // The main() method acts as the applet's entry point when it is run
- // as a standalone application. It is ignored if the applet is run from
- // within an HTML page.
- //--------------------------------------------------------------------------
- public static void main(String args[])
- {
- XMLViewer applet_XMLViewer = new XMLViewer();
- // Create Toplevel Window to contain applet XMLViewer
- //----------------------------------------------------------------------
- XMLViewerFrame frame = new XMLViewerFrame("XMLViewer",applet_XMLViewer);
-
- // Must show Frame before we size it so insets() will return valid values
- //----------------------------------------------------------------------
- frame.show();
- frame.hide();
- frame.resize(frame.insets().left + frame.insets().right + 640,
- frame.insets().top + frame.insets().bottom + 480);
-
- // The following code starts the applet running within the scrollpane
- // window, which is running within the fram window.
- // It also calls GetParameters() to retrieve parameter values from the
- // command line, and sets m_fStandAlone to true to prevent init() from
- // trying to get them from the HTML page.
- //----------------------------------------------------------------------
-
- applet_XMLViewer.input = args[0];
- frame.add("Center",applet_XMLViewer);
- applet_XMLViewer.m_fStandAlone = true;
- applet_XMLViewer.init();
- applet_XMLViewer.start();
- frame.show();
- }
-
- public String getParameter(String name) {
- if (name.equals("Input") && input != null)
- return input;
- return super.getParameter(name);
- }
-
- // XMLViewer Class Constructor
- //--------------------------------------------------------------------------
- public XMLViewer()
- {
- // TODO: Add constructor code here
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: XMLViewer\r\n" +
- "Author: Chris Lovett and Jeffrey Goldberg\r\n" +
- "Created with Microsoft Visual J++ Version 1.1";
- }
-
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- createFrame();
- }
-
- public void createFrame()
- {
- // Create Toplevel Window to contain applet XMLViewer
- //----------------------------------------------------------------------
- frame = new XMLViewerFrame("XMLViewer",this);
-
- // Must show Frame before we size it so insets() will return valid values
- //----------------------------------------------------------------------
- frame.show();
- frame.hide();
- frame.resize(frame.insets().left + frame.insets().right + 640,
- frame.insets().top + frame.insets().bottom + 480);
-
- frame.setLayout(new BorderLayout());
-
- Panel panel = new Panel();
- panel.setLayout( new BorderLayout() );
-
- Panel buttonPanel = new Panel();
- buttonPanel.setLayout( new FlowLayout() );
- buttonPanel.add("Center", new Button("Parse"));
- buttonPanel.add("East", new Button("View"));
-
- Panel panel1 = new Panel();
- panel1.setLayout( new BorderLayout() );
-
- text = new TextField(); // 77
-
- panel1.add("Center", (Component)text);
- panel1.add("East", buttonPanel);
-
- Panel panel2 = new Panel();
- panel2.setLayout( new BorderLayout() );
-
- choice = new Choice();
- panel2.add("Center", (Component)choice);
-
- panel.add("North",panel1);
- panel.add("South",panel2);
-
- frame.add("North", panel);
-
- results = new TextArea(5,100);
- results.setEditable(false);
- frame.add("South", results);
-
- viewer = new XMLTreeView();
- frame.add("Center", new Scroller( viewer ));
-
- // Set the open and closed folder images.
- Image openFolder = getImage(getCodeBase(), OPENFOLDERPATH);
- Image closedFolder = getImage(getCodeBase(), CLOSEDFOLDERPATH);
- int h = openFolder.getHeight(this); // This forces images
- h = closedFolder.getHeight(this); // to load now.
- imageCount= 2;
- viewer.openFolder = openFolder;
- viewer.closedFolder = closedFolder;
-
- viewer.choice = choice; // The XMLViewer needs to deal with the choice.
-
- viewer.setBackground(Color.white);
-
- frame.show();
- frame.resize(640, 480);
-
- }
-
- public boolean handleEvent(Event evt)
- {
- switch (evt.id)
- {
- // Application shutdown (e.g. user chooses Close from the system menu).
- //------------------------------------------------------------------
- case Event.ACTION_EVENT:
- action(evt,evt.target);
- return true;
-
- default:
- return super.handleEvent(evt);
- }
- }
-
- public boolean action(Event evt, Object what)
- {
- if (evt.id == Event.ACTION_EVENT) {
- String s = (String)evt.arg;
- if (s.equals("Parse")) {
- results.setText("Parsing: " + text.getText());
- parse(text.getText());
- return true;
- } else if (s.equals("View")) {
- view(text.getText());
- return true;
- } else if (s.equals("FinishedParse")) {
- results.setText(thread.getResults());
- viewer.setDocument(thread.getDocument());
- } else if (s.equals("FinishedLoad")) {
- results.setText(thread.getResults());
- viewer.showText(thread.getBuffer());
- } else if (evt.target == choice ) {
- viewer.action(evt, what);
- }
- }
- return false;
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- // TODO: Place applet cleanup code here
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- // TODO: Place additional applet start code here
- String urlparam = getParameter("URL");
- parse(urlparam);
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- }
-
- public URL createURL(String fileName) {
- URL url = null;
- try {
- baseurl = getDocumentBase();
- url = new URL(baseurl,fileName);
- } catch (MalformedURLException ex) {
- try {
- File f = new File(fileName);
- url = new URL("file:///" + f.getAbsolutePath());
- } catch (MalformedURLException ex2) {
- }
- }
- return url;
- }
-
- public void load(String fileName)
- {
- boolean alive = frame.isAlive();
- if (! alive)
- {
- createFrame(); // create a new one !!
- }
- parse(fileName);
- if (frame != null)
- {
- frame.show();
- }
- }
-
- public void parse(String fileName)
- {
- try
- {
- URL url = createURL(fileName);
- text.setText(url.toString());
- viewer.setDocument(new Document());
- results.setText("Parsing " + url.toString() + "...");
- thread = new ParserThread(this,url,true,"FinishedParse");
- thread.start();
- } catch (Exception e){
- results.setText(e.toString());
- }
- }
-
- public void view(String fileName)
- {
- try
- {
- URL url = createURL(fileName);
- text.setText(url.toString());
- viewer.setDocument(new Document());
- results.setText("Loading " + url.toString() + "...");
- viewer.showText("");
- thread = new ParserThread(this,url,false,"FinishedLoad");
- thread.start();
- } catch (Exception e){
- results.setText(e.toString());
- }
- }
-
- public boolean imageUpdate(Image img, int infoflags,
- int x, int y, int width, int height)
- {
- // this method is called when GIF's are loaded so
- // we can force a repaint.
- if ((infoflags & ALLBITS) == ALLBITS) {
- // Image is now complete...
- repaint();
- imageCount--;
- if (imageCount <= 0)
- return false;
- }
- return true;
- }
-
- TextField text;
- Choice choice;
-
- TextArea results;
- Document d = new Document();
- XMLTreeView viewer;
- URL baseurl;
- XMLViewerFrame frame;
-
- ParserThread thread;
-
- final String OPENFOLDERPATH = "Open.gif";
- final String CLOSEDFOLDERPATH = "Closed.gif";
-
- int imageCount;
- }