Example usage for java.lang StringBuffer replace

List of usage examples for java.lang StringBuffer replace

Introduction

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

Prototype

@Override
public synchronized StringBuffer replace(int start, int end, String str) 

Source Link

Usage

From source file:architecture.common.util.TextUtils.java

private final static void linkURL(StringBuffer str, String target) {
    String urlToDisplay;//from  w  w w  .jav  a2  s. co  m

    int lastEndIndex = -1; // Stores the index position, within the whole
    // string, of the ending char of the last URL
    // found.

    String targetString = ((target == null) || (target.trim().length() == 0)) ? ""
            : (" target=\"" + target.trim() + '\"');

    while (true) {
        int linkStartIndex = getStartUrl(str, lastEndIndex);

        // if no more links found - then end the loop
        if (linkStartIndex == -1) {
            break;
        } else {
            // Get the whole URL...
            // We move forward and add each character to the URL string
            // until we encounter
            // an invalid URL character (we assume that the URL ends there).
            int linkEndIndex = linkStartIndex;
            String urlStr = "";

            while (true) {
                // if char at linkEndIndex is '&' then we look at the next 4
                // chars
                // to see if they make up "&" altogether. This is the
                // html coded
                // '&' and will pretty much stuff up an otherwise valid link
                // becos of the ';'.
                // We therefore have to remove it before proceeding...
                if (str.charAt(linkEndIndex) == '&') {
                    if (((linkEndIndex + 6) <= str.length())
                            && "&quot;".equals(str.substring(linkEndIndex, linkEndIndex + 6))) {
                        break;
                    } else if (((linkEndIndex + 5) <= str.length())
                            && "&amp;".equals(str.substring(linkEndIndex, linkEndIndex + 5))) {
                        str.replace(linkEndIndex, linkEndIndex + 5, "&");
                    }
                }

                if (isValidURLChar(str.charAt(linkEndIndex))) {
                    urlStr += str.charAt(linkEndIndex);
                    linkEndIndex++;

                    if (linkEndIndex == str.length()) { // Reached end of
                        // str...

                        break;
                    }
                } else {
                    break;
                }
            }

            // if the characters before the linkStart equal 'href="' then
            // don't link the url - CORE-44
            if (linkStartIndex >= 6) { // 6 = "href\"".length()

                String prefix = str.substring(linkStartIndex - 6, linkStartIndex);

                if ("href=\"".equals(prefix)) {
                    lastEndIndex = linkEndIndex;

                    continue;
                }
            }

            // if the characters after the linkEnd are '</a>' then this url
            // is probably already linked - CORE-44
            if (str.length() >= (linkEndIndex + 4)) { // 4 = "</a>".length()

                String suffix = str.substring(linkEndIndex, linkEndIndex + 4);

                if ("</a>".equals(suffix)) {
                    lastEndIndex = linkEndIndex + 4;

                    continue;
                }
            }

            // Decrement linkEndIndex back by 1 to reflect the real ending
            // index position of the URL...
            linkEndIndex--;

            // If the last char of urlStr is a '.' we exclude it. It is most
            // likely a full stop and
            // we don't want that to be part of an url.
            while (true) {
                char lastChar = urlStr.charAt(urlStr.length() - 1);

                if (lastChar == '.') {
                    urlStr = urlStr.substring(0, urlStr.length() - 1);
                    linkEndIndex--;
                } else {
                    break;
                }
            }

            // if the URL had a '(' before it, and has a ')' at the end,
            // trim the last ')' from the url
            // ie '(www.opensymphony.com)' => '(<a
            // href="http://www.openymphony.com/">www.opensymphony.com</a>)'
            char lastChar = urlStr.charAt(urlStr.length() - 1);

            if (lastChar == ')') {
                if ((linkStartIndex > 0) && ('(' == (str.charAt(linkStartIndex - 1)))) {
                    urlStr = urlStr.substring(0, urlStr.length() - 1);
                    linkEndIndex--;
                }
            } else if (lastChar == '\'') {
                if ((linkStartIndex > 0) && ('\'' == (str.charAt(linkStartIndex - 1)))) {
                    urlStr = urlStr.substring(0, urlStr.length() - 1);
                    linkEndIndex--;
                }
            }
            // perhaps we ended with '&gt;', '&lt;' or '&quot;'
            // We need to strip these
            // ie '&quot;www.opensymphony.com&quot;' => '&quot;<a
            // href="http://www.openymphony.com/">www.opensymphony.com</a>&quot;'
            // ie '&lt;www.opensymphony.com&gt;' => '&lt;<a
            // href="http://www.openymphony.com/">www.opensymphony.com</a>&gt;'
            else if (lastChar == ';') {
                // 6 = "&quot;".length()
                if ((urlStr.length() > 6) && "&quot;".equalsIgnoreCase(urlStr.substring(urlStr.length() - 6))) {
                    urlStr = urlStr.substring(0, urlStr.length() - 6);
                    linkEndIndex -= 6;
                }
                // 4 = "&lt;".length() || "&gt;".length()
                else if (urlStr.length() > 4) {
                    final String endingStr = urlStr.substring(urlStr.length() - 4);

                    if ("&lt;".equalsIgnoreCase(endingStr) || "&gt;".equalsIgnoreCase(endingStr)) {
                        urlStr = urlStr.substring(0, urlStr.length() - 4);
                        linkEndIndex -= 4;
                    }
                }
            }

            // we got the URL string, now we validate it and convert it into
            // a hyperlink...
            urlToDisplay = htmlEncode(urlStr);

            if (urlStr.toLowerCase().startsWith("www.")) {
                urlStr = "http://" + urlStr;
            }

            if (verifyHierachicalURI(urlStr, new String[] { "javascript" })) {
                // Construct the hyperlink for the url...
                String urlLink = "<a href=\"" + urlStr + '\"' + targetString + '>' + urlToDisplay + "</a>";

                // Remove the original urlStr from str and put urlLink there
                // instead...
                str.replace(linkStartIndex, linkEndIndex + 1, urlLink);

                // Set lastEndIndex to reflect the position of the end of
                // urlLink
                // within the whole string...
                lastEndIndex = (linkStartIndex - 1) + urlLink.length();
            } else {
                // lastEndIndex is different from the one above cos' there's
                // no
                // <a href...> tags added...
                lastEndIndex = (linkStartIndex - 1) + urlStr.length();
            }
        }
    }
}

