List of usage examples for java.util Scanner next
public String next()
From source file:it.acubelab.smaph.SmaphAnnotator.java
/** * Issue the query to bing, return the json object. * /*from ww w. j av a 2 s.c om*/ * @param query * the query. * @param retryLeft * how many retry left we have (if zero, will return an empty * object in case of failure). * @return the JSON object as returned by the Bing Api. * @throws Exception * is the call to the API failed. */ private synchronized JSONObject queryBing(String query, int retryLeft) throws Exception { boolean forceCacheOverride = retryLeft < BING_RETRY; if (forceCacheOverride) Thread.sleep(1000); String accountKeyAuth = Base64.encode((bingKey + ":" + bingKey).getBytes(), 0); URL url = new URL( "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%2Bspell%2BRelatedSearch%27&Query=%27" + URLEncoder.encode(query, "utf8") + "%27&Options=%27EnableHighlighting%27&Market=%27en-US%27&Adult=%27Off%27&$format=Json"); JSONObject result = null; byte[] compressed = url2jsonCache.get(url.toExternalForm()); if (compressed != null) result = new JSONObject(SmaphUtils.decompress(compressed)); boolean cached = !forceCacheOverride && result != null; SmaphAnnotatorDebugger.out.printf("%s%s %s%n", forceCacheOverride ? "<forceCacheOverride>" : "", cached ? "<cached>" : "Querying", url); if (!cached) { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(0); connection.setRequestProperty("Authorization", "Basic " + accountKeyAuth); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Content-Type", "multipart/form-data"); connection.setUseCaches(false); if (connection.getResponseCode() != 200) { Scanner s = new Scanner(connection.getErrorStream()).useDelimiter("\\A"); System.err.printf("Got HTTP error %d. Message is: %s%n", connection.getResponseCode(), s.next()); s.close(); throw new RuntimeException("Got response code:" + connection.getResponseCode()); } Scanner s = new Scanner(connection.getInputStream()).useDelimiter("\\A"); String resultStr = s.hasNext() ? s.next() : ""; result = new JSONObject(resultStr); url2jsonCache.put(url.toExternalForm(), SmaphUtils.compress(result.toString())); increaseFlushCounter(); } if (recacheNeeded(result) && retryLeft > 0) return queryBing(query, retryLeft - 1); return result; }
From source file:org.apache.openaz.xacml.std.json.JSONResponse.java
/** * Read characters from the given <code>InputStream</code> and parse them into an XACML * {@link org.apache.openaz.xacml.api.Request} object. * * @param is// w w w . j av a2s . c o m * @return * @throws JSONStructureException */ public static Response load(InputStream is) throws JSONStructureException { // TODO - ASSUME that order of members within an object does not matter (Different from XML, in JSON // everything is handled as Maps so order does not matter) // ensure shorthand map is set up if (shorthandMap == null) { initShorthandMap(); } // ensure that we have an instance of the DataTypeFactory for generating AttributeValues by DataType if (dataTypeFactory == null) { try { dataTypeFactory = DataTypeFactory.newInstance(); if (dataTypeFactory == null) { throw new NullPointerException("No DataTypeFactory found"); } } catch (FactoryException e) { throw new JSONStructureException("Unable to find DataTypeFactory, e=" + e); } } // create a new Response object to be filled in StdMutableResponse stdMutableResponse = null; String json = null; ObjectMapper mapper = null; try { // read the inputStream into a buffer (trick found online scans entire input looking for // end-of-file) java.util.Scanner scanner = new java.util.Scanner(is); scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY); // TODO - ASSUME that any duplicated component is a bad thing (probably indicating an error in the // incoming JSON) mapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true); Map<?, ?> root = mapper.readValue(json, Map.class); // // Does the Response exist? // List<?> resultList = (List<?>) root.remove("Response"); if (resultList == null) { throw new JSONStructureException("No \"Response\" property found."); } checkUnknown("Top-level message", root); stdMutableResponse = new StdMutableResponse(); // handle each Result object for (int resultIndex = 0; resultIndex < resultList.size(); resultIndex++) { // each item should be a Map<?,?> containing a Result, otherwise it is an error Object resultObj = resultList.get(resultIndex); if (resultObj == null || !(resultObj instanceof Map)) { throw new JSONStructureException( "Response contains null Result or list instead of Result object"); } StdMutableResult stdMutableResult = new StdMutableResult(); Map<?, ?> resultMap = (Map<?, ?>) resultObj; // Must have a Decision Object decisionObject = resultMap.remove("Decision"); if (decisionObject == null) { throw new JSONStructureException("Result must have Decision"); } Decision decision = Decision.get(decisionObject.toString()); if (decision == null) { throw new JSONStructureException( "Unknown value for Decision: '" + decisionObject.toString() + "'"); } stdMutableResult.setDecision(decision); // may have Status Object statusObject = resultMap.remove("Status"); if (statusObject != null) { if (!(statusObject instanceof Map)) { throw new JSONStructureException( "Status must be an object, not type '" + statusObject.getClass().getName() + "'"); } StdMutableStatus stdMutableStatus = new StdMutableStatus(); Map<?, ?> statusMap = (Map<?, ?>) statusObject; // optional message Object messageObject = statusMap.remove("StatusMessage"); if (messageObject != null) { stdMutableStatus.setStatusMessage(messageObject.toString()); } // optional detail Object detailObject = statusMap.remove("StatusDetail"); if (detailObject != null) { StdMutableStatusDetail statusDetail = new StdMutableStatusDetail(); // TODO - PROBLEM: The JSON spec says only that the status Detail is raw XML rather // than a JSON object. Therefore we cannot discriminate what is inside the string we // just got. // TODO Fortunately there is only one thing it can be: a MissingAttributeDetail. // TODO Unfortunately the MissingAttributeDetail contains multiple optional elements // including 0 or more values, which makes it non-trivial to parse the XML // representation. // TODO Unfortunately the JSON spec does not say how the XML is formatted // (with/without whitespace, etc). // // First of all, the String is possible escaped. // // The meaning of "escaped" is defined in section 4.2.3.1 in the JSON spec // String unescapedContent = detailObject.toString().replace("\\\"", "\""); unescapedContent = unescapedContent.replace("\\\\", "\\"); // need to add a root element so that the MissingAttributeDetail elements are findable unescapedContent = "<ROOT>" + unescapedContent + "</ROOT>"; // logger.info("Escaped content: \n" + unescapedContent); Document doc = null; try (InputStream bis = new ByteArrayInputStream(unescapedContent.getBytes("UTF-8"))) { doc = DOMUtil.loadDocument(bis); } catch (Exception ex) { throw new JSONStructureException( "Unable to parse Content '" + detailObject.toString() + "'"); } // ASSUME that this can only be an array of MissingAttributeDetail. Example: // <MissingAttributeDetail // Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" // AttributeId="urn:att:xacml:resource:application:motsid" // DataType="http://www.w3.org/2001/XMLSchema#integer"> // <AttributeValue // DataType="http://www.w3.org/2001/XMLSchema#integer">56</AttributeValue> // </MissingAttributeDetail>" Element docElement = doc.getDocumentElement(); NodeList missingAttributeDetailList = docElement .getElementsByTagName("MissingAttributeDetail"); for (int madNodeIndex = 0; madNodeIndex < missingAttributeDetailList .getLength(); madNodeIndex++) { Node madNode = missingAttributeDetailList.item(madNodeIndex); StdMutableMissingAttributeDetail mutableMAD = new StdMutableMissingAttributeDetail(); NamedNodeMap attributeMap = madNode.getAttributes(); Node attributeNode = attributeMap.getNamedItem("AttributeId"); if (attributeNode == null) { throw new JSONStructureException("MissingAttributeDetail missing AttributeId"); } mutableMAD.setAttributeId(new IdentifierImpl(attributeNode.getNodeValue())); Node categoryNode = attributeMap.getNamedItem("Category"); if (categoryNode == null) { throw new JSONStructureException("MissingAttributeDetail missing Category"); } mutableMAD.setCategory(new IdentifierImpl(categoryNode.getNodeValue())); Node dataTypeNode = attributeMap.getNamedItem("DataType"); if (dataTypeNode == null) { throw new JSONStructureException("MissingAttributeDetail missing DataType"); } mutableMAD.setDataTypeId(new IdentifierImpl(dataTypeNode.getNodeValue())); Node issuerNode = attributeMap.getNamedItem("Issuer"); if (issuerNode != null) { mutableMAD.setIssuer(issuerNode.getNodeValue()); } // get any value elements NodeList childNodeList = madNode.getChildNodes(); for (int childIndex = 0; childIndex < childNodeList.getLength(); childIndex++) { Node childNode = childNodeList.item(childIndex); if (!childNode.getNodeName().equals("AttributeValue")) { continue; } Node childDataTypeNode = childNode.getAttributes().getNamedItem("DataType"); if (childDataTypeNode == null) { throw new JSONStructureException( "MissingAttributeDetail contains AttributeValue '" + childNode.getNodeValue() + "' with no DataType"); } String dataType = childDataTypeNode.getNodeValue(); // this probably is not a shorthand, but look it up anyway. The full Ids are // in the table too. Identifier valueDataTypeId = shorthandMap.get(dataType); if (valueDataTypeId == null) { throw new JSONStructureException( "MissingAttibuteDetail contains AttributeValue with unknown DataType=" + dataType); } // if Id is known then it is reasonable to do the following without checking DataType<?> valueDataType = dataTypeFactory.getDataType(valueDataTypeId); AttributeValue<?> attributeValue; try { // for some reason the value may be the value of a child of this node // rather than the value of this node itself. Node valueNode = childNode; if (valueNode.hasChildNodes()) { valueNode = valueNode.getFirstChild(); } attributeValue = valueDataType.createAttributeValue(valueNode.getNodeValue()); } catch (Exception ex) { throw new JSONStructureException( "Unable to create AttributeValue from MissingAttributeDetail AttributeValue '" + childNode.getNodeValue() + "', error was: " + ex.getMessage()); } mutableMAD.addAttributeValue(attributeValue); } statusDetail.addMissingAttributeDetail(mutableMAD); } stdMutableStatus.setStatusDetail(statusDetail); } // optional StatusCode which may contain recursive child StatusCode Object statusCodeObject = statusMap.remove("StatusCode"); if (statusCodeObject != null) { if (!(statusCodeObject instanceof Map)) { throw new JSONStructureException("StatusCode must be object"); } StatusCode statusCode = parseStatusCode((Map<?, ?>) statusCodeObject); stdMutableStatus.setStatusCode(statusCode); } checkUnknown("Status", statusMap); stdMutableResult.setStatus(stdMutableStatus); } // may have Obligations Object obligationsObject = resultMap.remove("Obligations"); if (obligationsObject != null) { parseObligationsOrAdvice(obligationsObject, stdMutableResult, true); } // may have Advice Object adviceObject = resultMap.remove("AssociatedAdvice"); if (adviceObject != null) { parseObligationsOrAdvice(adviceObject, stdMutableResult, false); } // may have Category (a.k.a Attributes) // TODO - POSSIBLE NAME CHANGE - XML core calls this "Attributes", but name in JSON standard // is questionable. // TODO The variables here are named "Attributes" because that is the internal name in our // objects (based on the Core spec). Object attributesObject = resultMap.remove("Category"); if (attributesObject != null) { if (!(attributesObject instanceof List)) { throw new JSONStructureException("Category must be list"); } List<?> attributesList = (List<?>) attributesObject; for (Object categoryObject : attributesList) { if (categoryObject == null || !(categoryObject instanceof Map)) { throw new JSONStructureException("Category array item must be object"); } Map<?, ?> categoryMap = (Map<?, ?>) categoryObject; StdMutableAttributeCategory stdMutableAttributeCategory = new StdMutableAttributeCategory(); // mandatory CategoryId Object categoryIdObject = categoryMap.remove("CategoryId"); if (categoryIdObject == null) { throw new JSONStructureException("Category array item must contain CategoryId"); } Identifier categoryId = new IdentifierImpl(categoryIdObject.toString()); stdMutableAttributeCategory.setCategory(categoryId); // optional Attributes Object attributeListObject = categoryMap.remove("Attribute"); if (attributeListObject != null) { if (!(attributeListObject instanceof List)) { throw new JSONStructureException("Category memeber Attribute must be list"); } List<?> attributeList = (List<?>) attributeListObject; // get each attribute and add to category for (Object attributeMapObject : attributeList) { if (attributeMapObject == null || !(attributeMapObject instanceof Map)) { throw new JSONStructureException( "Category member Attribute list item must be object"); } Map<?, ?> attributeMap = (Map<?, ?>) attributeMapObject; StdMutableAttribute stdMutableAttribute = new StdMutableAttribute(); // optional IncludeInResult // TODO - Odd situation!!: We are reading a string representing a Result which // includes Attributes. // TODO In this case, what does it mean if "IncludeInResult=false"? // TODO The Attribute is obviously included in this Result because it is in // the file/string we are reading. // TODO Our choice: Always include the Attribute. If the IncludeInResult is // included in the input, set it's value in the object as directed. // TODO This may cause mismatches between a Result read in and a new text // generated from the internal Result object. Object includeInResultObject = attributeMap.remove("IncludeInResult"); // the fact that the attribute is in the input means this should be true stdMutableAttribute.setIncludeInResults(true); if (includeInResultObject != null) { // need to check the value in the input try { boolean include = DataTypes.DT_BOOLEAN.convert(includeInResultObject) .booleanValue(); // set the value in the object exactly as directed, whether it makes // sense or not stdMutableAttribute.setIncludeInResults(include); } catch (DataTypeException e) { throw new JSONStructureException( "Category member Attribute list item has IncludeInResult value '" + includeInResultObject.toString() + "' which is not boolean"); } } // category is not part of Attribute in spec - it is used internally to link // attribute to Category stdMutableAttribute.setCategory(categoryId); // mandatory Id Object aaIdObject = attributeMap.remove("AttributeId"); if (aaIdObject == null) { throw new JSONStructureException( "Category member Attribute list item missing AttributeId"); } stdMutableAttribute.setAttributeId(new IdentifierImpl(aaIdObject.toString())); // get the optional DataType so we know what to do with the mandatory value Object dataTypeObject = attributeMap.remove("DataType"); Identifier dataTypeId = null; if (dataTypeObject != null) { dataTypeId = shorthandMap.get(dataTypeObject.toString()); // if there was a DataType given it must be a real one if (dataTypeId == null) { throw new JSONStructureException( "Category member Attribute list item has unknown DataType='" + dataTypeObject.toString() + "'"); } } else { // if DataType not given, use String dataTypeId = DataTypes.DT_STRING.getId(); } // mandatory Value Object valueObject = attributeMap.remove("Value"); if (valueObject == null) { throw new JSONStructureException( "Category member Attribute list item missing Value"); } AttributeValue<?> attributeValue = null; try { DataType<?> dataType = new StdDataTypeFactory().getDataType(dataTypeId); if (dataType == DataTypes.DT_XPATHEXPRESSION) { // XPAthExpressions are complex data types that need special // translation from the JSON form to the internal form attributeValue = convertMapToXPathExpression(valueObject); } else { // everything other than XPathExpressions are simple values that the // DataTypes know how to handle attributeValue = dataType.createAttributeValue(valueObject); } } catch (DataTypeException e) { throw new JSONStructureException("Category member Attribute list item Value='" + valueObject.toString() + "' not of type '" + dataTypeId + "'"); } stdMutableAttribute.addValue(attributeValue); // optional Issuer Object issuerObject = attributeMap.remove("Issuer"); if (issuerObject != null) { stdMutableAttribute.setIssuer(issuerObject.toString()); } checkUnknown("Category Attribute list item", attributeMap); stdMutableAttributeCategory.add(stdMutableAttribute); } } checkUnknown("Category", categoryMap); // if none of the attributes are returned, do not return the category either if (stdMutableAttributeCategory.getAttributes().size() > 0) { stdMutableResult.addAttributeCategory(stdMutableAttributeCategory); } } } // may have PolicyIdentifierList Object policyIdObject = resultMap.remove("PolicyIdentifier"); if (policyIdObject != null) { if (!(policyIdObject instanceof Map)) { throw new JSONStructureException("PolicyIdentifier must be object"); } Map<?, ?> policyIdMap = (Map<?, ?>) policyIdObject; // optional PolicyIdReference list Object policyIdReferenceObject = policyIdMap.remove("PolicyIdReference"); if (policyIdReferenceObject != null) { parseIdReferences(policyIdReferenceObject, stdMutableResult, false); } // optional PolicySetIdReferenceList Object policySetIdReferenceObject = policyIdMap.remove("PolicySetIdReference"); if (policySetIdReferenceObject != null) { parseIdReferences(policySetIdReferenceObject, stdMutableResult, true); } checkUnknown("PolicyIdentifier", policyIdMap); } checkUnknown("Result", resultMap); // add this result to the Response stdMutableResponse.add(stdMutableResult); } return stdMutableResponse; } catch (JsonParseException e) { throw new JSONStructureException("Unable to parse JSON '" + json + "', exception: " + e, e); } catch (JsonMappingException e) { throw new JSONStructureException("Unable to map JSON '" + json + "', exception: " + e, e); } catch (IOException e) { throw new JSONStructureException("Unable to read JSON input, exception: " + e, e); } // all done // return new StdRequest(stdMutableRequest); // throw new JSONStructureException("JSONResponse load string and load from file not implemented"); }
From source file:com.inmobi.conduit.AbstractService.java
protected Table<String, Long, Long> parseCountersFile(FileSystem fs) { List<Path> partFiles = listPartFiles(tmpCounterOutputPath, fs); if (partFiles == null || partFiles.size() == 0) { LOG.warn("No counters files generated by mapred job"); return null; }//from w ww .j a va 2 s .com Table<String, Long, Long> result = HashBasedTable.create(); for (Path filePath : partFiles) { FSDataInputStream fin = null; Scanner scanner = null; try { fin = fs.open(filePath); scanner = new Scanner(fin); while (scanner.hasNext()) { String counterNameValue = null; try { counterNameValue = scanner.next(); String tmp[] = counterNameValue.split(ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER); if (tmp.length < 4) { LOG.error("Malformed counter name,skipping " + counterNameValue); continue; } String streamFileNameCombo = tmp[0] + ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER + tmp[1]; Long publishTimeWindow = Long.parseLong(tmp[2]); Long numOfMsgs = Long.parseLong(tmp[3]); result.put(streamFileNameCombo, publishTimeWindow, numOfMsgs); } catch (Exception e) { LOG.error("Counters file has malformed line with counter name = " + counterNameValue + " ..skipping the line", e); } } } catch (IOException e1) { LOG.error("Error while opening file " + filePath + " Skipping"); continue; } finally { try { if (fin != null) { fin.close(); } if (scanner != null) { scanner.close(); } } catch (Exception e) { LOG.warn("Error while closing file " + filePath + " or scanner"); } } } return result; }
From source file:org.aprilis.jrest.compile.Compile.java
/** * //from w ww. j a v a2 s . co m */ private void loadDefinitions(short shRepositoryDepth) { try { for (File definitionFile : mfDefinitionFile.listFiles()) { if (definitionFile.getName().endsWith(Constants.gsDefFileExtension)) { // Read the contents of the definition file only upto the // first "!" character is noticed; which would be // corresponding to a definition, and repeat the same until // the time EOF is reached // boolean hasEncounteredErrors = false; FileInputStream fInputStream = new FileInputStream(definitionFile); Scanner defFileScanner = new Scanner(fInputStream); defFileScanner.useDelimiter(Constants.gsJsonDefinitionDelimiter); if (definitionFile.getName().equalsIgnoreCase(Constants.gsJrestDefinitionFileName) == false) { while (defFileScanner.hasNext()) { String jsonContent = defFileScanner.next(); if (loadJsonDefinition(jsonContent) == false) { // Something went wrong with the JSON we attempted // to parse, so, tell the user that JSON is screwed mLogger.error(String.format(Exceptions.gsParseError, definitionFile.getName(), jsonContent)); hasEncounteredErrors = true; } // if(loadJsonDefinition(jsonContent) == false) } // while(defFileScanner.hasNext()) } else { while (defFileScanner.hasNext()) { String jsonContent = defFileScanner.next(); if (loadJrestDefinition(jsonContent) == false) { // Something went wrong with the JSON we attempted // to parse, so, tell the user that JSON is screwed mLogger.error(String.format(Exceptions.gsParseError, definitionFile.getName(), jsonContent)); hasEncounteredErrors = true; } // if(loadJrestDefinition(jsonContent) == false) } // while(defFileScanner.hasNext()) } // if(parseJsonDefinition(jsonContent) == false) defFileScanner.close(); fInputStream.close(); if (shRepositoryDepth == Constants.gshMinRepoDepth) { if (hasEncounteredErrors == false) { File repoFile = new File(msPathToJrestRepo, definitionFile.getName()); if ((repoFile.exists() == true) && (repoFile.isDirectory() == false)) { repoFile.delete(); } // if ((repoFile.exists() == true) && (repoFile.isDirectory() == // false)) definitionFile.renameTo(new File(msPathToJrestRepo, definitionFile.getName())); } else { definitionFile.renameTo( new File(definitionFile.getAbsoluteFile() + Constants.gsJsonDefErrorFileExtn)); } // if (hasEncounteredErrors == false) } // if (shRepositoryDepth == Constants.gshMinRepoDepth) } // if(definitionFile.getName().endsWith(..) } // for(File definitionFile : mfDefinitionFile.listFiles()) if (shRepositoryDepth == Constants.gshMaxRepoDepth) { mbJrestRepositoryRead = true; } } catch (Exception e) { e.printStackTrace(moPrintWriter); mLogger.error(moStringWriter.toString()); } // end of try ... catch block }
From source file:com.concursive.connect.web.modules.wiki.utils.WikiToHTMLUtils.java
protected static String parseTable(WikiToHTMLContext context, Connection db, BufferedReader in, String line, StringBuffer sb) throws Exception { if (line == null) { return line; }//from ww w. j a va 2 s . com // Implement tables as... // ||header col1||header col2 // !continued|| // |colA1|colA2 // !continued // !continued| // |colB1|colB2 [[Security, Registration, Invitation|Installation Options]]| append(context, sb, "<table class=\"wikiTable\">"); append(context, sb, CRLF); int rowHighlight = 0; int rowCount = 0; // Keep track of the table's custom styles HashMap<Integer, String> cStyle = new HashMap<Integer, String>(); while (line != null && (line.startsWith("|") || line.startsWith("!"))) { // Build a complete line String lineToParse = line; while (!line.endsWith("|")) { line = in.readLine(); if (line == null) { // there is an error in the line to process return null; } if (line.startsWith("!")) { lineToParse += CRLF + line.substring(1); } } line = lineToParse; // Determine if the row can output boolean canOutput = true; // Track the row being processed ++rowCount; String cellType = null; Scanner sc = null; if (line.startsWith("||") && line.endsWith("||")) { cellType = "th"; sc = new Scanner(line).useDelimiter("[|][|]"); // sc = new Scanner(line.substring(2, line.length() - 2)).useDelimiter("[|][|]"); } else if (line.startsWith("|")) { cellType = "td"; sc = new Scanner(line.substring(1, line.length() - 1)).useDelimiter("\\|(?=[^\\]]*(?:\\[|$))"); } if (sc != null) { // Determine the column span int colSpan = 1; // Determine the cell being output int cellCount = 0; while (sc.hasNext()) { String cellData = sc.next(); if (cellData.length() == 0) { ++colSpan; continue; } // Track the cell count being output ++cellCount; if (rowCount == 1) { // Parse and validate the style input LOG.debug("Checking style value: " + cellData); if (cellData.startsWith("{") && cellData.endsWith("}")) { String[] style = cellData.substring(1, cellData.length() - 1).split(":"); String attribute = style[0].trim(); String value = style[1].trim(); if ("width".equals(attribute)) { // Validate the width style if (StringUtils.hasAllowedOnly("0123456789%.", value)) { cStyle.put(cellCount, attribute + ": " + value + ";"); } } else { LOG.debug("Unsupported style: " + cellData); } canOutput = false; } } // Output the header if (canOutput) { if (cellCount == 1) { // This is the table header row if ("td".equals(cellType)) { rowHighlight = (rowHighlight != 1 ? 1 : 2); append(context, sb, "<tr class=\"row" + rowHighlight + "\">"); } else { append(context, sb, "<tr>"); } } // Start the cell output append(context, sb, "<" + cellType); // Output the colspan if (colSpan > 1) { append(context, sb, " colspan=\"" + colSpan + "\""); } // Output any style if (cStyle.size() > 0 && rowCount == 2) { String style = cStyle.get(cellCount); if (style != null) { append(context, sb, " style=\"" + style + "\""); } } // End the cell output append(context, sb, ">"); // Output the data if (" ".equals(cellData) || "".equals(cellData)) { // Put a blank space in blank cells for output consistency append(context, sb, " "); } else { // Output the cell as a complete wiki String formatted = getHTML(context, db, cellData, true).trim(); LOG.trace(formatted); sb.append(formatted); } // Close the cell append(context, sb, "</" + cellType + ">"); } } if (canOutput) { append(context, sb, "</tr>"); append(context, sb, CRLF); } } // read another line to see if it's part of the table line = in.readLine(); } append(context, sb, "</table>"); append(context, sb, CRLF); return line; }
From source file:codeu.chat.client.commandline.Chat.java
public String doOneTestCommand(Scanner lineScanner) { final Scanner tokenScanner = new Scanner(lineScanner.nextLine()); if (!tokenScanner.hasNext()) { return ""; }/*from w ww .j a v a 2 s . c om*/ final String token = tokenScanner.next(); if (token.equals("exit")) { alive = false; } else if (token.equals("help")) { help(); } else if (token.equals("sign-in")) { if (!tokenScanner.hasNext()) { System.out.println("ERROR: No user name supplied."); } else { signInUser(tokenScanner.nextLine().trim()); // Automatically create the bot user and the conversation with the bot once a user signs in if (clientContext.user.lookup(BOT_NAME) == null) // Check whether the bot and convo already exist { addUser(BOT_NAME); clientContext.conversation.startConversation(BOT_CONVO, clientContext.user.getCurrent().id); } } } else if (token.equals("sign-out")) { if (!clientContext.user.hasCurrent()) { System.out.println("ERROR: Not signed in."); } else { signOutUser(); } } else if (token.equals("current")) { showCurrent(); } else if (token.equals("u-add")) { if (!tokenScanner.hasNext()) { System.out.println("ERROR: Username not supplied."); } else { addUser(tokenScanner.nextLine().trim()); } } else if (token.equals("u-list-all")) { showAllUsers(); } else if (token.equals("c-add")) { if (!clientContext.user.hasCurrent()) { System.out.println("ERROR: Not signed in."); } else { if (!tokenScanner.hasNext()) { System.out.println("ERROR: Conversation title not supplied."); } else { final String title = tokenScanner.nextLine().trim(); clientContext.conversation.startConversation(title, clientContext.user.getCurrent().id); } } } else if (token.equals("c-list-all")) { clientContext.conversation.showAllConversations(); } else if (token.equals("c-select")) { selectConversation(lineScanner); } else if (token.equals("m-add")) { if (!clientContext.user.hasCurrent()) { System.out.println("ERROR: Not signed in."); } else if (!clientContext.conversation.hasCurrent()) { System.out.println("ERROR: No conversation selected."); } else { if (!tokenScanner.hasNext()) { System.out.println("ERROR: Message body not supplied."); } else { final String body = tokenScanner.nextLine().trim(); clientContext.message.addMessage(clientContext.user.getCurrent().id, clientContext.conversation.getCurrentId(), body); respondAsBot(body, false); } } } else if (token.equals("m-list-all")) { if (!clientContext.conversation.hasCurrent()) { System.out.println("ERROR: No conversation selected."); } else { clientContext.message.showAllMessages(); } } else if (token.equals("m-next")) { if (!clientContext.conversation.hasCurrent()) { System.out.println("ERROR: No conversation selected."); } else if (!tokenScanner.hasNextInt()) { System.out.println("Command requires an integer message index."); } else { clientContext.message.selectMessage(tokenScanner.nextInt()); } } else if (token.equals("m-show")) { if (!clientContext.conversation.hasCurrent()) { System.out.println("ERROR: No conversation selected."); } else { final int count = (tokenScanner.hasNextInt()) ? tokenScanner.nextInt() : 1; clientContext.message.showMessages(count); } } else { System.out.format("Command not recognized: %s\n", token); System.out.format("Command line rejected: %s%s\n", token, (tokenScanner.hasNext()) ? tokenScanner.nextLine() : ""); System.out.println("Type \"help\" for help."); } tokenScanner.close(); return response; }
From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java
private String getTextFromS3(String s3BucketName, String s3Key) { GetObjectRequest getObjectRequest = new GetObjectRequest(s3BucketName, s3Key); String embeddedText = null;/*from ww w.j ava2s . c o m*/ S3Object obj = null; try { obj = clientConfiguration.getAmazonS3Client().getObject(getObjectRequest); } catch (AmazonServiceException e) { String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received."; LOG.error(errorMessage, e); throw new AmazonServiceException(errorMessage, e); } catch (AmazonClientException e) { String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received."; LOG.error(errorMessage, e); throw new AmazonClientException(errorMessage, e); } try { InputStream objContent = obj.getObjectContent(); java.util.Scanner objContentScanner = new java.util.Scanner(objContent, "UTF-8"); objContentScanner.useDelimiter("\\A"); embeddedText = objContentScanner.hasNext() ? objContentScanner.next() : ""; objContentScanner.close(); objContent.close(); } catch (IOException e) { String errorMessage = "Failure when handling the message which was read from S3 object. Message was not received."; LOG.error(errorMessage, e); throw new AmazonClientException(errorMessage, e); } return embeddedText; }
From source file:org.apache.tajo.engine.planner.physical.TestPhysicalPlanner.java
@Test public final void testPartitionedStorePlanWithMaxFileSize() throws IOException, PlanningException { // Preparing working dir and input fragments FileFragment[] frags = FileStorageManager.splitNG(conf, "default.score_large", largeScore.getMeta(), new Path(largeScore.getPath()), Integer.MAX_VALUE); TaskAttemptId id = LocalTajoTestingUtility.newTaskAttemptId(masterPlan); Path workDir = CommonTestingUtil.getTestDir("target/test-data/testPartitionedStorePlanWithMaxFileSize"); // Setting session variables QueryContext queryContext = new QueryContext(conf, session); queryContext.setInt(SessionVars.MAX_OUTPUT_FILE_SIZE, 1); // Preparing task context TaskAttemptContext ctx = new TaskAttemptContext(queryContext, id, new FileFragment[] { frags[0] }, workDir); ctx.setOutputPath(new Path(workDir, "part-01-000000")); // SortBasedColumnPartitionStoreExec will be chosen by default. ctx.setEnforcer(new Enforcer()); Expr context = analyzer.parse(CreateTableAsStmts[4]); LogicalPlan plan = planner.createPlan(queryContext, context); LogicalNode rootNode = optimizer.optimize(plan); // Executing CREATE TABLE PARTITION BY PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf); PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode); exec.init();/*from w w w . j av a 2 s . co m*/ exec.next(); exec.close(); FileSystem fs = sm.getFileSystem(); FileStatus[] list = fs.listStatus(workDir); // checking the number of partitions assertEquals(2, list.length); List<Fragment> fragments = Lists.newArrayList(); int i = 0; for (FileStatus status : list) { assertTrue(status.isDirectory()); long fileVolumSum = 0; FileStatus[] fileStatuses = fs.listStatus(status.getPath()); for (FileStatus fileStatus : fileStatuses) { fileVolumSum += fileStatus.getLen(); fragments.add(new FileFragment("partition", fileStatus.getPath(), 0, fileStatus.getLen())); } assertTrue("checking the meaningfulness of test", fileVolumSum > StorageUnit.MB && fileStatuses.length > 1); long expectedFileNum = (long) Math.ceil(fileVolumSum / (float) StorageUnit.MB); assertEquals(expectedFileNum, fileStatuses.length); } TableMeta outputMeta = CatalogUtil.newTableMeta(StoreType.CSV); Scanner scanner = new MergeScanner(conf, rootNode.getOutSchema(), outputMeta, TUtil.newList(fragments)); scanner.init(); long rowNum = 0; while (scanner.next() != null) { rowNum++; } // checking the number of all written rows assertTrue(largeScore.getStats().getNumRows() == rowNum); scanner.close(); }
From source file:org.apache.tajo.engine.planner.physical.TestPhysicalPlanner.java
@Test public final void testPartitionedStorePlanWithEmptyGroupingSet() throws IOException, PlanningException { FileFragment[] frags = FileStorageManager.splitNG(conf, "default.score", score.getMeta(), new Path(score.getPath()), Integer.MAX_VALUE); TaskAttemptId id = LocalTajoTestingUtility.newTaskAttemptId(masterPlan); Path workDir = CommonTestingUtil .getTestDir("target/test-data/testPartitionedStorePlanWithEmptyGroupingSet"); TaskAttemptContext ctx = new TaskAttemptContext(new QueryContext(conf), id, new FileFragment[] { frags[0] }, workDir);//ww w . ja v a 2 s . c o m ctx.setEnforcer(new Enforcer()); Expr expr = analyzer.parse(QUERIES[14]); LogicalPlan plan = planner.createPlan(defaultContext, expr); LogicalNode rootNode = plan.getRootBlock().getRoot(); int numPartitions = 1; DataChannel dataChannel = new DataChannel(masterPlan.newExecutionBlockId(), masterPlan.newExecutionBlockId(), ShuffleType.HASH_SHUFFLE, numPartitions); dataChannel.setShuffleKeys(new Column[] {}); ctx.setDataChannel(dataChannel); optimizer.optimize(plan); TableMeta outputMeta = CatalogUtil.newTableMeta(dataChannel.getStoreType()); FileSystem fs = sm.getFileSystem(); QueryId queryId = id.getTaskId().getExecutionBlockId().getQueryId(); ExecutionBlockId ebId = id.getTaskId().getExecutionBlockId(); PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf); PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode); exec.init(); exec.next(); exec.close(); ctx.getHashShuffleAppenderManager().close(ebId); String executionBlockBaseDir = queryId.toString() + "/output" + "/" + ebId.getId() + "/hash-shuffle"; Path queryLocalTmpDir = new Path(conf.getVar(ConfVars.WORKER_TEMPORAL_DIR) + "/" + executionBlockBaseDir); FileStatus[] list = fs.listStatus(queryLocalTmpDir); List<Fragment> fragments = new ArrayList<Fragment>(); for (FileStatus status : list) { assertTrue(status.isDirectory()); FileStatus[] files = fs.listStatus(status.getPath()); for (FileStatus eachFile : files) { fragments.add(new FileFragment("partition", eachFile.getPath(), 0, eachFile.getLen())); } } assertEquals(numPartitions, fragments.size()); Scanner scanner = new MergeScanner(conf, rootNode.getOutSchema(), outputMeta, TUtil.newList(fragments)); scanner.init(); Tuple tuple; int i = 0; while ((tuple = scanner.next()) != null) { assertEquals(60, tuple.get(0).asInt4()); // sum assertEquals(3, tuple.get(1).asInt4()); // max assertEquals(1, tuple.get(2).asInt4()); // min i++; } assertEquals(1, i); scanner.close(); // Examine the statistics information assertEquals(1, ctx.getResultStats().getNumRows().longValue()); fs.delete(queryLocalTmpDir, true); }