KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > tuple > TupleSet


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5  
6 package com.hp.hpl.jena.util.tuple ;
7
8 import java.io.* ;
9 import java.util.* ;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /**
15  * @author Andy Seaborne
16  * @version $Id: TupleSet.java,v 1.6 2005/02/21 12:19:27 andy_seaborne Exp $
17  */

18
19 public class TupleSet implements Iterator
20 {
21     BufferedReader in ;
22     public String JavaDoc line = null ;
23     public int lineNumber = 0 ;
24
25     static final char COMMENTCHAR = '#' ;
26     List current = null ;
27     boolean finished = false ;
28
29     protected static Log logger = LogFactory.getLog( TupleSet.class );
30     
31     /** Creates new TupleSet */
32     public TupleSet(Reader r)
33     {
34         if ( ! ( r instanceof BufferedReader ) )
35             in = new BufferedReader(r) ;
36         else
37             in = (BufferedReader)r;
38     }
39
40     public boolean hasNext()
41     {
42         if ( finished ) return false ;
43
44         if ( current == null )
45             current = tuple() ;
46         return current != null ;
47     }
48
49     public Object JavaDoc next()
50     {
51         if ( hasNext() )
52         {
53             List x = current ;
54             current = null ;
55             return x ;
56         }
57         else
58             return null ;
59     }
60
61
62     public void remove()
63     {
64         throw new java.lang.UnsupportedOperationException JavaDoc("TupleSet.remove") ;
65     }
66
67     private List tuple()
68     {
69
70         try {
71             lineNumber ++ ;
72             line = in.readLine() ;
73         } catch (IOException e) {}
74
75         if ( line == null )
76         {
77             finished = true ;
78             return null ;
79         }
80
81         //System.out.println("Line: "+line) ;
82
List tuple = new ArrayList() ;
83         int i = 0 ;
84         int j = 0 ;
85         boolean errorFound = false ;
86
87      tupleLoop:
88         for (;;)
89         {
90             // Move to beginning of next item.
91
i = skipwhitespace(line, j) ;
92
93             if ( i < 0 )
94                 break ;
95
96             int iStart = -2 ; // Points to the beginning of the item as found
97
int jStart = -2 ; // Points to the item without quotes
98
int iFinish = -2 ; // Points after the end of the item as found
99
int jFinish = -2 ; // Points after the end of the item without quotes
100
int dtStart = -2 ; // Points to start of datatype (after < quote)
101
int dtFinish = -2 ; // Points to end of datatype
102
int type = TupleItem.UNKNOWN;
103
104             switch (line.charAt(i))
105             {
106                 case COMMENTCHAR:
107                     break tupleLoop ;
108                 case '<':
109                     type = TupleItem.URI ;
110                     iStart = i ;
111                     jStart = i+1 ;
112                     int newPosn = parseURI(i, line) ;
113                     if (newPosn < 0)
114                     {
115                         errorFound = true;
116                         break tupleLoop;
117                     }
118                     j = newPosn ;
119                     
120                     iFinish = j+1 ;
121                     jFinish = j ;
122                     break ;
123                 case '"':
124                     type = TupleItem.STRING ;
125                     iStart = i ;
126                     jStart = i+1 ;
127                     boolean inEscape = false ;
128                     for ( j = i+1 ; j < line.length() ; j++ )
129                     {
130                         char ch = line.charAt(j) ;
131                         if ( inEscape )
132                         {
133                             // ToDo: escape
134
inEscape = false ;
135                             continue ;
136                         }
137                         // Not an escape
138
if ( ch == '"' )
139                             break ;
140
141                         if ( ch == '\\' )
142                             inEscape = true ;
143                         if ( ch == '\n' || ch == '\r' )
144                         {
145                             errorFound = true ;
146                             break tupleLoop;
147                             
148                         }
149                     }
150                     
151                     // Malformed
152
if ( j == line.length() )
153                     {
154                         errorFound = true ;
155                         break tupleLoop;
156                     }
157                     
158                     iFinish = j+1 ;
159                     jFinish = j ;
160                     // RDF literals may be followed by their type.
161

162                     if ( j < line.length()-3
163                          && line.charAt(j+1) == '^'
164                          && line.charAt(j+2) == '^'
165                          && line.charAt(j+3) == '<' )
166                     {
167                         dtFinish = parseURI(j+3, line) ;
168                         dtStart = j+4 ;
169                         if (dtFinish < 0)
170                         {
171                             errorFound = true;
172                             break tupleLoop;
173                         }
174                         j = dtFinish+1 ;
175                         //String dt = line.substring(dtStart, dtFinish) ;
176
//System.out.println("I see a datatype:"+dt) ;
177
}
178                     
179                     break ;
180                 case '_':
181                     type = TupleItem.ANON ;
182                     iStart = i ;
183                     for ( j = i+1 ; j < line.length() ; j++ )
184                     {
185                         char ch = line.charAt(j) ;
186                         if ( ch == ' ' || ch == '\t' || ch == '.' )
187                             break ;
188                         if ( ! Character.isLetterOrDigit(ch) && ! (ch == '_') && ! (ch == ':') )
189                         {
190                             errorFound = true ;
191                             break tupleLoop ;
192                         }
193                     }
194                     iFinish = j ;
195                     jStart = iStart ;
196                     jFinish = iFinish ;
197                     break ;
198                 case '.':
199                 case '\n':
200                 case '\r':
201                     return tuple ;
202                 default:
203                     type = TupleItem.UNQUOTED ;
204                     iStart = i ;
205                     jStart = i ;
206                     for ( j = i+1 ; j < line.length() ; j++ )
207                     {
208                         char ch = line.charAt(j) ;
209                         if ( ch == ' ' || ch == '\t' || ch == '.' )
210                             break ;
211
212                         //if ( ! Character.isLetterOrDigit(line.charAt(i)) )
213
//{
214
// errorFound = true ;
215
// break tupleLoop;
216
//}
217
}
218                     // Malformed
219
if ( j == line.length()+1 )
220                     {
221                         errorFound = true ;
222                         break tupleLoop;
223                     }
224                     iFinish = j ;
225                     jFinish = j ;
226                     break ;
227             }
228             String JavaDoc item = line.substring(jStart, jFinish) ;
229             String JavaDoc literal = line.substring(iStart, iFinish) ;
230             String JavaDoc dt = null ;
231             if ( dtStart > 0 )
232                 dt = line.substring(dtStart, dtFinish) ;
233             
234             tuple.add(new TupleItem(item, literal, type, dt)) ;
235             j++ ;
236             // End of item.
237
}
238         //End of this line.
239
if ( errorFound )
240         {
241             logger.error( "Error in TupleSet.tuple: " + line );
242             
243             String JavaDoc s = "" ;
244             int k = 0 ;
245             for ( ; k < i ; k++ ) s = s+" " ;
246             s = s+"^" ;
247             for ( ; k < j-1 ; k++ ) s=s+" " ;
248             s = s+"^" ;
249             logger.error( s ) ;
250             return null ;
251         }
252
253         if ( tuple.size() == 0 )
254         {
255             // Nothing found : loop by tail recursion
256
return tuple() ;
257         }
258         return tuple ;
259     }
260
261     private int skipwhitespace(String JavaDoc s, int i)
262     {
263         for ( ; i < s.length() ; i++ )
264         {
265             char ch = s.charAt(i) ;
266             // Horizonal whitespace
267
if ( ch != ' ' && ch != '\t' )
268                 return i ;
269         }
270         return -1 ;
271     }
272
273     private int parseURI(int i, String JavaDoc line)
274     {
275         int j;
276         for (j = i + 1; j < line.length(); j++)
277         {
278             char ch = line.charAt(j);
279             if (ch == '>')
280                 break;
281             if (ch == '\n' || ch == '\r')
282                 return -1;
283         }
284         // Malformed
285
if (j == line.length())
286             return -2;
287         return j ;
288     }
289 }
290
291 /*
292  * (c) Copyright 2001 Hewlett-Packard Development Company, LP
293  * All rights reserved.
294  *
295  * Redistribution and use in source and binary forms, with or without
296  * modification, are permitted provided that the following conditions
297  * are met:
298  * 1. Redistributions of source code must retain the above copyright
299  * notice, this list of conditions and the following disclaimer.
300  * 2. Redistributions in binary form must reproduce the above copyright
301  * notice, this list of conditions and the following disclaimer in the
302  * documentation and/or other materials provided with the distribution.
303  * 3. The name of the author may not be used to endorse or promote products
304  * derived from this software without specific prior written permission.
305  *
306  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
307  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
308  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
309  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
310  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
311  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
312  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
313  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
314  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
315  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
316  */

317
Popular Tags