1 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 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 [] args) 35 { 36 CommandLine cmd = new CommandLine() ; 37 38 boolean verbose = false ; 39 boolean debug = false ; 40 boolean reallyDoIt = true ; 41 42 45 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 url = cmd.getArg(modelDecl).getValue() ; 85 log.debug("URL of model = '"+url+"'") ; 86 87 String 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 format = "N3" ; 98 99 if ( cmd.contains("format") ) 100 format = cmd.getArg("format").getValue() ; 101 102 104 List paramNames = new ArrayList() ; 105 Map params = new HashMap() ; 106 107 for ( Iterator iter = cmd.items().iterator() ; iter.hasNext() ; ) 108 { 109 String s = (String )iter.next(); 110 int j = s.indexOf('=') ; 111 String name = s.substring(0,j) ; 112 String value = s.substring(j+1) ; 113 if ( name.equals("lang" )) 114 { 115 lang = value ; 116 continue ; 117 } 118 119 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 name = (String )iter.next(); 130 String value = (String )params.get(name) ; 131 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 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 | Popular Tags |