List of usage examples for java.lang StringBuffer replace
@Override public synchronized StringBuffer replace(int start, int end, String str)
From source file:com.meiah.core.util.StringUtil.java
/** * /*from w w w . j ava 2 s .c o m*/ * @description strKeyword?strSplit??? * @param strSplit * @author zhangj * @date Jun 14, 2012 */ public static List<String> getEngKeyword(StringBuffer sbKeyword, String strSplit) { if (Validator.isNull(strSplit)) { strSplit = "\""; //""???? } List<String> result = new ArrayList<String>(); int index = 0, end = 0; int start = sbKeyword.indexOf(strSplit); //"? int count = 1; while (index != -1) { index = sbKeyword.indexOf(strSplit, index + 1); if (index != -1) { if (index == start) { continue; } if (count % 2 == 0) { start = index; } else { end = index; } if (start < end) { result.add(sbKeyword.substring(start + 1, end)); sbKeyword.replace(start + 1, end, ""); index = start + 1; start = 0; end = 0; } count++; } } return result; }
From source file:org.codehaus.tycho.eclipsepackaging.ProductExportMojo.java
private void copyExecutable(TargetEnvironment environment, File target) throws MojoExecutionException, MojoFailureException { getLog().debug("Creating launcher"); FeatureDescription feature;//from w w w.java2 s .co m // eclipse 3.2 if (isEclipse32Platform()) { feature = featureResolutionState.getFeature("org.eclipse.platform.launchers", null); } else { feature = featureResolutionState.getFeature("org.eclipse.equinox.executable", null); } if (feature == null) { throw new MojoExecutionException("RPC delta feature not found!"); } File location = feature.getLocation(); String os = environment.getOs(); String ws = environment.getWs(); String arch = environment.getArch(); File osLauncher = new File(location, "bin/" + ws + "/" + os + "/" + arch); try { // Don't copy eclipsec file IOFileFilter eclipsecFilter = FileFilterUtils .notFileFilter(FileFilterUtils.prefixFileFilter("eclipsec")); FileUtils.copyDirectory(osLauncher, target, eclipsecFilter); } catch (IOException e) { throw new MojoExecutionException("Unable to copy launcher executable", e); } File launcher = getLauncher(environment, target); // make launcher executable try { getLog().debug("running chmod"); ArchiveEntryUtils.chmod(launcher, 0755, null); } catch (ArchiverException e) { throw new MojoExecutionException("Unable to make launcher being executable", e); } File osxEclipseApp = null; // Rename launcher if (productConfiguration.getLauncher() != null && productConfiguration.getLauncher().getName() != null) { String launcherName = productConfiguration.getLauncher().getName(); String newName = launcherName; // win32 has extensions if (PlatformPropertiesUtils.OS_WIN32.equals(os)) { String extension = FilenameUtils.getExtension(launcher.getAbsolutePath()); newName = launcherName + "." + extension; } else if (PlatformPropertiesUtils.OS_MACOSX.equals(os)) { // the launcher is renamed to "eclipse", because // this is the value of the CFBundleExecutable // property within the Info.plist file. // see http://jira.codehaus.org/browse/MNGECLIPSE-1087 newName = "eclipse"; } getLog().debug("Renaming launcher to " + newName); File newLauncher = new File(launcher.getParentFile(), newName); if (!launcher.renameTo(newLauncher)) { throw new MojoExecutionException("Could not rename native launcher to " + newName); } launcher = newLauncher; // macosx: the *.app directory is renamed to the // product configuration launcher name // see http://jira.codehaus.org/browse/MNGECLIPSE-1087 if (PlatformPropertiesUtils.OS_MACOSX.equals(os)) { newName = launcherName + ".app"; getLog().debug("Renaming Eclipse.app to " + newName); File eclipseApp = new File(target, "Eclipse.app"); osxEclipseApp = new File(eclipseApp.getParentFile(), newName); eclipseApp.renameTo(osxEclipseApp); // ToDo: the "Info.plist" file must be patched, so that the // property "CFBundleName" has the value of the // launcherName variable } } // icons if (productConfiguration.getLauncher() != null) { if (PlatformPropertiesUtils.OS_WIN32.equals(os)) { getLog().debug("win32 icons"); List<String> icons = productConfiguration.getW32Icons(); if (icons != null) { getLog().debug(icons.toString()); try { String[] args = new String[icons.size() + 1]; args[0] = launcher.getAbsolutePath(); int pos = 1; for (String string : icons) { args[pos] = string; pos++; } IconExe.main(args); } catch (Exception e) { throw new MojoExecutionException("Unable to replace icons", e); } } else { getLog().debug("icons is null"); } } else if (PlatformPropertiesUtils.OS_LINUX.equals(os)) { String icon = productConfiguration.getLinuxIcon(); if (icon != null) { try { File sourceXPM = new File(project.getBasedir(), removeFirstSegment(icon)); File targetXPM = new File(launcher.getParentFile(), "icon.xpm"); FileUtils.copyFile(sourceXPM, targetXPM); } catch (IOException e) { throw new MojoExecutionException("Unable to create ico.xpm", e); } } } else if (PlatformPropertiesUtils.OS_MACOSX.equals(os)) { String icon = productConfiguration.getMacIcon(); if (icon != null) { try { if (osxEclipseApp == null) { osxEclipseApp = new File(target, "Eclipse.app"); } File source = new File(project.getBasedir(), removeFirstSegment(icon)); File targetFolder = new File(osxEclipseApp, "/Resources/" + source.getName()); FileUtils.copyFile(source, targetFolder); // Modify eclipse.ini File iniFile = new File(osxEclipseApp + "/Contents/MacOS/eclipse.ini"); if (iniFile.exists() && iniFile.canWrite()) { StringBuffer buf = new StringBuffer(FileUtils.readFileToString(iniFile)); int pos = buf.indexOf("Eclipse.icns"); buf.replace(pos, pos + 12, source.getName()); FileUtils.writeStringToFile(iniFile, buf.toString()); } } catch (Exception e) { throw new MojoExecutionException("Unable to create macosx icon", e); } } } } // eclipse 3.2 if (isEclipse32Platform()) { File startUpJar = new File(location, "bin/startup.jar"); try { FileUtils.copyFileToDirectory(startUpJar, target); } catch (IOException e) { throw new MojoExecutionException("Unable to copy startup.jar executable", e); } } }
From source file:net.mlw.vlh.adapter.hibernate3.util.StatementBuilder.java
/** * Usage of filters: {key} -> :keyName add to query's parameter map keyValue * [key] -> keyValue/*from w w w. java 2 s. c o m*/ * * @param hql * @param whereClause * @return Query for ordinary list * @throws HibernateException * @throws ParseException */ public Query generate(Session session, StringBuffer hql, Map whereClause, boolean isRemoveEmptyStrings) throws HibernateException, ParseException { if (whereClause == null) { whereClause = Collections.EMPTY_MAP; } Map arguments = new HashMap(); // Include or exclude the filters. for (int i = 0, end = 0, start; ((start = hql.toString().indexOf("/~", end)) >= 0); i++) { end = hql.toString().indexOf("~/", start); String key = hql.substring(start + 2, hql.indexOf(":", start)); Object value = whereClause.get(key); if (isValuePopulated(value, isRemoveEmptyStrings)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("The filter key=[" + key + "] with the value=[" + value + "] is accepted by the hql preprocesor"); } hql.replace(start, end + 2, hql.substring(hql.indexOf(":", start) + 1, end)); } else { if (LOGGER.isInfoEnabled()) { LOGGER.info("The filter key=[" + key + "] is removed from the query by the hql preprocesor."); } hql.replace(start, end + 2, ""); } end -= start; } // Replace any [key] with the value in the whereClause Map. for (int i = 0, end = 0, start; ((start = hql.toString().indexOf('[', end)) >= 0); i++) { end = hql.toString().indexOf(']', start); String key = hql.substring(start + 1, end); Object value = whereClause.get(key); hql.replace(start, end + 1, (value == null) ? "" : value.toString()); end -= (key.length() + 2); } // Replace any "{key}" with the value in the whereClause Map, // then placing the value in the partameters list. for (int i = 0, end = 0, start; ((start = hql.toString().indexOf('{', end)) >= 0); i++) { end = hql.toString().indexOf('}', start); String key = hql.substring(start + 1, end); Object value = whereClause.get(key); if (value == null) { throw new NullPointerException("Property '" + key + "' was not provided."); } arguments.put(key, value); hql.replace(start, end + 1, ":" + key); end -= (key.length() + 2); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("The final query is " + hql); } Query query = session.createQuery(hql.toString()); // Now set all the patameters on the statement. if (setters == null) { for (Iterator keys = arguments.keySet().iterator(); keys.hasNext();) { String key = (String) keys.next(); Object value = arguments.get(key); if (value instanceof List) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Setting a paremeterList to the query."); } query.setParameterList(key, ((List) value).toArray()); } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug( "Using the default setter for key=[" + key + "] with the value=[" + value + "]"); } getDefaultSetter().set(query, key, value); } } } else { for (Iterator keys = arguments.keySet().iterator(); keys.hasNext();) { String key = (String) keys.next(); Object value = arguments.get(key); getSetter(key).set(query, key, value); } } return query; }
From source file:org.alfresco.integrations.google.docs.webscripts.CreateContent.java
/** * Create a new content item for a document, spreadsheet or presentation which is to be edited in Google Docs * * <p>The name of the file is generated automatically, based on the type of content. In the event of a clash with * an existing file, the file name will have a numeric suffix placed on the end of it before the file extension, * which will be incremented until a valid name is found.</p> * * @param parentNodeRef NodeRef identifying the folder where the content will be created * @param contentType The type of content to be created, one of 'document', 'spreadsheet' or 'presentation' * @param mimetype The mimetype of the new content item, used to determine the file extension to add * @return A FileInfo object representing the new content item. Call fileInfo.getNodeRef() to get the nodeRef *///from ww w. j a v a 2 s . co m private NodeRef createFile(final NodeRef parentNodeRef, final String contentType, final String mimetype) { String baseName = getNewFileName(contentType), fileExt = fileNameUtil.getExtension(mimetype); final StringBuffer sb = new StringBuffer(baseName); if (fileExt != null && !fileExt.equals("")) { sb.append(".").append(fileExt); } int i = 0, maxCount = 1000; // Limit the damage should something go horribly wrong and a FileExistsException is always thrown while (i <= maxCount) { List<String> parts = new ArrayList<String>(1); parts.add(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, sb.toString()).toPrefixString()); try { if (fileFolderService.resolveNamePath(parentNodeRef, parts, false) == null) { return fileFolderService.create(parentNodeRef, sb.toString(), ContentModel.TYPE_CONTENT) .getNodeRef(); } else { log.debug("Filename " + sb.toString() + " already exists"); String name = fileNameUtil.incrementFileName(sb.toString()); sb.replace(0, sb.length(), name); if (log.isDebugEnabled()) log.debug("new file name " + sb.toString()); } } catch (FileNotFoundException e) // We should never catch this because we set mustExist=false { throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unexpected FileNotFoundException", e); } i++; } throw new WebScriptException(HttpStatus.SC_CONFLICT, "Too many untitled files. Try renaming some existing documents."); }
From source file:analytics.storage.store2csv.java
@Override public void storeElementValueData(HashMap<String, Integer> data, String metricName, String dataProvider, String analysisType, String headerColumn, String element, Logger logger, int time) { // TODO Auto-generated method stub String sFileName = dataProvider + analysisType + ".csv"; Properties props = new Properties(); try {/*from w w w. ja va 2 s . c om*/ props.load(new FileInputStream("configure.properties")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); System.exit(-1); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); System.exit(-1); } ; File anls = new File(props.getProperty(AnalyticsConstants.resultsPath) + "Analysis_Results"); if (!anls.exists()) anls.mkdir(); else { // if (temporal == false) { // FileUtils.deleteQuietly(anls); // anls.mkdir(); // } } File dir = new File(anls, dataProvider); if (!dir.exists()) dir.mkdir(); File file = new File(dir, sFileName); FileWriter writer; BufferedWriter bw = null; BufferedReader reader = null; try { if (file.exists() && time == 0) file.delete(); // if (!file.exists() && time == 0) { writer = new FileWriter(file); bw = new BufferedWriter(writer); createHeaders(bw, metricName, headerColumn); Set<String> keySet = data.keySet(); Iterator<String> iterator = keySet.iterator(); StringBuffer logString = new StringBuffer(); StringBuffer key = new StringBuffer(); while (iterator.hasNext()) { // String key = iterator.next(); key.append(iterator.next()); Integer value = data.get(key.toString()); if (key.toString().contains(",")) key.replace(0, key.length(), key.toString().replace(",", "/")); // key = key.toString().replace(",", "/"); // bw.append(element); // bw.append(','); bw.append(key); logString.append(dataProvider); logString.append(" " + element); logString.append(" " + key.toString().replace(" ", "_")); // logString.append(" " + key.replace(" ", "_")); bw.append(','); bw.append(String.valueOf(value)); logString.append(" " + String.valueOf(value)); bw.newLine(); logger.info(logString.toString()); logString.delete(0, logString.capacity()); key.delete(0, key.length()); } bw.close(); // } else if (file.exists() && time == 0) { // file.delete(); // writer = new FileWriter(file); // bw = new BufferedWriter(writer); // createHeaders(bw, metricName, headerColumn); // // Set<String> keySet = data.keySet(); // Iterator<String> iterator = keySet.iterator(); // StringBuffer logString = new StringBuffer(); // // StringBuffer key = new StringBuffer(); // // while (iterator.hasNext()) { // // String key = iterator.next(); // key.append(iterator.next()); // // Integer value = data.get(key.toString()); // // if (key.toString().contains(",")) // key.replace(0, key.length(), // key.toString().replace(",", "/")); // // key = key.toString().replace(",", "/"); // // // bw.append(element); // // bw.append(','); // bw.append(key); // logString.append(dataProvider); // logString.append(" " + element); // logString.append(" " + key.toString().replace(" ", "_")); // // logString.append(" " + key.replace(" ", "_")); // bw.append(','); // bw.append(String.valueOf(value)); // logString.append(" " + String.valueOf(value)); // bw.newLine(); // // logger.info(logString.toString()); // logString.delete(0, logString.capacity()); // key.delete(0, key.length()); // } // bw.close(); // } else if (file.exists() && time > 0) { // // reader = new BufferedReader(new FileReader(file)); // // File temp = new File(dir, "temp.csv"); // // writer = new FileWriter(temp); // bw = new BufferedWriter(writer); // // String line; // int counter = 0; // // // Set<String> keySet = data.keySet(); // // Iterator<String> iterator = keySet.iterator(); // StringBuffer logString = new StringBuffer(); // StringBuffer key = new StringBuffer(); // while ((line = reader.readLine()) != null) { // String[] split = line.split(","); // // System.out.println(line); // // if (counter == 0) { // line = line + "," + metricName; // bw.append(line); // bw.newLine(); // // } else { // // String key = iterator.next(); // // String key = split[0]; // key.append(split[0]); // Integer value = data.get(key); // // // if (key.contains(",")) // // key = key.replace(",", "/"); // if (key.toString().contains(",")) // key.replace(0, key.length(), key.toString() // .replace(",", "/")); // // line = line + "," + value; // bw.append(line); // logString.append(dataProvider); // logString.append(" " + element); // logString // .append(" " + key.toString().replace(" ", "_")); // logString.append(" " + value); // // bw.newLine(); // // logger.info(logString.toString()); // logString.delete(0, logString.capacity()); // key.delete(0, key.length()); // } // // counter += 1; // // } // bw.close(); // FileUtils.copyFile(temp, file); // temp.delete(); // reader.close(); // // } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (reader != null) reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:de.marcelkapfer.morseconverter.MainActivity.java
public void decode(View view) { EditText editText = (EditText) findViewById(R.id.edit_message); StringBuffer message = new StringBuffer(editText.getText()); if (message.toString().endsWith(" ")) { message = message.deleteCharAt(message.length() - 1); }/*ww w .j a v a 2 s . c om*/ // Variables StringBuffer input = new StringBuffer(); input = input.replace(0, input.length(), message.toString().toUpperCase()); StringBuffer output = new StringBuffer(); if (input.toString().equals("")) { tfOutput = "Please enter at least one character"; After(view); } else if (input.toString().equals("LETTERSPACE")) { tfOutput = "#"; After(view); } else if (input.toString().equals("END OF WORK")) { tfOutput = "000101"; After(view); } else if (input.toString().equals("ERROR")) { tfOutput = "00000000"; After(view); } else if (input.toString().equals("STARTING SIGNAL")) { tfOutput = "10101"; After(view); } else if (input.toString().equals("ENDING SIGNAL")) { tfOutput = "01010"; After(view); } else if (input.toString().equals("UNDERSTOOD")) { tfOutput = "00010"; After(view); } else if (input.toString().equals("WAIT")) { tfOutput = "01000"; After(view); } else if (input.toString().equals("SOS")) { tfOutput = "000111000"; After(view); } else if (input.toString().equals("LETTER SPACE")) { tfOutput = "#"; After(view); } else if (input.toString().equals("WORD SPACE")) { tfOutput = "+"; After(view); } else { for (int c = input.length(); c > 0; c--) { if (input.toString().startsWith(" ")) { if (output.toString().endsWith("#")) { output.delete(output.length() - 1, output.length()); } output.append("+"); input.delete(0, 1); } else if (input.toString().startsWith("A")) { output.append("01#"); input.delete(0, 1); } else if (input.toString().startsWith("B")) { output.append("1000#"); input.delete(0, 1); } else if (input.toString().startsWith("C")) { output.append("1010#"); input.delete(0, 1); } else if (input.toString().startsWith("D")) { output.append("100#"); input.delete(0, 1); } else if (input.toString().startsWith("E")) { output.append("0#"); input.delete(0, 1); } else if (input.toString().startsWith("F")) { output.append("0010#"); input.delete(0, 1); } else if (input.toString().startsWith("G")) { output.append("110#"); input.delete(0, 1); } else if (input.toString().startsWith("H")) { output.append("0000#"); input.delete(0, 1); } else if (input.toString().startsWith("I")) { output.append("00#"); input.delete(0, 1); } else if (input.toString().startsWith("J")) { output.append("0111#"); input.delete(0, 1); } else if (input.toString().startsWith("K")) { output.append("101#"); input.delete(0, 1); } else if (input.toString().startsWith("L")) { output.append("0100#"); input.delete(0, 1); } else if (input.toString().startsWith("M")) { output.append("11#"); input.delete(0, 1); } else if (input.toString().startsWith("N")) { output.append("10#"); input.delete(0, 1); } else if (input.toString().startsWith("O")) { output.append("111#"); input.delete(0, 1); } else if (input.toString().startsWith("P")) { output.append("0110#"); input.delete(0, 1); } else if (input.toString().startsWith("Q")) { output.append("1101#"); input.delete(0, 1); } else if (input.toString().startsWith("R")) { output.append("010#"); input.delete(0, 1); } else if (input.toString().startsWith("S")) { output.append("000#"); input.delete(0, 1); } else if (input.toString().startsWith("T")) { output.append("1#"); input.delete(0, 1); } else if (input.toString().startsWith("U")) { output.append("001#"); input.delete(0, 1); } else if (input.toString().startsWith("V")) { output.append("0001#"); input.delete(0, 1); } else if (input.toString().startsWith("W")) { output.append("011#"); input.delete(0, 1); } else if (input.toString().startsWith("X")) { output.append("1001#"); input.delete(0, 1); } else if (input.toString().startsWith("Y")) { output.append("1011#"); input.delete(0, 1); } else if (input.toString().startsWith("Z")) { output.append("1100#"); input.delete(0, 1); } else if (input.toString().startsWith("0")) { output.append("11111#"); input.delete(0, 1); } else if (input.toString().startsWith("1")) { output.append("01111#"); input.delete(0, 1); } else if (input.toString().startsWith("2")) { output.append("00111#"); input.delete(0, 1); } else if (input.toString().startsWith("3")) { output.append("00011#"); input.delete(0, 1); } else if (input.toString().startsWith("4")) { output.append("00001#"); input.delete(0, 1); } else if (input.toString().startsWith("5")) { output.append("00000#"); input.delete(0, 1); } else if (input.toString().startsWith("6")) { output.append("10000#"); input.delete(0, 1); } else if (input.toString().startsWith("7")) { output.append("11000#"); input.delete(0, 1); } else if (input.toString().startsWith("8")) { output.append("11100#"); input.delete(0, 1); } else if (input.toString().startsWith("9")) { output.append("11110#"); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("0101#"); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("1110#"); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("0011#"); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("00011000#"); input.delete(0, 1); } else if (input.toString().startsWith(".")) { output.append("010101#"); input.delete(0, 1); } else if (input.toString().startsWith(",")) { output.append("110011#"); input.delete(0, 1); } else if (input.toString().startsWith(":")) { output.append("111000#"); input.delete(0, 1); } else if (input.toString().startsWith(";")) { output.append("101010#"); input.delete(0, 1); } else if (input.toString().startsWith("?")) { output.append("001100#"); input.delete(0, 1); } else if (input.toString().startsWith("!")) { output.append("101011#"); input.delete(0, 1); } else if (input.toString().startsWith("-")) { output.append("100001#"); input.delete(0, 1); } else if (input.toString().startsWith("_")) { output.append("001101#"); input.delete(0, 1); } else if (input.toString().startsWith("(")) { output.append("10110#"); input.delete(0, 1); } else if (input.toString().startsWith(")")) { output.append("101101#"); input.delete(0, 1); } else if (input.toString().startsWith("=")) { output.append("10001#"); input.delete(0, 1); } else if (input.toString().startsWith("+")) { output.append("01010#"); input.delete(0, 1); } else if (input.toString().startsWith("/")) { output.append("10010#"); input.delete(0, 1); } else if (input.toString().startsWith("@")) { output.append("011010#"); input.delete(0, 1); } else if (input.toString().startsWith("'")) { output.append("011110#"); input.delete(0, 1); } else if (input.toString().startsWith("$")) { output.append("0001001#"); input.delete(0, 1); } else { tfOutput = "Code not listed or wrong."; } } if (output.toString().endsWith("#")) { output.delete(output.length() - 1, output.length()); } tfOutput = output.toString(); lastFragment = 0; After(view); } }
From source file:de.marcelkapfer.morseconverter.MainActivity.java
public void llm(View view) { EditText editText = (EditText) findViewById(R.id.edit_message); StringBuffer message = new StringBuffer(editText.getText()); if (message.toString().endsWith(" ")) { message = message.deleteCharAt(message.length() - 1); }//from w w w . j a va 2s. c om // Variables // Variables StringBuffer input = new StringBuffer(); input = input.replace(0, input.length(), message.toString().toUpperCase()); StringBuffer output = new StringBuffer(); if (input.toString().equals("")) { tfOutput = "Please enter at least one character"; After(view); } else if (input.toString().equals("LETTERSPACE")) { tfOutput = " "; After(view); } else if (input.toString().equals("END OF WORK")) { tfOutput = "...-.-"; After(view); } else if (input.toString().equals("ERROR")) { tfOutput = "........"; After(view); } else if (input.toString().equals("STARTING SIGNAL")) { tfOutput = "-.-.-"; After(view); } else if (input.toString().equals("ENDING SIGNAL")) { tfOutput = ".-.-."; After(view); } else if (input.toString().equals("UNDERSTOOD")) { tfOutput = "...-."; After(view); } else if (input.toString().equals("WAIT")) { tfOutput = ".-..."; After(view); } else if (input.toString().equals("SOS")) { tfOutput = "...---..."; After(view); } else if (input.toString().equals("LETTER SPACE")) { tfOutput = " "; After(view); } else if (input.toString().equals("WORD SPACE")) { tfOutput = " "; After(view); } else { for (int c = input.length(); c > 0; c--) { if (input.toString().startsWith(" ")) { if (output.toString().endsWith(" ")) { output.delete(output.length() - 3, output.length()); } output.append(" "); input.delete(0, 1); } else if (input.toString().startsWith("A")) { output.append(".- "); input.delete(0, 1); } else if (input.toString().startsWith("B")) { output.append("-... "); input.delete(0, 1); } else if (input.toString().startsWith("C")) { output.append("-.-. "); input.delete(0, 1); } else if (input.toString().startsWith("D")) { output.append("-.. "); input.delete(0, 1); } else if (input.toString().startsWith("E")) { output.append(". "); input.delete(0, 1); } else if (input.toString().startsWith("F")) { output.append("..-. "); input.delete(0, 1); } else if (input.toString().startsWith("G")) { output.append("--. "); input.delete(0, 1); } else if (input.toString().startsWith("H")) { output.append(".... "); input.delete(0, 1); } else if (input.toString().startsWith("I")) { output.append(".. "); input.delete(0, 1); } else if (input.toString().startsWith("J")) { output.append(".--- "); input.delete(0, 1); } else if (input.toString().startsWith("K")) { output.append("-.- "); input.delete(0, 1); } else if (input.toString().startsWith("L")) { output.append(".-.. "); input.delete(0, 1); } else if (input.toString().startsWith("M")) { output.append("-- "); input.delete(0, 1); } else if (input.toString().startsWith("N")) { output.append("-. "); input.delete(0, 1); } else if (input.toString().startsWith("O")) { output.append("--- "); input.delete(0, 1); } else if (input.toString().startsWith("P")) { output.append(".--. "); input.delete(0, 1); } else if (input.toString().startsWith("Q")) { output.append("--.- "); input.delete(0, 1); } else if (input.toString().startsWith("R")) { output.append(".-. "); input.delete(0, 1); } else if (input.toString().startsWith("S")) { output.append("... "); input.delete(0, 1); } else if (input.toString().startsWith("T")) { output.append("- "); input.delete(0, 1); } else if (input.toString().startsWith("U")) { output.append("..- "); input.delete(0, 1); } else if (input.toString().startsWith("V")) { output.append("...- "); input.delete(0, 1); } else if (input.toString().startsWith("W")) { output.append(".-- "); input.delete(0, 1); } else if (input.toString().startsWith("X")) { output.append("-..- "); input.delete(0, 1); } else if (input.toString().startsWith("Y")) { output.append("-.-- "); input.delete(0, 1); } else if (input.toString().startsWith("Z")) { output.append("--.. "); input.delete(0, 1); } else if (input.toString().startsWith("0")) { output.append("----- "); input.delete(0, 1); } else if (input.toString().startsWith("1")) { output.append(".---- "); input.delete(0, 1); } else if (input.toString().startsWith("2")) { output.append("..--- "); input.delete(0, 1); } else if (input.toString().startsWith("3")) { output.append("...-- "); input.delete(0, 1); } else if (input.toString().startsWith("4")) { output.append("....- "); input.delete(0, 1); } else if (input.toString().startsWith("5")) { output.append("..... "); input.delete(0, 1); } else if (input.toString().startsWith("6")) { output.append("-.... "); input.delete(0, 1); } else if (input.toString().startsWith("7")) { output.append("--... "); input.delete(0, 1); } else if (input.toString().startsWith("8")) { output.append("---.. "); input.delete(0, 1); } else if (input.toString().startsWith("9")) { output.append("----. "); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append(".-.- "); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("---. "); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("..-- "); input.delete(0, 1); } else if (input.toString().startsWith("")) { output.append("...--... "); input.delete(0, 1); } else if (input.toString().startsWith(".")) { output.append(".-.-.- "); input.delete(0, 1); } else if (input.toString().startsWith(",")) { output.append("--..-- "); input.delete(0, 1); } else if (input.toString().startsWith(":")) { output.append("---... "); input.delete(0, 1); } else if (input.toString().startsWith(";")) { output.append("-.-.-. "); input.delete(0, 1); } else if (input.toString().startsWith("?")) { output.append("..--.. "); input.delete(0, 1); } else if (input.toString().startsWith("!")) { output.append("-.-.-- "); input.delete(0, 1); } else if (input.toString().startsWith("-")) { output.append("-....- "); input.delete(0, 1); } else if (input.toString().startsWith("_")) { output.append("..--.- "); input.delete(0, 1); } else if (input.toString().startsWith("(")) { output.append("-.--. "); input.delete(0, 1); } else if (input.toString().startsWith(")")) { output.append("-.--.- "); input.delete(0, 1); } else if (input.toString().startsWith("=")) { output.append("-...- "); input.delete(0, 1); } else if (input.toString().startsWith("+")) { output.append(".-.-. "); input.delete(0, 1); } else if (input.toString().startsWith("/")) { output.append("-..-. "); input.delete(0, 1); } else if (input.toString().startsWith("@")) { output.append(".--.-. "); input.delete(0, 1); } else if (input.toString().startsWith("'")) { output.append(".----. "); input.delete(0, 1); } else if (input.toString().startsWith("$")) { output.append("...-..- "); input.delete(0, 1); } else { tfOutput = "Code not listed or wrong."; } } if (output.toString().endsWith(" ")) { output.delete(output.length() - 3, output.length()); } tfOutput = output.toString(); lastFragment = 1; After(view); } }
From source file:org.ala.spatial.util.AnalysisJobMaxent.java
public void readReplaceBetween(String fname, String startOldText, String endOldText, String replText) { String line;// w w w .ja v a 2 s.c o m StringBuffer sb = new StringBuffer(); try { FileInputStream fis = new FileInputStream(fname); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } int start, end; start = sb.indexOf(startOldText); if (start >= 0) { end = sb.indexOf(endOldText, start + 1); sb.replace(start, end + endOldText.length(), replText); } reader.close(); BufferedWriter out = new BufferedWriter(new FileWriter(fname)); out.write(sb.toString()); out.close(); } catch (Throwable e) { System.err.println("*** exception ***"); e.printStackTrace(System.out); } }
From source file:org.jboss.dashboard.commons.text.StringUtil.java
/** * @param template the parametrized template * @param params the parameters structure * @param leftMark the left delimiter of the parameters to replace * @param rightMark the right delimiter of the parameters to replace * @param nullifyNotFound if true, change the not found params to "null"; * if false, don't change * @return The template with parameters replaced * @see #parseASCIITemplate(String,Map)/*from ww w. ja v a 2s . c o m*/ */ public static String parseASCIITemplate(String template, Map params, String leftMark, String rightMark, boolean nullifyNotFound) { if (params == null) { if (!nullifyNotFound) { return template; } else { // Como hay que cambiar los parmetros por "null", creamos una // tabla de parmetros vaca params = new HashMap(); } } StringBuffer buffer = new StringBuffer(template); int leftIndex, rightIndex = 0; while ((leftIndex = indexOf(leftMark, rightIndex, buffer)) != -1) { rightIndex = indexOf(rightMark, leftIndex + leftMark.length(), buffer); if (rightIndex == -1) { break; // no right mark, this is likely to mean bad syntax } String param = buffer.substring(leftIndex + leftMark.length(), rightIndex); rightIndex += rightMark.length(); if (params.containsKey(param)) { Object ref = params.get(param); String value = ref == null ? "null" : ref.toString(); buffer.replace(leftIndex, rightIndex, value); rightIndex -= leftMark.length() + param.length() + rightMark.length(); rightIndex += value.length(); } else if (nullifyNotFound) { String value = "null"; buffer.replace(leftIndex, rightIndex, value); rightIndex -= leftMark.length() + param.length() + rightMark.length(); rightIndex += value.length(); } } return buffer.toString(); }
From source file:gate.util.Files.java
/** * This method updates an XML element with a new set of attributes. * If the element is not found the XML is unchanged. The attributes * keys and values must all be Strings.//www . j a v a2 s . c om * * @param xml A stream of the XML data. * @param elementName The name of the element to update. * @param newAttrs The new attributes to place on the element. * @return A string of the whole XML source, with the element updated. */ public static String updateXmlElement(BufferedReader xml, String elementName, Map<String, String> newAttrs) throws IOException { String line = null; String nl = Strings.getNl(); StringBuffer newXml = new StringBuffer(); // read the whole source while ((line = xml.readLine()) != null) { newXml.append(line); newXml.append(nl); } // find the location of the element int start = newXml.toString().indexOf("<" + elementName); if (start == -1) return newXml.toString(); int end = newXml.toString().indexOf(">", start); if (end == -1) return newXml.toString(); // check if the old element is empty (ends in "/>") or not boolean isEmpty = false; if (newXml.toString().charAt(end - 1) == '/') isEmpty = true; // create the new element string with the new attributes StringBuffer newElement = new StringBuffer(); newElement.append("<"); newElement.append(elementName); // add in the new attributes Iterator<Map.Entry<String, String>> iter = newAttrs.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); String key = entry.getKey(); String value = entry.getValue(); newElement.append(" "); newElement.append(DocumentXmlUtils.combinedNormalisation(key)); newElement.append("=\""); newElement.append(DocumentXmlUtils.combinedNormalisation(value)); newElement.append("\"" + nl); } // terminate the element if (isEmpty) newElement.append("/"); newElement.append(">"); // replace the old string newXml.replace(start, end + 1, newElement.toString()); return newXml.toString(); }