Home | History | Annotate | Download | only in rss
      1 /*******************************************************************************
      2  * Copyright (c) 2005, 2006 IBM Corporation and others.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *     IBM Corporation - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.releng.generators.rss;
     12 
     13 import java.io.File;
     14 import java.io.FileWriter;
     15 import java.io.IOException;
     16 import java.io.PrintStream;
     17 import java.io.PrintWriter;
     18 import java.util.Date;
     19 
     20 import org.apache.tools.ant.BuildException;
     21 import org.apache.tools.ant.Task;
     22 import org.apache.tools.ant.util.DateUtils;
     23 
     24 import org.eclipse.releng.util.rss.Messages;
     25 import org.eclipse.releng.util.rss.RSSFeedUtil;
     26 
     27 //TODO: bug - can't run CreateFeed and AddEntry back when debug=2
     28 
     29 /**
     30  * Parameters:
     31  *   debug - more output to console - eg., 0|1|2
     32  *
     33  *   file - path to the XML file that will be created - eg., /path/to/file.to.create.xml
     34  *   project - project's name, used to label the feed - eg., Eclipse, EMF, UML2
     35  *   feedURL - URL of the feed where it will be published - eg., http://servername/path/to/feed.xml
     36  * @author nickb
     37  *
     38  */
     39 public class RSSFeedCreateFeedTask extends Task {
     40 
     41   private int debug = 0;
     42 
     43   //$ANALYSIS-IGNORE codereview.java.rules.portability.RulePortabilityLineSeparators
     44   private static final String NL="\n"; //$NON-NLS-1$
     45   private static final String NS = ""; //$NON-NLS-1$
     46   private static final String SP = " "; //$NON-NLS-1$
     47 
     48   //required fields
     49   private File file;
     50   private String project;
     51   private String feedURL;
     52 
     53   //optional
     54   public void setDebug(int debug) { this.debug = debug; }
     55 
     56   //required fields
     57   public void setFile(String file) {
     58     if (isNullString(file))
     59     { System.err.println(Messages.getString("RSSFeedCommon.FileError")); }  //$NON-NLS-1$
     60     else
     61     { this.file = new File(file); }
     62   }
     63   public void setProject(String project) {
     64     if (isNullString(project))
     65     { System.err.println(Messages.getString("RSSFeedCommon.ProjectError")); }  //$NON-NLS-1$
     66     else
     67     { this.project = project; }
     68   }
     69   public void setFeedURL(String feedURL) {
     70     if (isNullString(feedURL))
     71     { System.err.println(Messages.getString("RSSFeedCommon.FeedURLError")); }  //$NON-NLS-1$
     72     else
     73     { this.feedURL = feedURL; }
     74   }
     75 
     76   // The method executing the task
     77   public void execute() throws BuildException {
     78     if (debug>0) {
     79       System.out.println(Messages.getString("RSSFeedCreateFeedTask.Creating") + project + SP + Messages.getString("RSSFeedCommon.RSSFeedFile") + SP + file.toString() + ", " + Messages.getString("RSSFeedCommon.ToBePublishedAt") + feedURL); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
     80     }
     81     writeFeedXML(createFeedXML(),file);
     82     if (debug>1) {
     83       writeFeedXML(createFeedXML(),System.out);
     84     }
     85   }
     86 
     87   private String createFeedXML() {
     88     StringBuffer sb = new StringBuffer();
     89     sb.append("<?xml-stylesheet href=\"http://www.blogger.com/styles/atom.css\" type=\"text/css\"?>" + NL); //$NON-NLS-1$
     90     sb.append("<feed xmlns=\"http://www.w3.org/2005/Atom\">" + NL); //$NON-NLS-1$
     91     sb.append("  <title>" + project + SP + Messages.getString("RSSFeedCreateFeedTask.Builds") + "</title>" + NL); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     92     sb.append("  <link rel=\"self\" type=\"application/atom+xml\" href=\"" + (!isNullString(feedURL)?feedURL:NS) + "\"/>" + NL); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     93     sb.append("  <updated>" + getTimestamp() + "</updated>" + NL); //$NON-NLS-1$ //$NON-NLS-2$
     94     sb.append("  <author>" + NL); //$NON-NLS-1$
     95     sb.append("    <name>" + (!isNullString(project)?project + SP : NS) + Messages.getString("RSSFeedCreateFeedTask.BuildTeam") + "</name>" + NL); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
     96     sb.append("  </author>" + NL); //$NON-NLS-1$
     97     sb.append("  <id>" + (!isNullString(feedURL)?feedURL:NS) + "</id>" + NL); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     98     sb.append("</feed>" + NL + NL); //$NON-NLS-1$
     99     return sb.toString();
    100   }
    101 
    102   private void writeFeedXML(String feedXML,File file) {
    103     try{
    104       PrintWriter writer = new PrintWriter(new FileWriter(file));
    105       writer.println(feedXML);
    106       writer.flush();
    107       writer.close();
    108     } catch (IOException e){
    109       System.out.println(Messages.getString("RSSFeedCreateFeedTask.UnableToWriteToFile")+file); //$NON-NLS-1$
    110     }
    111 
    112   }
    113 
    114   private void writeFeedXML(String feedXML, PrintStream ps) {
    115     PrintWriter writer = new PrintWriter(ps);
    116     writer.println(feedXML);
    117     writer.flush();
    118     writer.close();
    119   }
    120 
    121   private String getTimestamp() { // eg., 2006-04-10T20:40:08Z
    122     return DateUtils.format(new Date(), DateUtils.ISO8601_DATETIME_PATTERN) + "Z";  //$NON-NLS-1$
    123   }
    124 
    125   private static boolean isNullString(String str)
    126   {
    127     return RSSFeedUtil.isNullString(str);
    128   }
    129 
    130 }