KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > joseki > query


1 /*
2  * (c) Copyright 2004 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  */

6
7 package joseki;
8
9 import com.hp.hpl.jena.joseki.*;
10 import com.hp.hpl.jena.rdf.model.*;
11
12 import jena.cmdline.*;
13 import org.apache.commons.logging.* ;
14
15 import java.util.*;
16
17
18 /** lib2.Query
19  * A simple application that builds and executes a Joseki-style GET query.
20  *
21  * @author Andy Seaborne
22  * @version $Id: query.java,v 1.4 2004/04/30 14:13:14 andy_seaborne Exp $
23  */

24
25 public class query
26 {
27     static {
28         if ( System.getProperty("log4j.configuration") == null )
29             System.setProperty("log4j.configuration", "file:etc/log4j.properties") ;
30     } ;
31
32     static Log log = LogFactory.getLog(query.class);
33     
34     public static void main(String JavaDoc[] args)
35     {
36         CommandLine cmd = new CommandLine() ;
37         
38         boolean verbose = false ;
39         boolean debug = false ;
40         boolean reallyDoIt = true ;
41         
42         // Usage: --model URL --lang name=value
43
// NB, values do not need to be encoded.
44

45         // Lang is special : it gest to the front of the front of the query string
46
// (not necessary - just pretty)
47

48         ArgDecl verboseDecl = new ArgDecl(false, "-v", "--verbose") ;
49         ArgDecl modelDecl = new ArgDecl(true, "model", "url") ;
50         ArgDecl langDecl = new ArgDecl(true, "lang") ;
51         ArgDecl debugDecl = new ArgDecl(false, "debug") ;
52         ArgDecl helpDecl = new ArgDecl(false, "help", "h") ;
53         ArgDecl formatDecl = new ArgDecl(true, "--fmt", "--format") ;
54         ArgDecl noActionDecl = new ArgDecl(false, "-n", "--noAction") ;
55         
56         cmd.add(verboseDecl) ;
57         cmd.add(debugDecl) ;
58         cmd.add(helpDecl) ;
59         cmd.add(modelDecl) ;
60         cmd.add(langDecl) ;
61         cmd.add(formatDecl) ;
62         cmd.add(noActionDecl) ;
63         
64         cmd.process(args) ;
65         
66         if ( cmd.contains(helpDecl) )
67         {
68             usage(System.out) ;
69             System.exit(0) ;
70         }
71         
72         if ( cmd.contains(verboseDecl))
73             verbose = true ;
74         
75         if ( cmd.contains(noActionDecl) )
76             reallyDoIt = false ;
77         
78         if ( !cmd.contains(modelDecl) )
79         {
80             System.err.println("Missing required parameter: --model") ;
81             System.exit(2) ;
82         }
83         
84         String JavaDoc url = cmd.getArg(modelDecl).getValue() ;
85         log.debug("URL of model = '"+url+"'") ;
86         
87         String JavaDoc lang = null ;
88         if ( cmd.contains(langDecl) )
89             lang = cmd.getArg(langDecl).getValue() ;
90
91         if ( lang != null )
92             log.debug("Language of query = '"+lang+"'") ;
93         else
94             log.debug("No --lang supplied") ;
95         
96         
97         String JavaDoc format = "N3" ;
98         
99         if ( cmd.contains("format") )
100             format = cmd.getArg("format").getValue() ;
101         
102         // Now process remainder.
103

104         List paramNames = new ArrayList() ;
105         Map params = new HashMap() ;
106         
107         for ( Iterator iter = cmd.items().iterator() ; iter.hasNext() ; )
108         {
109             String JavaDoc s = (String JavaDoc)iter.next();
110             int j = s.indexOf('=') ;
111             String JavaDoc name = s.substring(0,j) ;
112             String JavaDoc value = s.substring(j+1) ;
113             if ( name.equals("lang" ))
114             {
115                 lang = value ;
116                 continue ;
117             }
118             
119             //value = Convert.encWWWForm(value) ;
120
log.debug("Name = "+name+" :: Value = "+value) ;
121             params.put(name, value) ;
122             paramNames.add(name) ;
123         }
124
125         HttpQuery q = new HttpQuery(url, lang) ;
126         
127         for ( Iterator iter = paramNames.iterator() ; iter.hasNext() ; )
128         {
129             String JavaDoc name = (String JavaDoc)iter.next();
130             String JavaDoc value = (String JavaDoc)params.get(name) ;
131             //log.debug("Name = "+name+" :: Value = "+value) ;
132

133             q.addParam(name,value) ;
134         }
135         
136         log.debug("URL:: "+q.toString()) ;
137         
138         if ( verbose )
139             System.out.println("URL:: "+q.toString()) ;
140         
141         if ( !reallyDoIt )
142             System.exit(0) ;
143         
144         log.debug("Attempt request") ;
145         
146         Model model = null ;
147         
148         try {
149             model = q.exec();
150             log.debug("200 - OK") ;
151         } catch (HttpException httpEx)
152         {
153             log.debug(httpEx.getResponseCode()+" - "+httpEx.getMessage());
154             System.err.println("Http error: "+httpEx.getResponseCode()+" - "+httpEx.getMessage()) ;
155             System.exit(1) ;
156         }
157         model.write(System.out, format) ;
158     }
159     
160     static void usage(java.io.PrintStream JavaDoc out)
161     {
162         out.println("Usage: "+query.class.getName()+" --model URL [--lang queryLang] httpQueryString");
163         out.println(" --help Print this message") ;
164         out.println(" -n No action (don't do teh query - just build it") ;
165         out.println(" -v Verbose - print the request") ;
166         out.println(" --format fmt Print model using RDF syntax N3, RDF/XML, RDF/XML-ABBREV or N-TRIPLES") ;
167     }
168 }
169
170 /*
171  * (c) Copyright 2004 Hewlett-Packard Development Company, LP
172  * All rights reserved.
173  *
174  * Redistribution and use in source and binary forms, with or without
175  * modification, are permitted provided that the following conditions
176  * are met:
177  * 1. Redistributions of source code must retain the above copyright
178  * notice, this list of conditions and the following disclaimer.
179  * 2. Redistributions in binary form must reproduce the above copyright
180  * notice, this list of conditions and the following disclaimer in the
181  * documentation and/or other materials provided with the distribution.
182  * 3. The name of the author may not be used to endorse or promote products
183  * derived from this software without specific prior written permission.
184  *
185  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195  */
Popular Tags