From source file:org.agnitas.beans.impl.MailingImpl.java

/**
 * Scans a textblock for trackable links and replaces them with encoded
 * rdir-links.//from  w w  w .  j  a va2s .c  o  m
 */
public String insertTrackableLinks(String aText1, int customerID, ApplicationContext con) {
    if (this.trackableLinks == null) {
        return aText1;
    }

    /*
     * trackableLinks is an unordered HashMap. When there are 2 links in the
     * Map, where one is part of the other, this could lead to an link
     * replacement, depending on the map ordering.
     * 
     * Link 1: http://www.mydomain.de Link 2:
     * http://www.mydomain.de/path/index.htm
     * 
     * If Link 1 is returned before Link 2 from the iterator this resulted
     * in: http://rdir.de/r.html?uid=<uid of Link1>/path/index.htm
     */
    Comparator<String> reverseString = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o2.compareTo(o1);
        }
    };

    Set<String> sorted = new TreeSet<String>(reverseString);
    sorted.addAll(this.trackableLinks.keySet());

    Iterator<String> i = sorted.iterator();
    String aLink = null;
    int start_link = 0;
    int end_link = 0;
    TrackableLink aLinkObj = null;
    StringBuffer aBuf = new StringBuffer(aText1);
    boolean isHref = false;

    if (aText1 == null) {
        return null;
    }

    while (i.hasNext()) {
        aLink = i.next();
        end_link = 0;
        while ((start_link = aBuf.indexOf(aLink, end_link)) != -1) {
            end_link = start_link + 1;
            isHref = false;
            if (start_link > 5 && (aBuf.substring(start_link - 6, start_link).equalsIgnoreCase("href=\""))) {
                isHref = true;
            }
            if (start_link > 6 && (aBuf.substring(start_link - 7, start_link).equalsIgnoreCase("href=\""))) {
                isHref = true;
            }
            if (aBuf.length() > (start_link + aLink.length())) {
                if (!(aBuf.charAt(start_link + aLink.length()) == ' '
                        || aBuf.charAt(start_link + aLink.length()) == '\''
                        || aBuf.charAt(start_link + aLink.length()) == '"')) {
                    isHref = false;
                }
            }
            if (isHref) {
                aLinkObj = (TrackableLink) this.trackableLinks.get(aLink);
                aBuf.replace(start_link, start_link + aLink.length(),
                        aLinkObj.encodeTagStringLinkTracking(con, customerID));
            }
        }
    }
    return aBuf.toString();
}

