Example usage for java.lang StringBuffer indexOf

List of usage examples for java.lang StringBuffer indexOf

Introduction

In this page you can find the example usage for java.lang StringBuffer indexOf.

Prototype

@Override
public int indexOf(String str) 

Source Link

Usage

From source file:org.apache.poi.ss.examples.ToCSV.java

/**
 * Checks to see whether the field - which consists of the formatted
 * contents of an Excel worksheet cell encapsulated within a String - contains
 * any embedded characters that must be escaped. The method is able to
 * comply with either Excel's or UNIX formatting conventions in the
 * following manner;/* w  ww .jav  a 2  s. c  o  m*/
 *
 * With regard to UNIX conventions, if the field contains any embedded
 * field separator or EOL characters they will each be escaped by prefixing
 * a leading backspace character. These are the only changes that have yet
 * emerged following some research as being required.
 *
 * Excel has other embedded character escaping requirements, some that emerged
 * from empirical testing, other through research. Firstly, with regards to
 * any embedded speech marks ("), each occurrence should be escaped with
 * another speech mark and the whole field then surrounded with speech marks.
 * Thus if a field holds <em>"Hello" he said</em> then it should be modified
 * to appear as <em>"""Hello"" he said"</em>. Furthermore, if the field
 * contains either embedded separator or EOL characters, it should also
 * be surrounded with speech marks. As a result <em>1,400</em> would become
 * <em>"1,400"</em> assuming that the comma is the required field separator.
 * This has one consequence in, if a field contains embedded speech marks
 * and embedded separator characters, checks for both are not required as the
 * additional set of speech marks that should be placed around ay field
 * containing embedded speech marks will also account for the embedded
 * separator.
 *
 * It is worth making one further note with regard to embedded EOL
 * characters. If the data in a worksheet is exported as a CSV file using
 * Excel itself, then the field will be surounded with speech marks. If the
 * resulting CSV file is then re-imports into another worksheet, the EOL
 * character will result in the original simgle field occupying more than
 * one cell. This same 'feature' is replicated in this classes behaviour.
 *
 * @param field An instance of the String class encapsulating the formatted
 *        contents of a cell on an Excel worksheet.
 * @return A String that encapsulates the formatted contents of that
 *         Excel worksheet cell but with any embedded separator, EOL or
 *         speech mark characters correctly escaped.
 */
private String escapeEmbeddedCharacters(String field) {
    StringBuffer buffer = null;

    // If the fields contents should be formatted to confrom with Excel's
    // convention....
    if (this.formattingConvention == ToCSV.EXCEL_STYLE_ESCAPING) {

        // Firstly, check if there are any speech marks (") in the field;
        // each occurrence must be escaped with another set of spech marks
        // and then the entire field should be enclosed within another
        // set of speech marks. Thus, "Yes" he said would become
        // """Yes"" he said"
        if (field.contains("\"")) {
            buffer = new StringBuffer(field.replaceAll("\"", "\\\"\\\""));
            buffer.insert(0, "\"");
            buffer.append("\"");
        } else {
            // If the field contains either embedded separator or EOL
            // characters, then escape the whole field by surrounding it
            // with speech marks.
            buffer = new StringBuffer(field);
            if ((buffer.indexOf(this.separator)) > -1 || (buffer.indexOf("\n")) > -1) {
                buffer.insert(0, "\"");
                buffer.append("\"");
            }
        }
        return (buffer.toString().trim());
    }
    // The only other formatting convention this class obeys is the UNIX one
    // where any occurrence of the field separator or EOL character will
    // be escaped by preceding it with a backslash.
    else {
        if (field.contains(this.separator)) {
            field = field.replaceAll(this.separator, ("\\\\" + this.separator));
        }
        if (field.contains("\n")) {
            field = field.replaceAll("\n", "\\\\\n");
        }
        return (field);
    }
}

From source file:com.sap.research.connectivity.gw.GWOperationsUtils.java

public void addRemoteFieldInPersistenceMethods(JavaSourceFileEditor entityClassFile,
        Map.Entry<String[], String> fieldObj) {
    ArrayList<JavaSourceMethod> globalMethodList = entityClassFile.getGlobalMethodList();
    String pluralRemoteEntity = GwUtils.getInflectorPlural(entityClassFile.CLASS_NAME, Locale.ENGLISH);
    String smallRemoteEntity = StringUtils.uncapitalize(entityClassFile.CLASS_NAME);

    for (JavaSourceMethod method : globalMethodList) {
        String methodName = method.getMethodName();
        /*//from   ww  w .ja  va  2  s.  c  o  m
         * We insert the new field in the persist and merge methods
         */
        if (methodName.endsWith("persist") || methodName.endsWith("merge")) {
            StringBuffer methodBody = new StringBuffer(method.getMethodBody());
            methodBody.insert(methodBody.lastIndexOf(".execute()"), makeGWPersistFieldCode("", fieldObj));
            method.setMethodBody(methodBody.toString());
        }
        /*
         * We insert the new field in the findAll and find<Entity>Entries methods
         */
        else if (methodName.endsWith("findAll" + pluralRemoteEntity)
                || methodName.endsWith("find" + entityClassFile.CLASS_NAME + "Entries")) {
            StringBuffer methodBody = new StringBuffer(method.getMethodBody());
            methodBody.insert(methodBody.indexOf("virtual" + entityClassFile.CLASS_NAME + "List.add"),
                    makeGWShowFieldCode("", smallRemoteEntity + "Instance", smallRemoteEntity + "Item",
                            fieldObj));
            method.setMethodBody(methodBody.toString());
        }
        /*
         * We insert the new field in the find<Entity> method
         */
        else if (methodName.endsWith("find" + entityClassFile.CLASS_NAME)) {
            StringBuffer methodBody = new StringBuffer(method.getMethodBody());
            methodBody.insert(methodBody.indexOf("return "), makeGWShowFieldCode("",
                    "virtual" + entityClassFile.CLASS_NAME, smallRemoteEntity, fieldObj));
            method.setMethodBody(methodBody.toString());
        }
    }
}

