File Source: newmediaobjecttask.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.ant.metaweblog.NewMediaObjectTask__static_init
          */
     1  /*
     2   * Copyright (c) 2003-2005, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.ant.metaweblog;
    33  
    34  import java.io.File;
    35  import java.io.FileInputStream;
    36  import java.io.InputStream;
    37  import java.util.Hashtable;
    38  import java.util.Vector;
    39  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.ant.metaweblog.NewMediaObjectTask()
          * 
          *  Postconditions:
          *    this.handler == &"metaWeblog"
          */
    40  import org.apache.tools.ant.BuildException;
    41  import org.apache.tools.ant.Task;
    42  import org.apache.xmlrpc.XmlRpcClient;
    43  
    44  /**
    45   * Ant task to post a file to a blog via the MetaWeblog API.
    46   *
    47   * @author    Simon Brown
    48   */
    49  public class NewMediaObjectTask extends Task {
    50  
    51    private String blogid;
    52    private String username;
    53    private String password;
    54    private String src;
    55    private String dest;
    56    private String url;
    57    private String handler = "metaWeblog";
    58  
    59    /**
    60     * Performs the work of this task.
    61     *
    62     * @throws org.apache.tools.ant.BuildException   if something goes wrong
    63     */
           /* 
    P/P     *  Method: void execute()
            * 
            *  Preconditions:
            *    init'ed(this.blogid)
            *    init'ed(this.dest)
            *    init'ed(this.handler)
            *    init'ed(this.password)
            *    init'ed(this.src)
            *    init'ed(this.url)
            *    init'ed(this.username)
            * 
            *  Presumptions:
            *    java.lang.System.out != null
            *    org.apache.xmlrpc.XmlRpcClient:execute(...)@87 != null
            */
    64    public void execute() throws BuildException {
    65  
    66      try {
    67        System.out.println("Calling " + handler + ".newMediaObject at " + url);
    68        System.out.println(" blogid=" + blogid);
    69        System.out.println(" username=" + username);
    70        System.out.println(" password=********");
    71        System.out.println(" name=" + dest);
    72        System.out.println(" type=" + "");
    73  
    74        XmlRpcClient xmlrpc = new XmlRpcClient(url);
    75        Vector params = new Vector();
    76        params.add(blogid);
    77        params.add(username);
    78        params.add(password);
    79  
    80        Hashtable struct = new Hashtable();
    81        struct.put("name", dest);
    82        struct.put("type", "");
    83        struct.put("bits", readFile());
    84  
    85        params.add(struct);
    86  
    87        struct = (Hashtable)xmlrpc.execute(handler + ".newMediaObject", params);
    88  
    89        System.out.println("URL for media object is " + struct.get("url"));
    90  
    91      } catch (Exception e) {
    92        throw new BuildException(e);
    93      }
    94    }
    95  
           /* 
    P/P     *  Method: byte[] readFile()
            * 
            *  Preconditions:
            *    init'ed(this.src)
            * 
            *  Presumptions:
            *    java.io.File:length(...)@100 >= 0
            * 
            *  Postconditions:
            *    return_value == &new byte[](readFile#3)
            *    new byte[](readFile#3) num objects == 1
            *    (soft) return_value.length <= 264-1
            */
    96    private byte[] readFile() throws Exception {
    97      byte bytes[];
    98      File file = new File(src);
    99      InputStream in = new FileInputStream(src);
   100      bytes = new byte[(int)file.length()];
   101      in.read(bytes);
   102      in.close();
   103  
   104      return bytes;
   105    }
   106  
   107    /**
   108     * Sets the blog id.
   109     *
   110     * @param blogid    a String
   111     */
           /* 
    P/P     *  Method: void setBlogid(String)
            * 
            *  Postconditions:
            *    this.blogid == blogid
            *    init'ed(this.blogid)
            */
   112    public void setBlogid(String blogid) {
   113      this.blogid = blogid;
   114    }
   115  
   116    /**
   117     * Sets the XML-RPC username.
   118     *
   119     * @param username    a String
   120     */
           /* 
    P/P     *  Method: void setUsername(String)
            * 
            *  Postconditions:
            *    this.username == username
            *    init'ed(this.username)
            */
   121    public void setUsername(String username) {
   122      this.username = username;
   123    }
   124  
   125    /**
   126     * Sets the XML-RPC password.
   127     *
   128     * @param password    a String
   129     */
           /* 
    P/P     *  Method: void setPassword(String)
            * 
            *  Postconditions:
            *    this.password == password
            *    init'ed(this.password)
            */
   130    public void setPassword(String password) {
   131      this.password = password;
   132    }
   133  
   134    /**
   135     * Sets the URL of the XML-RPC call.
   136     *
   137     * @param url   the URL as a String
   138     */
           /* 
    P/P     *  Method: void setUrl(String)
            * 
            *  Postconditions:
            *    this.url == url
            *    init'ed(this.url)
            */
   139    public void setUrl(String url) {
   140      this.url = url;
   141    }
   142  
   143    /**
   144     * Sets the name of the XML-RPC Blogger API handler (e.g. "blogger").
   145     *
   146     * @param handler   the name of the handler as a String
   147     */
           /* 
    P/P     *  Method: void setHandler(String)
            * 
            *  Postconditions:
            *    this.handler == handler
            *    init'ed(this.handler)
            */
   148    public void setHandler(String handler) {
   149      this.handler = handler;
   150    }
   151  
   152    /**
   153     * Sets the source file.
   154     *
   155     * @param src   the source as a String
   156     */
           /* 
    P/P     *  Method: void setSrc(String)
            * 
            *  Postconditions:
            *    this.src == src
            *    init'ed(this.src)
            */
   157    public void setSrc(String src) {
   158      this.src = src;
   159    }
   160  
   161    /**
   162     * Sets the destination file.
   163     *
   164     * @param dest   the destination as a String
   165     */
           /* 
    P/P     *  Method: void setDest(String)
            * 
            *  Postconditions:
            *    this.dest == dest
            *    init'ed(this.dest)
            */
   166    public void setDest(String dest) {
   167      this.dest = dest;
   168    }
   169  
   170  }








SofCheck Inspector Build Version : 2.22510
newmediaobjecttask.java 2010-Jun-25 19:40:32
newmediaobjecttask.class 2010-Jul-19 20:23:40