List of usage examples for java.util Formatter close
public void close()
From source file:com.dubsar_dictionary.Dubsar.model.Sense.java
@Override public void parseData(Object jsonResponse) throws JSONException { JSONArray response = (JSONArray) jsonResponse; JSONArray _word = response.getJSONArray(1); JSONArray _synset = response.getJSONArray(2); int wordId = _word.getInt(0); String wordName = _word.getString(1); String wordPos = _word.getString(2); int synsetId = _synset.getInt(0); mGloss = new String(_synset.getString(1)); setPos(wordPos);/* ww w .j ava 2 s . com*/ mWord = new Word(wordId, wordName, getPartOfSpeech()); mName = new String(wordName); mSynset = new Synset(synsetId, mGloss, getPartOfSpeech()); mIsWeakWordReference = mIsWeakSynsetReference = false; mWordReference = null; mSynsetReference = null; setLexname(response.getString(3)); getSynset().setLexname(getLexname()); if (!response.isNull(4)) { setMarker(response.getString(4)); } setFreqCnt(response.getInt(5)); JSONArray _synonyms = response.getJSONArray(6); mSynonyms = new ArrayList<Sense>(_synonyms.length()); int j; int senseId; String senseName; for (j = 0; j < _synonyms.length(); ++j) { JSONArray _synonym = _synonyms.getJSONArray(j); senseId = _synonym.getInt(0); senseName = _synonym.getString(1); String marker = null; if (!_synonym.isNull(2)) { marker = _synonym.getString(2); } int freqCnt = _synonym.getInt(3); Sense synonym = new Sense(senseId, senseName, getSynset()); synonym.setMarker(marker); synonym.setFreqCnt(freqCnt); mSynonyms.add(synonym); } JSONArray _verbFrames = response.getJSONArray(7); mVerbFrames = new ArrayList<String>(_verbFrames.length()); for (j = 0; j < _verbFrames.length(); ++j) { String frame = _verbFrames.getString(j); // Replace %s in verb frames with the name of the word // TODO: Make that a bold span. StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer); formatter.format(frame, new Object[] { getName() }); formatter.close(); mVerbFrames.add(buffer.toString()); } JSONArray _samples = response.getJSONArray(8); mSamples = new ArrayList<String>(_samples.length()); for (j = 0; j < _samples.length(); ++j) { mSamples.add(_samples.getString(j)); } parsePointers(response.getJSONArray(9)); }
From source file:fr.gael.dhus.sync.smart.download.ProductDownloadTask.java
/** * raise an IOException with the given StatusLine and cause Header (cause may be null). *//*w w w. j a v a2s .co m*/ private void raiseFailure(StatusLine stl, Header cause) throws IOException { Formatter ff = new Formatter(); ff.format("Cannot download %s, Reason='%s' (HTTP%d)", this.url, stl.getReasonPhrase(), stl.getStatusCode()); if (cause != null) { String cause_msg = cause.getValue(); if (cause_msg != null && !cause_msg.isEmpty()) { ff.format(" Cause='%s'", cause_msg); } } IOException exception = new IOException(ff.out().toString()); ff.close(); throw exception; }
From source file:org.openecomp.sdc.common.config.AbsEcompErrorManager.java
private EcompErrorInfo setDescriptionParams(EcompErrorInfo ecompErrorInfo, String ecompErrorName, String... descriptionParams) { String description = ecompErrorInfo.getDescription(); // Counting number of params in description int countMatches = StringUtils.countMatches(description, PARAM_STR); // Catching cases when there are more params passed than there are in // the description (formatter will ignore extra params and won't throw // exception) if (countMatches != descriptionParams.length) { return getErrorInfoForDescriptionParamsMismatch(ecompErrorName); }//from ww w . ja v a2 s . c om // Setting params of the description if any StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb, Locale.US); try { formatter.format(description, (Object[]) descriptionParams).toString(); ecompErrorInfo.setDescription(formatter.toString()); } catch (IllegalFormatException e) { // Number of passed params doesn't match number of params in config // file return getErrorInfoForDescriptionParamsMismatch(ecompErrorName); } finally { formatter.close(); } return ecompErrorInfo; }
From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java
public static String formatTime(Calendar cal) { final String S_ProcName = "formatTime"; if (cal == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1, "cal"); }//from w w w . jav a 2 s . c om StringBuffer buff = new StringBuffer(); Formatter fmt = new Formatter(buff); fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY)); buff.append(':'); fmt.format("%1$02d", cal.get(Calendar.MINUTE)); buff.append(':'); fmt.format("%1$02d", cal.get(Calendar.SECOND)); String retval = buff.toString(); fmt.close(); return (retval); }
From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java
public static String formatDate(Calendar cal) { final String S_ProcName = "formatDate"; if (cal == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1, "cal"); }/*from w w w .j av a 2 s. c o m*/ StringBuffer buff = new StringBuffer(); Formatter fmt = new Formatter(buff); fmt.format("%1$04d", cal.get(Calendar.YEAR)); buff.append('-'); fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1); buff.append('-'); fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH)); String retval = buff.toString(); fmt.close(); return (retval); }
From source file:com.pivotal.gemfire.tools.pulse.internal.service.MemberClientsService.java
public JSONObject execute(final HttpServletRequest request) throws Exception { // get cluster object Cluster cluster = Repository.get().getCluster(); // json object to be sent as response JSONObject responseJSON = new JSONObject(); try {//from w ww . j a v a 2 s. c o m JSONObject requestDataJSON = new JSONObject(request.getParameter("pulseData")); String memberName = requestDataJSON.getJSONObject("MemberClients").getString("memberName"); JSONArray clientListJson = new JSONArray(); Cluster.Member clusterMember = cluster.getMember(StringUtils.makeCompliantName(memberName)); if (clusterMember != null) { responseJSON.put("memberId", clusterMember.getId()); responseJSON.put(this.NAME, clusterMember.getName()); responseJSON.put(this.HOST, clusterMember.getHost()); // member's clients Cluster.Client[] memberClients = clusterMember.getMemberClients(); for (Cluster.Client memberClient : memberClients) { JSONObject regionJSON = new JSONObject(); regionJSON.put("clientId", memberClient.getId()); regionJSON.put(this.NAME, memberClient.getName()); regionJSON.put(this.HOST, memberClient.getHost()); regionJSON.put("queueSize", memberClient.getQueueSize()); regionJSON.put("uptime", TimeUtils.convertTimeSecondsToHMS(memberClient.getUptime())); Formatter fmt = new Formatter(); regionJSON.put("cpuUsage", fmt.format("%.4f", memberClient.getCpuUsage()).toString()); // regionJSON.put("cpuUsage", memberClient.getCpuUsage()); regionJSON.put("threads", memberClient.getThreads()); regionJSON.put("gets", memberClient.getGets()); regionJSON.put("puts", memberClient.getPuts()); clientListJson.put(regionJSON); fmt.close(); } responseJSON.put("memberClients", clientListJson); } // Send json response return responseJSON; } catch (JSONException e) { throw new Exception(e); } }
From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java
public static String formatXmlString(String str) { final String S_ProcName = "formatXmlString"; if (str == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1, "str"); }/*from w w w .j a v a 2 s.co m*/ StringBuffer buff = new StringBuffer(); char ch; int idx; int len = str.length(); for (idx = 0; idx < len; idx++) { ch = str.charAt(idx); if (Character.isWhitespace(ch)) { buff.append(ch); } else { switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '~': case '!': case '#': case '$': case '%': case '^': case '*': case '(': case ')': case '-': case '_': case '+': case '=': case '{': case '}': case '[': case ']': case ':': case ';': case ',': case '.': case '?': case '/': case '\\': case '|': buff.append(ch); break; case '\'': buff.append("'"); break; case '"': buff.append("""); break; case '&': buff.append("&"); break; case '<': buff.append("<"); break; case '>': buff.append(">"); break; default: if (!Character.isValidCodePoint(ch)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(CFLibXmlUtil.class, S_ProcName, "Only valid code points can be formatted, ch is out of range"); } StringBuffer fmtbuff = new StringBuffer(); fmtbuff.append("&#"); Formatter fmt = new Formatter(fmtbuff); fmt.format("%1$d", ch); fmtbuff.append(";"); String tmp = fmtbuff.toString(); buff.append(tmp); fmt.close(); break; } } } String retval = buff.toString(); return (retval); }
From source file:org.kie.workbench.common.forms.migration.tool.pipelines.basic.impl.AbstractFormAdapter.java
protected void migrateFields(final Set<Field> fields, final FormDefinition newForm, final FormMigrationSummary formSummary) { LayoutHelper helper = new LayoutHelper(); fields.forEach(originalField -> { if (!StringUtils.isEmpty(originalField.getMovedToForm())) { return; }/*from w w w. ja va 2 s . c o m*/ if (!Boolean.TRUE.equals(originalField.getGroupWithPrevious())) { helper.newRow(); } String originalTypeCode = originalField.getFieldType().getCode(); FieldAdapter adapter = adaptersRegistry.get(originalTypeCode); if (adapter == null) { // trying a backup adapter UnSupportedFieldAdapter unSupportedFieldAdapter = unSupportedAdapters.get(originalTypeCode); if (unSupportedFieldAdapter != null) { warn("Problems migrating field '" + originalField.getFieldName() + "': the original field has an unsupported field type '" + originalTypeCode + "'. It will be added on the new Form as a '" + unSupportedFieldAdapter.getNewFieldType() + "'"); unSupportedFieldAdapter.parseField(originalField, formSummary, newForm, helper::add); } else { warn("Cannot migrate field '" + originalField.getFieldName() + "': Unsupported field type '" + originalTypeCode + "'"); Formatter formatter = new Formatter(); formatter.format(FormsMigrationConstants.UNSUPORTED_FIELD_HTML_TEMPLATE, originalField.getFieldName(), originalTypeCode); LayoutComponent component = new LayoutComponent(FormsMigrationConstants.HTML_COMPONENT); component.addProperty(FormsMigrationConstants.HTML_CODE_PARAMETER, formatter.toString()); formatter.close(); helper.add(component); } } else { try { adapter.parseField(originalField, formSummary, newForm, helper::add); } catch (Exception ex) { warn("Cannot migrate field '" + originalField.getFieldName() + "': Unexpected error, see message for details"); ex.printStackTrace(migrationContext.getSystem().err()); } } }); newForm.setLayoutTemplate(helper.build()); }
From source file:com.yeldi.yeldibazaar.ManageRepo.java
private void redraw() { try {/*from ww w .j a va 2 s . c om*/ DB db = DB.getDB(); repos = db.getRepos(); } finally { DB.releaseDB(); } List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); Map<String, Object> server_line; for (DB.Repo repo : repos) { server_line = new HashMap<String, Object>(); server_line.put("address", repo.address); if (repo.inuse) { server_line.put("inuse", R.drawable.btn_check_on); } else { server_line.put("inuse", R.drawable.btn_check_off); } if (repo.pubkey != null) { try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.update(Hasher.unhex(repo.pubkey)); byte[] fingerprint = digest.digest(); Formatter formatter = new Formatter(new StringBuilder()); formatter.format("%02X", fingerprint[0]); for (int i = 1; i < fingerprint.length; i++) { formatter.format(i % 5 == 0 ? " %02X" : ":%02X", fingerprint[i]); } server_line.put("fingerprint", formatter.toString()); formatter.close(); } catch (Exception e) { Log.w("FDroid", "Unable to get certificate fingerprint.\n" + Log.getStackTraceString(e)); } } result.add(server_line); } SimpleAdapter show_out = new SimpleAdapter(this, result, R.layout.repolisticons, new String[] { "address", "inuse", "fingerprint" }, new int[] { R.id.uri, R.id.img, R.id.fingerprint }); setListAdapter(show_out); }
From source file:org.opengeoportal.proxy.controllers.DynamicOgcController.java
/** * <p>Encodes characters in the query or fragment part of the URI. * * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more * forgiving, we must escape the problematic characters. See the URI class for the spec. * * @param in example: name=value&foo=bar#fragment *///from www . j av a 2 s . c o m static CharSequence encodeUriQuery(CharSequence in) { //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things. StringBuilder outBuf = null; Formatter formatter = null; for (int i = 0; i < in.length(); i++) { char c = in.charAt(i); boolean escape = true; if (c < 128) { if (asciiQueryChars.get((int) c)) { escape = false; } } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii escape = false; } if (!escape) { if (outBuf != null) outBuf.append(c); } else { //escape if (outBuf == null) { outBuf = new StringBuilder(in.length() + 5 * 3); outBuf.append(in, 0, i); formatter = new Formatter(outBuf); } //leading %, 0 padded, width 2, capital hex formatter.format("%%%02X", (int) c);//TODO formatter.close(); } } return outBuf != null ? outBuf : in; }