home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / plugin / composer / ComposerDocument.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  4.6 KB  |  162 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.plugin.composer;
  20.  
  21. import java.io.*;
  22. import java.util.*;
  23. import netscape.plugin.composer.io.*;
  24. import java.net.*;
  25. import netscape.javascript.JSObject;
  26.  
  27. /** An impelementation of the HTML document.
  28.  */
  29. class ComposerDocument extends Document{
  30.     public ComposerDocument(Composer composer, String document, URL base,
  31.         String workDirectory, URL workDirectoryURL, String eventName, URL documentURL,
  32.         JSObject jsobject){
  33.         this.composer = composer;
  34.         this.text = document;
  35.         this.workDirectory = workDirectory;
  36.         this.base = base;
  37.         this.workDirectoryURL = workDirectoryURL;
  38.         this.eventName = eventName;
  39.         this.documentURL = documentURL;
  40.         this.jsobject=jsobject;
  41.     }
  42.  
  43.     public URL getBase(){
  44.         return base;
  45.     }
  46.  
  47.     public File getWorkDirectory(){
  48.         return new File(workDirectory);
  49.     }
  50.  
  51.     public URL getWorkDirectoryURL(){
  52.         return workDirectoryURL;
  53.     }
  54.  
  55.     public Reader getInput() throws IOException {
  56.         if ( in != null ) {
  57.             throw new IOException("Must close existing input stream first.");
  58.         }
  59.         in = new ComposerDocumentReader(this, text.toCharArray());
  60.         return in;
  61.     }
  62.     public Writer getOutput() throws IOException {
  63.         doOutputCheck();
  64.         out = new ComposerDocumentWriter(this);
  65.         return out;
  66.     }
  67.  
  68.     private void doOutputCheck() throws IOException {
  69.         if ( out != null ) {
  70.             throw new IOException("Must close existing output stream first.");
  71.         }
  72.     }
  73.     public String getText() {
  74.         return text;
  75.     }
  76.     public void setText(String text) throws IOException {
  77.         doOutputCheck();
  78.         this.text = text;
  79.         composer.newText(text);
  80.     }
  81.     void inputDone(ComposerDocumentReader in) {
  82.         if ( in == this.in ) {
  83.             this.in = null;
  84.         }
  85.         else {
  86.             // Do nothing.
  87.         }
  88.     }
  89.     void outputDone(ComposerDocumentWriter out, String text) {
  90.         if ( out == this.out ) {
  91.             this.out = null;
  92.             try {
  93.                 setText(text);
  94.             }
  95.             catch(IOException e){
  96.                 System.err.println("Couldn't set text.");
  97.                 e.printStackTrace();
  98.             }
  99.         }
  100.         else {
  101.             // Do nothing.
  102.         }
  103.     }
  104.  
  105.     public URL getDocumentURL(){
  106.         return documentURL;
  107.     }
  108.  
  109.     public String getEventName(){
  110.         return eventName;
  111.     }
  112.  
  113.     public void redirectDocumentOpen(String newURL){
  114.         if ( eventName.equals("edit") ){
  115.             composer.newText(newURL);
  116.         }
  117.     }
  118.  
  119.     /** get the jsobject from the document.  If no javascript in the
  120.      *  runtime, this will return null
  121.      */
  122.     public JSObject getJSObject(){return jsobject;}
  123.  
  124.     Composer getComposer() { return composer; }
  125.  
  126.     private Composer composer;
  127.     private String workDirectory;
  128.     private String text;
  129.     private URL base;
  130.     private URL workDirectoryURL;
  131.     private String eventName;
  132.     private URL documentURL;
  133.     private ComposerDocumentReader in;
  134.     private ComposerDocumentWriter out;
  135.     private PluginManager manager;
  136.     private JSObject jsobject;
  137. }
  138.  
  139. class ComposerDocumentReader extends CharArrayReader {
  140.     public ComposerDocumentReader(ComposerDocument document, char[] text){
  141.         super(text);
  142.         this.document = document;
  143.     }
  144.  
  145.     public void close() {
  146.         document.inputDone(this);
  147.         super.close();
  148.     }
  149.     ComposerDocument document;
  150. }
  151.  
  152. class ComposerDocumentWriter extends CharArrayWriter {
  153.     public ComposerDocumentWriter(ComposerDocument document){
  154.         this.document = document;
  155.     }
  156.     public void close() {
  157.         document.outputDone(this, toString());
  158.         super.close();
  159.     }
  160.     ComposerDocument document;
  161. }
  162.