From source file:com.panet.imeta.job.entries.dtdvalidator.JobEntryDTDValidator.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*from www.  j  a v a 2s. co  m*/

    String realxmlfilename = getRealxmlfilename();
    String realDTDfilename = getRealDTDfilename();

    FileObject xmlfile = null;
    FileObject DTDfile = null;

    try

    {

        if (xmlfilename != null && ((dtdfilename != null && !dtdintern) || (dtdintern))) {
            xmlfile = KettleVFS.getFileObject(realxmlfilename);

            if (xmlfile.exists())

            {

                //URL xmlFile = new URL (KettleVFS.getFilename(xmlfile));
                URL xmlFile = new File(KettleVFS.getFilename(xmlfile)).toURI().toURL();

                // open XML File
                BufferedReader xmlBufferedReader = new BufferedReader(
                        new InputStreamReader(xmlFile.openStream()));
                StringBuffer xmlStringbuffer = new StringBuffer("");

                char[] buffertXML = new char[1024];
                int LenXML = -1;
                while ((LenXML = xmlBufferedReader.read(buffertXML)) != -1)
                    xmlStringbuffer.append(buffertXML, 0, LenXML);

                // Prepare parsing ...
                DocumentBuilderFactory DocBuilderFactory = DocumentBuilderFactory.newInstance();
                Document xmlDocDTD = null;
                DocumentBuilder DocBuilder = DocBuilderFactory.newDocumentBuilder();

                // Let's try to get XML document encoding
                DocBuilderFactory.setValidating(false);
                xmlDocDTD = DocBuilder
                        .parse(new ByteArrayInputStream(xmlStringbuffer.toString().getBytes("UTF-8")));

                String encoding = null;
                if (xmlDocDTD.getXmlEncoding() == null) {
                    encoding = "UTF-8";
                } else {
                    encoding = xmlDocDTD.getXmlEncoding();
                }

                int xmlStartDTD = xmlStringbuffer.indexOf("<!DOCTYPE");

                if (dtdintern) {
                    // DTD find in the XML document
                    if (xmlStartDTD != -1) {
                        if (log.isBasic())
                            log.logBasic(toString(), Messages
                                    .getString("JobEntryDTDValidator.ERRORDTDFound.Label", realxmlfilename));
                    } else {
                        if (log.isBasic())
                            log.logBasic(toString(), Messages
                                    .getString("JobEntryDTDValidator.ERRORDTDNotFound.Label", realxmlfilename));
                    }

                } else {
                    // DTD in external document
                    // If we find an intern declaration, we remove it
                    DTDfile = KettleVFS.getFileObject(realDTDfilename);

                    if (DTDfile.exists()) {
                        if (xmlStartDTD != -1) {
                            int EndDTD = xmlStringbuffer.indexOf(">", xmlStartDTD);
                            //String DocTypeDTD = xmlStringbuffer.substring(xmlStartDTD, EndDTD + 1);
                            xmlStringbuffer.replace(xmlStartDTD, EndDTD + 1, "");

                        }

                        String xmlRootnodeDTD = xmlDocDTD.getDocumentElement().getNodeName();

                        String RefDTD = "<?xml version='" + xmlDocDTD.getXmlVersion() + "' encoding='"
                                + encoding + "'?>\n<!DOCTYPE " + xmlRootnodeDTD + " SYSTEM '"
                                + KettleVFS.getFilename(DTDfile) + "'>\n";

                        int xmloffsetDTD = xmlStringbuffer.indexOf("<" + xmlRootnodeDTD);
                        xmlStringbuffer.replace(0, xmloffsetDTD, RefDTD);
                    } else {
                        log.logError(Messages.getString("JobEntryDTDValidator.ERRORDTDFileNotExists.Subject"),
                                Messages.getString("JobEntryDTDValidator.ERRORDTDFileNotExists.Msg",
                                        realDTDfilename));
                    }
                }

                if ((dtdintern && xmlStartDTD == -1 || (!dtdintern && !DTDfile.exists()))) {
                    result.setResult(false);
                    result.setNrErrors(1);
                } else {
                    DocBuilderFactory.setValidating(true);

                    // Let's parse now ...

                    xmlDocDTD = DocBuilder
                            .parse(new ByteArrayInputStream(xmlStringbuffer.toString().getBytes(encoding)));
                    if (log.isDetailed())
                        log.logDetailed(Messages.getString("JobEntryDTDValidator.DTDValidatorOK.Subject"),
                                Messages.getString("JobEntryDTDValidator.DTDValidatorOK.Label",
                                        realxmlfilename));

                    // Everything is OK
                    result.setResult(true);
                }

            } else {

                if (!xmlfile.exists()) {
                    log.logError(toString(),
                            Messages.getString("JobEntryDTDValidator.FileDoesNotExist.Label", realxmlfilename));
                }

                result.setResult(false);
                result.setNrErrors(1);
            }

        } else {
            log.logError(toString(), Messages.getString("JobEntryDTDValidator.AllFilesNotNull.Label"));
            result.setResult(false);
            result.setNrErrors(1);
        }

    }

    catch (Exception e) {
        log.logError(Messages.getString("JobEntryDTDValidator.ErrorDTDValidator.Subject"),
                Messages.getString("JobEntryDTDValidator.ErrorDTDValidator.Label", realxmlfilename,
                        realDTDfilename, e.getMessage()));

        result.setResult(false);
        result.setNrErrors(1);
    } finally {
        try {
            if (xmlfile != null)
                xmlfile.close();

            if (DTDfile != null)
                DTDfile.close();

        } catch (IOException e) {
        }
    }

    return result;
}

From source file:org.talend.designer.runprocess.java.JavaProcessor.java

/**
 * DOC chuang Comment method "computeMethodSizeIfNeeded".
 * /*from  w w  w  .  j  a  v a 2 s. c  o m*/
 * @param processCode
 * @return
 */