From source file:com.twelve.capital.external.feed.util.HttpImpl.java

@Override
public String getCompleteURL(HttpServletRequest request) {
    StringBuffer sb = request.getRequestURL();

    if (sb == null) {
        sb = new StringBuffer();
    }/*  w w  w .ja  v a  2  s  .com*/

    if (request.getQueryString() != null) {
        sb.append(StringPool.QUESTION);
        sb.append(request.getQueryString());
    }

    String proxyPath = PortalUtil.getPathProxy();

    if (Validator.isNotNull(proxyPath)) {
        int x = sb.indexOf(Http.PROTOCOL_DELIMITER) + Http.PROTOCOL_DELIMITER.length();
        int y = sb.indexOf(StringPool.SLASH, x);

        sb.insert(y, proxyPath);
    }

    String completeURL = sb.toString();

    if (request.isRequestedSessionIdFromURL()) {
        HttpSession session = request.getSession();

        String sessionId = session.getId();

        completeURL = PortalUtil.getURLWithSessionId(completeURL, sessionId);
    }

    if (_log.isWarnEnabled()) {
        if (completeURL.contains("?&")) {
            _log.warn("Invalid url " + completeURL);
        }
    }

    return completeURL;
}

From source file:de.rrze.idmone.utils.jpwgen.PwGenerator.java

/**
 * The real password generation is performed in this method
 * /*from   w ww.  j  ava  2 s.  co m*/
 * @param size
 *            the length of the password
 * @param pw_flags
 *            the settings for the password
 * @return the newly created password
 */
private synchronized static String phonemes(int size, int pw_flags, Random random) {
    int c, i, len, flags, feature_flags;
    int prev, should_be;
    boolean first;
    String str;
    char ch;
    StringBuffer buf = new StringBuffer();

    do {
        buf.delete(0, buf.length());
        feature_flags = pw_flags;
        c = 0;
        prev = 0;
        should_be = 0;
        first = true;
        should_be = random.nextBoolean() ? VOWEL : CONSONANT;

        while (c < size) {

            i = random.nextInt(PW_ELEMENTS.length);
            str = PW_ELEMENTS[i].getValue();
            len = str.length();
            flags = PW_ELEMENTS[i].getType();
            /* Filter on the basic type of the next element */
            if ((flags & should_be) == 0) {
                continue;
            }
            /* Handle the NOT_FIRST flag */
            if (first && ((flags & NOT_FIRST) != 0))
                continue;
            /* Don't allow VOWEL followed a Vowel/Dipthong pair */
            if (((prev & VOWEL) != 0) && ((flags & VOWEL) != 0) && ((flags & DIPTHONG) != 0))
                continue;
            /* Don't allow us to overflow the buffer */
            if (len > size - c)
                continue;
            /*
             * OK, we found an element which matches our criteria, let's do
             * it!
             */
            buf.append(str);

            c += len;
            /* Handle the AMBIGUOUS flag */
            if ((pw_flags & PW_AMBIGUOUS) != 0) {
                int k = -1;
                for (int j = 0; j < PW_AMBIGUOUS_SYMBOLS.length(); j++) {
                    k = buf.indexOf(String.valueOf(PW_AMBIGUOUS_SYMBOLS.charAt(j)));
                    if (k != -1)
                        break;
                }
                if (k != -1) {
                    buf.delete(k, buf.length());
                    c = buf.length();
                }
            }

            /* Time to stop? */
            if (c >= size) {
                // System.out.println("BREAK 1: "+c + " - "+size);
                break;
            }
            /*
             * Handle PW_DIGITS
             */
            if ((pw_flags & PW_DIGITS) != 0) {
                if (!first && (random.nextInt(10) < 3)) {
                    do {
                        ch = (new Integer(random.nextInt(10))).toString().charAt(0);
                    } while (((pw_flags & PW_AMBIGUOUS) != 0) && (PW_AMBIGUOUS_SYMBOLS.indexOf(ch) != -1));
                    c++;
                    buf = buf.append(ch);
                    feature_flags &= ~PW_DIGITS;

                    first = true;
                    prev = 0;
                    should_be = random.nextBoolean() ? VOWEL : CONSONANT;
                    continue;
                }
            }

            /* Handle PW_SYMBOLS */
            if ((pw_flags & PW_SYMBOLS) != 0) {
                if (!first && (random.nextInt(10) < 2)) {
                    do {
                        ch = PW_SPECIAL_SYMBOLS.charAt(random.nextInt(PW_SPECIAL_SYMBOLS.length()));
                    } while (((pw_flags & PW_AMBIGUOUS) != 0) && (PW_AMBIGUOUS_SYMBOLS.indexOf(ch) != -1));
                    c++;
                    buf = buf.append(ch);
                    feature_flags &= ~PW_SYMBOLS;
                }
            } else if ((pw_flags & PW_SYMBOLS_REDUCED) != 0) {
                if (!first && (random.nextInt(10) < 2)) {
                    do {
                        ch = PW_SPECIAL_SYMBOLS_REDUCED
                                .charAt(random.nextInt(PW_SPECIAL_SYMBOLS_REDUCED.length()));
                    } while (((pw_flags & PW_AMBIGUOUS) != 0) && (PW_AMBIGUOUS_SYMBOLS.indexOf(ch) != -1));
                    c++;
                    buf = buf.append(ch);
                    feature_flags &= ~PW_SYMBOLS_REDUCED;
                }
            }

            /* Handle PW_UPPERS */
            if ((pw_flags & PW_UPPERS) != 0) {
                if ((first || ((flags & CONSONANT) != 0)) && (random.nextInt(10) < 2)) {
                    int lastChar = buf.length() - 1;
                    buf.setCharAt(lastChar, Character.toUpperCase(buf.charAt(lastChar)));
                    feature_flags &= ~PW_UPPERS;
                }
            }

            /*
             * OK, figure out what the next element should be
             */
            if (should_be == CONSONANT) {
                should_be = VOWEL;
            } else { /* should_be == VOWEL */
                if (((prev & VOWEL) != 0) || ((flags & DIPTHONG) != 0) || (random.nextInt(10) > 3))
                    should_be = CONSONANT;
                else
                    should_be = VOWEL;
            }
            prev = flags;
            first = false;

        }
    } while ((feature_flags & (PW_UPPERS | PW_DIGITS | PW_SYMBOLS | PW_SYMBOLS_REDUCED)) != 0);

    return buf.toString();
}

