List of usage examples for java.lang StringBuffer replace
@Override public synchronized StringBuffer replace(int start, int end, String str)
From source file:org.wso2.mobile.utils.persistence.EMMDBInitializer.java
private void executeSQLScript() throws Exception { String databaseType = EMMDBInitializer.getDatabaseType(dataSource.getConnection()); boolean keepFormat = false; if ("oracle".equals(databaseType)) { delimiter = "/"; } else if ("db2".equals(databaseType)) { delimiter = "/"; } else if ("openedge".equals(databaseType)) { delimiter = "/"; keepFormat = true;/*from w ww . j a v a2 s .co m*/ } String dbScriptLocation = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbScriptLocation); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!keepFormat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } sql.append(keepFormat ? "\n" : " ").append(line); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (!keepFormat && line.contains("--")) { sql.append("\n"); } if ((checkStringBufferEndsWith(sql, delimiter))) { executeSQL(sql.substring(0, sql.length() - delimiter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeSQL(sql.toString()); } } catch (IOException e) { log.error("Error occurred while executing SQL script for creating emm database", e); throw new Exception("Error occurred while executing SQL script for creating emm database", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.wso2.carbon.social.sql.SocialDBInitilizer.java
private void executeSQLScript() throws Exception { String databaseType = SocialDBInitilizer.getDatabaseType(dataSource.getConnection()); log.info("Executing DB script for :" + databaseType); boolean keepFormat = false; if ("oracle".equals(databaseType)) { delimiter = "/"; } else if ("db2".equals(databaseType)) { delimiter = "/"; } else if ("openedge".equals(databaseType)) { delimiter = "/"; keepFormat = true;/*from w ww. java2 s . c o m*/ } String dbScriptLocation = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbScriptLocation); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!keepFormat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } sql.append(keepFormat ? "\n" : " ").append(line); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (!keepFormat && line.contains("--")) { sql.append("\n"); } if ((checkStringBufferEndsWith(sql, delimiter))) { executeSQL(sql.substring(0, sql.length() - delimiter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeSQL(sql.toString()); } } catch (IOException e) { log.error("Error occurred while executing SQL script for creating SOCIAL database", e); throw new Exception("Error occurred while executing SQL script for creating SOCIAL database", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.wso2.carbon.identity.application.common.persistence.IdentityApplicationDBInitializer.java
private void executeSQLScript() throws IdentityApplicationManagementException, SQLException, IOException { String databaseType = getDatabaseType(dataSource.getConnection()); if (databaseType == null) { String msg = "Unsupported database: Database will not be created automatically by the Carbon Server. " + "Please create the database using appropriate database scripts for the database."; log.warn(msg);//from www . j a v a2 s .co m } boolean keepFormat = false; if ("oracle".equals(databaseType)) { delimiter = "/"; } else if ("db2".equals(databaseType)) { delimiter = "/"; } String dbScriptLocation = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbScriptLocation); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!keepFormat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } sql.append(keepFormat ? "\n" : " ").append(line); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (!keepFormat && line.contains("--")) { sql.append("\n"); } if ((checkStringBufferEndsWith(sql, delimiter))) { executeSQL(sql.substring(0, sql.length() - delimiter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeSQL(sql.toString()); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } } }
From source file:org.wso2.carbon.dashboard.portal.core.datasource.DSDataSourceManager.java
/** * To execute the table creation script depending on the datasource user using * * @throws DashboardPortalException// w ww .jav a 2 s. co m */ private void executeScript() throws DashboardPortalException { String databaseType; try { databaseType = dataSource.getConnection().getMetaData().getDatabaseProductName().toLowerCase(); } catch (SQLException e) { throw new DashboardPortalException("Error occurred while getting database type", e); } if (databaseType.equalsIgnoreCase(DataSourceConstants.MSSQL_PRODUCT_NAME)) { databaseType = DataSourceConstants.MSSQL_SCRIPT_NAME; } if (databaseType.equalsIgnoreCase(DataSourceConstants.ORACLE_SCRIPT_NAME) || databaseType.equalsIgnoreCase(DataSourceConstants.DB2_SCRIPT_NAME) || databaseType.equalsIgnoreCase(DataSourceConstants.ORACLE_RAC_SCRIPT_NAME)) { delimeter = "/"; } String dbScriptLocation = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbScriptLocation); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } sql.append(" ").append(line); if (line.contains("--")) { sql.append("\n"); } if (sql.toString().endsWith(delimeter)) { executeQuery(sql.substring(0, sql.length() - delimeter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeQuery(sql.toString()); } } catch (IOException e) { throw new DashboardPortalException( "Error occurred while executing SQL script for creating Dashboard Server database", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { log.error("Error occurred while closing stream for Identity SQL script", e); } } } }
From source file:org.wso2.carbon.identity.core.persistence.IdentityDBInitializer.java
private void executeSQLScript() { String databaseType = null;/*from w w w . j a v a 2 s . com*/ try { databaseType = IdentityDBInitializer.getDatabaseType(dataSource.getConnection()); } catch (Exception e) { throw new IdentityRuntimeException("Error occurred while getting database type"); } boolean keepFormat = false; if ("oracle".equals(databaseType)) { delimiter = "/"; } else if ("db2".equals(databaseType)) { delimiter = "/"; } else if ("openedge".equals(databaseType)) { delimiter = "/"; keepFormat = true; } String dbScriptLocation = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbScriptLocation); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!keepFormat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } sql.append(keepFormat ? "\n" : " ").append(line); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (!keepFormat && line.contains("--")) { sql.append("\n"); } if ((checkStringBufferEndsWith(sql, delimiter))) { executeSQL(sql.substring(0, sql.length() - delimiter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeSQL(sql.toString()); } } catch (IOException e) { throw new IdentityRuntimeException( "Error occurred while executing SQL script for creating identity database", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { log.error("Error occurred while closing stream for Identity SQL script", e); } } } }
From source file:org.wso2.carbon.utils.dbcreator.DatabaseCreator.java
/** * executes content in SQL script//from w w w .ja v a 2 s.com * * @return StringBuffer * @throws Exception */ private void executeSQLScript() throws Exception { String databaseType = DatabaseCreator.getDatabaseType(this.conn); boolean keepFormat = false; if ("oracle".equals(databaseType)) { delimiter = "/"; } else if ("db2".equals(databaseType)) { delimiter = "/"; } else if ("openedge".equals(databaseType)) { delimiter = "/"; keepFormat = true; } String dbscriptName = getDbScriptLocation(databaseType); StringBuffer sql = new StringBuffer(); BufferedReader reader = null; try { InputStream is = new FileInputStream(dbscriptName); reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!keepFormat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } sql.append(keepFormat ? "\n" : " ").append(line); // SQL defines "--" as a comment to EOL // and in Oracle it may contain a hint // so we cannot just remove it, instead we must end it if (!keepFormat && line.indexOf("--") >= 0) { sql.append("\n"); } if ((checkStringBufferEndsWith(sql, delimiter))) { executeSQL(sql.substring(0, sql.length() - delimiter.length())); sql.replace(0, sql.length(), ""); } } // Catch any statements not followed by ; if (sql.length() > 0) { executeSQL(sql.toString()); } } catch (IOException e) { log.error("Error occurred while executing SQL script for creating registry database", e); throw new Exception("Error occurred while executing SQL script for creating registry database", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.apache.axis.wsdl.toJava.JavaGeneratorFactory.java
/** Refactored to call recursively for JAX-RPC 1.1 spec 4.2.5. */ protected int javifyTypeEntryName(SymbolTable symbolTable, TypeEntry entry, HashMap anonQNames, int uniqueNum) { TypeEntry tEntry = entry;//from w w w . j a v a2 s. c o m String dims = tEntry.getDimensions(); TypeEntry refType = tEntry.getRefType(); while (refType != null) { tEntry = refType; dims += tEntry.getDimensions(); refType = tEntry.getRefType(); } TypeEntry te = tEntry; while (te != null) { TypeEntry base = SchemaUtils.getBaseType(te, symbolTable); if (base == null) break; uniqueNum = javifyTypeEntryName(symbolTable, base, anonQNames, uniqueNum); if (Utils.getEnumerationBaseAndValues(te.getNode(), symbolTable) == null && SchemaUtils.getComplexElementExtensionBase(te.getNode(), symbolTable) == null && te.getContainedAttributes() == null) { if (!SchemaUtils.isSimpleTypeWithUnion(te.getNode())) { if (base.isSimpleType()) { // Case 1: // <simpleType name="mySimpleStringType"> // <restriction base="xs:string"> // </restriction> // </simpleType> te.setSimpleType(true); te.setName(base.getName()); te.setRefType(base); } if (base.isBaseType()) { // Case 2: // <simpleType name="FooString"> // <restriction base="foo:mySimpleStringType"> // </restriction> // </simpleType> te.setBaseType(true); te.setName(base.getName()); te.setRefType(base); } } } if (!te.isSimpleType()) break; te = base; } // Need to javify the ref'd TypeEntry if it was not // already processed if (tEntry.getName() == null) { boolean processed = false; // true if the java name is already determined // Get the QName of the ref'd TypeEntry, which // is will be used to javify the name QName typeQName = tEntry.getQName(); // In case of <xsd:list itemType="...">, // set typeQName to the value of the itemType attribute. QName itemType = SchemaUtils.getListItemType(tEntry.getNode()); if (itemType != null) { // Get the typeEntry so we know the absolute base type TypeEntry itemEntry = symbolTable.getTypeEntry(itemType, false); // TODO - If the itemEntry is not found, we need to throw // an exception. "Item is referenced, but not defined" javifyTypeEntryName(symbolTable, itemEntry, anonQNames, uniqueNum); // Grab the referenced type, If it's there. TypeEntry refedEntry = itemEntry.getRefType(); QName baseName = refedEntry == null ? itemEntry.getQName() : refedEntry.getQName(); typeQName = new QName(baseName.getNamespaceURI(), baseName.getLocalPart() + "[]"); } if (emitter.isDeploy()) { Class class1 = (Class) emitter.getQName2ClassMap().get(typeQName); if (class1 != null && !class1.isArray()) { tEntry.setName(getJavaClassName(class1)); processed = true; } } if (!processed) { if ((typeQName.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) < 0)) { // Normal Case: The ref'd type is not anonymous // Simply construct the java name from // the qName tEntry.setName(emitter.getJavaName(typeQName)); } else { // This is an anonymous type name. // Axis uses '>' as a nesting token to generate // unique qnames for anonymous types. // Only consider the localName after the last '>' // when generating the java name // String localName = typeQName.getLocalPart(); // localName = // localName.substring( // localName.lastIndexOf( // SymbolTable.ANON_TOKEN)+1); // typeQName = new QName(typeQName.getNamespaceURI(), // localName); String localName = typeQName.getLocalPart(); // Check to see if this is an anonymous type, // if it is, replace Axis' ANON_TOKEN with // an underscore to make sure we don't run // into name collisions with similarly named // non-anonymous types StringBuffer sb = new StringBuffer(localName); int aidx; while ((aidx = sb.toString().indexOf(SymbolTable.ANON_TOKEN)) > -1) { sb.replace(aidx, aidx + SymbolTable.ANON_TOKEN.length(), ""); char c = sb.charAt(aidx); if (Character.isLetter(c) && Character.isLowerCase(c)) { sb.setCharAt(aidx, Character.toUpperCase(c)); } } localName = sb.toString(); typeQName = new QName(typeQName.getNamespaceURI(), localName); if (emitter.isTypeCollisionProtection() && !emitter.getNamespaceExcludes() .contains(new NamespaceSelector(typeQName.getNamespaceURI()))) { // If there is already an existing type, // there will be a collision. // If there is an existing anon type, // there will be a collision. // In both cases, mangle the name. if (symbolTable.getType(typeQName) != null || anonQNames.get(typeQName) != null) { localName += "Type" + uniqueNum++; typeQName = new QName(typeQName.getNamespaceURI(), localName); } anonQNames.put(typeQName, typeQName); } // Now set the name with the constructed qname tEntry.setName(emitter.getJavaName(typeQName)); } } // if (!processed) Vector elements = tEntry.getContainedElements(); if (elements != null) { for (int i = 0; i < elements.size(); i++) { ElementDecl elem = (ElementDecl) elements.get(i); String varName = emitter.getJavaVariableName(typeQName, elem.getQName(), true); elem.setName(varName); } } Vector attributes = tEntry.getContainedAttributes(); if (attributes != null) { for (int i = 0; i < attributes.size(); i++) { ContainedAttribute attr = (ContainedAttribute) attributes.get(i); String varName = emitter.getJavaVariableName(typeQName, attr.getQName(), false); attr.setName(varName); } } } // Set the entry with the same name as the ref'd entry // but add the appropriate amount of dimensions entry.setName(tEntry.getName() + dims); return uniqueNum; }
From source file:org.jivesoftware.community.util.StringUtils.java
public static String wordWrap(String input, int width, Locale locale) { if (input == null) return ""; if (width < 5) return input; if (width >= input.length()) return input; if (locale == null) locale = JiveGlobals.getLocale(); StringBuffer buf = new StringBuffer(input); boolean endOfLine = false; int lineStart = 0; for (int i = 0; i < buf.length(); i++) { if (buf.charAt(i) == '\n') { lineStart = i + 1;// w w w .j a v a 2s .co m endOfLine = true; } if (i <= (lineStart + width) - 1) continue; if (!endOfLine) { int limit = i - lineStart - 1; BreakIterator breaks = BreakIterator.getLineInstance(locale); breaks.setText(buf.substring(lineStart, i)); int end = breaks.last(); if (end == limit + 1 && !Character.isWhitespace(buf.charAt(lineStart + end))) end = breaks.preceding(end - 1); if (end != -1 && end == limit + 1) { buf.replace(lineStart + end, lineStart + end + 1, "\n"); lineStart += end; continue; } if (end != -1 && end != 0) { buf.insert(lineStart + end, '\n'); lineStart = lineStart + end + 1; } else { buf.insert(i, '\n'); lineStart = i + 1; } } else { buf.insert(i, '\n'); lineStart = i + 1; endOfLine = false; } } return buf.toString(); }
From source file:Main.java
/** * Escape the given XML string (character text or attribute value; unescaped) * according to either XML 1.0 or XML 1.1 specification. * //from w ww .j a va 2s. c o m * Only element content (excluding markup) and attributes (values) need to be escaped. * Markup, comments, CDATA sections, and prosessing instructions need to be left unchanged. * * * @param writer The writer where the escaped string is written to. * @param str The given XML string (character text or attribute value; unescaped). * @param xmlVersion The XML version (1.0 or 1.1). * @param isAttributeValue <code>true</code> if the given XML string is attribute value, * otherwise <code>false</code>. */ private static void escape(Writer writer, String str, String xmlVersion, boolean isAttributeValue) throws Exception { StringBuffer sb = new StringBuffer(""); // First, escape all characters except the > character for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); switch (c) { // less-than: // Always escape. case '<': { sb.append("<"); break; } // ampersand: // Always escape. case '&': { sb.append("&"); break; } // CR (CARRIAGE RETURN): // Always escape. case '\r': { sb.append("
"); break; } // LF (LINE FEED) or sometimes NL (NEW LINE): // Escape in attribute values. case '\n': { if (isAttributeValue == true) { sb.append("
"); } else { sb.append("\n"); } break; } // TAB (TABULATOR): // Escape in attribute values. case '\t': { if (isAttributeValue == true) { sb.append("	"); } else { sb.append("\t"); } break; } // single-quote: // Escape in attribute values quoted with the same kind of quote. case '\'': { if (isAttributeValue == true) { sb.append("'"); } else { sb.append("\'"); } break; } // double-quote: // Escape in attribute values quoted with the same kind of quote. case '\"': { if (isAttributeValue == true) { sb.append("""); } else { sb.append("\""); } break; } // else, default print char default: { if (("1.1".equals(xmlVersion) == true) && (((c >= 0x1) && (c <= 0x1F)) || ((c >= 0x7F) && (c <= 0x9F)) || (c == 0x2028))) { sb.append("&#x"); sb.append(Integer.toHexString(c).toUpperCase()); sb.append(";"); } else { sb.append(c); } } } } // Finally, escape the > character if necessary // greater-than: // Escape in text if it immediately follows two close-square-brackets, as // that sequence is only allowed as the end of a CDATA marked section. if (isAttributeValue == false) { sb.replace(0, sb.length(), sb.toString().replace("]]>", "]]>")); } writer.write(sb.toString()); }
From source file:com.isecpartners.gizmo.HttpRequest.java
private String rewriteMethodLine(StringBuffer workingContents) { int host_index = workingContents.toString().toUpperCase().indexOf("HOST:"); if (host_index != -1) { int host_line_end_index = workingContents.toString().indexOf("\r\n", host_index); host = workingContents.toString().substring(host_index + 6, host_line_end_index).trim(); } else {/*from ww w .ja v a 2s. c o m*/ this.url = makeURL(workingContents); host = url.substring(url.indexOf("//") + 2, url.indexOf("/", url.indexOf("//") + 2)); } if (host.contains(":")) { port = Integer.parseInt(host.substring(host.indexOf(":") + 1)); user_defined_port = true; host = host.substring(0, host.indexOf(":")); } final String GET_HTTP = "^[a-zA-Z]+\\s+[hH][tT][tT][pP].*"; if (header.matches(GET_HTTP)) { String tmp = workingContents.toString().substring(0, workingContents.indexOf("\r\n")).trim() + "\r\n"; int start = tmp.indexOf("http://") + 8; String prefix = tmp.substring(0, start - 8); int end = tmp.indexOf("/", start); String postfix = tmp.substring(end, tmp.length()); String whole = prefix + postfix; workingContents.replace(0, tmp.length(), whole); } return host; }