KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > TimeText


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

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 JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Calendar JavaDoc;
36
37 /**
38  * <p>Comment object containing a formatted timestamp (current time of timestamp given).</p>
39  */

40 public class TimeText extends Comment
41 {
42     private static final String JavaDoc TIME_MARKER = "@t";
43
44     private Date JavaDoc textDate = null;
45     private DateFormat JavaDoc dateFormat;
46     private ArrayList JavaDoc parsedList;
47
48     TimeText( String JavaDoc text, String JavaDoc pattern ) throws RrdException
49     {
50         this( text, new SimpleDateFormat JavaDoc( pattern ) );
51     }
52
53     TimeText( String JavaDoc text, DateFormat JavaDoc dateFormat ) throws RrdException
54     {
55         super( text );
56         this.dateFormat = dateFormat;
57
58         // Check if we can locate the placeholder for the timestamp (@t)
59
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 JavaDoc text, String JavaDoc pattern, long timestamp ) throws RrdException
64     {
65         this( text, new SimpleDateFormat JavaDoc( pattern ), new Date JavaDoc( timestamp * 1000 ) );
66     }
67
68     TimeText( String JavaDoc text, DateFormat JavaDoc dateFormat, long timestamp ) throws RrdException
69     {
70         this( text, dateFormat, new Date JavaDoc( timestamp * 1000 ) );
71     }
72
73     TimeText( String JavaDoc text, String JavaDoc pattern, Date JavaDoc date ) throws RrdException
74     {
75         this( text, new SimpleDateFormat JavaDoc( pattern ), date );
76     }
77
78     TimeText( String JavaDoc text, DateFormat JavaDoc dateFormat, Date JavaDoc date ) throws RrdException
79     {
80         super( text );
81         this.textDate = date;
82         this.dateFormat = dateFormat;
83
84         // Check if we can locate the placeholder for the timestamp (@t)
85
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 JavaDoc text, String JavaDoc pattern, Calendar JavaDoc cal ) throws RrdException
90     {
91         this( text, new SimpleDateFormat JavaDoc( pattern ), cal.getTime() );
92     }
93
94     TimeText( String JavaDoc text, DateFormat JavaDoc dateFormat, Calendar JavaDoc cal ) throws RrdException
95     {
96         this( text, dateFormat, cal.getTime() );
97     }
98
99     ArrayList JavaDoc getTokens()
100     {
101         parsedList = new ArrayList JavaDoc( oList );
102
103         // Create time string
104
String JavaDoc timeStr = dateFormat.format( (textDate != null ? textDate : new Date JavaDoc()) );
105
106         // Replace all values
107
for (int i = 0; i < oList.size(); i += 2 )
108         {
109             String JavaDoc str = (String JavaDoc) 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 JavaDoc )
124             xml.writeTag( "pattern", ((SimpleDateFormat JavaDoc) dateFormat).toPattern());
125         else
126             xml.writeTag( "pattern", "" ); // A custom DateFormat can't be exported
127
if ( textDate != null )
128             xml.writeTag( "value", Util.getTimestamp(textDate) );
129         xml.closeTag();
130     }
131 }
132
Popular Tags