1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.RrdException; 28 import org.jrobin.core.Util; 29 import org.jrobin.core.XmlWriter; 30 31 import java.text.DateFormat ; 32 import java.text.SimpleDateFormat ; 33 import java.util.ArrayList ; 34 import java.util.Date ; 35 import java.util.Calendar ; 36 37 40 public class TimeText extends Comment 41 { 42 private static final String TIME_MARKER = "@t"; 43 44 private Date textDate = null; 45 private DateFormat dateFormat; 46 private ArrayList parsedList; 47 48 TimeText( String text, String pattern ) throws RrdException 49 { 50 this( text, new SimpleDateFormat ( pattern ) ); 51 } 52 53 TimeText( String text, DateFormat dateFormat ) throws RrdException 54 { 55 super( text ); 56 this.dateFormat = dateFormat; 57 58 if ( text.indexOf(TIME_MARKER) < 0 ) 60 throw new RrdException( "Could not find where to place timestamp. No @t placeholder found."); 61 } 62 63 TimeText( String text, String pattern, long timestamp ) throws RrdException 64 { 65 this( text, new SimpleDateFormat ( pattern ), new Date ( timestamp * 1000 ) ); 66 } 67 68 TimeText( String text, DateFormat dateFormat, long timestamp ) throws RrdException 69 { 70 this( text, dateFormat, new Date ( timestamp * 1000 ) ); 71 } 72 73 TimeText( String text, String pattern, Date date ) throws RrdException 74 { 75 this( text, new SimpleDateFormat ( pattern ), date ); 76 } 77 78 TimeText( String text, DateFormat dateFormat, Date date ) throws RrdException 79 { 80 super( text ); 81 this.textDate = date; 82 this.dateFormat = dateFormat; 83 84 if ( text.indexOf(TIME_MARKER) < 0 ) 86 throw new RrdException( "Could not find where to place timestamp. No @t placeholder found."); 87 } 88 89 TimeText( String text, String pattern, Calendar cal ) throws RrdException 90 { 91 this( text, new SimpleDateFormat ( pattern ), cal.getTime() ); 92 } 93 94 TimeText( String text, DateFormat dateFormat, Calendar cal ) throws RrdException 95 { 96 this( text, dateFormat, cal.getTime() ); 97 } 98 99 ArrayList getTokens() 100 { 101 parsedList = new ArrayList ( oList ); 102 103 String timeStr = dateFormat.format( (textDate != null ? textDate : new Date ()) ); 105 106 for (int i = 0; i < oList.size(); i += 2 ) 108 { 109 String str = (String ) oList.get(i); 110 111 str = str.replaceAll(TIME_MARKER, timeStr); 112 113 parsedList.set( i, str ); 114 } 115 116 return parsedList; 117 } 118 119 void exportXmlTemplate(XmlWriter xml) 120 { 121 xml.startTag("time"); 122 xml.writeTag( "format", text ); 123 if ( dateFormat instanceof SimpleDateFormat ) 124 xml.writeTag( "pattern", ((SimpleDateFormat ) dateFormat).toPattern()); 125 else 126 xml.writeTag( "pattern", "" ); if ( textDate != null ) 128 xml.writeTag( "value", Util.getTimestamp(textDate) ); 129 xml.closeTag(); 130 } 131 } 132 | Popular Tags |