From source file:org.opencms.search.CmsSearchParameters.java

/**
 * Creates a merged parameter set from this parameters, restricted by the given other parameters.<p>
 * //from   w w w .ja  va2s  . c o m
 * This is mainly intended for "search in search result" functions.<p>
 * 
 * The restricted query is build of the queries of both parameters, appended with AND.<p>
 * 
 * The lists in the restriction for <code>{@link #getFields()}</code>, <code>{@link #getRoots()}</code> and
 * <code>{@link #getCategories()}</code> are <b>intersected</b> with the lists of this search parameters. Only
 * elements contained in both lists are included for the created search parameters. 
 * If a list in either the restriction or in this search parameters is <code>null</code>, 
 * the list from the other search parameters is used directly.<p> 
 * 
 * The values for
 * <code>{@link #isCalculateCategories()}</code>
 * and <code>{@link #getSort()}</code> of this parameters are used for the restricted parameters.<p>
 * 
 * @param restriction the parameters to restrict this parameters with
 * @return the restricted parameters
 */
@SuppressWarnings("unchecked")
public CmsSearchParameters restrict(CmsSearchParameters restriction) {

    // append queries
    StringBuffer query = new StringBuffer(256);
    if (getQuery() != null) {
        // don't blow up unnecessary closures (if CmsSearch is reused and restricted several times)
        boolean closure = !getQuery().startsWith("+(");
        if (closure) {
            query.append("+(");
        }
        query.append(getQuery());
        if (closure) {
            query.append(")");
        }
    }
    if (restriction.getQuery() != null) {
        // don't let Lucene max terms be exceeded in case someone reuses a CmsSearch and continuously restricts 
        // query with the same restrictions...
        if (query.indexOf(restriction.getQuery()) < 0) {
            query.append(" +(");
            query.append(restriction.getQuery());
            query.append(")");
        }
    }

    // restrict fields
    List<String> fields = null;
    if ((m_fields != null) && (m_fields.size() > 0)) {
        if ((restriction.getFields() != null) && (restriction.getFields().size() > 0)) {
            fields = ListUtils.intersection(m_fields, restriction.getFields());
        } else {
            fields = m_fields;
        }
    } else {
        fields = restriction.getFields();
    }

    // restrict roots
    List<String> roots = null;
    if ((m_roots != null) && (m_roots.size() > 0)) {
        if ((restriction.getRoots() != null) && (restriction.getRoots().size() > 0)) {
            roots = ListUtils.intersection(m_roots, restriction.getRoots());
            // TODO: This only works if there are equal paths in both parameter sets - for two distinct sets 
            //       all root restrictions are dropped with an empty list. 
        } else {
            roots = m_roots;
        }
    } else {
        roots = restriction.getRoots();
    }

    // restrict categories
    List<String> categories = null;
    if ((m_categories != null) && (m_categories.size() > 0)) {
        if ((restriction.getCategories() != null) && (restriction.getCategories().size() > 0)) {
            categories = ListUtils.intersection(m_categories, restriction.getCategories());
        } else {
            categories = m_categories;
        }
    } else {
        categories = restriction.getCategories();
    }

    // restrict resource types
    List<String> resourceTypes = null;
    if ((m_resourceTypes != null) && (m_resourceTypes.size() > 0)) {
        if ((restriction.getResourceTypes() != null) && (restriction.getResourceTypes().size() > 0)) {
            resourceTypes = ListUtils.intersection(m_resourceTypes, restriction.getResourceTypes());
        } else {
            resourceTypes = m_resourceTypes;
        }
    } else {
        resourceTypes = restriction.getResourceTypes();
    }

    // create the new search parameters 
    CmsSearchParameters result = new CmsSearchParameters(query.toString(), fields, roots, categories,
            resourceTypes, m_calculateCategories, m_sort);
    result.setIndex(getIndex());
    return result;
}

