List of usage examples for java.io BufferedReader BufferedReader
public BufferedReader(Reader in)
From source file:PopClean.java
public static void main(String args[]) { try {/* w w w . ja v a 2 s . c om*/ String hostname = null, username = null, password = null; int port = 110; int sizelimit = -1; String subjectPattern = null; Pattern pattern = null; Matcher matcher = null; boolean delete = false; boolean confirm = true; // Handle command-line arguments for (int i = 0; i < args.length; i++) { if (args[i].equals("-user")) username = args[++i]; else if (args[i].equals("-pass")) password = args[++i]; else if (args[i].equals("-host")) hostname = args[++i]; else if (args[i].equals("-port")) port = Integer.parseInt(args[++i]); else if (args[i].equals("-size")) sizelimit = Integer.parseInt(args[++i]); else if (args[i].equals("-subject")) subjectPattern = args[++i]; else if (args[i].equals("-debug")) debug = true; else if (args[i].equals("-delete")) delete = true; else if (args[i].equals("-force")) // don't confirm confirm = false; } // Verify them if (hostname == null || username == null || password == null || sizelimit == -1) usage(); // Make sure the pattern is a valid regexp if (subjectPattern != null) { pattern = Pattern.compile(subjectPattern); matcher = pattern.matcher(""); } // Say what we are going to do System.out .println("Connecting to " + hostname + " on port " + port + " with username " + username + "."); if (delete) { System.out.println("Will delete all messages longer than " + sizelimit + " bytes"); if (subjectPattern != null) System.out.println("that have a subject matching: [" + subjectPattern + "]"); } else { System.out.println("Will list subject lines for messages " + "longer than " + sizelimit + " bytes"); if (subjectPattern != null) System.out.println("that have a subject matching: [" + subjectPattern + "]"); } // If asked to delete, ask for confirmation unless -force is given if (delete && confirm) { System.out.println(); System.out.print("Do you want to proceed (y/n) [n]: "); System.out.flush(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); String response = console.readLine(); if (!response.equals("y")) { System.out.println("No messages deleted."); System.exit(0); } } // Connect to the server, and set up streams s = new Socket(hostname, port); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); // Read the welcome message from the server, confirming it is OK. System.out.println("Connected: " + checkResponse()); // Now log in send("USER " + username); // Send username, wait for response send("PASS " + password); // Send password, wait for response System.out.println("Logged in"); // Check how many messages are waiting, and report it String stat = send("STAT"); StringTokenizer t = new StringTokenizer(stat); System.out.println(t.nextToken() + " messages in mailbox."); System.out.println("Total size: " + t.nextToken()); // Get a list of message numbers and sizes send("LIST"); // Send LIST command, wait for OK response. // Now read lines from the server until we get . by itself List msgs = new ArrayList(); String line; for (;;) { line = in.readLine(); if (line == null) throw new IOException("Unexpected EOF"); if (line.equals(".")) break; msgs.add(line); } // Now loop through the lines we read one at a time. // Each line should specify the message number and its size. int nummsgs = msgs.size(); for (int i = 0; i < nummsgs; i++) { String m = (String) msgs.get(i); StringTokenizer st = new StringTokenizer(m); int msgnum = Integer.parseInt(st.nextToken()); int msgsize = Integer.parseInt(st.nextToken()); // If the message is too small, ignore it. if (msgsize <= sizelimit) continue; // If we're listing messages, or matching subject lines // find the subject line for this message String subject = null; if (!delete || pattern != null) { subject = getSubject(msgnum); // get the subject line // If we couldn't find a subject, skip the message if (subject == null) continue; // If this subject does not match the pattern, then // skip the message if (pattern != null) { matcher.reset(subject); if (!matcher.matches()) continue; } // If we are listing, list this message if (!delete) { System.out.println("Subject " + msgnum + ": " + subject); continue; // so we never delete it } } // If we were asked to delete, then delete the message if (delete) { send("DELE " + msgnum); if (pattern == null) System.out.println("Deleted message " + msgnum); else System.out.println("Deleted message " + msgnum + ": " + subject); } } // When we're done, log out and shutdown the connection shutdown(); } catch (Exception e) { // If anything goes wrong print exception and show usage System.err.println(e); usage(); // Always try to shutdown nicely so the server doesn't hang on us shutdown(); } }
From source file:de.akquinet.dustjs.DustEngine.java
public static void main(String[] args) throws URISyntaxException { Options cmdOptions = new Options(); cmdOptions.addOption(DustOptions.CHARSET_OPTION, true, "Input file charset encoding. Defaults to UTF-8."); cmdOptions.addOption(DustOptions.DUST_OPTION, true, "Path to a custom dust.js for Rhino version."); try {//from w w w . j ava2 s. c om CommandLineParser cmdParser = new GnuParser(); CommandLine cmdLine = cmdParser.parse(cmdOptions, args); DustOptions options = new DustOptions(); if (cmdLine.hasOption(DustOptions.CHARSET_OPTION)) { options.setCharset(cmdLine.getOptionValue(DustOptions.CHARSET_OPTION)); } if (cmdLine.hasOption(DustOptions.DUST_OPTION)) { options.setDust(new File(cmdLine.getOptionValue(DustOptions.DUST_OPTION)).toURI().toURL()); } DustEngine engine = new DustEngine(options); String[] files = cmdLine.getArgs(); String src = null; if (files == null || files.length == 0) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while (-1 != (n = in.read(buffer))) { sw.write(buffer, 0, n); } src = sw.toString(); } if (src != null && !src.isEmpty()) { System.out.println(engine.compile(src, "test")); return; } if (files.length == 1) { System.out.println(engine.compile(new File(files[0]))); return; } if (files.length == 2) { engine.compile(new File(files[0]), new File(files[1])); return; } } catch (IOException ioe) { System.err.println("Error opening input file."); } catch (ParseException pe) { System.err.println("Error parsing arguments."); } HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar dust-engine.jar input [output] [options]", cmdOptions); System.exit(1); }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.FilterTranTable.java
public static void main(String[] args) { Options options = new Options(); options.addOption(INPUT_PARAM, null, true, INPUT_DESC); options.addOption(OUTPUT_PARAM, null, true, OUTPUT_DESC); options.addOption(CommonParams.MEM_FWD_INDEX_PARAM, null, true, CommonParams.MEM_FWD_INDEX_DESC); options.addOption(CommonParams.GIZA_ITER_QTY_PARAM, null, true, CommonParams.GIZA_ITER_QTY_PARAM); options.addOption(CommonParams.GIZA_ROOT_DIR_PARAM, null, true, CommonParams.GIZA_ROOT_DIR_PARAM); options.addOption(CommonParams.MIN_PROB_PARAM, null, true, CommonParams.MIN_PROB_DESC); options.addOption(CommonParams.MAX_WORD_QTY_PARAM, null, true, CommonParams.MAX_WORD_QTY_PARAM); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); try {/*from w ww. ja v a2 s . c o m*/ CommandLine cmd = parser.parse(options, args); String outputFile = null; outputFile = cmd.getOptionValue(OUTPUT_PARAM); if (null == outputFile) { Usage("Specify 'A name of the output file'", options); } String gizaRootDir = cmd.getOptionValue(CommonParams.GIZA_ROOT_DIR_PARAM); if (null == gizaRootDir) { Usage("Specify '" + CommonParams.GIZA_ROOT_DIR_DESC + "'", options); } String gizaIterQty = cmd.getOptionValue(CommonParams.GIZA_ITER_QTY_PARAM); if (null == gizaIterQty) { Usage("Specify '" + CommonParams.GIZA_ITER_QTY_DESC + "'", options); } float minProb = 0; String tmpf = cmd.getOptionValue(CommonParams.MIN_PROB_PARAM); if (tmpf != null) { minProb = Float.parseFloat(tmpf); } int maxWordQty = Integer.MAX_VALUE; String tmpi = cmd.getOptionValue(CommonParams.MAX_WORD_QTY_PARAM); if (null != tmpi) { maxWordQty = Integer.parseInt(tmpi); } String memFwdIndxName = cmd.getOptionValue(CommonParams.MEM_FWD_INDEX_PARAM); if (null == memFwdIndxName) { Usage("Specify '" + CommonParams.MEM_FWD_INDEX_DESC + "'", options); } System.out.println("Filtering index: " + memFwdIndxName + " max # of frequent words: " + maxWordQty + " min. probability:" + minProb); VocabularyFilterAndRecoder filter = new FrequentIndexWordFilterAndRecoder(memFwdIndxName, maxWordQty); String srcVocFile = CompressUtils.findFileVariant(gizaRootDir + "/source.vcb"); System.out.println("Source vocabulary file: " + srcVocFile); GizaVocabularyReader srcVoc = new GizaVocabularyReader(srcVocFile, filter); String dstVocFile = CompressUtils.findFileVariant(gizaRootDir + "/target.vcb"); System.out.println("Target vocabulary file: " + dstVocFile); GizaVocabularyReader dstVoc = new GizaVocabularyReader(CompressUtils.findFileVariant(dstVocFile), filter); String inputFile = CompressUtils.findFileVariant(gizaRootDir + "/output.t1." + gizaIterQty); BufferedReader finp = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(inputFile))); BufferedWriter fout = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outputFile))); try { String line; int prevSrcId = -1; int wordQty = 0; long addedQty = 0; long totalQty = 0; boolean isNotFiltered = false; for (totalQty = 0; (line = finp.readLine()) != null;) { ++totalQty; // Skip empty lines line = line.trim(); if (line.isEmpty()) continue; GizaTranRec rec = new GizaTranRec(line); if (rec.mSrcId != prevSrcId) { ++wordQty; } if (totalQty % REPORT_INTERVAL_QTY == 0) { System.out.println(String.format( "Processed %d lines (%d source word entries) from '%s', added %d lines", totalQty, wordQty, inputFile, addedQty)); } // isNotFiltered should be set after procOneWord if (rec.mSrcId != prevSrcId) { if (rec.mSrcId == 0) isNotFiltered = true; else { String wordSrc = srcVoc.getWord(rec.mSrcId); isNotFiltered = filter == null || (wordSrc != null && filter.checkWord(wordSrc)); } } prevSrcId = rec.mSrcId; if (rec.mProb >= minProb && isNotFiltered) { String wordDst = dstVoc.getWord(rec.mDstId); if (filter == null || (wordDst != null && filter.checkWord(wordDst))) { fout.write(String.format(rec.mSrcId + " " + rec.mDstId + " " + rec.mProb)); fout.newLine(); addedQty++; } } } System.out.println( String.format("Processed %d lines (%d source word entries) from '%s', added %d lines", totalQty, wordQty, inputFile, addedQty)); } finally { finp.close(); fout.close(); } } catch (ParseException e) { Usage("Cannot parse arguments", options); } catch (Exception e) { e.printStackTrace(); System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:CourserankConnector.java
public static void main(String[] args) throws Exception { /////////////////////////////////////// //Tagger init //MaxentTagger tagger = new MaxentTagger("models/english-left3words-distsim.tagger"); ////* w ww . j a v a 2 s . c o m*/ //CLIENT INITIALIZATION ImportData importCourse = new ImportData(); HttpClient httpclient = new DefaultHttpClient(); httpclient = WebClientDevWrapper.wrapClient(httpclient); try { /* httpclient.getCredentialsProvider().setCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials("eadrian", "eactresp1")); */ ////////////////////////////////////////////////// //Get Course Bulletin Departments page List<Course> courses = new ArrayList<Course>(); HttpGet httpget = new HttpGet("http://explorecourses.stanford.edu"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); String bulletinpage = ""; //STORE RETURNED HTML TO BULLETINPAGE if (entity != null) { //System.out.println("Response content length: " + entity.getContentLength()); InputStream i = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { bulletinpage += line; //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(entity); /////////////////////////////////////////////////////////////////////////////// //Login to Courserank httpget = new HttpGet("https://courserank.com/stanford/main"); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); String page = ""; if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); InputStream i = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { page += line; //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(entity); //////////////////////////////////////////////////// //POST REQUEST LOGIN HttpPost post = new HttpPost("https://www.courserank.com/stanford/main"); List<NameValuePair> pairs = new ArrayList<NameValuePair>(2); pairs.add(new BasicNameValuePair("RT", "")); pairs.add(new BasicNameValuePair("action", "login")); pairs.add(new BasicNameValuePair("password", "trespass")); pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu")); post.setEntity(new UrlEncodedFormEntity(pairs)); System.out.println("executing request" + post.getRequestLine()); HttpResponse resp = httpclient.execute(post); HttpEntity ent = resp.getEntity(); System.out.println("----------------------------------------"); if (ent != null) { System.out.println("Response content length: " + ent.getContentLength()); InputStream i = ent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(ent); /////////////////////////////////////////////////// //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home"); System.out.println("executing request" + gethome.getRequestLine()); HttpResponse gresp = httpclient.execute(gethome); HttpEntity gent = gresp.getEntity(); System.out.println("----------------------------------------"); if (ent != null) { System.out.println("Response content length: " + gent.getContentLength()); InputStream i = gent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { //System.out.println(line); } br.close(); i.close(); } ///////////////////////////////////////////////////////////////////////////////// //Parse Bulletin String results = getToken(bulletinpage, "RESULTS HEADER", "Additional Searches"); String[] depts = results.split("href"); //SPLIT FOR EACH DEPARTMENT LINK, ITERATE boolean ready = false; for (int i = 1; i < depts.length; i++) { //EXTRACT LINK, DEPARTMENT NAME AND ABBREVIATION String dept = new String(depts[i]); String abbr = getToken(dept, "(", ")"); String name = getToken(dept, ">", "("); name.trim(); //System.out.println(tagger.tagString(name)); String link = getToken(dept, "=\"", "\">"); System.out.println(name + " : " + abbr + " : " + link); System.out.println("======================================================================"); if (i <= 10 || i >= 127) //values to keep it to undergraduate courses. Excludes law, med, business, overseas continue; /*if (i<=46) continue; */ //Start at BIOHOP /*if (abbr.equals("INTNLREL")) ready = true; if (!ready) continue;*/ //Construct department course search URL //Then request page String URL = "http://explorecourses.stanford.edu/" + link + "&filter-term-Autumn=on&filter-term-Winter=on&filter-term-Spring=on"; httpget = new HttpGet(URL); //System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); //ystem.out.println("----------------------------------------"); //System.out.println(response.getStatusLine()); String rpage = ""; if (entity != null) { //System.out.println("Response content length: " + entity.getContentLength()); InputStream in = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { rpage += line; //System.out.println(line); } br.close(); in.close(); } EntityUtils.consume(entity); //Process results page List<Course> deptCourses = new ArrayList<Course>(); List<Course> result = processResultPage(rpage); deptCourses.addAll(result); //While there are more result pages, keep going boolean more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299)); boolean morepages = anotherPage(rpage); while (morepages && more) { URL = nextURL(URL); httpget = new HttpGet(URL); //System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); //System.out.println("----------------------------------------"); //System.out.println(response.getStatusLine()); rpage = ""; if (entity != null) { //System.out.println("Response content length: " + entity.getContentLength()); InputStream in = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { rpage += line; //System.out.println(line); } br.close(); in.close(); } EntityUtils.consume(entity); morepages = anotherPage(rpage); result = processResultPage(rpage); deptCourses.addAll(result); more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299)); /*String mores = more? "yes": "no"; String pagess = morepages?"yes":"no"; System.out.println("more: "+mores+" morepages: "+pagess); System.out.println("more");*/ } //Get course ratings for all department courses via courserank deptCourses = getRatings(httpclient, abbr, deptCourses); for (int j = 0; j < deptCourses.size(); j++) { Course c = deptCourses.get(j); System.out.println("" + c.title + " : " + c.rating); c.tags = name; c.code = c.code.trim(); c.department = name; c.deptAB = abbr; c.writeToDatabase(); //System.out.println(tagger.tagString(c.title)); } } if (!page.equals("")) return; /////////////////////////////////////////////////// //Get Course Bulletin Department courses /* httpget = new HttpGet("https://courserank.com/stanford/main"); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); page = ""; if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); InputStream i = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { page +=line; //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(entity); //////////////////////////////////////////////////// //POST REQUEST LOGIN HttpPost post = new HttpPost("https://www.courserank.com/stanford/main"); List<NameValuePair> pairs = new ArrayList<NameValuePair>(2); pairs.add(new BasicNameValuePair("RT", "")); pairs.add(new BasicNameValuePair("action", "login")); pairs.add(new BasicNameValuePair("password", "trespass")); pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu")); post.setEntity(new UrlEncodedFormEntity(pairs)); System.out.println("executing request" + post.getRequestLine()); HttpResponse resp = httpclient.execute(post); HttpEntity ent = resp.getEntity(); System.out.println("----------------------------------------"); if (ent != null) { System.out.println("Response content length: " + ent.getContentLength()); InputStream i = ent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(ent); /////////////////////////////////////////////////// //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home"); System.out.println("executing request" + gethome.getRequestLine()); HttpResponse gresp = httpclient.execute(gethome); HttpEntity gent = gresp.getEntity(); System.out.println("----------------------------------------"); if (ent != null) { System.out.println("Response content length: " + gent.getContentLength()); InputStream i = gent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { //System.out.println(line); } br.close(); i.close(); } //////////////////////////////////////// //GETS FIRST PAGE OF RESULTS EntityUtils.consume(gent); post = new HttpPost("https://www.courserank.com/stanford/search_results"); pairs = new ArrayList<NameValuePair>(2); pairs.add(new BasicNameValuePair("filter_term_currentYear", "on")); pairs.add(new BasicNameValuePair("query", "")); post.setEntity(new UrlEncodedFormEntity(pairs)); System.out.println("executing request" + post.getRequestLine()); resp = httpclient.execute(post); ent = resp.getEntity(); System.out.println("----------------------------------------"); String rpage = ""; if (ent != null) { System.out.println("Response content length: " + ent.getContentLength()); InputStream i = ent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { rpage += line; System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(ent); //////////////////////////////////////////////////// //PARSE FIRST PAGE OF RESULTS //int index = rpage.indexOf("div class=\"searchItem"); String []classSplit = rpage.split("div class=\"searchItem"); for (int i=1; i<classSplit.length; i++) { String str = classSplit[i]; //ID String CID = getToken(str, "course?id=","\">"); // CODE String CODE = getToken(str,"class=\"code\">" ,":</"); //TITLE String NAME = getToken(str, "class=\"title\">","</"); //DESCRIP String DES = getToken(str, "class=\"description\">","</"); //TERM String TERM = getToken(str, "Terms:", "|"); //UNITS String UNITS = getToken(str, "Units:", "<br/>"); //WORKLOAD String WLOAD = getToken(str, "Workload:", "|"); //GER String GER = getToken(str, "GERs:", "</d"); //RATING int searchIndex = 0; float rating = 0; while (true) { int ratingIndex = str.indexOf("large_Full", searchIndex); if (ratingIndex ==-1) { int halfratingIndex = str.indexOf("large_Half", searchIndex); if (halfratingIndex == -1) break; else rating += .5; break; } searchIndex = ratingIndex+1; rating++; } String RATING = ""+rating; //GRADE String GRADE = getToken(str, "div class=\"unofficialGrade\">", "</"); if (GRADE.equals("NOT FOUND")) { GRADE = getToken(str, "div class=\"officialGrade\">", "</"); } //REVIEWS String REVIEWS = getToken(str, "class=\"ratings\">", " ratings"); System.out.println(""+CODE+" : "+NAME + " : "+CID); System.out.println("----------------------------------------"); System.out.println("Term: "+TERM+" Units: "+UNITS+ " Workload: "+WLOAD + " Grade: "+ GRADE); System.out.println("Rating: "+RATING+ " Reviews: "+REVIEWS); System.out.println("=========================================="); System.out.println(DES); System.out.println("=========================================="); } /////////////////////////////////////////////////// //GETS SECOND PAGE OF RESULTS post = new HttpPost("https://www.courserank.com/stanford/search_results"); pairs = new ArrayList<NameValuePair>(2); pairs.add(new BasicNameValuePair("filter_term_currentYear", "on")); pairs.add(new BasicNameValuePair("page", "2")); pairs.add(new BasicNameValuePair("query", "")); post.setEntity(new UrlEncodedFormEntity(pairs)); System.out.println("executing request" + post.getRequestLine()); resp = httpclient.execute(post); ent = resp.getEntity(); System.out.println("----------------------------------------"); if (ent != null) { System.out.println("Response content length: " + ent.getContentLength()); InputStream i = ent.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { //System.out.println(line); } br.close(); i.close(); } EntityUtils.consume(ent); /* httpget = new HttpGet("https://github.com/"); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); page = ""; if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); InputStream i = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(i)); String line; while ((line = br.readLine()) != null) { page +=line; System.out.println(line); } br.close(); i.close(); }*/ EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:it.geosolutions.sfs.web.Start.java
public static void main(String[] args) { final Server jettyServer = new Server(); try {//from w w w. j a va2s . c o m SocketConnector conn = new SocketConnector(); String portVariable = System.getProperty("jetty.port"); int port = parsePort(portVariable); if (port <= 0) port = 8082; conn.setPort(port); conn.setAcceptQueueSize(100); conn.setMaxIdleTime(1000 * 60 * 60); conn.setSoLingerTime(-1); // Use this to set a limit on the number of threads used to respond requests // BoundedThreadPool tp = new BoundedThreadPool(); // tp.setMinThreads(8); // tp.setMaxThreads(8); // conn.setThreadPool(tp); // SSL host name given ? String sslHost = System.getProperty("ssl.hostname"); SslSocketConnector sslConn = null; // if (sslHost!=null && sslHost.length()>0) { // Security.addProvider(new BouncyCastleProvider()); // sslConn = getSslSocketConnector(sslHost); // } if (sslConn == null) { jettyServer.setConnectors(new Connector[] { conn }); } else { conn.setConfidentialPort(sslConn.getPort()); jettyServer.setConnectors(new Connector[] { conn, sslConn }); } /*Constraint constraint = new Constraint(); constraint.setName(Constraint.__BASIC_AUTH);; constraint.setRoles(new String[]{"user","admin","moderator"}); constraint.setAuthenticate(true); ConstraintMapping cm = new ConstraintMapping(); cm.setConstraint(constraint); cm.setPathSpec("/*"); SecurityHandler sh = new SecurityHandler(); sh.setUserRealm(new HashUserRealm("MyRealm","/Users/jdeolive/realm.properties")); sh.setConstraintMappings(new ConstraintMapping[]{cm}); WebAppContext wah = new WebAppContext(sh, null, null, null);*/ WebAppContext wah = new WebAppContext(); wah.setContextPath("/sfs"); wah.setWar("src/main/webapp"); jettyServer.setHandler(wah); wah.setTempDirectory(new File("target/work")); //this allows to send large SLD's from the styles form wah.getServletContext().getContextHandler().setMaxFormContentSize(1024 * 1024 * 2); String jettyConfigFile = System.getProperty("jetty.config.file"); if (jettyConfigFile != null) { // log.info("Loading Jetty config from file: " + jettyConfigFile); (new XmlConfiguration(new FileInputStream(jettyConfigFile))).configure(jettyServer); } jettyServer.start(); /* * Reads from System.in looking for the string "stop\n" in order to gracefully terminate * the jetty server and shut down the JVM. This way we can invoke the shutdown hooks * while debugging in eclipse. Can't catch CTRL-C to emulate SIGINT as the eclipse * console is not propagating that event */ Thread stopThread = new Thread() { @Override public void run() { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; try { while (true) { line = reader.readLine(); if ("stop".equals(line)) { jettyServer.stop(); System.exit(0); } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }; stopThread.setDaemon(true); stopThread.run(); // use this to test normal stop behaviour, that is, to check stuff that // need to be done on container shutdown (and yes, this will make // jetty stop just after you started it...) // jettyServer.stop(); } catch (Exception e) { // log.log(Level.SEVERE, "Could not start the Jetty server: " + e.getMessage(), e); if (jettyServer != null) { try { jettyServer.stop(); } catch (Exception e1) { // log.log(Level.SEVERE, // "Unable to stop the " + "Jetty server:" + e1.getMessage(), e1); } } } }
From source file:com.yahoo.semsearch.fastlinking.EntityContextFastEntityLinker.java
/** * Context-aware command line entity linker * @param args arguments (see -help for further info) * @throws Exception/*from www .jav a2s. c o m*/ */ public static void main(String args[]) throws Exception { SimpleJSAP jsap = new SimpleJSAP(EntityContextFastEntityLinker.class.getName(), "Interactive mode for entity linking", new Parameter[] { new FlaggedOption("hash", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'h', "hash", "quasi succint hash"), new FlaggedOption("vectors", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'v', "vectors", "Word vectors file"), new FlaggedOption("labels", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'l', "labels", "File containing query2entity labels"), new FlaggedOption("id2type", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'i', "id2type", "File with the id2type mapping"), new Switch("centroid", 'c', "centroid", "Use centroid-based distances and not LR"), new FlaggedOption("map", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'm', "map", "Entity 2 type mapping "), new FlaggedOption("threshold", JSAP.STRING_PARSER, "-20", JSAP.NOT_REQUIRED, 'd', "threshold", "Score threshold value "), new FlaggedOption("entities", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'e', "entities", "Entities word vectors file"), }); JSAPResult jsapResult = jsap.parse(args); if (jsap.messagePrinted()) return; double threshold = Double.parseDouble(jsapResult.getString("threshold")); QuasiSuccinctEntityHash hash = (QuasiSuccinctEntityHash) BinIO.loadObject(jsapResult.getString("hash")); EntityContext queryContext; if (!jsapResult.getBoolean("centroid")) { queryContext = new LREntityContext(jsapResult.getString("vectors"), jsapResult.getString("entities"), hash); } else { queryContext = new CentroidEntityContext(jsapResult.getString("vectors"), jsapResult.getString("entities"), hash); } HashMap<String, ArrayList<EntityRelevanceJudgment>> labels = null; if (jsapResult.getString("labels") != null) { labels = readTrainingData(jsapResult.getString("labels")); } String map = jsapResult.getString("map"); HashMap<String, String> entities2Type = null; if (map != null) entities2Type = readEntity2IdFile(map); EntityContextFastEntityLinker linker = new EntityContextFastEntityLinker(hash, queryContext); final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String q; for (;;) { System.out.print(">"); q = br.readLine(); if (q == null) { System.err.println(); break; // CTRL-D } if (q.length() == 0) continue; long time = -System.nanoTime(); try { List<EntityResult> results = linker.getResults(q, threshold); //List<EntityResult> results = linker.getResultsGreedy( q, 5 ); //int rank = 0; for (EntityResult er : results) { if (entities2Type != null) { String name = er.text.toString().trim(); String newType = entities2Type.get(name); if (newType == null) newType = "NF"; System.out.println(q + "\t span: \u001b[1m [" + er.text + "] \u001b[0m eId: " + er.id + " ( t= " + newType + ")" + " score: " + er.score + " ( " + er.s.span + " ) "); //System.out.println( newType + "\t" + q + "\t" + StringUtils.remove( q, er.s.span.toString() ) + " \t " + er.text ); break; /* } else { System.out.print( "[" + er.text + "(" + String.format("%.2f",er.score) +")] "); System.out.println( "span: \u001b[1m [" + er.text + "] \u001b[0m eId: " + er.id + " ( t= " + typeMapping.get( hash.getEntity( er.id ).type ) + " score: " + er.score + " ( " + er.s.span + " ) " ); } rank++; */ } else { if (labels == null) { System.out.println(q + "\t" + er.text + "\t" + er.score); } else { ArrayList<EntityRelevanceJudgment> jds = labels.get(q); String label = "NF"; if (jds != null) { EntityRelevanceJudgment relevanceOfEntity = relevanceOfEntity(er.text, jds); label = relevanceOfEntity.label; } System.out.println(q + "\t" + er.text + "\t" + label + "\t" + er.score); break; } } System.out.println(); } time += System.nanoTime(); System.out.println("Time to rank and print the candidates:" + time / 1000000. + " ms"); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.sencko.basketball.stats.advanced.FIBAJsonParser.java
public static void main(String[] args) throws Exception { //http://nalb.bg/%D1%81%D0%BB%D0%B5%D0%B4%D0%B5%D1%82%D0%B5-%D0%BC%D0%B0%D1%87%D0%BE%D0%B2%D0%B5%D1%82%D0%B5-%D0%BE%D1%82-%D0%BD%D0%B0%D0%BB%D0%B1-%D0%BD%D0%B0-%D0%B6%D0%B8%D0%B2%D0%BE/ //http://nalb.bg/%D1%81%D0%BB%D0%B5%D0%B4%D0%B5%D1%82%D0%B5-%D0%B4%D0%B2%D1%83%D0%B1%D0%BE%D0%B8%D1%82%D0%B5-%D0%BE%D1%82-%D0%BD%D0%B0%D0%BB%D0%B1-%D0%BD%D0%B0-%D0%B6%D0%B8%D0%B2%D0%BE-%D0%B8-%D0%B2-%D0%BF%D0%B5%D1%82/ //http://nalb.bg/%D1%81%D0%BB%D0%B5%D0%B4%D0%B5%D1%82%D0%B5-%D0%B4%D0%B2%D1%83%D0%B1%D0%BE%D0%B8%D1%82%D0%B5-%D0%BE%D1%82-%D0%BD%D0%B0%D0%BB%D0%B1-%D0%B8-%D0%B2-%D1%87%D0%B5%D1%82%D0%B2%D1%8A%D1%80%D1%82%D0%B8%D1%8F/ builder = new GsonBuilder().registerTypeAdapter(Integer.class, new IntegerAdapter()) .registerTypeAdapter(String.class, new StringAdapter()) .registerTypeAdapter(Action.class, new ActionAdapter()).setPrettyPrinting(); BufferedReader reader = new BufferedReader( new InputStreamReader(FIBAJsonParser.class.getResourceAsStream("/games.txt"))); String location = null;/*from w w w. java2 s .c o m*/ while ((location = reader.readLine()) != null) { location = location.trim(); try { readLocation(location); } catch (Exception ex) { ex.printStackTrace(); } } /* readLocation("48965/13/13/23/68uR30BQLmzJM"); readLocation("48965/13/15/86/78CJrCjRx5mh6"); readLocation("48965/13/13/18/12pOKqzKs5nE"); readLocation("48965/13/29/11/33upB2PIPn3MI"); readLocation("48965/13/13/21/38Gqht27dPT1"); readLocation("48965/13/15/84/999JtqK7Eao"); readLocation("48965/13/29/07/66ODts76QU17A"); */ }
From source file:com.arsdigita.util.parameter.ParameterPrinter.java
public static final void main(final String[] args) throws IOException { CommandLine line = null;// w ww.ja va 2 s . c o m try { line = new PosixParser().parse(OPTIONS, args); } catch (ParseException e) { System.err.println(e.getMessage()); System.exit(1); } String[] outFile = line.getArgs(); if (outFile.length != 1) { System.out.println("Usage: ParameterPrinter [--html] [--file config-list-file] output-file"); System.exit(1); } if (line.hasOption("usage")) { System.out.println("Usage: ParameterPrinter [--html] [--file config-list-file] output-file"); System.exit(0); } if (line.hasOption("file")) { String file = line.getOptionValue("file"); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String configClass; while ((configClass = reader.readLine()) != null) { register(configClass); } } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } } else { register("com.arsdigita.runtime.RuntimeConfig"); register("com.arsdigita.web.WebConfig"); register("com.arsdigita.templating.TemplatingConfig"); register("com.arsdigita.kernel.KernelConfig"); register("com.arsdigita.kernel.security.SecurityConfig"); register("com.arsdigita.mail.MailConfig"); register("com.arsdigita.versioning.VersioningConfig"); register("com.arsdigita.search.SearchConfig"); register("com.arsdigita.search.lucene.LuceneConfig"); register("com.arsdigita.kernel.security.SecurityConfig"); register("com.arsdigita.bebop.BebopConfig"); register("com.arsdigita.dispatcher.DispatcherConfig"); register("com.arsdigita.workflow.simple.WorkflowConfig"); register("com.arsdigita.cms.ContentSectionConfig"); } if (line.hasOption("html")) { final StringWriter sout = new StringWriter(); final PrintWriter out = new PrintWriter(sout); writeXML(out); final XSLTemplate template = new XSLTemplate( ParameterPrinter.class.getResource("ParameterPrinter_html.xsl")); final Source source = new StreamSource(new StringReader(sout.toString())); final Result result = new StreamResult(new File(outFile[0])); template.transform(source, result); } else { final PrintWriter out = new PrintWriter(new FileWriter(outFile[0])); writeXML(out); } }
From source file:OpenDataExtractor.java
public static void main(String[] args) { System.out.println("\nSeleziona in che campo fare la ricerca: "); System.out.println("Elenco completo delle gare in formato (1)"); // cc16106a-1a65-4c34-af13-cc045d181452 System.out.println("Composizione delle commissioni giudicatrici (2)"); // c90f1ffb-c315-4f59-b0e3-b0f2f8709127 System.out.println("Registro delle imprese escluse dalle gare (3)"); // 2ea798cc-1f52-4fc8-a28e-f92a6f409cb8 System.out.println("Registro delle imprese invitate alle gare (4)"); // a124b6af-ae31-428a-8ac5-bb341feb3c46 System.out.println("Registro delle imprese che hanno partecipato alle gare (5)");// e58396cf-1145-4cb1-84a4-34311238a0c6 System.out.println("Anagrafica completa dei contratti per l'acquisizione di beni e servizi (6)"); // aa6a8664-5ef5-43eb-a563-910e25161798 System.out.println("Fornitori (7)"); // 253c8ac9-8335-4425-84c5-4a90be863c00 System.out.println("Autorizzazioni subappalti per i lavori (8)"); // 4f9ba542-5768-4b39-a92a-450c45eabd5d System.out.println("Aggiudicazioni delle gare per i lavori (9)"); // 34e298ad-3a99-4feb-9614-b9c4071b9d8e System.out.println("Dati cruscotto lavori per area (10)"); // 1ee3ea1b-58e3-48bf-8eeb-36ac313eeaf8 System.out.println("Dati cruscotto lavori per lotto (11)"); // cbededce-269f-48d2-8c25-2359bf246f42 int count = 0; int scelta;/*from w w w.j a va2s. c o m*/ String id_ref = ""; Scanner scanner; try { do { scanner = new Scanner(System.in); scelta = scanner.nextInt(); switch (scelta) { case 1: id_ref = "cc16106a-1a65-4c34-af13-cc045d181452&q="; break; case 2: id_ref = "c90f1ffb-c315-4f59-b0e3-b0f2f8709127&q="; break; case 3: id_ref = "2ea798cc-1f52-4fc8-a28e-f92a6f409cb8&q="; break; case 4: id_ref = "a124b6af-ae31-428a-8ac5-bb341feb3c46&q="; break; case 5: id_ref = "e58396cf-1145-4cb1-84a4-34311238a0c6&q="; break; case 6: id_ref = "aa6a8664-5ef5-43eb-a563-910e25161798&q="; break; case 7: id_ref = "253c8ac9-8335-4425-84c5-4a90be863c00&q="; break; case 8: id_ref = "4f9ba542-5768-4b39-a92a-450c45eabd5d&q="; break; case 9: id_ref = "34e298ad-3a99-4feb-9614-b9c4071b9d8e&q="; break; case 10: id_ref = "1ee3ea1b-58e3-48bf-8eeb-36ac313eeaf8&q="; break; case 11: id_ref = "cbededce-269f-48d2-8c25-2359bf246f42&q="; break; default: System.out.println("Numero non selezionabile"); System.out.println("Reinserisci"); break; } } while (scelta <= 0 || scelta > 11); scanner = new Scanner(System.in); System.out.println("Inserisci un parametro di ricerca: "); String record = scanner.nextLine(); String limit = "&limit=10000"; System.out.println("id di riferimento: " + id_ref); System.out.println("___________________________"); String requestString = "http://dati.openexpo2015.it/catalog/api/action/datastore_search?resource_id=" + id_ref + record + limit; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(requestString); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String result = ""; String resline = ""; while ((resline = rd.readLine()) != null) { result += resline; } if (result != null) { JSONObject jsonObject = new JSONObject(result); JSONObject resultJson = (JSONObject) jsonObject.get("result"); JSONArray resultJsonFields = (JSONArray) resultJson.get("fields"); ArrayList<TypeFields> type = new ArrayList<TypeFields>(); TypeFields type1; JSONObject temp; while (count < resultJsonFields.length()) { temp = (JSONObject) resultJsonFields.get(count); type1 = new TypeFields(temp.getString("id"), temp.getString("type")); type.add(type1); count++; } JSONArray arr = (JSONArray) resultJson.get("records"); count = 0; while (count < arr.length()) { System.out.println("Entry numero: " + count); temp = (JSONObject) arr.get(count); for (TypeFields temp2 : type) { System.out.println(temp2.nameType + ": " + temp.get(temp2.nameType)); } count++; System.out.println("--------------------------"); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:drmaas.sandbox.http.LoginTest.java
public static void main(String[] args) throws Exception { //1. For SSL/* w w w . jav a 2s. c o m*/ DefaultHttpClient base = new DefaultHttpClient(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier verifier = new X509HostnameVerifier() { @Override public void verify(String string, SSLSocket ssls) throws IOException { } @Override public void verify(String string, X509Certificate xc) throws SSLException { } @Override public void verify(String string, String[] strings, String[] strings1) throws SSLException { } @Override public boolean verify(String string, SSLSession ssls) { return true; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, verifier); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); DefaultHttpClient httpclient = new DefaultHttpClient(ccm, base.getParams()); httpclient.setRedirectStrategy(new LaxRedirectStrategy()); try { HttpPost httpost; HttpResponse response; HttpEntity entity; List<Cookie> cookies; BufferedReader rd; String line; List<NameValuePair> nvps = new ArrayList<NameValuePair>(); //log in httpost = new HttpPost("myloginurl"); nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("login", "Log In")); nvps.add(new BasicNameValuePair("os_username", "foo")); nvps.add(new BasicNameValuePair("os_password", "foobar")); nvps.add(new BasicNameValuePair("os_cookie", "true")); nvps.add(new BasicNameValuePair("os_destination", "")); httpost.setEntity(new UrlEncodedFormEntity(nvps)); response = httpclient.execute(httpost); System.out.println(response.toString()); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }