home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / viewer / XMLViewer.java < prev    next >
Encoding:
Java Source  |  1997-10-31  |  12.7 KB  |  411 lines

  1. //******************************************************************************
  2. // XMLViewer.java:    Applet
  3. //
  4. //******************************************************************************
  5. import com.ms.xml.parser.*;
  6. import com.ms.xml.om.Document;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.applet.*;
  11. import java.awt.*;
  12. import java.awt.Choice;
  13.  
  14. import XMLViewerFrame;
  15. import XMLTreeView;
  16. import Scroller;
  17.  
  18. //==============================================================================
  19. class ParserThread extends Thread 
  20. {
  21.     Applet owner;
  22.     URL url;
  23.     String action;
  24.     String results;
  25.     Document doc;
  26.     String buffer;
  27.     boolean parse;
  28.  
  29.     public ParserThread(Applet owner, URL url, boolean parse, String action) 
  30.     {             
  31.         this.owner = owner;
  32.         this.url = url;
  33.         this.action = action;
  34.         this.parse = parse;
  35.     }
  36.  
  37.     public void run() {
  38.         if (parse)
  39.             parse();
  40.         else
  41.             load();
  42.  
  43.         owner.handleEvent(new Event(owner,Event.ACTION_EVENT,action));
  44.     }
  45.     
  46.     void parse()
  47.     {
  48.         ByteArrayOutputStream out = new ByteArrayOutputStream();            
  49.         PrintStream o = new PrintStream(out);
  50.         try {
  51.             try {
  52.                 doc = new Document();
  53.                 doc.load(url);
  54.                 o.println("Done.");
  55.             } catch (ParseException e) {
  56.                 doc.reportError(e, out);
  57.             }
  58.         } catch (ParseException e){
  59.             o.println(e.toString());
  60.         }
  61.         results = out.toString();
  62.     }
  63.  
  64.     void load()
  65.     {
  66.         buffer = new String();
  67.         try 
  68.         {
  69.             InputStream i = url.openStream();
  70.             int rc = 0;
  71.             byte b[] = new byte[8192];           
  72.             while (rc >= 0)
  73.             {
  74.                 rc = i.read(b);
  75.                 if( rc > 0 )
  76.                 {
  77.                     buffer = buffer + new String(b);
  78.                     b = new byte[8192];
  79.                 }
  80.             }
  81.             i.close();
  82.             results = "Done";
  83.         } catch (IOException e) {  
  84.             results = e.toString();
  85.         }
  86.     }
  87.  
  88.     public String getResults()
  89.     {
  90.         return results;
  91.     }
  92.  
  93.     public Document getDocument()
  94.     {
  95.         return doc;
  96.     }
  97.  
  98.     public String getBuffer()
  99.     {
  100.         return buffer;
  101.     }
  102. }
  103.  
  104.  
  105. //==============================================================================
  106. // Main Class for applet XMLViewer
  107. //
  108. //==============================================================================
  109. public class XMLViewer extends Applet
  110. {
  111.     String input;
  112.  
  113.     // STANDALONE APPLICATION SUPPORT:
  114.     //        m_fStandAlone will be set to true if applet is run standalone
  115.     //--------------------------------------------------------------------------
  116.     private boolean m_fStandAlone = false;
  117.  
  118.     // STANDALONE APPLICATION SUPPORT
  119.     //     The main() method acts as the applet's entry point when it is run
  120.     // as a standalone application. It is ignored if the applet is run from
  121.     // within an HTML page.
  122.     //--------------------------------------------------------------------------
  123.     public static void main(String args[])
  124.     {
  125.         XMLViewer applet_XMLViewer = new XMLViewer();
  126.         // Create Toplevel Window to contain applet XMLViewer
  127.         //----------------------------------------------------------------------
  128.         XMLViewerFrame frame = new XMLViewerFrame("XMLViewer",applet_XMLViewer);
  129.  
  130.         // Must show Frame before we size it so insets() will return valid values
  131.         //----------------------------------------------------------------------
  132.         frame.show();
  133.         frame.hide();
  134.         frame.resize(frame.insets().left + frame.insets().right  + 640,
  135.                      frame.insets().top  + frame.insets().bottom + 480);
  136.  
  137.         // The following code starts the applet running within the scrollpane 
  138.         // window, which is running within the fram window.
  139.         // It also calls GetParameters() to retrieve parameter values from the
  140.         // command line, and sets m_fStandAlone to true to prevent init() from
  141.         // trying to get them from the HTML page.
  142.         //----------------------------------------------------------------------     
  143.  
  144.         applet_XMLViewer.input = args[0];        
  145.         frame.add("Center",applet_XMLViewer);
  146.         applet_XMLViewer.m_fStandAlone = true;
  147.         applet_XMLViewer.init();
  148.         applet_XMLViewer.start();
  149.         frame.show();
  150.     }
  151.  
  152.     public String getParameter(String name) {
  153.         if (name.equals("Input") && input != null)
  154.             return input;
  155.         return super.getParameter(name);
  156.     }
  157.  
  158.     // XMLViewer Class Constructor
  159.     //--------------------------------------------------------------------------
  160.     public XMLViewer()
  161.     {
  162.         // TODO: Add constructor code here
  163.     }
  164.  
  165.     // APPLET INFO SUPPORT:
  166.     //        The getAppletInfo() method returns a string describing the applet's
  167.     // author, copyright date, or miscellaneous information.
  168.     //--------------------------------------------------------------------------
  169.     public String getAppletInfo()
  170.     {
  171.         return "Name: XMLViewer\r\n" +
  172.                "Author: Chris Lovett and Jeffrey Goldberg\r\n" +
  173.                "Created with Microsoft Visual J++ Version 1.1";
  174.     }
  175.  
  176.  
  177.     // The init() method is called by the AWT when an applet is first loaded or
  178.     // reloaded.  Override this method to perform whatever initialization your
  179.     // applet needs, such as initializing data structures, loading images or
  180.     // fonts, creating frame windows, setting the layout manager, or adding UI
  181.     // components.
  182.     //--------------------------------------------------------------------------
  183.     public void init()
  184.     {
  185.         createFrame();
  186.     }
  187.  
  188.     public void createFrame()
  189.     {
  190.         // Create Toplevel Window to contain applet XMLViewer
  191.         //----------------------------------------------------------------------
  192.         frame = new XMLViewerFrame("XMLViewer",this);
  193.  
  194.         // Must show Frame before we size it so insets() will return valid values
  195.         //----------------------------------------------------------------------
  196.         frame.show();
  197.         frame.hide();
  198.         frame.resize(frame.insets().left + frame.insets().right  + 640,
  199.                      frame.insets().top  + frame.insets().bottom + 480);
  200.         
  201.         frame.setLayout(new  BorderLayout());
  202.  
  203.         Panel panel = new Panel();
  204.         panel.setLayout( new BorderLayout() );
  205.  
  206.         Panel buttonPanel = new Panel();
  207.         buttonPanel.setLayout( new FlowLayout() );
  208.         buttonPanel.add("Center", new Button("Parse"));
  209.         buttonPanel.add("East", new Button("View"));        
  210.  
  211.         Panel panel1 = new Panel();
  212.         panel1.setLayout( new BorderLayout() );
  213.  
  214.         text = new TextField();  // 77
  215.  
  216.         panel1.add("Center", (Component)text);
  217.         panel1.add("East", buttonPanel);
  218.  
  219.         Panel panel2 = new Panel();
  220.         panel2.setLayout( new BorderLayout() );
  221.  
  222.         choice = new Choice();
  223.         panel2.add("Center", (Component)choice);
  224.  
  225.         panel.add("North",panel1);
  226.         panel.add("South",panel2);
  227.  
  228.         frame.add("North", panel);
  229.  
  230.         results = new TextArea(5,100);
  231.         results.setEditable(false);
  232.         frame.add("South", results);
  233.         
  234.         viewer = new XMLTreeView();
  235.         frame.add("Center", new Scroller( viewer ));
  236.        
  237.         // Set the open and closed folder images.
  238.         Image openFolder = getImage(getCodeBase(), OPENFOLDERPATH);
  239.         Image closedFolder = getImage(getCodeBase(), CLOSEDFOLDERPATH);            
  240.         int h = openFolder.getHeight(this);       // This forces images
  241.         h = closedFolder.getHeight(this);     // to load now.
  242.         imageCount= 2;
  243.         viewer.openFolder = openFolder;
  244.         viewer.closedFolder = closedFolder;
  245.  
  246.         viewer.choice = choice;  // The XMLViewer needs to deal with the choice.
  247.  
  248.         viewer.setBackground(Color.white);
  249.         
  250.         frame.show();
  251.         frame.resize(640, 480);
  252.  
  253.     }
  254.  
  255.     public boolean handleEvent(Event evt)
  256.     {
  257.         switch (evt.id)
  258.         {
  259.             // Application shutdown (e.g. user chooses Close from the system menu).
  260.             //------------------------------------------------------------------
  261.             case Event.ACTION_EVENT:
  262.                 action(evt,evt.target);
  263.                 return true;
  264.  
  265.             default:
  266.                 return super.handleEvent(evt);
  267.         }             
  268.     }
  269.  
  270.     public boolean action(Event  evt, Object  what)
  271.     {
  272.         if (evt.id == Event.ACTION_EVENT) {
  273.             String s = (String)evt.arg;
  274.             if (s.equals("Parse")) {
  275.                 results.setText("Parsing: " + text.getText());
  276.                 parse(text.getText());
  277.                 return true;
  278.             } else if (s.equals("View")) {
  279.                 view(text.getText());
  280.                 return true;
  281.             } else if (s.equals("FinishedParse")) {                
  282.                 results.setText(thread.getResults());
  283.                 viewer.setDocument(thread.getDocument());
  284.             } else if (s.equals("FinishedLoad")) {                
  285.                 results.setText(thread.getResults());
  286.                 viewer.showText(thread.getBuffer());
  287.             } else if (evt.target == choice ) {
  288.                 viewer.action(evt, what);
  289.             }       
  290.         }
  291.         return false;
  292.     }        
  293.  
  294.     // Place additional applet clean up code here.  destroy() is called when
  295.     // when you applet is terminating and being unloaded.
  296.     //-------------------------------------------------------------------------
  297.     public void destroy()
  298.     {
  299.         // TODO: Place applet cleanup code here
  300.     }
  301.  
  302.     // The start() method is called when the page containing the applet
  303.     // first appears on the screen. The AppletWizard's initial implementation
  304.     // of this method starts execution of the applet's thread.
  305.     //--------------------------------------------------------------------------
  306.     public void start()
  307.     {
  308.         // TODO: Place additional applet start code here
  309.         String urlparam = getParameter("URL");
  310.         parse(urlparam);
  311.     }
  312.     
  313.     //        The stop() method is called when the page containing the applet is
  314.     // no longer on the screen. The AppletWizard's initial implementation of
  315.     // this method stops execution of the applet's thread.
  316.     //--------------------------------------------------------------------------
  317.     public void stop()
  318.     {
  319.     }
  320.  
  321.     public URL createURL(String fileName) {
  322.         URL url = null;
  323.         try {
  324.             baseurl = getDocumentBase();
  325.             url = new URL(baseurl,fileName);
  326.         } catch (MalformedURLException ex) {
  327.             try {
  328.                 File f = new File(fileName);
  329.                 url = new URL("file:///" + f.getAbsolutePath());
  330.             } catch (MalformedURLException ex2) {
  331.             }
  332.         }
  333.         return url;
  334.     }
  335.  
  336.     public void load(String fileName)
  337.     {
  338.         boolean alive = frame.isAlive();
  339.         if (! alive)
  340.         {
  341.             createFrame(); // create a new one !!
  342.         }
  343.         parse(fileName);
  344.         if (frame != null)
  345.         {
  346.             frame.show();
  347.         }
  348.     }
  349.  
  350.     public void parse(String fileName)
  351.     {
  352.         try
  353.         {
  354.             URL url = createURL(fileName);
  355.             text.setText(url.toString());
  356.             viewer.setDocument(new Document());
  357.             results.setText("Parsing " + url.toString() + "...");
  358.             thread = new ParserThread(this,url,true,"FinishedParse");
  359.             thread.start();
  360.         } catch (Exception e){
  361.             results.setText(e.toString());
  362.         }
  363.     }
  364.  
  365.     public void view(String fileName)
  366.     {
  367.         try
  368.         {
  369.             URL url = createURL(fileName);
  370.             text.setText(url.toString());
  371.             viewer.setDocument(new Document());
  372.             results.setText("Loading " + url.toString() + "...");
  373.             viewer.showText("");
  374.             thread = new ParserThread(this,url,false,"FinishedLoad");
  375.             thread.start();
  376.         } catch (Exception e){
  377.             results.setText(e.toString());
  378.         }        
  379.     }
  380.  
  381.     public boolean imageUpdate(Image  img, int  infoflags,
  382.                                 int  x, int  y, int  width, int  height)
  383.     {
  384.         // this method is called when GIF's are loaded so
  385.         // we can force a repaint.
  386.         if ((infoflags & ALLBITS) == ALLBITS) {
  387.             // Image is now complete...
  388.             repaint();
  389.             imageCount--;
  390.             if (imageCount <= 0) 
  391.                 return false;
  392.         }
  393.         return true;
  394.     }
  395.  
  396.     TextField text;
  397.     Choice choice;
  398.  
  399.     TextArea results;
  400.     Document d = new Document();
  401.     XMLTreeView viewer;
  402.     URL baseurl;
  403.     XMLViewerFrame frame;
  404.  
  405.     ParserThread thread;
  406.  
  407.     final String OPENFOLDERPATH   = "Open.gif";
  408.     final String CLOSEDFOLDERPATH = "Closed.gif";
  409.  
  410.     int imageCount;
  411. }