From source file:marytts.tools.dbselection.WikipediaMarkupCleaner.java

public Vector<String> removeMarkup(String page) {
    StringBuffer str = new StringBuffer("");
    StringBuffer line = null;
    Vector<String> textList = new Vector<String>();

    boolean endOfText = false;
    Scanner s = null;//from   ww w .  ja  v  a2s  . c  o  m
    try {
        s = new Scanner(page);
        while (s.hasNext() && !endOfText) {

            line = new StringBuffer(s.nextLine());
            // process text until it finds any of these labels:
            if (line.indexOf("==References") >= 0 || line.indexOf("== References") >= 0
                    || line.indexOf("==See also") >= 0 || line.indexOf("== See also") >= 0
                    || line.indexOf("==External links and sources") >= 0
                    || line.indexOf("==External links") >= 0 || line.indexOf("== External links") >= 0
                    || line.indexOf("== External Links") >= 0
                    || line.indexOf("== External links and sources") >= 0 || line.indexOf("==Notes") >= 0
                    || line.indexOf("== Notes") >= 0 || line.indexOf("==Sources") >= 0
                    || line.indexOf("== Sources") >= 0 || line.indexOf("==Foreign") >= 0
                    || line.indexOf("== Foreign") >= 0 || line.indexOf("==Discussion") >= 0) {
                endOfText = true;
            } else {
                // when removing sections it might add more lines that might contain again more labels to remove
                boolean clean = false;
                while (!clean && line.length() > 0) {
                    clean = true;
                    if (line.indexOf("<noinclude") >= 0) {
                        line = removeSection(s, line, "<noinclude", "</noinclude>");
                        clean = false;
                    }

                    if (line.indexOf("<includeonly") >= 0) {
                        line = removeSection(s, line, "<includeonly", "</includeonly>");
                        clean = false;
                    }

                    if (line.indexOf("<onlyinclude") >= 0) {
                        line = removeSection(s, line, "<onlyinclude", "</onlyinclude>");
                        clean = false;
                    }

                    if (line.indexOf("<table") >= 0) { // tables
                        line = removeSection(s, line, "<table", "</table>");
                        clean = false;
                    }

                    if (line.indexOf("<TABLE") >= 0) {
                        line = removeSection(s, line, "<TABLE", "</TABLE>");
                        clean = false;
                    }

                    if (line.indexOf("{{col-begin}}") >= 0) {
                        line = removeSection(s, line, "{{col-begin}}", "{{col-end}}");
                        clean = false;
                    }

                    if (line.indexOf("{|") >= 0) { // this is a table, this should go before {{ because a table can contain {{ }}
                        line = removeSectionTable(s, line, "{|", "|}");
                        clean = false;
                    }

                    if (line.indexOf("<ref") >= 0) { // references
                        line = removeSectionRef(s, line); // This is special because it can be <ref>, <ref, </ref> or />
                        clean = false;
                    }

                    if (line.indexOf("<REF") >= 0) {
                        line = removeSection(s, line, "<REF", "</REF>");
                        clean = false;
                    }

                    if (line.indexOf("<Ref") >= 0) {
                        line = removeSection(s, line, "<Ref", "</Ref>");
                        clean = false;
                    }
                    if (line.indexOf("<reF") >= 0) {
                        line = removeSection(s, line, "<reF", "</reF>");
                        clean = false;
                    }

                    if (line.indexOf("{{start box}}") >= 0) {
                        line = removeSection(s, line, "{{start box}}", "{{end box}}");
                        clean = false;
                    }

                    if (line.indexOf("{{") >= 0) {
                        line = removeSection(s, line, "{{", "}}");
                        clean = false;
                    }

                    if (line.indexOf("<!--") >= 0) {
                        line = removeSection(s, line, "<!--", "-->");
                        clean = false;
                    }

                    if (line.indexOf("\\mathrel{|") >= 0) {
                        line = removeSection(s, line, "\\mathrel{|", "}");
                        clean = false;
                    }

                    if (line.indexOf("<gallery") >= 0) { // gallery might contain several images
                        line = removeSection(s, line, "<gallery", "</gallery>");
                        clean = false;
                    }

                    if (line.indexOf("[[Image:") >= 0) {
                        line = removeSectionImage(s, line, "[[Image:", "]]");
                        clean = false;
                    }

                    if (line.indexOf("<div") >= 0) { // span and div tags are used to separate images from text
                        line = removeSection(s, line, "<div", "</div>");
                        clean = false;
                    }

                    if (line.indexOf("<DIV") >= 0) {
                        line = removeSectionImage(s, line, "<DIV", "</DIV>");
                        clean = false;
                    }

                    if (line.indexOf("<span") >= 0) {
                        line = removeSection(s, line, "<span", "</span>");
                        clean = false;
                    }

                    if (line.indexOf("<math>") >= 0) {
                        line = removeSection(s, line, "<math>", "</math>");
                        clean = false;
                    }

                    if (line.indexOf("<timeline>") >= 0) {
                        line = removeSection(s, line, "<timeline>", "</timeline>");
                        clean = false;
                    }

                    if (line.indexOf("<nowiki") >= 0) {
                        line = removeSection(s, line, "<nowiki", "</nowiki>");
                        clean = false;
                    }

                    if (line.indexOf("<source") >= 0) {
                        line = removeSection(s, line, "<source", "</source>");
                        clean = false;
                    }

                    if (line.indexOf("<code") >= 0) {
                        line = removeSection(s, line, "<code", "</code>");
                        clean = false;
                    }

                    if (line.indexOf("<imagemap") >= 0) {
                        line = removeSection(s, line, "<imagemap", "</imagemap>");
                        clean = false;
                    }

                    if (line.indexOf("<poem") >= 0) {
                        line = removeSection(s, line, "<poem", "</poem>");
                        clean = false;
                    }

                    if (line.indexOf("<h1") >= 0) {
                        line = removeSection(s, line, "<h1", "</h1>");
                        clean = false;
                    }

                    if (line.indexOf("<pre") >= 0) {
                        line = removeSection(s, line, "<pre", "</pre>");
                        clean = false;
                    }

                } // while the line/text is not clean (or does not have tags to remove)

                // here filter bulleted and numbered short lines
                if (line.length() > 0) {
                    if ((line.toString().startsWith("*") || line.toString().startsWith("#")
                            || line.toString().startsWith(";") || line.toString().startsWith(".")
                            || line.toString().startsWith(",") || line.toString().startsWith("&")
                            || line.toString().startsWith("}") || line.toString().startsWith("]")
                            || line.toString().startsWith("|") || line.toString().startsWith("ca:")
                            || line.toString().startsWith("cs:") || line.toString().startsWith("de:")
                            || line.toString().startsWith("es:") || line.toString().startsWith("fr:")
                            || line.toString().startsWith("it:") || line.toString().startsWith("hu:")
                            || line.toString().startsWith("ja:") || line.toString().startsWith("no:")
                            || line.toString().startsWith("pt:") || line.toString().startsWith("sl:")
                            || line.toString().startsWith("fi:") || line.toString().startsWith("sv:")
                            || line.toString().startsWith("tr:") || line.toString().startsWith("zh:")
                            || line.toString().startsWith("Category:") || line.toString().startsWith("!style=")
                            || line.toString().startsWith("!  style=") || line.toString().startsWith("!align=")
                            || line.toString().startsWith("::<code") || line.toString().endsWith("]]"))
                            && line.length() < 200)
                        line = new StringBuffer("");
                }
                // Now if the line is not empty, remove:
                //   '''''bold & italic'''''
                //   '''bold'''
                //   ''italic''
                // Internal links: 
                //   [[Name of page]]
                //   [[Name of page|Text to display]]
                // External links:
                //   [http://www.example.org Text to display]
                //   [http://www.example.org]
                //    http://www.example.org
                if (line.length() > 0) {

                    line = new StringBuffer(line.toString().replaceAll("'''''", ""));
                    line = new StringBuffer(line.toString().replaceAll("'''", ""));
                    line = new StringBuffer(line.toString().replaceAll("''", ""));

                    line = processInternalAndExternalLinks(line);

                    // this will convert HTML &nbsp; &ndash; etc. 
                    String strlineNoHTML = StringEscapeUtils.unescapeHtml(line.toString());
                    line = new StringBuffer(strlineNoHTML);

                    // The previous does not remove all HTML stuff, so here it is done some manually
                    line = new StringBuffer(line.toString().replaceAll("<big>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</big>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<blockquote>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</blockquote>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<BLOCKQUOTE>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</BLOCKQUOTE>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<sup>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</sup>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<sub>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</sub>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<small>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</small>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<ul>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</ul>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<UL>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</UL>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<br>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<br", ""));
                    line = new StringBuffer(line.toString().replaceAll("<BR>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<br", ""));
                    line = new StringBuffer(line.toString().replaceAll("<br/>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<Center>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<center>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</center>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<CENTER>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</CENTER>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<cite>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</cite>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<li>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</li>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<LI>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</LI>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<dl>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</dl>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<dt>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</dt>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<dd>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</dd>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<b>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</b>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<p>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</p>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<u>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</u>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<tt>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</tt>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<i>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</i>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<I>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</I>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<s>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</s>", ""));
                    line = new StringBuffer(line.toString().replaceAll("<em>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</em>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</br>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</div>", ""));
                    line = new StringBuffer(line.toString().replaceAll("</ref>", ""));
                    line = new StringBuffer(line.toString().replaceAll("/>", ""));

                    // Removing quotation marks
                    line = new StringBuffer(line.toString().replaceAll("\"", ""));
                    // these quotations have a strange/problematic symbol different from "
                    line = new StringBuffer(line.toString().replaceAll("", ""));
                    line = new StringBuffer(line.toString().replaceAll("?", ""));
                    // these symbol are also problematic, here they are changed.
                    line = new StringBuffer(line.toString().replaceAll("", "'"));
                    line = new StringBuffer(line.toString().replaceAll("", "-"));
                    line = new StringBuffer(line.toString().replaceAll("", "-"));

                    line = new StringBuffer(line.toString().replaceAll("", " "));
                    line = new StringBuffer(line.toString().replaceAll("", " "));

                    // finally sections and lists
                    boolean is_title = false;
                    if (line.toString().startsWith("==")) {
                        is_title = true;
                    }
                    line = new StringBuffer(line.toString().replaceAll("\\s*==+$|==+", ""));
                    if (is_title) {
                        line.append(".");
                    }

                    // bulleted list and numbered list
                    if (line.toString().startsWith("***") || line.toString().startsWith("*#*"))
                        line.replace(0, 3, "");
                    if (line.toString().startsWith("**") || line.toString().startsWith(":*")
                            || line.toString().startsWith("*#") || line.toString().startsWith("##")
                            || line.toString().startsWith("::"))
                        line.replace(0, 2, "");
                    if (line.toString().startsWith("*") || line.toString().startsWith("#"))
                        line.replace(0, 1, "");
                    if (line.toString().startsWith(";") || line.toString().startsWith(";")) // in glossaries definitions start with ;
                        line.replace(0, 1, "");

                    // remove this when the text is almost clean
                    if (line.indexOf("<font") >= 0)
                        line = removeSection(s, line, "<font", ">");
                    line = new StringBuffer(line.toString().replaceAll("</font>", ""));

                    if (line.indexOf("<blockquote") >= 0)
                        line = removeSection(s, line, "<blockquote", ">");

                    if (line.indexOf("<ol") >= 0)
                        line = removeSection(s, line, "<ol", ">");

                    if (line.indexOf("<http:") >= 0)
                        line = removeSection(s, line, "<http:", ">");

                    // finally concatenate the line  
                    str.append(line);
                    if (!str.toString().endsWith("\n"))
                        str.append("\n");

                    line = null;

                    // check length of the text 
                    if (str.length() > maxTextLength) {
                        textList.add(str.toString());
                        //System.out.println("\n-----------\n" + str.toString());
                        str = new StringBuffer("");
                    }

                }

            } // endOfText=false

        } // while has more lines

    } finally {
        if (s != null)
            s.close();
    }

    if (!str.toString().contentEquals(""))
        textList.add(str.toString());
    return textList;
}

