List of usage examples for java.util Scanner hasNext
public boolean hasNext()
From source file:com.rockhoppertech.music.scale.Scale.java
/** * @param name/* w w w .j ava 2 s . c o m*/ * the name to set */ public void setName(final String name) { if (name.contains(",")) { Scanner s = new Scanner(name); s.useDelimiter(","); this.name = s.next(); while (s.hasNext()) { aliases.add(s.next()); } s.close(); } else { this.name = name; } }
From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java
private HashMap<String, String> parseEpub(File file) throws IOException, TMFVisibleException { LOG.debug("[parseEpub] - BEGIN"); ZipFile fi = new ZipFile(file); for (Enumeration e = fi.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); if (entry.getName().endsWith("ncx")) { InputStream tocMaybeDirty = fi.getInputStream(entry); Scanner scanner = new Scanner(tocMaybeDirty, "UTF-8").useDelimiter("\\A"); String theString = scanner.hasNext() ? scanner.next() : ""; tocMaybeDirty.close();/*from w w w.j av a 2 s . c o m*/ scanner.close(); String res = theString.replaceAll(">[\\s]*?<", "><"); InputStream toc = new ByteArrayInputStream(res.getBytes(StandardCharsets.UTF_8)); try { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(toc); toc.close(); if (doc.hasChildNodes()) { findNavMap(doc.getChildNodes()); } } catch (Exception ex) { LOG.error("Unable to navigate the TOC"); } removeEmptyTOC(epub); //search anchors in links and split Set set = epub.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); if (me.getValue().toString().contains("#")) { String[] parts = me.getValue().toString().split("#"); String anchor = parts[1]; epub.put(me.getKey().toString(), anchor); } } } if (entry.getName().endsWith("opf")) { //manage files because order is important InputStream content = fi.getInputStream(entry); Scanner scanner = new Scanner(content, "UTF-8").useDelimiter("\\A"); String contentString = scanner.hasNext() ? scanner.next() : ""; content.close(); scanner.close(); String filenameRegex = "href=\"(.*.htm(|l))\".*media-type=\"application/xhtml"; Pattern pattern = Pattern.compile(filenameRegex); Matcher matcher = pattern.matcher(contentString); Integer count = 0; while (matcher.find()) { files.put(count, matcher.group(1)); count++; } } if (entry.getName().endsWith("html") || entry.getName().endsWith("htm") || entry.getName().endsWith("xhtml")) { InputStream htmlFile = fi.getInputStream(entry); Scanner scanner = new Scanner(htmlFile, "UTF-8").useDelimiter("\\A"); String htmlString = scanner.hasNext() ? scanner.next() : ""; String regex1 = htmlString.replaceAll("^[^_]*?<body>", ""); //remove head String regex2 = regex1.replaceAll("</body>.*$", ""); //remove tail String htmlCleaned = regex2.replaceAll("<a.*?/>", ""); //anchor with one tag String[] bits = entry.getName().split("/"); String fileName = bits[bits.length - 1]; htmls.put(fileName, htmlCleaned); } } fi.close(); Integer i; for (i = 0; i < files.size(); i++) { stringBuilder.append("<p id=\"" + files.get(i) + "\"></p>"); // "anchor" also the heads of each files stringBuilder.append(htmls.get(files.get(i))); } String htmlAll = stringBuilder.toString(); /* We have all needed files, start to split For each link -> made a chunk Start from the bottom */ Metadata metadata = new Metadata(); Parser parser = new HtmlParser(); ListIterator<Map.Entry<String, String>> iter = new ArrayList<>(epub.entrySet()).listIterator(epub.size()); while (iter.hasPrevious()) { Map.Entry<String, String> me = iter.previous(); try { ContentHandler contenthandler = new BodyContentHandler(10 * htmlAll.length()); Scanner sc = new Scanner(htmlAll); sc.useDelimiter("id=\"" + me.getValue().toString() + "\">"); htmlAll = sc.next(); InputStream stream = new ByteArrayInputStream(sc.next().getBytes(StandardCharsets.UTF_8)); parser.parse(stream, contenthandler, metadata, new ParseContext()); String chapterText = contenthandler.toString().toLowerCase().replaceAll("\\d+.*", ""); String chapterTextWithoutNo = chapterText.replaceAll("\\d+.*", ""); // Remove the Project Gutenberg meta information from the text String chapterTextCleaned = chapterTextWithoutNo.split("end of the project gutenberg ebook")[0]; epub.put(me.getKey().toString(), chapterTextCleaned); } catch (Exception ex) { LOG.error("Unable to parse content for index: " + me.getKey() + ", this chapter will be deleted"); removeChapter(epub, me.getKey().toString()); } } /* I remove the Project Gutenberg license chapter from the Map, because it is useless for the classification and it generates a Lucene Exception in case of the Italian language (the license text is always in English). You can use this method in order to remove each chapter that is useless for classifying your Epub document. */ removeChapter(epub, "A Word from Project Gutenberg"); removeEmptyItems(epub); //If the Epub file has a bad structure, I try to use the basic Epub extractor of Tika. if (epub.size() == 0) { LOG.info("The Epub file has a bad structure. Try to use the Tika extractor"); epub.put("All text", autoParseAll(file)); } removeEmptyItems(epub); if (epub.size() == 0) { LOG.error("Unable to extract text from this Epub"); throw new TMFVisibleException("Unable to extract any text from this Epub."); } removeDownloadedFile(TEMPORARY_PATH); LOG.debug("[parseEpub] - END"); return epub; }
From source file:com.ibm.devops.dra.AbstractDevOpsAction.java
/** * Get the build number//from ww w . ja v a 2 s . com * @param build * @return */ public String getBuildNumber(String jobName, Run build) { String jName = ""; Scanner s = new Scanner(jobName).useDelimiter("/"); while (s.hasNext()) { // this will loop through the string until the last string(job name) is reached. jName = s.next(); } s.close(); String buildNumber = jName + ":" + build.getNumber(); return buildNumber; }
From source file:org.mifos.sdk.client.internal.RestClientService.java
/** * Retrieves a client image./*from w w w .ja v a 2s .co m*/ * @param clientId the client ID * @param maxWidth Optional: the maximum width of the image * @param maxHeight Optional: the maximum height of the image * @return a {@link ClientImage} with the image and the type if found, null otherwise * @throws MifosXConnectException * @throws MifosXResourceException */ public ClientImage findImage(Long clientId, Long maxWidth, Long maxHeight) throws MifosXConnectException, MifosXResourceException { Preconditions.checkNotNull(clientId); final RestAdapter adapter = new RestAdapter.Builder().setClient(new OkClient(new OkHttpClient())) .setEndpoint(this.connectionProperties.getUrl()).setConverter(new Converter() { @Override public Object fromBody(TypedInput body, Type type) throws ConversionException { try { Scanner s = new Scanner(body.in()).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } catch (IOException e) { } return null; } @Override public TypedOutput toBody(Object object) { return null; } }).build(); final RetrofitClientService clientService = adapter.create(RetrofitClientService.class); ClientImage clientImage = null; try { final String responseString = clientService.findImage(this.authenticationKey, this.connectionProperties.getTenant(), clientId, maxWidth, maxHeight); final byte[] responseBinaryData = Base64.decodeBase64(responseString.split(",")[1]); try { final ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBinaryData); final BufferedImage image = ImageIO.read(inputStream); ClientImage.Type type = null; if (responseString.startsWith("data:image/png")) { type = ClientImage.Type.PNG; } else if (responseString.startsWith("data:image/jpeg")) { type = ClientImage.Type.JPEG; } else if (responseString.startsWith("data:image/gif")) { type = ClientImage.Type.GIF; } clientImage = ClientImage.image(image).type(type).build(); } catch (IOException e) { throw new IllegalStateException(e); } } catch (RetrofitError error) { if (error.getKind() == RetrofitError.Kind.NETWORK) { throw new MifosXConnectException(ErrorCode.NOT_CONNECTED); } else if (error.getKind() == RetrofitError.Kind.CONVERSION || error.getResponse().getStatus() == 401) { throw new MifosXConnectException(ErrorCode.INVALID_AUTHENTICATION_TOKEN); } else if (error.getResponse().getStatus() == 404) { throw new MifosXResourceException(ErrorCode.CLIENT_IMAGE_NOT_FOUND); } else if (error.getResponse().getStatus() == 403) { final String message = ServerResponseUtil.parseResponse(error.getResponse()); throw new MifosXResourceException(message); } else { throw new MifosXConnectException(ErrorCode.UNKNOWN); } } return clientImage; }
From source file:org.springfield.lou.servlet.LouServlet.java
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.addHeader("Access-Control-Allow-Origin", alloworigin); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.addHeader("Access-Control-Allow-Headers", "Content-Type,Range,If-None-Match,Accept-Ranges"); response.addHeader("Access-Control-Expose-Headers", "Content-Range"); //read the data from the put request //System.out.println("PUT REQ="+request.getRequestURI()); String mt = request.getContentType(); if (mt.equals("application/data")) { handleFileUpload(request);/* w w w . j a v a2s .c om*/ return; } InputStream inst = request.getInputStream(); String data; // reads the data from inputstring to a normal string. java.util.Scanner s = new java.util.Scanner(inst).useDelimiter("\\A"); data = (s.hasNext()) ? s.next() : null; if (data == null) { return; } //System.out.println("DATA="+data); Map<String, String[]> params = request.getParameterMap(); // lets find the correct nlication Html5ApplicationInterface app = null; String url = request.getRequestURI(); int pos = url.indexOf("/domain/"); if (pos != -1) { String tappname = url.substring(pos); app = ApplicationManager.instance().getApplication(tappname); } if (data.indexOf("put(") == 0) { // System.out.println("DO PUT WAS CALLED"); app.putData(data); return; } if (data.indexOf("stop(") == 0) { //System.out.println("RECIEVED STOP FROP CLIENT"); String screenid = data.substring(5, data.length() - 1); app.removeScreen(screenid, null); return; } //build an org.w3c.dom.Document DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(data)); Document doc = builder.parse(is); //get the the user information from the xml Element root = (Element) doc.getElementsByTagName("fsxml").item(0); Element screenxml = (Element) root.getElementsByTagName("screen").item(0); String screenId = screenxml.getElementsByTagName("screenId").item(0).getTextContent(); // does this screen already have a id ? //System.out.println("SCREENID="+screenId); if (!screenId.equals("-1") && app.getScreen(screenId) != null) { // ok so we should find it and its attached app Screen screen = app.getScreen(screenId); screen.setSeen(); screen.setParameters(params); //System.out.println("OLD SCREEN = "+screen.getId()); response.setContentType("text/xml; charset=UTF-8"); OutputStream out = response.getOutputStream(); //PrintWriter out = response.getWriter(); String msg = screen.getMsg(); if (msg == null) { // bad bad bad try { synchronized (screen) { //screen.wait(); screen.wait(2 * 1000); // turned into 'highspeed' for testing so 2 seconds instead of 60, also means eddie.js change } } catch (InterruptedException e) { // System.out.println("got interrupt.. getting data"); } msg = screen.getMsg(); //System.out.println("MSG="+msg); if (msg == null) { // simulated a drop connection //System.out.println("SIM DROP"); msg = "set(synctime)=" + new Date().toString(); out.write(msg.getBytes()); out.flush(); out.close(); return; } } long starttime = new Date().getTime(); //System.out.println("data2="+msg); out.write(msg.getBytes()); out.flush(); out.close(); long endtime = new Date().getTime(); PerformanceManager.addNetworkCallTime(endtime - starttime); } else { //System.out.println("lost flow why ? screenId="+screenId+" "+app.getScreen(screenId)); if (!screenId.equals("-1")) { System.out.println("Sending stop"); response.setContentType("text/xml; charset=UTF-8"); OutputStream out = response.getOutputStream(); //PrintWriter out = response.getWriter(); out.write(XMLHelper.createScreenIdFSXML("-1", false).getBytes()); out.flush(); out.close(); } else { //System.out.println("PARAMS="+params); Capabilities caps = getCapabilities(root); // extend this with Location info caps.addCapability("ipnumber", request.getRemoteAddr()); caps.addCapability("servername", request.getServerName()); String ref = request.getHeader("Referer"); //System.out.println("REF="+ref); if (ref != null) { caps.addCapability("referer", ref); } else { caps.addCapability("referer", ""); } Screen screen = app.getNewScreen(caps, params); //System.out.println("PARAMSET="+params); screen.setParameters(params); // see if we need to override the location //String ploc = screen.getParameter("location"); //if (ploc!=null) screen.getLocation().setId(ploc); response.setContentType("text/xml; charset=UTF-8"); OutputStream out = response.getOutputStream(); out.write(XMLHelper.createScreenIdFSXML(screen.getId(), true).getBytes()); out.flush(); out.close(); } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return; }
From source file:com.tesora.dve.tools.CLIBuilder.java
private boolean hasRequiredArg(final Scanner scanner, final String argName) throws PEException { if (!scanner.hasNext()) { if (argName != null) { throw new PEException("Expecting argument '" + argName + "'"); }/*w ww . ja v a 2 s .c o m*/ return false; } return true; }
From source file:com.inmobi.conduit.local.LocalStreamServiceTest.java
private void testClusterName(String configName, String currentClusterName) throws Exception { ConduitConfigParser parser = new ConduitConfigParser(configName); ConduitConfig config = parser.getConfig(); Set<String> streamsToProcess = new HashSet<String>(); streamsToProcess.addAll(config.getSourceStreams().keySet()); Set<String> clustersToProcess = new HashSet<String>(); Set<TestLocalStreamService> services = new HashSet<TestLocalStreamService>(); Cluster currentCluster = null;//w w w . ja v a2 s . co m for (SourceStream sStream : config.getSourceStreams().values()) { for (String cluster : sStream.getSourceClusters()) { clustersToProcess.add(cluster); } } if (currentClusterName != null) { currentCluster = config.getClusters().get(currentClusterName); } for (String clusterName : clustersToProcess) { Cluster cluster = config.getClusters().get(clusterName); cluster.getHadoopConf().set("mapred.job.tracker", super.CreateJobConf().get("mapred.job.tracker")); TestLocalStreamService service = new TestLocalStreamService(config, cluster, currentCluster, new NullCheckPointProvider(), streamsToProcess); services.add(service); } for (TestLocalStreamService service : services) { FileSystem fs = service.getFileSystem(); service.preExecute(); if (currentClusterName != null) Assert.assertEquals(service.getCurrentCluster().getName(), currentClusterName); // creating a job with empty input path Path tmpJobInputPath = new Path("/tmp/job/input/path"); Map<FileStatus, String> fileListing = new TreeMap<FileStatus, String>(); Set<FileStatus> trashSet = new HashSet<FileStatus>(); // checkpointKey, CheckPointPath Table<String, String, String> checkpointPaths = HashBasedTable.create(); service.createMRInput(tmpJobInputPath, fileListing, trashSet, checkpointPaths); Job testJobConf = service.createJob(tmpJobInputPath, 1000); testJobConf.waitForCompletion(true); int numberOfCountersPerFile = 0; long sumOfCounterValues = 0; Path outputCounterPath = new Path(new Path(service.getCluster().getTmpPath(), service.getName()), "counters"); FileStatus[] statuses = fs.listStatus(outputCounterPath, new PathFilter() { public boolean accept(Path path) { return path.toString().contains("part"); } }); for (FileStatus fileSt : statuses) { Scanner scanner = new Scanner(fs.open(fileSt.getPath())); while (scanner.hasNext()) { String counterNameValue = null; try { counterNameValue = scanner.next(); String tmp[] = counterNameValue.split(ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER); Assert.assertEquals(4, tmp.length); Long numOfMsgs = Long.parseLong(tmp[3]); numberOfCountersPerFile++; sumOfCounterValues += numOfMsgs; } catch (Exception e) { LOG.error("Counters file has malformed line with counter name =" + counterNameValue + "..skipping the line", e); } } } // Should have 2 counters for each file Assert.assertEquals(NUMBER_OF_FILES * 2, numberOfCountersPerFile); // sum of all counter values should be equal to total number of messages Assert.assertEquals(NUMBER_OF_FILES * 3, sumOfCounterValues); Assert.assertEquals(testJobConf.getConfiguration().get(FS_DEFAULT_NAME_KEY), service.getCurrentCluster().getHadoopConf().get(FS_DEFAULT_NAME_KEY)); Assert.assertEquals(testJobConf.getConfiguration().get(SRC_FS_DEFAULT_NAME_KEY), service.getCluster().getHadoopConf().get(FS_DEFAULT_NAME_KEY)); if (currentCluster == null) Assert.assertEquals(testJobConf.getConfiguration().get(FS_DEFAULT_NAME_KEY), testJobConf.getConfiguration().get(SRC_FS_DEFAULT_NAME_KEY)); service.getFileSystem().delete(new Path(service.getCluster().getRootDir()), true); } }
From source file:ml.shifu.shifu.core.ConfusionMatrix.java
public void computeConfusionMatrix() throws IOException { SourceType sourceType = evalConfig.getDataSet().getSource(); List<Scanner> scanners = ShifuFileUtils.getDataScanners(pathFinder.getEvalScorePath(evalConfig, sourceType), sourceType);/* w w w . jav a 2 s. c o m*/ List<ModelResultObject> moList = new ArrayList<ModelResultObject>(); boolean isDir = ShifuFileUtils.isDir(pathFinder.getEvalScorePath(evalConfig, sourceType), sourceType); LOG.info("The size of scanner is {}", scanners.size()); int cnt = 0; for (Scanner scanner : scanners) { while (scanner.hasNext()) { if ((++cnt) % 10000 == 0) { LOG.info("Loaded " + cnt + " records."); } String[] raw = scanner.nextLine().split("\\|"); if ((!isDir) && cnt == 1) { // if the evaluation score file is the local file, skip the // first line since we add continue; } String tag = CommonUtils.trimTag(raw[targetColumnIndex]); if (StringUtils.isBlank(tag)) { if (Math.random() < 0.01) { LOG.warn("Empty target value!!"); } continue; } double weight = 1.0d; if (this.weightColumnIndex > 0) { try { weight = Double.parseDouble(raw[1]); } catch (NumberFormatException e) { // Do nothing } } double score = 0; try { score = Double.parseDouble(raw[scoreColumnIndex]); } catch (NumberFormatException e) { // user set the score column wrong ? if (Math.random() < 0.05) { LOG.warn("The score column - {} is not integer. Is score column set correctly?", raw[scoreColumnIndex]); } continue; } moList.add(new ModelResultObject(score, tag, weight)); } // release resource scanner.close(); } LOG.info("Totally loaded " + cnt + " records."); if (cnt == 0 || moList.size() == 0) { LOG.error("No score read, the EvalScore did not genernate or is null file"); throw new ShifuException(ShifuErrorCode.ERROR_EVALSCORE); } ConfusionMatrixCalculator calculator = new ConfusionMatrixCalculator(modelConfig.getPosTags(evalConfig), modelConfig.getNegTags(evalConfig), moList); BufferedWriter confMatWriter = ShifuFileUtils.getWriter( pathFinder.getEvalMatrixPath(evalConfig, evalConfig.getDataSet().getSource()), evalConfig.getDataSet().getSource()); calculator.calculate(confMatWriter); confMatWriter.close(); }
From source file:org.talend.dataprep.schema.csv.CSVFastHeaderAndTypeAnalyzer.java
/** * Performs type analysis for fields of the record at the specified index. * /*ww w .j a va2s . c o m*/ * @param i the index of the record to be analyzed. * @return the list of types of the record */ private List<Integer> setFieldType(int i) { List<Integer> result = new ArrayList<>(); String line = i < sampleLines.size() ? sampleLines.get(i) : null; if (StringUtils.isEmpty(line)) { return result; } List<String> fields = readLine(line); for (String field : fields) { Scanner scanner = new Scanner(field); scanner.useDelimiter(Character.toString(separator.getSeparator())); // called integer but we are looking for long in Java parlance if (scanner.hasNextLong()) { result.add(INTEGER); scanner.next(); } else if (scanner.hasNextDouble()) { result.add(DECIMAL); scanner.next(); } else if (scanner.hasNextBoolean()) { result.add(BOOLEAN); scanner.next(); } else { String text = scanner.hasNext() ? scanner.next() : StringUtils.EMPTY; switch (text) { case "": result.add(EMPTY); break; default: // used to detect a stable length of a field (may be it is a date or a pattern) result.add(text.length()); } } scanner.close(); } return result; }