List of usage examples for java.lang StringBuilder insert
@Override public StringBuilder insert(int offset, double d)
From source file:forge.game.card.Card.java
public String getAbilityText(final CardState state) { final CardTypeView type = state.getType(); final StringBuilder sb = new StringBuilder(); if (!mayPlay.isEmpty()) { sb.append("May be played by: "); sb.append(/* w w w .j a v a 2 s.c om*/ Lang.joinHomogenous(mayPlay.entrySet(), new Function<Entry<Player, CardPlayOption>, String>() { @Override public String apply(final Entry<Player, CardPlayOption> entry) { return entry.getKey().toString() + entry.getValue().toString(); } })); sb.append("\r\n"); } if (type.isInstant() || type.isSorcery()) { sb.append(abilityTextInstantSorcery(state)); if (haunting != null) { sb.append("Haunting: ").append(haunting); sb.append("\r\n"); } while (sb.toString().endsWith("\r\n")) { sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + 3); } return sb.toString().replaceAll("CARDNAME", state.getName()); } if (monstrous) { sb.append("Monstrous\r\n"); } if (renowned) { sb.append("Renowned\r\n"); } if (manifested) { sb.append("Manifested\r\n"); } sb.append(keywordsToText(getUnhiddenKeywords(state))); // Give spellText line breaks for easier reading sb.append("\r\n"); sb.append(text.replaceAll("\\\\r\\\\n", "\r\n")); sb.append("\r\n"); // Triggered abilities for (final Trigger trig : state.getTriggers()) { if (!trig.isSecondary()) { sb.append(trig.toString().replaceAll("\\\\r\\\\n", "\r\n")).append("\r\n"); } } // Replacement effects for (final ReplacementEffect replacementEffect : state.getReplacementEffects()) { if (!replacementEffect.isSecondary()) { sb.append(replacementEffect.toString()).append("\r\n"); } } // static abilities for (final StaticAbility stAb : state.getStaticAbilities()) { sb.append(stAb.toString()).append("\r\n"); } final List<String> addedManaStrings = new ArrayList<>(); boolean primaryCost = true; boolean isNonAura = !type.hasSubtype("Aura"); for (final SpellAbility sa : state.getSpellAbilities()) { // only add abilities not Spell portions of cards if (sa == null || !state.getType().isPermanent()) { continue; } boolean isNonAuraPermanent = (sa instanceof SpellPermanent) && isNonAura; if (isNonAuraPermanent && primaryCost) { // For Alt costs, make sure to display the cost! primaryCost = false; continue; } final String sAbility = formatSpellAbility(sa); if (sa.getManaPart() != null) { if (addedManaStrings.contains(sAbility)) { continue; } addedManaStrings.add(sAbility); } if (isNonAuraPermanent) { sb.insert(0, "\r\n"); sb.insert(0, sAbility); } else if (!sAbility.endsWith(state.getName() + "\r\n")) { sb.append(sAbility); sb.append("\r\n"); } } // NOTE: if (sb.toString().contains(" (NOTE: ")) { sb.insert(sb.indexOf("(NOTE: "), "\r\n"); } if (sb.toString().contains("(NOTE: ") && sb.toString().contains(".) ")) { sb.insert(sb.indexOf(".) ") + 3, "\r\n"); } // replace triple line feeds with double line feeds int start; final String s = "\r\n\r\n\r\n"; while (sb.toString().contains(s)) { start = sb.lastIndexOf(s); if ((start < 0) || (start >= sb.length())) { break; } sb.replace(start, start + 4, "\r\n"); } return sb.toString().replaceAll("CARDNAME", state.getName()).trim(); }
From source file:edu.ku.brc.specify.conversion.ConvertVerifier.java
/** * @param oldSQL/* ww w. jav a 2s. c o m*/ * @param newSQL * @return * @throws SQLException */ private StatusType compareRecords(final String desc, final String oldCatNumArg, final String newCatNumArg, final String oldSQLArg, final String newSQLArg, final boolean nullsAreOK, final boolean notRetarded) throws SQLException { boolean dbg = false; if (dbg) { System.out.println(oldSQLArg); System.out.println(newSQLArg); } if (dbg) { System.out.println("\n" + desc); dump(desc, oldDBConn, compareTo6DBs ? newSQLArg : oldSQLArg); dump(desc, newDBConn, newSQLArg); } String oldCatNum = oldCatNumArg; String newCatNum = newCatNumArg; if (compareTo6DBs) { oldCatNum = newCatNumArg; } if (notRetarded) { getResultSetsNotRetarded(oldSQLArg, newSQLArg, oldCatNum, newCatNum); } else { getResultSets(oldSQLArg, newSQLArg); } try { boolean hasOldRec = oldDBRS.next(); boolean hasNewRec = newDBRS.next(); if (!hasOldRec && !hasNewRec) { return StatusType.COMPARE_OK; } if (!hasOldRec) { if (nullsAreOK) { log.error(desc + " - No Old Record for [" + oldCatNum + "]"); tblWriter.logErrors(oldCatNum, "No Old Record"); return StatusType.NO_OLD_REC; } return StatusType.COMPARE_OK; } if (!hasNewRec) { log.error(desc + " - No New Record for [" + newCatNum + "]"); tblWriter.logErrors(newCatNum, "No New Record"); return StatusType.NO_NEW_REC; } //check number of rows, if not equal don't try to compare oldDBRS.last(); newDBRS.last(); if (oldDBRS.getRow() != newDBRS.getRow()) { String msg = desc + " Cat Num [" + oldCatNum + "]: Sp5 DB has " + oldDBRS.getRow() + " related records. Sp6 DB has " + newDBRS.getRow(); log.error(msg); tblWriter.logErrors(newCatNum, msg); return oldDBRS.getRow() < newDBRS.getRow() ? StatusType.NO_NEW_REC : StatusType.NO_OLD_REC; } oldDBRS.first(); newDBRS.first(); String oldNewIdStr = oldCatNum + " / " + newCatNum; boolean checkForAgent = newSQL.indexOf("a.LastName") > -1; ResultSetMetaData oldRsmd = oldDBRS.getMetaData(); ResultSetMetaData newRsmd = newDBRS.getMetaData(); PartialDateConv datePair = new PartialDateConv(); Calendar cal = Calendar.getInstance(); StringBuilder errSB = new StringBuilder(); while (hasNewRec && hasOldRec) { errSB.setLength(0); int oldColInx = 0; int newColInx = 0; String idMsgStr = ""; int numCols = newRsmd.getColumnCount(); for (int col = 0; col < numCols; col++) { newColInx++; oldColInx++; if (dbg) { System.out.println("\ncol " + col + " / " + oldRsmd.getColumnCount()); System.out.println("newColInx " + newColInx); System.out.println("oldColInx " + oldColInx); System.out.println(oldRsmd.getColumnName(oldColInx)); System.out.println(newRsmd.getColumnName(newColInx)); } Object newObj = newDBRS.getObject(newColInx); Object oldObj = oldDBRS.getObject(oldColInx); if (oldObj == null && newObj == null) { String colName = newRsmd.getColumnName(newColInx); if (StringUtils.contains(colName, "Date") && StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; if (compareTo6DBs) oldColInx++; } continue; } if (col == 0) { idMsgStr = String.format(" - Rec Ids[%s / %s] ", (oldObj != null ? oldObj : -1), (newObj != null ? newObj : -1)); continue; } String oldColName = oldRsmd.getColumnName(oldColInx); if (oldColName.equals("PreparationMethod") && newObj != null) { String newObjStr = newObj.toString(); if ((oldObj == null && !newObjStr.equalsIgnoreCase("Misc")) || (oldObj != null && !newObjStr.equalsIgnoreCase(oldObj.toString()))) { String msg = idMsgStr + "Old Value was null and shouldn't have been for Old CatNum [" + oldCatNum + "] Field [" + oldColName + "] oldObj[" + oldObj + "] newObj [" + newObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldCatNum, msg); return StatusType.OLD_VAL_NULL; } continue; } if (oldObj == null && !StringUtils.contains(oldColName, "LastName")) { if (!oldColName.equals("PreparationMethod") || !newObj.equals("Misc")) { String msg = idMsgStr + "Old Value was null and shouldn't have been for Old CatNum [" + oldCatNum + "] Field [" + oldColName + "] New Val[" + newObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldCatNum, msg); return StatusType.OLD_VAL_NULL; } } if (newObj == null) { String clsName = newRsmd.getColumnClassName(newColInx); String colName = newRsmd.getColumnName(newColInx); if (compareTo6DBs) { if (!clsName.equals("java.sql.Date") || oldObj != null) { String msg = "New Value was null and shouldn't have been for Key Value New CatNo[" + newCatNum + "] Field [" + colName + "] [" + oldObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(newCatNum, msg); return StatusType.NEW_VAL_NULL; } } else { if (!clsName.equals("java.sql.Date") || (!(oldObj instanceof String) && ((Number) oldObj).intValue() != 0)) { String msg = "New Value was null and shouldn't have been for Key Value New CatNo[" + newCatNum + "] Field [" + colName + "] [" + oldObj + "]"; log.error(desc + " - " + msg); if (tblWriter != null && newCatNum != null && msg != null) tblWriter.logErrors(newCatNum, msg); dbg = true; return StatusType.NEW_VAL_NULL; } } if (StringUtils.contains(colName, "Date") && StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; if (compareTo6DBs) oldColInx++; } continue; } //String colName = newRsmd.getColumnName(col); //System.out.println(newObj.getClass().getName()+" "+oldObj.getClass().getName()); if (newObj instanceof java.sql.Date) { boolean isPartialDate = false; Byte partialDateType = null; if (StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; partialDateType = newDBRS.getByte(newColInx); isPartialDate = true; } if (compareTo6DBs) { Object dateObj = oldDBRS.getObject(oldColInx); boolean isPartialDate2 = false; Byte partialDateType2 = null; if (StringUtils.contains(oldRsmd.getColumnName(oldColInx + 1), "DatePrecision")) { oldColInx++; partialDateType2 = newDBRS.getByte(oldColInx); isPartialDate2 = true; } else { log.error("Next isn't DatePrecision and can't be!"); tblWriter.logErrors(oldNewIdStr, errSB.toString()); } if (!newObj.equals(dateObj) || (isPartialDate2 && !partialDateType2.equals(partialDateType))) { errSB.insert(0, oldColName + " "); errSB.append("["); errSB.append(datePair); errSB.append("]["); errSB.append(dateFormatter.format((Date) newObj)); errSB.append("] oldDate["); errSB.append(dateFormatter.format((Date) dateObj)); errSB.append("]"); log.error(errSB.toString()); tblWriter.logErrors(oldNewIdStr, errSB.toString()); return StatusType.BAD_DATE; } } else { int oldIntDate = oldDBRS.getInt(oldColInx); if (oldIntDate == 0) { continue; } BasicSQLUtils.getPartialDate(oldIntDate, datePair, false); if (partialDateType != null) { boolean ok = StringUtils.isNotEmpty(datePair.getPartial()) && StringUtils.isNumeric(datePair.getPartial()); if (!ok || (Byte.parseByte(datePair.getPartial()) != partialDateType.byteValue())) { errSB.append("Partial Dates Type do not match. Old[" + datePair.getPartial() + "] New [" + partialDateType.byteValue() + "]"); // error partial dates don't match } } cal.setTime((Date) newObj); if (StringUtils.isNotEmpty(datePair.getDateStr()) && !datePair.getDateStr().equalsIgnoreCase("null")) { int year = Integer.parseInt(datePair.getDateStr().substring(0, 4)); int mon = Integer.parseInt(datePair.getDateStr().substring(5, 7)); int day = Integer.parseInt(datePair.getDateStr().substring(8, 10)); if (mon > 0) mon--; boolean isYearOK = true; int yr = cal.get(Calendar.YEAR); if (year != yr) { errSB.append("Year mismatch Old[" + year + "] New [" + yr + "] "); isYearOK = false; } if (mon != cal.get(Calendar.MONTH)) { errSB.append("Month mismatch Old[" + mon + "] New [" + cal.get(Calendar.MONTH) + "] "); } if (day != cal.get(Calendar.DAY_OF_MONTH)) { errSB.append("Day mismatch Old[" + day + "] New [" + cal.get(Calendar.DAY_OF_MONTH) + "] "); } if (errSB.length() > 0 && (!isYearOK || !isPartialDate)) { errSB.insert(0, oldColName + " "); errSB.append("["); errSB.append(datePair); errSB.append("]["); errSB.append(dateFormatter.format((Date) newObj)); errSB.append("]"); log.error(errSB.toString()); tblWriter.logErrors(oldNewIdStr, errSB.toString()); return StatusType.BAD_DATE; } } else { //String msg = "Date contains the string 'NULL'"; //log.error(msg); //tblWriter.logErrors(oldNewIdStr, msg); //return StatusType.BAD_DATE; } } } else if (newObj instanceof Float || newObj instanceof Double) { String s1 = String.format("%10.5f", newObj instanceof Float ? (Float) newObj : (Double) newObj); String s2 = String.format("%10.5f", oldObj instanceof Float ? (Float) oldObj : (Double) oldObj); if (!s1.equals(s2)) { String msg = idMsgStr + "Columns don't compare[" + s1 + "][" + s2 + "] [" + newRsmd.getColumnName(col) + "][" + oldRsmd.getColumnName(oldColInx) + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; } } else { String newColName = newRsmd.getColumnName(newColInx); if (checkForAgent && StringUtils.contains(newColName, "LastName")) { String lastName = oldDBRS.getString(oldColInx); String agentName = oldDBRS.getString(oldColInx + 1); // The 'Name' Column String newLastName = newDBRS.getString(newColInx); if (!newLastName.equals(lastName) && !newLastName.equals(agentName)) { String msg = idMsgStr + "Name Columns don't compare[" + newObj + "][" + oldObj + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); log.error(oldSQLArg + "\n" + newSQLArg); return StatusType.NO_COMPARE; } } else if (StringUtils.contains(newColName, "YesNo")) { boolean yesNoNew = newDBRS.getBoolean(newColInx); boolean yesNoOld = oldDBRS.getInt(oldColInx) != 0; if (yesNoNew != yesNoOld) { String msg = idMsgStr + "Columns don't Cat Num[" + oldCatNum + "] compare[" + yesNoNew + "][" + yesNoOld + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; } } else if (!newObj.equals(oldObj)) { String msg = idMsgStr + "Columns don't Cat Num[" + oldCatNum + "] compare[" + newObj + "][" + oldObj + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; /*boolean isOK = false; if (oldObj instanceof String) { String oldStr = (String)oldObj; String newStr = (String)newObj; String lof = "\\r\\n"; int inx = newStr.indexOf(lof); if (inx > -1) { String tok = oldStr.substring(0, inx); if (newStr.equals(tok)) { isOK = true; } } } if (!isOK) { log.error(desc+ " - Columns don't compare["+newObj+"]["+oldObj+"] ["+newRsmd.getColumnName(newColInx)+"]["+oldRsmd.getColumnName(oldColInx)+"]"); return false; }*/ } } } hasOldRec = oldDBRS.next(); hasNewRec = newDBRS.next(); if (!hasOldRec && !hasNewRec) { return StatusType.COMPARE_OK; } if (!hasOldRec) { log.error(desc + idMsgStr + " - No Old Record for [" + oldCatNum + "]"); tblWriter.logErrors(oldNewIdStr, "No Old Record for [" + oldCatNum + "]"); return StatusType.NO_OLD_REC; } if (!hasNewRec) { log.error(desc + idMsgStr + " No New Record for [" + newCatNum + "]"); tblWriter.logErrors(oldNewIdStr, "No New Record for [" + newCatNum + "]"); return StatusType.NO_NEW_REC; } } } finally { doneWithRS(); } return StatusType.COMPARE_OK; }
From source file:com.monead.semantic.workbench.SemanticWorkbench.java
/** * Load the provided file as an ontology replacing any assertions currently * in the assertions text area./*from w w w . java2s .c o m*/ * * @return The message to be presented on the status line */ private String loadOntologyFile() { final int chunkSize = 32000; StringBuilder allData; char[] chunk; long totalBytesRead = 0; int chunksRead = 0; int maxChunks; int bytesRead = -1; ProgressMonitor monitor = null; Reader reader = null; String message; boolean loadCanceled = false; if (rdfFileSource.isFile()) { lastDirectoryUsed = rdfFileSource.getBackingFile().getParentFile(); } assertionsInput.setText(""); invalidateModel(false); allData = new StringBuilder(); chunk = new char[chunkSize]; setStatus("Loading file " + rdfFileSource.getAbsolutePath()); if (rdfFileSource.length() > 0 && rdfFileSource.length() < MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA) { maxChunks = (int) (rdfFileSource.length() / chunkSize); } else { maxChunks = (int) (MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA / chunkSize); } if (rdfFileSource.length() % chunkSize > 0) { ++maxChunks; } // Assume the file can be loaded hasIncompleteAssertionsInput = false; monitor = new ProgressMonitor(this, "Loading assertions from " + rdfFileSource.getName(), "0 bytes read", 0, maxChunks); try { reader = new InputStreamReader(rdfFileSource.getInputStream()); while (!loadCanceled && (rdfFileSource.isUrl() || chunksRead < maxChunks) && (bytesRead = reader.read(chunk)) > -1) { totalBytesRead += bytesRead; chunksRead = (int) (totalBytesRead / chunk.length); if (chunksRead < maxChunks) { allData.append(chunk, 0, bytesRead); } if (chunksRead >= maxChunks) { monitor.setMaximum(chunksRead + 1); } monitor.setProgress(chunksRead); monitor.setNote("Read " + INTEGER_COMMA_FORMAT.format(totalBytesRead) + (rdfFileSource.isFile() ? " of " + INTEGER_COMMA_FORMAT.format(rdfFileSource.length()) : " bytes") + (chunksRead >= maxChunks ? " (Determining total file size)" : "")); loadCanceled = monitor.isCanceled(); } if (!loadCanceled && rdfFileSource.isUrl()) { rdfFileSource.setLength(totalBytesRead); } if (!loadCanceled && rdfFileSource.length() > MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA) { // The entire file was not loaded hasIncompleteAssertionsInput = true; } if (hasIncompleteAssertionsInput) { StringBuilder warningMessage; warningMessage = new StringBuilder(); warningMessage.append("The file is too large to display. However the entire file will be loaded\n"); warningMessage.append("into the model when it is built.\n\nDisplay size limit (bytes): "); warningMessage.append(INTEGER_COMMA_FORMAT.format(MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA)); if (rdfFileSource.isFile()) { warningMessage.append("\nFile size (bytes):"); warningMessage.append(INTEGER_COMMA_FORMAT.format(rdfFileSource.length())); } warningMessage.append("\n\n"); warningMessage.append("Note that the assersions text area will not permit editing\n"); warningMessage.append("of the partially loaded file and the 'save assertions' menu\n"); warningMessage.append("option will be disabled. These limitations are enabled\n"); warningMessage.append("to prevent the accidental loss of information from the\n"); warningMessage.append("source assertions file."); JOptionPane.showMessageDialog(this, warningMessage.toString(), "Max Display Size Reached", JOptionPane.WARNING_MESSAGE); // Add text to the assertions text area to highlight the fact that the // entire file was not loaded into the text area allData.insert(0, "# First " + INTEGER_COMMA_FORMAT.format(MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA) + " of " + INTEGER_COMMA_FORMAT.format(rdfFileSource.length()) + " bytes displayed\n\n"); allData.insert(0, "# INCOMPLETE VERSION of the file: " + rdfFileSource.getAbsolutePath() + "\n"); allData.append("\n\n# INCOMPLETE VERSION of the file: " + rdfFileSource.getAbsolutePath() + "\n"); allData.append("# First " + INTEGER_COMMA_FORMAT.format(MAX_ASSERTION_BYTES_TO_LOAD_INTO_TEXT_AREA) + " of " + INTEGER_COMMA_FORMAT.format(rdfFileSource.length()) + " bytes displayed\n"); } // Set the loaded assertions into the text area, cleaning up Windows \r\n // endings, if found if (!loadCanceled) { assertionsInput.setText(allData.toString().replaceAll("\r\n", "\n")); assertionsInput.setSelectionEnd(0); assertionsInput.setSelectionStart(0); assertionsInput.moveCaretPosition(0); assertionsInput.scrollRectToVisible(new Rectangle(0, 0, 1, 1)); message = "Loaded file" + (hasIncompleteAssertionsInput ? " (incomplete)" : "") + ": " + rdfFileSource.getName(); addRecentAssertedTriplesFile(rdfFileSource); // Select the assertions tab SwingUtilities.invokeLater(new Runnable() { public void run() { tabbedPane.setSelectedIndex(TAB_NUMBER_ASSERTIONS); setFocusOnCorrectTextArea(); } }); } else { message = "Assertions file load canceled by user"; } } catch (Throwable throwable) { setStatus("Unable to load file: " + rdfFileSource.getName()); JOptionPane.showMessageDialog(this, "Error: Unable to read file\n\n" + rdfFileSource.getAbsolutePath() + "\n\n" + throwable.getMessage(), "Error Reading File", JOptionPane.ERROR_MESSAGE); LOGGER.error("Unable to load the file: " + rdfFileSource.getAbsolutePath(), throwable); message = "Unable to load the file: " + rdfFileSource.getAbsolutePath(); } finally { if (reader != null) { try { reader.close(); } catch (Throwable throwable) { LOGGER.error("Unable to close input file", throwable); } } if (monitor != null) { monitor.close(); } } return message; }