From source file:org.ejbca.ui.web.protocol.OCSPServletBase.java

/**
 * Reads the request bytes and verifies min and max size of the request. If an error occurs it throws a MalformedRequestException. 
 * Can get request bytes both from a HTTP GET and POST request
 * /*from w  w w .ja va  2  s.  co m*/
 * @param request
 * @param response
 * @return the request bytes or null if an error occured.
 * @throws IOException In case there is no stream to read
 * @throws MalformedRequestException 
 */
private byte[] checkAndGetRequestBytes(HttpServletRequest request)
        throws IOException, MalformedRequestException {
    final byte[] ret;
    // Get the request data
    String method = request.getMethod();
    String remoteAddress = request.getRemoteAddr();
    final int n = request.getContentLength();
    if (m_log.isDebugEnabled()) {
        m_log.debug(">checkAndGetRequestBytes. Received " + method + " request with content length: " + n
                + " from " + remoteAddress);
    }
    if (n > LimitLengthASN1Reader.MAX_REQUEST_SIZE) {
        String msg = intres.getLocalizedMessage("ocsp.toolarge", LimitLengthASN1Reader.MAX_REQUEST_SIZE, n);
        m_log.info(msg);
        throw new MalformedRequestException(msg);
    }
    // So we passed basic tests, now we can read the bytes, but still keep an eye on the size
    // we can not fully trust the sent content length.
    if (StringUtils.equals(method, "POST")) {
        final ServletInputStream in = request.getInputStream(); // ServletInputStream does not have to be closed, container handles this
        ret = new LimitLengthASN1Reader(in, n).readFirstASN1Object();
        if (n > ret.length) {
            // The client is sending more data than the OCSP request. It might be slightly broken or trying to bog down the server on purpose.
            // In the interest of not breaking existing systems that might have slightly broken clients we just log for a warning for now.
            String msg = intres.getLocalizedMessage("ocsp.additionaldata", ret.length, n);
            m_log.warn(msg);
            //throw new MalformedRequestException(msg);   // Responding with MALFORMED_REQUEST. 
        }
    } else if (StringUtils.equals(method, "GET")) {
        // GET request
        final StringBuffer url = request.getRequestURL();
        // RFC2560 A.1.1 says that request longer than 255 bytes SHOULD be sent by POST, we support GET for longer requests anyway.
        if (url.length() <= LimitLengthASN1Reader.MAX_REQUEST_SIZE) {
            final String decodedRequest;
            try {
                // We have to extract the pathInfo manually, to avoid multiple slashes being converted to a single
                // According to RFC 2396 2.2 chars only have to encoded if they conflict with the purpose, so
                // we can for example expect both '/' and "%2F" in the request.
                final String fullServletpath = request.getContextPath() + request.getServletPath();
                final int paramIx = Math.max(url.indexOf(fullServletpath), 0) + fullServletpath.length() + 1;
                final String requestString = paramIx < url.length() ? url.substring(paramIx) : "";
                decodedRequest = URLDecoder.decode(requestString, "UTF-8").replaceAll(" ", "+");
                //                  if (m_log.isDebugEnabled()) {
                //                     m_log.debug("URL: "+url.toString());
                //                  }
            } catch (Exception e) {
                String msg = intres.getLocalizedMessage("ocsp.badurlenc");
                m_log.info(msg);
                throw new MalformedRequestException(e);
            }
            if (decodedRequest != null && decodedRequest.length() > 0) {
                if (m_log.isDebugEnabled()) {
                    // Don't log the request if it's too long, we don't want to cause denial of service by filling log files or buffers.
                    if (decodedRequest.length() < 2048) {
                        m_log.debug("decodedRequest: " + decodedRequest);
                    } else {
                        m_log.debug("decodedRequest too long to log: " + decodedRequest.length());
                    }
                }
                try {
                    ret = org.ejbca.util.Base64.decode(decodedRequest.getBytes());
                } catch (Exception e) {
                    String msg = intres.getLocalizedMessage("ocsp.badurlenc");
                    m_log.info(msg);
                    throw new MalformedRequestException(e);
                }
            } else {
                String msg = intres.getLocalizedMessage("ocsp.missingreq");
                m_log.info(msg);
                throw new MalformedRequestException(msg);
            }
        } else {
            String msg = intres.getLocalizedMessage("ocsp.toolarge", LimitLengthASN1Reader.MAX_REQUEST_SIZE,
                    url.length());
            m_log.info(msg);
            throw new MalformedRequestException(msg);
        }
    } else {
        // Strange, an unknown method
        String msg = intres.getLocalizedMessage("ocsp.unknownmethod", method);
        m_log.info(msg);
        throw new MalformedRequestException(msg);
    }
    // Make a final check that we actually received something
    if ((ret == null) || (ret.length == 0)) {
        String msg = intres.getLocalizedMessage("ocsp.emptyreq", remoteAddress);
        m_log.info(msg);
        throw new MalformedRequestException(msg);
    }
    return ret;
}

