List of usage examples for java.util Scanner hasNext
public boolean hasNext()
From source file:org.olat.core.util.vfs.version.VersionsFileManager.java
private boolean isVersionsXmlFile(VFSLeaf fVersions) { if (fVersions == null || !fVersions.exists()) { return false; }/*from w w w. j a v a 2 s .c om*/ InputStream in = fVersions.getInputStream(); if (in == null) { return false; } Scanner scanner = new Scanner(in); scanner.useDelimiter(TAG_PATTERN); boolean foundVersionsTag = false; while (scanner.hasNext()) { String tag = scanner.next(); if ("versions".equals(tag)) { foundVersionsTag = true; break; } } scanner.close(); IOUtils.closeQuietly(in); return foundVersionsTag; }
From source file:com.aurel.track.dbase.InitDatabase.java
private static void insertPostLoadData(String file) { Connection coni = null;//from w ww . java 2 s .c o m Connection cono = null; try { coni = getConnection(); cono = getConnection(); Statement ostmt = cono.createStatement(); if (isFirstStartEver) { LOGGER.info("Filling some post load data..."); try { URL populateURL = ApplicationBean.getInstance().getServletContext().getResource(file); InputStream in = populateURL.openStream(); java.util.Scanner s = new java.util.Scanner(in, "UTF-8"); s.useDelimiter(";"); String st = null; StringBuilder stb = new StringBuilder(); int line = 0; while (s.hasNext()) { stb.append(s.nextLine().trim()); st = stb.toString(); ++line; if (!st.isEmpty() && !st.startsWith("--") && !st.startsWith("/*")) { if (st.endsWith(";")) { stb = new StringBuilder(); // clear buffer st = st.substring(0, st.length() - 1); // remove the semicolon try { ostmt.executeUpdate(st); LOGGER.info(st); } catch (Exception exc) { LOGGER.error("Problem inserting post-load data: " + exc.getMessage()); LOGGER.error("Line " + line + ": " + st); } } else { stb.append(" "); } } else { stb = new StringBuilder(); } } s.close(); in.close(); } catch (Exception e) { System.err.println(ExceptionUtils.getStackTrace(e)); } LOGGER.info("Post-load data is okay."); } ApplicationStarter.getInstance().actualizePercentComplete( ApplicationStarter.getInstance().INIT_DB_DATA_STEP, ApplicationStarter.INIT_DB_DATA_TEXT); } catch (Exception e) { LOGGER.error("Problem inserting post-load objects: " + e.getMessage()); LOGGER.debug(STACKTRACE, e); } finally { if (coni != null) { try { coni.close(); } catch (SQLException e) { LOGGER.info("Closing connection failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } if (cono != null) { try { cono.close(); } catch (SQLException e) { LOGGER.info("Closing connection failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:com.tesora.dve.tools.DVEConfigCLI.java
public void cmd_locate_catalog(Scanner scanner) throws PEException { if (scanner.hasNext()) { final String url = buildValidCatalogUrlFromLocation(scan(scanner, "url")); final String user = scan(scanner, "user"); final String password = scan(scanner, "password"); final String database = scan(scanner); final DBHelper dbHelper = new DBHelper(url, user, password); try {//from w w w.j av a 2s . co m dbHelper.connect(); } catch (final Exception e) { throw new PEException( "Failed to connect to DVE catalog database at '" + url + "' - " + e.getMessage(), e); } finally { dbHelper.disconnect(); } try { final Properties serverProps = PEFileUtils.loadPropertiesFromClasspath(Main.class, PEConstants.SERVER_FILE_NAME); serverProps.setProperty(DBHelper.CONN_URL, url); serverProps.setProperty(DBHelper.CONN_USER, user); serverProps.setProperty(DBHelper.CONN_PASSWORD, password); serverProps.setProperty(DBHelper.CONN_DBNAME, (database != null) ? database : PEConstants.CATALOG); PEFileUtils.savePropertiesToClasspath(Main.class, PEConstants.SERVER_FILE_NAME, serverProps); printlnDots("Updating properties file at: " + PEFileUtils .convert(PEFileUtils.getClasspathURL(Main.class, PEConstants.SERVER_FILE_NAME))); } catch (final Exception e) { throw new PEException("Failed to update '" + PEConstants.SERVER_FILE_NAME + "' - " + e.getMessage(), e); } try { catHelper.close(); catHelper = new CatalogHelper(Main.class); } catch (final Exception e) { throw new PEException("Failed to re-create catalog helper - " + e.getMessage(), e); } } final Properties props = catHelper.getProperties(); printlnDots("DVE catalog location settings are:"); printlnIndent("URL = " + props.getProperty(DBHelper.CONN_URL, "")); printlnIndent("User = " + props.getProperty(DBHelper.CONN_USER, "")); printlnIndent("Password = " + props.getProperty(DBHelper.CONN_PASSWORD, "")); printlnIndent("Database = " + props.getProperty(DBHelper.CONN_DBNAME, "")); }
From source file:org.aprilis.jrest.compile.Compile.java
/** * //from w w w. jav a 2 s.c om */ 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 w ww . j a v a 2 s . c o m*/ // 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:edu.umd.cs.buildServer.BuildServer.java
public boolean alreadyRunning() throws Exception { int oldPid = getPidFileContents(false); if (oldPid < 0) return false; ProcessBuilder b = new ProcessBuilder( new String[] { "/bin/ps", "xww", "-o", "pid,lstart,user,state,pcpu,cputime,args" }); String user = System.getProperty("user.name"); Process p = b.start();//from ww w.j a v a 2s . c o m try { Scanner s = new Scanner(p.getInputStream()); String header = s.nextLine(); // log.trace("ps header: " + header); while (s.hasNext()) { String txt = s.nextLine(); if (!txt.contains(user)) continue; int pid = Integer.parseInt(txt.substring(0, 5).trim()); if (pid == oldPid) { if (!isQuiet()) { System.out.println("BuildServer is already running"); System.out.println(txt); } return true; } } s.close(); } finally { p.destroy(); } long pidLastModified = getPidFile().lastModified(); String msg = "Previous buildserver pid " + oldPid + " died; had started at " + new Date(pidLastModified); System.out.println(msg); File currentFile = getCurrentFile(); long currentFileLastModified = currentFile.lastModified(); if (currentFile.exists() && currentFileLastModified >= pidLastModified) { Scanner scanner = new Scanner(currentFile); int submissionPK = scanner.nextInt(); int testSetupPK = scanner.nextInt(); scanner.nextLine(); // skip EOL String kind = scanner.nextLine().trim(); String load = scanner.nextLine().trim(); scanner.close(); reportBuildServerDeath(submissionPK, testSetupPK, currentFileLastModified, kind, load); } currentFile.delete(); getPidFile().delete(); System.out.println("Deleting old pid file"); return false; }
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/* ww w. j a va 2 s . c om*/ * @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:codeu.chat.client.commandline.Chat.java
public String doOneTestCommand(Scanner lineScanner) { final Scanner tokenScanner = new Scanner(lineScanner.nextLine()); if (!tokenScanner.hasNext()) { return ""; }/*from w w w . ja v a2 s. com*/ 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:ml.shifu.shifu.core.processor.EvalModelProcessor.java
private double[] locateMaxMinScoreFromFile(SourceType sourceType, String maxMinScoreFolder) throws IOException { List<Scanner> scanners = null; double maxScore = Double.MIN_VALUE; double minScore = Double.MAX_VALUE; try {/*from w w w. j a v a 2 s. co m*/ // here only works for 1 reducer scanners = ShifuFileUtils.getDataScanners(maxMinScoreFolder, sourceType); for (Scanner scanner : scanners) { if (scanner.hasNext()) { String line = scanner.nextLine().trim(); String[] splits = line.split(","); if (splits.length >= 2) { double localMaxScore = Double.parseDouble(splits[0]); if (maxScore < localMaxScore) { maxScore = localMaxScore; } Double localMinScore = Double.parseDouble(splits[1]); if (minScore > localMinScore) { minScore = localMinScore; } } } } } finally { if (scanners != null) { for (Scanner scanner : scanners) { if (scanner != null) { scanner.close(); } } } } return new double[] { maxScore, minScore }; }
From source file:ml.shifu.shifu.core.processor.VarSelectModelProcessor.java
private Map<Integer, ColumnStatistics> readSEValuesToMap(String seOutputFiles, SourceType source) throws IOException { // here only works for 1 reducer FileStatus[] globStatus = ShifuFileUtils.getFileSystemBySourceType(source) .globStatus(new Path(seOutputFiles)); if (globStatus == null || globStatus.length == 0) { throw new RuntimeException("Var select MSE stats output file not exist."); }/*from w w w . j a v a2 s.c o m*/ Map<Integer, ColumnStatistics> map = new HashMap<Integer, ColumnStatistics>(); List<Scanner> scanners = null; try { scanners = ShifuFileUtils.getDataScanners(globStatus[0].getPath().toString(), source); for (Scanner scanner : scanners) { String str = null; while (scanner.hasNext()) { str = scanner.nextLine().trim(); String[] splits = CommonUtils.split(str, "\t"); if (splits.length == 5) { map.put(Integer.parseInt(splits[0].trim()), new ColumnStatistics(Double.parseDouble(splits[2]), Double.parseDouble(splits[3]), Double.parseDouble(splits[4]))); } } } } finally { if (scanners != null) { for (Scanner scanner : scanners) { if (scanner != null) { scanner.close(); } } } } return map; // should be a bug, if it always return null }