| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NewPostTask |
|
| 1.4444444444444444;1.444 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, 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.util.Hashtable; | |
| 35 | import java.util.StringTokenizer; | |
| 36 | import java.util.Vector; | |
| 37 | ||
| 38 | import org.apache.tools.ant.BuildException; | |
| 39 | import org.apache.tools.ant.Task; | |
| 40 | import org.apache.xmlrpc.XmlRpcClient; | |
| 41 | ||
| 42 | /** | |
| 43 | * Ant task to post to a blog via the MetaWeblog API. | |
| 44 | * | |
| 45 | * @author Simon Brown | |
| 46 | */ | |
| 47 | 0 | public class NewPostTask extends Task { |
| 48 | ||
| 49 | private String blogid; | |
| 50 | private String username; | |
| 51 | private String password; | |
| 52 | private String title; | |
| 53 | private String content; | |
| 54 | private String category; | |
| 55 | private String url; | |
| 56 | 0 | private String handler = "metaWeblog"; |
| 57 | ||
| 58 | /** | |
| 59 | * Performs the work of this task. | |
| 60 | * | |
| 61 | * @throws org.apache.tools.ant.BuildException if something goes wrong | |
| 62 | */ | |
| 63 | public void execute() throws BuildException { | |
| 64 | ||
| 65 | try { | |
| 66 | 0 | System.out.println("Calling " + handler + ".newPost at " + url); |
| 67 | 0 | System.out.println(" blogid=" + blogid); |
| 68 | 0 | System.out.println(" username=" + username); |
| 69 | 0 | System.out.println(" password=********"); |
| 70 | 0 | System.out.println(" title=" + title); |
| 71 | 0 | System.out.println(" content=" + content); |
| 72 | 0 | System.out.println(" category=" + category); |
| 73 | 0 | System.out.println(" publish=true"); |
| 74 | ||
| 75 | 0 | XmlRpcClient xmlrpc = new XmlRpcClient(url); |
| 76 | 0 | Vector params = new Vector(); |
| 77 | 0 | params.add(blogid); |
| 78 | 0 | params.add(username); |
| 79 | 0 | params.add(password); |
| 80 | ||
| 81 | 0 | Hashtable struct = new Hashtable(); |
| 82 | 0 | struct.put("title", title); |
| 83 | 0 | struct.put("description", content); |
| 84 | 0 | Vector categories = new Vector(); |
| 85 | 0 | if (category != null) { |
| 86 | 0 | StringTokenizer tok = new StringTokenizer(category, ","); |
| 87 | 0 | while (tok.hasMoreTokens()) { |
| 88 | 0 | categories.add(tok.nextToken().trim()); |
| 89 | } | |
| 90 | } | |
| 91 | 0 | struct.put("categories", categories); |
| 92 | ||
| 93 | 0 | params.add(struct); |
| 94 | 0 | params.add(Boolean.TRUE); |
| 95 | 0 | Object postId = xmlrpc.execute(handler + ".newPost", params); |
| 96 | ||
| 97 | 0 | System.out.println("New post id is " + postId); |
| 98 | ||
| 99 | 0 | } catch (Exception e) { |
| 100 | 0 | throw new BuildException(e); |
| 101 | 0 | } |
| 102 | 0 | } |
| 103 | ||
| 104 | /** | |
| 105 | * Sets the blog id. | |
| 106 | * | |
| 107 | * @param blogid a String | |
| 108 | */ | |
| 109 | public void setBlogid(String blogid) { | |
| 110 | 0 | this.blogid = blogid; |
| 111 | 0 | } |
| 112 | ||
| 113 | /** | |
| 114 | * Sets the XML-RPC username. | |
| 115 | * | |
| 116 | * @param username a String | |
| 117 | */ | |
| 118 | public void setUsername(String username) { | |
| 119 | 0 | this.username = username; |
| 120 | 0 | } |
| 121 | ||
| 122 | /** | |
| 123 | * Sets the XML-RPC password. | |
| 124 | * | |
| 125 | * @param password a String | |
| 126 | */ | |
| 127 | public void setPassword(String password) { | |
| 128 | 0 | this.password = password; |
| 129 | 0 | } |
| 130 | ||
| 131 | /** | |
| 132 | * Sets the title of the blog entry. | |
| 133 | * | |
| 134 | * @param title the blog entry title | |
| 135 | */ | |
| 136 | public void setTitle(String title) { | |
| 137 | 0 | this.title = title; |
| 138 | 0 | } |
| 139 | ||
| 140 | /** | |
| 141 | * Sets the blog entry content. | |
| 142 | * | |
| 143 | * @param content the blog entry content | |
| 144 | */ | |
| 145 | public void setContent(String content) { | |
| 146 | 0 | this.content = content; |
| 147 | 0 | } |
| 148 | ||
| 149 | /** | |
| 150 | * Sets the category to which the blog entry should be added to. | |
| 151 | * | |
| 152 | * @param category the category ID as a String | |
| 153 | */ | |
| 154 | public void setCategory(String category) { | |
| 155 | 0 | this.category = category; |
| 156 | 0 | } |
| 157 | ||
| 158 | /** | |
| 159 | * Sets the URL of the XML-RPC call. | |
| 160 | * | |
| 161 | * @param url the URL as a String | |
| 162 | */ | |
| 163 | public void setUrl(String url) { | |
| 164 | 0 | this.url = url; |
| 165 | 0 | } |
| 166 | ||
| 167 | /** | |
| 168 | * Sets the name of the XML-RPC Blogger API handler (e.g. "blogger"). | |
| 169 | * | |
| 170 | * @param handler the name of the handler as a String | |
| 171 | */ | |
| 172 | public void setHandler(String handler) { | |
| 173 | 0 | this.handler = handler; |
| 174 | 0 | } |
| 175 | ||
| 176 | } |