From source file:org.pentaho.database.dialect.AbstractDatabaseDialect.java

public String getURLWithExtraOptions(IDatabaseConnection connection) throws DatabaseDialectException {
    StringBuffer url = new StringBuffer(getURL(connection));
    if (supportsOptionsInURL()) {
        // OK, now add all the options...
        String optionIndicator = getExtraOptionIndicator();
        String optionSeparator = getExtraOptionSeparator();
        String valueSeparator = getExtraOptionValueSeparator();

        Map<String, String> map = connection.getExtraOptions();
        if (map.size() > 0) {
            Iterator<String> iterator = map.keySet().iterator();
            boolean first = true;
            while (iterator.hasNext()) {
                String typedParameter = (String) iterator.next();
                int dotIndex = typedParameter.indexOf('.');
                if (dotIndex >= 0) {
                    String typeCode = typedParameter.substring(0, dotIndex);
                    String parameter = typedParameter.substring(dotIndex + 1);
                    String value = map.get(typedParameter);

                    // Only add to the URL if it's the same database type code...
                    //
                    if (connection.getDatabaseType().getShortName().equals(typeCode)) {
                        if (first && url.indexOf(valueSeparator) == -1) {
                            url.append(optionIndicator);
                        } else {
                            url.append(optionSeparator);
                        }//ww w.  j a  v  a 2 s . co  m

                        url.append(parameter);
                        if (!isEmpty(value)) {
                            url.append(valueSeparator).append(value);
                        }
                        first = false;
                    }
                }
            }
        }
    }
    return url.toString();
}

