forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSearchClient.java
More file actions
159 lines (139 loc) · 5.12 KB
/
Copy pathCodeSearchClient.java
File metadata and controls
159 lines (139 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.codesearch;
import com.google.gdata.client.codesearch.CodeSearchService;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.data.codesearch.CodeSearchFeed;
import com.google.gdata.data.codesearch.CodeSearchEntry;
import com.google.gdata.data.codesearch.Match;
import sample.util.SimpleCommandLineParser;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URL;
/**
* An application that serves as a sample to show how the CodeSearchService
* can be used to retrieve data from Google Code Search.
*
*
*/
public class CodeSearchClient {
private static final String CODESEARCH_FEEDS_URL =
"https://www.google.com/codesearch/feeds/search?";
private CodeSearchService codesearchService;
private URL privateFeedUrl;
public CodeSearchClient(String query, String nresults, String start)
throws Exception {
codesearchService = new CodeSearchService("gdata-sample-codesearch");
privateFeedUrl = new URL(CODESEARCH_FEEDS_URL + "q=" + query
+ "&start-index=" + start +
"&max-results="+ nresults);
}
/**
* Driver for the sample.
*
* @param out outputStream to which to write status and messages
*/
public void run(PrintStream out) throws Exception {
retrieveFeed(out);
}
/**
* Retrieves a query feed.
*
* @param out outputStream on which to write status info
* @throws Exception if error in retrieving feed
*/
private void retrieveFeed(PrintStream out)
throws Exception {
PrintWriter writer = new PrintWriter(out);
XmlWriter xmlWriter = new XmlWriter(writer);
CodeSearchFeed myFeed =
codesearchService.getFeed(privateFeedUrl, CodeSearchFeed.class);
out.println("Retrieved feed: ");
out.println("Title: " + myFeed.getTitle().getPlainText());
out.println("Entries: " + myFeed.getEntries().size());
out.println("Updated: " + myFeed.getUpdated());
out.println("Start in: " + myFeed.getStartIndex());
out.println("Entries:");
for (CodeSearchEntry entry: myFeed.getEntries() ){
// Default Gdata elements
out.println("\tId: " + entry.getId());
out.println("\tTitle: " + entry.getTitle());
out.println("\tLink: " + entry.getHtmlLink().getHref());
out.println("\tUpdated: " + entry.getUpdated());
out.println("\tAuthor: " + entry.getAuthors().get(0).getName());
if (entry.getRights() != null)
out.println("\tLicense:" + entry.getRights().getPlainText());
// Codesearch Elements
out.println("\tPackage: ");
out.println("\t\t Name:" +
entry.getPackage().getName());
out.println("\t\t URI:" +
entry.getPackage().getUri());
entry.getPackage().generate(
xmlWriter,
codesearchService.getExtensionProfile());
out.println("XML: ");
writer.flush();
out.println("");
out.println("\tFile: " + entry.getFile().getName());
entry.getFile().generate(
xmlWriter,
codesearchService.getExtensionProfile());
out.println("XML: ");
writer.flush();
out.println("");
out.println("\tMatches: ");
for (Match m : entry.getMatches()) {
out.println(m.getLineNumber() + ": " +
m.getLineText().getPlainText());
m.generate(
xmlWriter,
codesearchService.getExtensionProfile());
out.println("XML: ");
writer.flush();
out.println("");
}
}
}
/**
* Main entry point. Parses arguments and creates and invokes the
* CodeSearchClient.
*/
public static void main(String[] arg)
throws Exception {
SimpleCommandLineParser parser = new SimpleCommandLineParser(arg);
String query = parser.getValue("query", "query", "q");
String nresults = parser.getValue("nresults", "nresults", "nr");
String start = parser.getValue("start", "index", "start");
boolean help = parser.containsKey("help", "h");
if (help || (query == null)) {
usage();
System.exit(1);
}
CodeSearchClient client = new CodeSearchClient(query, nresults, start);
client.run(System.out);
}
/**
* Prints usage of this application.
*/
private static void usage() {
System.out.println(
"Usage: java CodeSearchClient --query query_regex [--nresults number_of_results]"
+ " [--start start_index] ");
System.out.println(
"\nA simple application that uses the provided query\n" +
"and returns the results provided by the Google CodeSearch Service\n");
}
}