View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *     Vlad Skarzhevskyy <vlads at users.sourceforge.net> (JTidy servlet  development)
18   *
19   *  The contributing author(s) would like to thank all those who
20   *  helped with testing, bug fixes, and patience.  This wouldn't
21   *  have been possible without all of you.
22   *
23   *  COPYRIGHT NOTICE:
24   *
25   *  This software and documentation is provided "as is," and
26   *  the copyright holders and contributing author(s) make no
27   *  representations or warranties, express or implied, including
28   *  but not limited to, warranties of merchantability or fitness
29   *  for any particular purpose or that the use of the software or
30   *  documentation will not infringe any third party patents,
31   *  copyrights, trademarks or other rights.
32   *
33   *  The copyright holders and contributing author(s) will not be
34   *  liable for any direct, indirect, special or consequential damages
35   *  arising out of any use of the software or documentation, even if
36   *  advised of the possibility of such damage.
37   *
38   *  Permission is hereby granted to use, copy, modify, and distribute
39   *  this source code, or portions hereof, documentation and executables,
40   *  for any purpose, without fee, subject to the following restrictions:
41   *
42   *  1. The origin of this source code must not be misrepresented.
43   *  2. Altered versions must be plainly marked as such and must
44   *     not be misrepresented as being the original source.
45   *  3. This Copyright notice may not be removed or altered from any
46   *     source or altered source distribution.
47   *
48   *  The copyright holders and contributing author(s) specifically
49   *  permit, without fee, and encourage the use of this source code
50   *  as a component for supporting the Hypertext Markup Language in
51   *  commercial products. If you use this source code in a product,
52   *  acknowledgment is not required but would be appreciated.
53   *
54   */
55  
56  package org.w3c.tidy.servlet.data;
57  
58  import java.util.Map;
59  import java.util.Hashtable;
60  
61  import org.w3c.tidy.servlet.ResponseRecord;
62  import org.w3c.tidy.servlet.ResponseRecordRepository;
63  
64  
65  /***
66   * Static Class to store Validation results and Error.
67   * @todo automatically remove old data
68   * @author Vlad Skarzhevskyy <a href="mailto:skarzhevskyy@gmail.com">skarzhevskyy@gmail.com </a>
69   * @version $Revision: 1.3 $ ($Author: fgiust $)
70   */
71  public class DefaultResponseRecordRepository implements ResponseRecordRepository
72  {
73  
74      private Map data;
75  
76      private Object lastPK;
77  
78      /***
79       * Create the Repository
80       */
81      public DefaultResponseRecordRepository()
82      {
83          this.data = new Hashtable();
84      }
85  
86      /***
87       * {@inheritDoc}
88       */
89      public void addRecord(ResponseRecord result)
90      {
91          Object key = result.getRequestID();
92          this.data.put(key, result);
93          this.lastPK = key;
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public Object getLastPK()
100     {
101         return this.lastPK;
102     }
103 
104     /***
105      * {@inheritDoc}
106      */
107     public Object getResponseID(String keyString)
108     {
109 
110         if (keyString == null)
111         {
112             return new Long(0);
113         }
114 
115         if (keyString.equalsIgnoreCase("last"))
116         {
117             return getLastPK();
118         }
119         else
120         {
121             return new Long(keyString);
122         }
123     }
124 
125     /***
126      * {@inheritDoc}
127      */
128     public ResponseRecord getRecord(Object key)
129     {
130         if (key == null)
131         {
132             return null;
133         }
134         return (ResponseRecord) this.data.get(key);
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public ResponseRecord getRecord(Object key, int sleep)
141     {
142         if (key == null)
143         {
144             return null;
145         }
146         ResponseRecord item = null;
147         long stop = System.currentTimeMillis() + sleep;
148         while ((item == null) && (stop > System.currentTimeMillis()))
149         {
150             item = getRecord(key);
151             if ((item != null) || (sleep == 0))
152             {
153                 break;
154             }
155             try
156             {
157                 Thread.sleep(100);
158             }
159             catch (InterruptedException ignore)
160             {
161                 // break;
162             }
163         }
164         return item;
165     }
166 }