From source file:org.apache.axis2.jaxws.util.BaseWSDLLocator.java

protected String normalizePath(String parentLocation, String relativeLocation) {
    if (log.isDebugEnabled()) {
        log.debug(//from w w w  . j  a  va 2  s.  co  m
                "normalizePath, parentLocation= " + parentLocation + " relativeLocation= " + relativeLocation);
    }
    // Get the path from the module root to the directory containing the importing WSDL file.
    // Note this path will end in a "/" and will not contain any ".." path components.
    String pathFromRoot = convertURI(parentLocation);

    // Construct the path to the location relative to the module root based on the parent location, 
    // removing any ".." or "." path components.
    StringBuffer pathToRelativeLocation = new StringBuffer(pathFromRoot);
    StringTokenizer tokenizedRelativeLocation = new StringTokenizer(relativeLocation, WSDL_PATH_SEPERATOR);
    if (log.isDebugEnabled()) {
        log.debug("pathFromRoot = " + pathFromRoot);
        log.debug("relativeLocation = " + relativeLocation);
    }
    while (tokenizedRelativeLocation.hasMoreTokens()) {
        String nextToken = tokenizedRelativeLocation.nextToken();
        if (nextToken.equals("..")) {
            // Relative parent directory, so chop off the last path component in the path to back
            // up to the parent directory.  First delete the trailing "/" from the path if there 
            // is one, then delete characters from the end of the path until we find the next "/".
            int charToDelete = pathToRelativeLocation.length() - 1;
            if (pathToRelativeLocation.charAt(charToDelete) == WSDL_PATH_SEPERATOR_CHAR
                    || pathToRelativeLocation.charAt(charToDelete) == '\\') {
                pathToRelativeLocation.deleteCharAt(charToDelete--);
            }
            while (pathToRelativeLocation.charAt(charToDelete) != WSDL_PATH_SEPERATOR_CHAR
                    && pathToRelativeLocation.charAt(charToDelete) != '\\') {
                pathToRelativeLocation.deleteCharAt(charToDelete--);
            }
        } else if (nextToken.equals(".")) {
            // Relative current directory, do not add or delete any path components
        } else {
            // Make sure the current path ends in a "/"  or "\\" then append this path component
            // This handles locations within the module and URIs
            if ((pathToRelativeLocation.indexOf(String.valueOf(WSDL_PATH_SEPERATOR_CHAR)) != -1)
                    && (pathToRelativeLocation
                            .charAt(pathToRelativeLocation.length() - 1) != WSDL_PATH_SEPERATOR_CHAR)) {
                pathToRelativeLocation.append(WSDL_PATH_SEPERATOR_CHAR);
            }
            // This handles file based locations
            else if ((pathToRelativeLocation.indexOf("\\") != -1)
                    && (pathToRelativeLocation.charAt(pathToRelativeLocation.length() - 1) != '\\')) {
                pathToRelativeLocation.append('\\');
            }
            pathToRelativeLocation.append(nextToken);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Built path = " + pathToRelativeLocation.toString());
    }
    return pathToRelativeLocation.toString();
}

From source file:org.kchine.r.server.manager.ServerManager.java

public static RServices createRSsh(boolean keepAlive, String codeServerHostIp, int codeServerPort,
        Properties namingInfo, int memoryMinMegabytes, int memoryMaxMegabytes, String sshHostIp, int sshPort,
        String sshLogin, String sshPwd, String name, boolean showProgress, URL[] codeUrls, String logFile)
        throws BadSshHostException, BadSshLoginPwdException, Exception {

    if (showProgress) {
        createRSshProgressArea = new JTextArea();
        createRSshProgressBar = new JProgressBar(0, 100);
        createRSshProgressFrame = new JFrame("Create R Server via SSH");

        Runnable runnable = new Runnable() {
            public void run() {
                createRSshProgressArea.setFocusable(false);
                createRSshProgressBar.setIndeterminate(true);
                JPanel p = new JPanel(new BorderLayout());
                p.add(createRSshProgressBar, BorderLayout.SOUTH);
                p.add(new JScrollPane(createRSshProgressArea), BorderLayout.CENTER);
                createRSshProgressFrame.add(p);
                createRSshProgressFrame.pack();
                createRSshProgressFrame.setSize(300, 90);
                createRSshProgressFrame.setVisible(true);
                createRSshProgressFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                PoolUtils.locateInScreenCenter(createRSshProgressFrame);
            }/*from   w w  w . ja va 2 s  .  c  o m*/
        };

        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else {
            SwingUtilities.invokeLater(runnable);
        }
    }

    Connection conn = null;
    try {
        conn = new Connection(sshHostIp, sshPort);
        try {
            conn.connect();
        } catch (Exception e) {
            throw new BadSshHostException();
        }
        boolean isAuthenticated = conn.authenticateWithPassword(sshLogin, sshPwd);
        if (isAuthenticated == false)
            throw new BadSshLoginPwdException();

        InputStream is = ServerManager.class
                .getResourceAsStream("/org/kchine/r/server/manager/bootstrap/BootSsh.class");
        byte[] buffer = new byte[is.available()];
        try {
            for (int i = 0; i < buffer.length; ++i) {
                int b = is.read();
                buffer[i] = (byte) b;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String bootstrapDir = INSTALL_DIR + "classes/org/kchine/r/server/manager/bootstrap";
        new File(bootstrapDir).mkdirs();
        RandomAccessFile raf = new RandomAccessFile(bootstrapDir + "/BootSsh.class", "rw");
        raf.setLength(0);
        raf.write(buffer);
        raf.close();

        Session sess = null;
        try {
            sess = conn.openSession();
            sess.execCommand("mkdir -p RWorkbench/classes/org/kchine/r/server/manager/bootstrap");
            sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0);
        } finally {
            try {
                if (sess != null)
                    sess.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        new SCPClient(conn).put(bootstrapDir + "/BootSsh.class",
                "RWorkbench/classes/org/kchine/r/server/manager/bootstrap");
        try {
            sess = conn.openSession();

            String command = "java -classpath RWorkbench/classes org.kchine.r.server.manager.bootstrap.BootSsh"
                    + " " + new Boolean(keepAlive) + " " + codeServerHostIp + " " + codeServerPort + " "
                    + BootSsh.propertiesToString(namingInfo) + " " + "NULL" + " " + memoryMinMegabytes + " "
                    + memoryMaxMegabytes + " " + "System.out" + " "
                    + ((name == null || name.trim().equals("")) ? BootSsh.NO_NAME : name);

            if (codeUrls != null && codeUrls.length > 0) {
                for (int i = 0; i < codeUrls.length; ++i) {
                    command = command + " " + codeUrls[i];
                }
            }

            System.out.println("createRSsh command:" + command);
            sess.execCommand(command);

            InputStream stdout = new StreamGobbler(sess.getStdout());
            final BufferedReader brOut = new BufferedReader(new InputStreamReader(stdout));

            InputStream stderr = new StreamGobbler(sess.getStderr());
            final BufferedReader brErr = new BufferedReader(new InputStreamReader(stderr));
            final StringBuffer sshOutput = new StringBuffer();
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            String line = brOut.readLine();
                            if (line == null)
                                break;
                            sshOutput.append(line + "\n");
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Out Log Thread Died");
                }
            }).start();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                            String line = brErr.readLine();
                            if (line == null)
                                break;
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Err Log Thread Died");
                }
            }).start();

            sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0);

            int eIndex = sshOutput.indexOf(BootSsh.STUB_END_MARKER);
            if (eIndex != -1) {
                int bIndex = sshOutput.indexOf(BootSsh.STUB_BEGIN_MARKER);
                String stub = sshOutput.substring(bIndex + BootSsh.STUB_BEGIN_MARKER.length(), eIndex);
                return (RServices) PoolUtils.hexToStub(stub, ServerManager.class.getClassLoader());
            } else {
                return null;
            }

        } finally {
            try {
                if (sess != null)
                    sess.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (showProgress) {
            createRSshProgressFrame.setVisible(false);
        }
    }
}