private String computeMethodSizeIfNeeded(String processCode) {
    // must match TalendDesignerPrefConstants.DISPLAY_METHOD_SIZE
    boolean displayMethodSize = Boolean.parseBoolean(
            CorePlugin.getDefault().getDesignerCoreService().getPreferenceStore("displayMethodSize")); //$NON-NLS-1$
    if (displayMethodSize) {
        StringBuffer code = new StringBuffer(processCode);
        int fromIndex = 0;
        while (fromIndex != -1 && fromIndex < code.length()) {
            int methodStartPos = code.indexOf(METHOD_START_COMMENT, fromIndex);
            if (methodStartPos < 0) {
                break;
            }
            int sizeCommentPos = code.indexOf(SIZE_COMMENT, fromIndex);

            // move ahead to the start position of source code
            methodStartPos = code.indexOf("*/", sizeCommentPos) + 2; //$NON-NLS-1$

            int methodEndPos = code.indexOf(METHOD_END_COMMENT, fromIndex);
            if (methodEndPos < 0) {
                break;
            }
            // start position for next search
            fromIndex = methodEndPos + METHOD_END_COMMENT.length();
            // go back to the end position of source code
            methodEndPos = code.lastIndexOf("/*", methodEndPos); //$NON-NLS-1$
            int size = methodEndPos - methodStartPos;
            code.replace(sizeCommentPos, sizeCommentPos + SIZE_COMMENT.length(), String.valueOf(size));

        }
        return code.toString();
    } else {
        return processCode;
    }
}

From source file:org.etudes.mneme.impl.ImporteCollegeTextServiceImpl.java

/**
 * Parses a Fill in the Blank type question and adds to the pool
 * /* w  ww .  j  a  va 2  s. c o  m*/
 * @param pool
 *        pool where to add questions
 * @param lines
 *        text lines to parse and create a question
 * @return true if found Fill in the blank question
 * 
 * @throws AssessmentPermissionException
 */
protected boolean processTextFillBlanks(Pool pool, Part part, String[] lines)
        throws AssessmentPermissionException {
    String firstLine = lines[0].trim();
    if (!firstLine.startsWith("Collapse Question"))
        return false;
    String[] partsType = StringUtil.split(firstLine, "\t");

    String questionType = partsType[1].trim();
    if (!("FB".equals(questionType)))
        return false;

    String text = "";
    StringBuffer buildAnswers = new StringBuffer("{");
    Boolean isResponseTextual = null;
    // create the question
    Question question = this.questionService.newQuestion(pool, "mneme:FillBlanks");
    FillBlanksQuestionImpl f = (FillBlanksQuestionImpl) (question.getTypeSpecificQuestion());

    // case sensitive
    f.setCaseSensitive(Boolean.FALSE.toString());

    // mutually exclusive
    f.setAnyOrder(Boolean.FALSE.toString());

    for (int line = 0; line < lines.length; line++) {
        String processText = lines[line].trim();
        if (processText.length() == 0)
            continue;
        if (processText.startsWith("Points"))
            continue;
        if (processText.startsWith("Edit QuestionEdit"))
            break;

        String[] parts = StringUtil.split(processText, "\t");
        if (parts == null || parts.length == 0)
            return true;

        if (processText.startsWith("Instructor Explanation:")) {
            String[] feedbackText = new String[] { "" };
            line = getFeedbackText(lines, line, feedbackText);
            question.setFeedback(Validator.escapeHtml(feedbackText[0].trim()));
            break;
        }

        // set the text
        if (line == 0) {
            String[] questionText = new String[] { "" };
            line = getQuestionText(lines, line, questionText);
            text = Validator.escapeHtml(questionText[0].trim());
            question.getPresentation().setText(text);
        } else {
            String answer = parts[0];
            answer = Validator.escapeHtml(answer);
            buildAnswers.append(answer);
            buildAnswers.append("|");
            if (isResponseTextual == null)
                isResponseTextual = checkIfTextualorNumeric(answer);
            else
                isResponseTextual = (isResponseTextual || checkIfTextualorNumeric(answer));
        }
    }
    buildAnswers.replace(buildAnswers.length() - 1, buildAnswers.length(), "}");
    text = text.concat(buildAnswers.toString());
    question.getPresentation().setText(text);
    f.setText(text);

    // text or numeric
    if (isResponseTextual == null)
        isResponseTextual = true;
    f.setResponseTextual(isResponseTextual.toString());

    // survey
    question.setIsSurvey(false);

    // save
    question.getTypeSpecificQuestion().consolidate("");
    this.questionService.saveQuestion(question);

    QuestionPick questionPick = part.addPickDetail(question);
    questionPick.setPoints(Float.parseFloat("1"));

    return true;

}

From source file:usbong.android.utils.UsbongUtils.java

