avatar

625

Fix broken webserver side, due to bad date formatting on listings that don't have a start date. (Mostly store listings.) by mrs, 07 Sep, 2008 11:52 PM
Diff this changeset:
AuctionTransformer.java
mrs      1   package com.jbidwatcher.auction;
cyberfox 2   /*
cyberfox 3    * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
cyberfox 4    *
cyberfox 5    * Developed by mrs (Morgan Schweers)
cyberfox 6    */
cyberfox 7   
mrs      8   import com.jbidwatcher.util.config.JConfig;
mrs      9   import com.jbidwatcher.util.xml.XMLSerialize;
cyberfox 10  
cyberfox 11  import javax.xml.transform.stream.StreamSource;
cyberfox 12  import javax.xml.transform.stream.StreamResult;
cyberfox 13  import javax.xml.transform.*;
cyberfox 14  import java.io.*;
cyberfox 15  import java.util.List;
cyberfox 16  import java.util.Date;
cyberfox 17  import java.text.SimpleDateFormat;
cyberfox 18  
cyberfox 19  /**
cyberfox 20   * Created by IntelliJ IDEA.
cyberfox 21   * User: Morgan Schweers
cyberfox 22   * Date: Mar 24, 2006
cyberfox 23   * Time: 1:47:44 AM
cyberfox 24   *
cyberfox 25   */
mrs      26  public class AuctionTransformer implements ErrorListener, URIResolver {
cyberfox 27    public void transform(List<XMLSerialize> auctions) {
cyberfox 28      StringBuffer data = new StringBuffer("<auctions>\n");
cyberfox 29      for (XMLSerialize xs : auctions) {
cyberfox 30        data.append(xs.toXML().toString(1));
cyberfox 31      }
cyberfox 32      data.append("</auctions>\n");
cyberfox 33    }
cyberfox 34  
cyberfox 35    public String foo() { return "Foo!"; }
cyberfox 36  
cyberfox 37    public static String getTimeLeft(String auctionId) {
mrs      38      AuctionEntry ae = AuctionEntry.findByIdentifier(auctionId);
mrs      39      return (ae == null)?"(unknown)" : ae.getTimeLeft();
cyberfox 40    }
cyberfox 41  
cyberfox 42    private static SimpleDateFormat dateFmt = new SimpleDateFormat("dd-MMM-yy HH:mm:ss zzz");
cyberfox 43  
cyberfox 44    public static String formatDate(String when) {
mrs      45      try {
mrs      46        return dateFmt.format(new Date(Long.parseLong(when)));
mrs      47      } catch (NumberFormatException e) {
mrs      48        return "(unknown)";
mrs      49      }
cyberfox 50    }
cyberfox 51  
cyberfox 52    public static StringBuffer outputHTML(String loadFile) {
cyberfox 53      return outputHTML(loadFile, null);
cyberfox 54    }
cyberfox 55  
cyberfox 56    public static StringBuffer outputHTML(String loadFile, String xmlOutputFile) {
cyberfox 57      FileInputStream xmlIn = null;
cyberfox 58      InputStream xslIn = null;
cyberfox 59      FileOutputStream htmlOut = null;
cyberfox 60  
cyberfox 61      // create the XML content input source
cyberfox 62      // can be a DOM node, SAX stream, or any
cyberfox 63      // Java input stream/reader
cyberfox 64      try {
cyberfox 65        //String xmlInputFile = "myXMLinput.xml";
cyberfox 66        xmlIn = new FileInputStream(loadFile);
cyberfox 67        Source xmlSource = new StreamSource(xmlIn);
cyberfox 68  
cyberfox 69        // create the XSLT Stylesheet input source
cyberfox 70        // can be a DOM node, SAX stream, or a
cyberfox 71        // java input stream/reader
cyberfox 72        String xsltInputFile = "auctionTransform.xsl";
cyberfox 73  //      xslIn = new FileInputStream(xsltInputFile);
mrs      74        xslIn = JConfig.bestSource(AuctionTransformer.class.getClassLoader(), xsltInputFile);
cyberfox 75        Source xsltSource = new StreamSource(xslIn);
cyberfox 76  
cyberfox 77        // create the result target of the transformation
cyberfox 78        // can be a DOM node, SAX stream, or a java out
cyberfox 79        // stream/reader
cyberfox 80  
cyberfox 81        Result transResult;
cyberfox 82        StringWriter sw = null;
cyberfox 83        if(xmlOutputFile == null) {
cyberfox 84          sw = new StringWriter();
cyberfox 85          transResult = new StreamResult(sw);
cyberfox 86        } else {
cyberfox 87          htmlOut = new FileOutputStream(xmlOutputFile);
cyberfox 88          transResult = new StreamResult(htmlOut);
cyberfox 89        }
cyberfox 90  
cyberfox 91        // create the transformerfactory & transformer instance
cyberfox 92        TransformerFactory tf = TransformerFactory.newInstance();
mrs      93        //tf.setURIResolver(new AuctionTransformer());
cyberfox 94        Transformer t = tf.newTransformer(xsltSource);
mrs      95        t.setErrorListener(new AuctionTransformer());
cyberfox 96  
cyberfox 97        // execute transformation & fill result target object
cyberfox 98        t.transform(xmlSource, transResult);
cyberfox 99  
cyberfox 100       //  If we're outputting a buffer, return it, otherwise we've output
cyberfox 101       //  the file, and should just return null.
cyberfox 102       if(xmlOutputFile == null) return sw.getBuffer();
cyberfox 103     } catch(Exception ignored) {
cyberfox 104       ignored.printStackTrace();
cyberfox 105     } finally {
cyberfox 106       try {
cyberfox 107         if(xmlIn != null) xmlIn.close();
cyberfox 108         if(xslIn != null) xslIn.close();
cyberfox 109         if(htmlOut != null) htmlOut.close();
cyberfox 110       } catch(IOException ignored) {
cyberfox 111         //  Ignore exceptions on close.
cyberfox 112       }
cyberfox 113     }
cyberfox 114     return null;
cyberfox 115   }
cyberfox 116 
cyberfox 117   public void error(TransformerException exception) throws TransformerException {
cyberfox 118     System.err.println("Error occurred @ " + exception.getMessageAndLocation());
cyberfox 119   }
cyberfox 120 
cyberfox 121   public void fatalError(TransformerException exception) throws TransformerException {
cyberfox 122     System.err.println("Fatal error @ " + exception.getMessageAndLocation());
cyberfox 123   }
cyberfox 124 
cyberfox 125   public void warning(TransformerException exception) throws TransformerException {
cyberfox 126     System.err.println("Warning @ " + exception.getMessageAndLocation());
cyberfox 127   }
cyberfox 128 
cyberfox 129   public Source resolve(String href, String base) throws TransformerException {
cyberfox 130     System.err.println("href == " + href + ", base == " + base);
cyberfox 131     if(href.indexOf("jar") == -1) {
mrs      132       return new StreamSource(JConfig.bestSource(AuctionTransformer.class.getClassLoader(), href));
cyberfox 133     }
cyberfox 134     return null;
cyberfox 135   }
cyberfox 136 }

Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/auction/AuctionTransformer.java