KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > utils > SystemUtilities


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.utils;
38
39 import org.webharvest.runtime.variables.NodeVariable;
40 import org.webharvest.runtime.variables.IVariable;
41 import org.webharvest.exception.BaseException;
42
43 import java.text.SimpleDateFormat JavaDoc;
44 import java.util.Date JavaDoc;
45
46 /**
47  * Collection of useful constants and functions that are available in each
48  * scraper context.
49  */

50 public class SystemUtilities {
51
52     public static final IVariable lf = new NodeVariable("\n");
53     public static final IVariable tab = new NodeVariable("\t");
54     public static final IVariable cr = new NodeVariable("\r");
55     public static final IVariable space = new NodeVariable(" ");
56     public static final IVariable quot = new NodeVariable("\"");
57     public static final IVariable apos = new NodeVariable("\'");
58     public static final IVariable backspace = new NodeVariable("\b");
59
60     /**
61      * Returns formatted date/time for specified format string.
62      *
63      * @param format
64      */

65     public String JavaDoc datetime(Object JavaDoc format) {
66         if (format != null) {
67             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format.toString());
68             return formatter.format( new Date JavaDoc() );
69         }
70
71         throw new BaseException("Cannot return datetime for null format!");
72     }
73
74     /**
75      * Returns current date formated as "yyyyMMdd".
76      */

77     public String JavaDoc date() {
78         return datetime("yyyyMMdd");
79     }
80
81     /**
82      *
83      */

84     public String JavaDoc time() {
85         return datetime("HHmmss");
86     }
87     
88     /**
89      * Escapes XML string - special characters: &'"<> are
90      * replaced with XML escape sequences: &amp; &apos; &quot; &lt; &gt;
91      */

92     public String JavaDoc escapeXml(Object JavaDoc s) {
93         if (s != null) {
94             return CommonUtil.escapeXml(s.toString());
95         }
96
97         throw new BaseException("Cannot escape XML for null argumant!");
98     }
99     
100     /**
101      * Calculates full URL for specified page URL and link
102      * which could be full, absolute or relative like there can
103      * be found in A or IMG tags.
104      */

105     public String JavaDoc fullUrl(Object JavaDoc pageUrl, Object JavaDoc link) {
106         if (pageUrl != null && link != null) {
107             return CommonUtil.fullUrl(pageUrl.toString(), link.toString());
108         }
109
110         throw new BaseException("Cannot make full url for null argumants!");
111     }
112
113 }
Popular Tags