public static ArrayList<String> tokenizeJapaneseString(String s) {
    ArrayList<String> output = new ArrayList<String>();
    StringBuffer kanjiCommonPhrase = new StringBuffer();
    StringBuffer kanjiRarePhrase = new StringBuffer();
    StringBuffer hiraganaPhrase = new StringBuffer();
    StringBuffer katakanaPhrase = new StringBuffer();
    StringBuffer japanesePunctuationPhrase = new StringBuffer();
    StringBuffer fullWidthRomanCharAndHalfWidthKatakanaPhrase = new StringBuffer();
    StringBuffer othersPhrase = new StringBuffer();

    Log.d(">>>>inside: tokenizeJapaneseString", "" + s);

    int value;//w w w . ja  v  a2 s.  c  o  m
    //go through character by character
    for (int i = 0; i < s.length();) {
        value = (int) s.charAt(i);
        Log.d(">>>>", "" + s.charAt(i));

        //Reference: http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml;
        //last accessed: 4 Oct. 2014
        //Reference: http://stackoverflow.com/questions/3826918/how-to-classify-japanese-characters-as-either-kanji-or-kana;
        //last accessed: 4 Oct. 2014;
        //answer by Jack, Sept. 30, 2010
        //KANJI (COMMON)
        if (value >= 0x4e00 && value <= 0x9faf) {
            for (; i < s.length() && value >= 0x4e00 && value <= 0x9faf;) { //while Kanji Char
                kanjiCommonPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }
            //            Log.d(">>>","kanjiPhrase: "+kanjiPhrase);
            if (!kanjiCommonPhrase.toString().equals("")) {
                output.add(kanjiCommonPhrase.toString());
                kanjiCommonPhrase.replace(0, kanjiCommonPhrase.length(), "");
                //                Log.d(">>>","here");
                continue;
            }
        }
        //KANJI (RARE)
        if (value >= 0x3400 && value <= 0x4dbf) {
            for (; i < s.length() && value >= 0x3400 && value <= 0x4dbf;) {
                kanjiRarePhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }
            //            Log.d(">>>","kanjiPhrase: "+kanjiPhrase);
            if (!kanjiRarePhrase.toString().equals("")) {
                output.add(kanjiRarePhrase.toString());
                kanjiRarePhrase.replace(0, kanjiRarePhrase.length(), "");
                //                Log.d(">>>","here");
                continue;
            }
        } else if (value >= 0x3040 && value <= 0x309f) {
            //HIRAGANA
            for (; i < s.length() && value >= 0x3040 && value <= 0x309f;) {
                hiraganaPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }

            if (!hiraganaPhrase.toString().equals("")) {
                output.add(hiraganaPhrase.toString());
                hiraganaPhrase.replace(0, hiraganaPhrase.length(), "");
                continue;
            }
        } else if (value >= 0x30a0 && value <= 0x30ff) {
            //KATAKANA
            for (; i < s.length() && value >= 0x30a0 && value <= 0x30ff;) {
                katakanaPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }

            if (!katakanaPhrase.toString().equals("")) {
                output.add(katakanaPhrase.toString());
                katakanaPhrase.replace(0, katakanaPhrase.length(), "");
                continue;
            }
        } else if (value >= 0x3000 && value <= 0x303f) {
            //JAPANESE PUNCTUATION
            for (; i < s.length() && value >= 0x3000 && value <= 0x303f;) {
                japanesePunctuationPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }

            if (!japanesePunctuationPhrase.equals("")) {
                output.add(japanesePunctuationPhrase.toString());
                japanesePunctuationPhrase.replace(0, japanesePunctuationPhrase.length(), "");
                continue;
            }
        } else if (value >= 0xff00 && value <= 0xffef) {
            //FULL-WIDTH ROMAN CHARACTERS and HALF-WIDTH KATAKANA
            for (; i < s.length() && value >= 0xff00 && value <= 0xffef;) {
                fullWidthRomanCharAndHalfWidthKatakanaPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }

            if (!fullWidthRomanCharAndHalfWidthKatakanaPhrase.toString().equals("")) {
                output.add(fullWidthRomanCharAndHalfWidthKatakanaPhrase.toString());
                fullWidthRomanCharAndHalfWidthKatakanaPhrase.replace(0,
                        fullWidthRomanCharAndHalfWidthKatakanaPhrase.length(), "");
                continue;
            }
        } else {
            //IF NONE OF THE ABOVE
            for (; i < s.length() && ((value < 0x3000 || value > 0xffef));) {
                othersPhrase.append(s.charAt(i));
                i++;
                if (i < s.length()) {
                    value = (int) s.charAt(i);
                } else {
                    break;
                }
            }

            if (!othersPhrase.toString().equals("")) {
                output.add(othersPhrase.toString() + " ");
                othersPhrase.replace(0, othersPhrase.length(), "");
                continue;
            }
        }
        i++; //do increment here          
    }
    return output;
}

From source file:org.exoplatform.wiki.rendering.render.confluence.ConfluenceSyntaxEscapeHandler.java

