Package anvil.core.system

Source Code of anvil.core.system.AnyResource

/*
* $Id: AnyResource.java,v 1.6 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.system;

import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyBinary;
import anvil.core.time.AnyCalendar;
import anvil.script.compiler.CompiledModule;
import anvil.script.Context;
import anvil.server.Address;
import anvil.server.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.IOException;

/// @class Resource
/// Resource represents resources stored in zone's containers.
/// Resources may be obtained through <code>anvil.system.rawImport()</code>.

/**
* class AnyResource
*
* @author: Jani Lehtim�ki
*/
public class AnyResource extends AnyAbstractClass
{



 
  private Resource _resource;

 
  public AnyResource(Resource resource)
  {
    _resource = resource;
  }
 

  public final anvil.script.ClassType classOf() {
    return __class__;
  }

 
  public String toString()
  {
    return "Resource(" + _resource.getPathinfo() + ")";
  }
 
 
  public Object toObject()
  {
    return _resource;
  }
 

  /// @method getPathinfo
  /// Returns the pathinfo of this resource.
  /// @synopsis string getPathinfo()
  public Any m_getPathinfo()
  {
   
    return Any.create(_resource.getPathinfo());
  }


  /// @method getURL
  /// Returns the URL of this resource.
  /// @synopsis URL getURL()
  public Any m_getURL()
  {
    return new anvil.core.net.AnyURL(_resource.getURL());   
  }


  /// @method getLastModified
  /// Gets the time of last modification.
  /// @synopsis Calendar getLastModified()
  public Any m_getLastModified(Context context)
  {
    TimeZone tz = context.getTimeZone();
    Locale lc = context.getLocale();
    Calendar cal = Calendar.getInstance(tz, lc);
    cal.setTime(new Date(_resource.getLastModified()));
    return new anvil.core.time.AnyCalendar(cal);
  }


  /// @method getContentType
  /// Gets the content type of this resource.
  /// @synopsis string getContentType()
  public Any m_getContentType()
  {
    return Any.create(_resource.getContentType());
  }
 
 
  /// @method getContentLength
  /// Gets the content length of this resource.
  /// @synopsis int getContentLength()
  public Any m_getContentLength()
  {
    return Any.create(_resource.getLength());
  }
 
 
  /// @method getContent
  /// Gets the content of this resource.
  /// @synopsis binary getContent()
  /// @throws IOError If an I/O error occurs.
  public Any m_getContent(Context context)
  {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
      _resource.writeTo(output);
      return new AnyBinary(output.toByteArray());
    } catch (IOException e) {
      throw context.exception(e);
    }
  }


  /// @method getInputStream
  /// Gets an input stream that reads from this resource.
  /// @synopsis InputStream getInputStream()
  /// @throws IOError If an I/O error occurs.
  public Any m_getInputStream(Context context)
  {
    try {
      return new anvil.core.io.AnyInputStream(_resource.getInputStream());
    } catch (IOException e) {
      throw context.exception(e);
    }
  }
 
  /// @method writeTo
  /// Writes the content to given output stream. This
  /// is possibly more effient than explicitly getting
  /// the content out of resource.
  /// @synopsis void writeTo(OutputStream output)
  /// @throws IOError If an I/O error occurs.
  public static final Object[] p_writeTo = { null, "output" };
  public Any m_writeTo(Context context, Any stream)
  {
    if (stream instanceof anvil.core.io.AnyOutputStream) {
      try {
        OutputStream output = (OutputStream)stream.toObject();
        _resource.writeTo(output);
      } catch (IOException e) {
        throw context.exception(e);
      }   
    } else {
      throw context.BadParameter("OutputStream expected");
    }
    return NULL;
  }

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Resource", AnyResource.class,
    //DOC{{
    ""+
      " @class Resource\n" +
      " Resource represents resources stored in zone's containers.\n" +
      " Resources may be obtained through <code>anvil.system.rawImport()</code>.\n" +
      " @method getPathinfo\n" +
      " Returns the pathinfo of this resource.\n" +
      " @synopsis string getPathinfo()\n" +
      " @method getURL\n" +
      " Returns the URL of this resource.\n" +
      " @synopsis URL getURL()\n" +
      " @method getLastModified\n" +
      " Gets the time of last modification.\n" +
      " @synopsis Calendar getLastModified()\n" +
      " @method getContentType\n" +
      " Gets the content type of this resource.\n" +
      " @synopsis string getContentType()\n" +
      " @method getContentLength\n" +
      " Gets the content length of this resource.\n" +
      " @synopsis int getContentLength()\n" +
      " @method getContent\n" +
      " Gets the content of this resource.\n" +
      " @synopsis binary getContent()\n" +
      " @throws IOError If an I/O error occurs.\n" +
      " @method getInputStream\n" +
      " Gets an input stream that reads from this resource.\n" +
      " @synopsis InputStream getInputStream()\n" +
      " @throws IOError If an I/O error occurs.\n" +
      " @method writeTo\n" +
      " Writes the content to given output stream. This\n" +
      " is possibly more effient than explicitly getting\n" +
      " the content out of resource.\n" +
      " @synopsis void writeTo(OutputStream output)\n" +
      " @throws IOError If an I/O error occurs.\n"
    //}}DOC
    );
  static {
    SystemModule.class.getName();
  }

}
TOP

Related Classes of anvil.core.system.AnyResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.