public void escape(StringBuffer accumulatedBuffer, ConfluenceSyntaxListenerChain listenerChain,
        boolean escapeLastChar, Pattern escapeFirstIfMatching) {
    BlockStateChainingListener blockStateListener = listenerChain.getBlockStateChainingListener();

    // Escape tilde symbol (i.e. the escape character).
    // Note: This needs to be the first replacement since other replacements below also use the tilde symbol
    replaceAll(accumulatedBuffer, ESCAPE_CHAR, ESCAPE_CHAR + ESCAPE_CHAR);

    // When in a paragraph we need to escape symbols that are at beginning of lines and that could be confused
    // with list items, headers or tables.
    if (blockStateListener.isInLine() && isOnNewLine()) {

        // Look for list pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(LIST_PATTERN, accumulatedBuffer);

        // Look for header pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(HEADER_PATTERN, accumulatedBuffer);

        // Look for table character patterns at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(TABLE_PATTERN, accumulatedBuffer);

        // Look for quote pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(QUOTE_PATTERN, accumulatedBuffer);
    }//from w  w w.  j  a  va2 s .  co m

    // Escape table characters
    if (blockStateListener.isInTable()) {
        replaceAll(accumulatedBuffer, "|", ESCAPE_CHAR + "|");
        replaceAll(accumulatedBuffer, "||", ESCAPE_CHAR + "|" + ESCAPE_CHAR + "|");
    }

    if (escapeFirstIfMatching != null) {
        escapeFirstMatchedCharacter(escapeFirstIfMatching, accumulatedBuffer);
    }

    // When in a header we need to escape "=" symbols since otherwise they would
    // be confused for end of section characters.
    if (blockStateListener.isInHeader()) {
        replaceAll(accumulatedBuffer, "=", ESCAPE_CHAR + "=");
    }

    // Escape verbatim "{{{"
    replaceAll(accumulatedBuffer, "{{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape "{{"
    replaceAll(accumulatedBuffer, "{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape groups
    replaceAll(accumulatedBuffer, "(((", ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(");
    replaceAll(accumulatedBuffer, ")))", ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")");

    // Escape reserved keywords
    Matcher matcher = DOUBLE_CHARS_PATTERN.matcher(accumulatedBuffer.toString());
    for (int i = 0; matcher.find(); i = i + 2) {
        accumulatedBuffer.replace(matcher.start() + i, matcher.end() + i,
                ESCAPE_CHAR + matcher.group().charAt(0) + ESCAPE_CHAR + matcher.group().charAt(1));
    }

    // Escape ":" in "image:something", "attach:something" and "mailto:something"
    // Note: even though there are some restriction in the URI specification as to what character is valid after
    // the ":" character following the scheme we only check for characters greater than the space symbol for
    // simplicity.
    escapeURI(accumulatedBuffer, "image:");
    escapeURI(accumulatedBuffer, "attach:");
    escapeURI(accumulatedBuffer, "mailto:");

    // Escape last character if we're told to do so. This is to handle cases such as:
    // - onWord("hello:") followed by onFormat(ITALIC) which would lead to "hello://" if the ":" wasn't escaped
    // - onWord("{") followed by onMacro() which would lead to "{{{" if the "{" wasn't escaped
    if (escapeLastChar) {
        accumulatedBuffer.insert(accumulatedBuffer.length() - 1, '~');
    }

    // Escape begin link
    replaceAll(accumulatedBuffer, "[", ESCAPE_CHAR + "[");

    // Escape link label
    int linkLevel = getLinkLevel(listenerChain);

    if (linkLevel > 0) {
        // This need to be done after anything else because link label add another level of escaping (escaped as
        // link label and then escaped as wiki content).
        String escape = StringUtils.repeat(ESCAPE_CHAR, linkLevel);
        replaceAll(accumulatedBuffer, ESCAPE_CHAR, escape + ESCAPE_CHAR);
        replaceAll(accumulatedBuffer, "]", escape + "]");
        replaceAll(accumulatedBuffer, "^", escape + "^");
        replaceAll(accumulatedBuffer, "|", escape + "|");
    }
}

From source file:org.bibsonomy.scraper.url.kde.acm.ACMBasicScraper.java

@Override
protected boolean scrapeInternal(ScrapingContext sc) throws ScrapingException {
    sc.setScraper(this);

    try {//from w  w  w . j  a v  a 2  s.c o  m
        /*
         * extract the id from the URL
         */
        final Matcher matcher = URL_PARAM_ID_PATTERN.matcher(sc.getUrl().getQuery());
        if (matcher.find()) {
            String id = matcher.group(1);
            /*
             * If the id contains a dot ".", we use the part after the dot.
             * TODO: Can we do this nicer? 
             */
            final int indexOfDot = id.indexOf(".");
            if (indexOfDot > -1) {
                id = id.substring(indexOfDot + 1);
            }
            /*
             * Scrape entries from popup BibTeX site. BibTeX entry on these
             * pages looks like this: <PRE id="155273">@article{155273,
             * author = {The Author}, title = {This is the title}...}</pre>
             */
            final StringBuffer bibtexEntries = extractBibtexEntries(SITE_URL,
                    "exportformats.cfm?expformat=bibtex&id=" + id);

            /*
             * download the abstract
             * FIXME: 
             * 
             * IDs with a "dot" (e.g., 1082036.1082037 seem to have no abstract:
             * 
             * http://portal.acm.org/tab_abstract.cfm?id=1082036.1082037&usebody=tabbody
             * 
             * We must use only the part after the dot to get it:
             * 
             * http://portal.acm.org/tab_abstract.cfm?id=1082037&usebody=tabbody
             * 
             * This must be done!
             * 
             */
            final String abstrct = WebUtils
                    .getContentAsString(SITE_URL + "/tab_abstract.cfm?usebody=tabbody&id=" + id);
            if (present(abstrct)) {
                /*
                 * extract abstract from HTML
                 */
                final Matcher matcher2 = ABSTRACT_PATTERN.matcher(abstrct);
                if (matcher2.find()) {
                    /*
                     * FIXME: we don't get here for URL 
                     * 
                     * http://doi.acm.org/10.1145/1105664.1105676
                     *  
                     * Thus, the pattern fails for that abstract at
                     * 
                     * http://portal.acm.org/tab_abstract.cfm?id=1105676&usebody=tabbody
                     */
                    final String extractedAbstract = matcher2.group(2);
                    BibTexUtils.addFieldIfNotContained(bibtexEntries, "abstract", extractedAbstract);

                } else {
                    // log if abstract is not available
                    log.info("ACMBasicScraper: Abstract not available");
                }
            } else {
                // log if abstract is not available
                log.info("ACMBasicScraper: Abstract not available");
            }

            /*
             * Some entries (e.g., http://portal.acm.org/citation.cfm?id=500737.500755) seem
             * to have broken BibTeX entries with a "," too much at the end. We remove this
             * here.
             *
             * Some entries have the following end: "},\n} \n" instead of the BROKEN_END String.
             * So we have to adjust the starting index by the additional 2 symbols.
             */
            final int indexOf = bibtexEntries.indexOf(BROKEN_END,
                    bibtexEntries.length() - BROKEN_END.length() - 2);
            if (indexOf > 0) {
                bibtexEntries.replace(indexOf, bibtexEntries.length(), "}\n}");
            }

            final String result = DOIUtils.cleanDOI(bibtexEntries.toString().trim());
            //final String result = bibtexEntries.toString().trim();

            if (!"".equals(result)) {
                sc.setBibtexResult(result);
                return true;
            } else
                throw new ScrapingFailureException("getting bibtex failed");
        }
        return false;

    } catch (Exception e) {
        throw new InternalFailureException(e);
    }
}

From source file:org.janusgraph.graphdb.database.IndexSerializer.java

public Iterable<RawQuery.Result> executeQuery(IndexQueryBuilder query, final ElementCategory resultType,
        final BackendTransaction backendTx, final StandardJanusGraphTx transaction) {
    MixedIndexType index = getMixedIndex(query.getIndex(), transaction);
    Preconditions.checkArgument(index.getElement() == resultType,
            "Index is not configured for the desired result type: %s", resultType);
    String backingIndexName = index.getBackingIndexName();
    IndexProvider indexInformation = (IndexProvider) mixedIndexes.get(backingIndexName);

    StringBuffer qB = new StringBuffer(query.getQuery());
    final String prefix = query.getPrefix();
    Preconditions.checkNotNull(prefix);//  w  w  w  .  j a va 2s .  c  om
    //Convert query string by replacing
    int replacements = 0;
    int pos = 0;
    while (pos < qB.length()) {
        pos = qB.indexOf(prefix, pos);
        if (pos < 0)
            break;

        int startPos = pos;
        pos += prefix.length();
        StringBuilder keyBuilder = new StringBuilder();
        boolean quoteTerminated = qB.charAt(pos) == '"';
        if (quoteTerminated)
            pos++;
        while (pos < qB.length() && (Character.isLetterOrDigit(qB.charAt(pos))
                || (quoteTerminated && qB.charAt(pos) != '"') || qB.charAt(pos) == '*')) {
            keyBuilder.append(qB.charAt(pos));
            pos++;
        }
        if (quoteTerminated)
            pos++;
        int endPos = pos;
        String keyname = keyBuilder.toString();
        Preconditions.checkArgument(StringUtils.isNotBlank(keyname),
                "Found reference to empty key at position [%s]", startPos);
        String replacement;
        if (keyname.equals("*")) {
            replacement = indexInformation.getFeatures().getWildcardField();
        } else if (transaction.containsRelationType(keyname)) {
            PropertyKey key = transaction.getPropertyKey(keyname);
            Preconditions.checkNotNull(key);
            Preconditions.checkArgument(index.indexesKey(key),
                    "The used key [%s] is not indexed in the targeted index [%s]", key.name(),
                    query.getIndex());
            replacement = key2Field(index, key);
        } else {
            Preconditions.checkArgument(query.getUnknownKeyName() != null,
                    "Found reference to non-existant property key in query at position [%s]: %s", startPos,
                    keyname);
            replacement = query.getUnknownKeyName();
        }
        Preconditions.checkArgument(StringUtils.isNotBlank(replacement));

        qB.replace(startPos, endPos, replacement);
        pos = startPos + replacement.length();
        replacements++;
    }

    String queryStr = qB.toString();
    if (replacements <= 0)
        log.warn("Could not convert given {} index query: [{}]", resultType, query.getQuery());
    log.info("Converted query string with {} replacements: [{}] => [{}]", replacements, query.getQuery(),
            queryStr);
    RawQuery rawQuery = new RawQuery(index.getStoreName(), queryStr, query.getParameters());
    if (query.hasLimit())
        rawQuery.setLimit(query.getLimit());
    rawQuery.setOffset(query.getOffset());
    return Iterables.transform(backendTx.rawQuery(index.getBackingIndexName(), rawQuery),
            new Function<RawQuery.Result<String>, RawQuery.Result>() {
                @Nullable
                @Override
                public RawQuery.Result apply(@Nullable RawQuery.Result<String> result) {
                    return new RawQuery.Result(string2ElementId(result.getResult()), result.getScore());
                }
            });
}

From source file:org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeHandler.java

public void escape(StringBuffer accumulatedBuffer, XWikiSyntaxListenerChain listenerChain,
        boolean escapeLastChar, Pattern escapeFirstIfMatching) {
    BlockStateChainingListener blockStateListener = listenerChain.getBlockStateChainingListener();

    // Escape tilde symbol (i.e. the escape character).
    // Note: This needs to be the first replacement since other replacements below also use the tilde symbol
    replaceAll(accumulatedBuffer, ESCAPE_CHAR, ESCAPE_CHAR + ESCAPE_CHAR);

    // Escape anything that looks like starting of custom parameters
    replaceAll(accumulatedBuffer, "(%", ESCAPE_CHAR + "(%");

    // When in a paragraph we need to escape symbols that are at beginning of lines and that could be confused
    // with list items, headers or tables.
    if (blockStateListener.isInLine() && isOnNewLine()) {

        // Look for list pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(LIST_PATTERN, accumulatedBuffer);

        // Look for header pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(HEADER_PATTERN, accumulatedBuffer);

        // Look for table character patterns at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(TABLE_PATTERN, accumulatedBuffer);

        // Look for quote pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(QUOTE_PATTERN, accumulatedBuffer);
    }//from  w  w  w  .  j  a  v  a  2 s  .c  o m

    // Escape table characters
    if (blockStateListener.isInTable()) {
        replaceAll(accumulatedBuffer, "|", ESCAPE_CHAR + "|");
        replaceAll(accumulatedBuffer, "!!", ESCAPE_CHAR + "!!");
    }

    if (escapeFirstIfMatching != null) {
        escapeFirstMatchedCharacter(escapeFirstIfMatching, accumulatedBuffer);
    }

    // When in a header we need to escape "=" symbols since otherwise they would be confused for end of section
    // characters.
    if (blockStateListener.isInHeader()) {
        replaceAll(accumulatedBuffer, "=", ESCAPE_CHAR + "=");
    }

    // Escape verbatim "{{{"
    replaceAll(accumulatedBuffer, "{{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape "{{"
    replaceAll(accumulatedBuffer, "{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape groups
    replaceAll(accumulatedBuffer, "(((", ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(");
    replaceAll(accumulatedBuffer, ")))", ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")");

    // Escape reserved keywords
    Matcher matcher = DOUBLE_CHARS_PATTERN.matcher(accumulatedBuffer.toString());
    for (int i = 0; matcher.find(); i = i + 2) {
        accumulatedBuffer.replace(matcher.start() + i, matcher.end() + i,
                ESCAPE_CHAR + matcher.group().charAt(0) + ESCAPE_CHAR + matcher.group().charAt(1));
    }

    // Escape ":" in "image:something", "attach:something" and "mailto:something"
    // Note: even though there are some restriction in the URI specification as to what character is valid after
    // the ":" character following the scheme we only check for characters greater than the space symbol for
    // simplicity.
    escapeURI(accumulatedBuffer, "image:");
    escapeURI(accumulatedBuffer, "attach:");
    escapeURI(accumulatedBuffer, "mailto:");

    // Escape last character if we're told to do so. This is to handle cases such as:
    // - onWord("hello:") followed by onFormat(ITALIC) which would lead to "hello://" if the ":" wasn't escaped
    // - onWord("{") followed by onMacro() which would lead to "{{{" if the "{" wasn't escaped
    if (escapeLastChar) {
        accumulatedBuffer.insert(accumulatedBuffer.length() - 1, '~');
    }

    // Escape begin link
    replaceAll(accumulatedBuffer, "[[", ESCAPE_CHAR + "[" + ESCAPE_CHAR + "[");

    // Escape link label
    int linkLevel = getLinkLevel(listenerChain);

    if (linkLevel > 0) {
        // This need to be done after anything else because link label add another level of escaping (escaped as
        // link label and then escaped as wiki content).
        String escape = StringUtils.repeat(ESCAPE_CHAR, linkLevel);
        replaceAll(accumulatedBuffer, ESCAPE_CHAR, escape + ESCAPE_CHAR);
        replaceAll(accumulatedBuffer, "]]", escape + "]" + escape + "]");
        replaceAll(accumulatedBuffer, ">>", escape + ">" + escape + ">");
        replaceAll(accumulatedBuffer, "||", escape + "|" + escape + "|");
    }
}