Example usage for java.lang CharSequence subSequence

List of usage examples for java.lang CharSequence subSequence

Introduction

In this page you can find the example usage for java.lang CharSequence subSequence.

Prototype

CharSequence subSequence(int start, int end);

Source Link

Document

Returns a CharSequence that is a subsequence of this sequence.

Usage

From source file:org.archive.crawler.extractor.ExtractorJS.java

public static long considerStrings(CrawlURI curi, CharSequence cs, CrawlController controller,
        boolean handlingJSFile) {
    long foundLinks = 0;
    Matcher strings = TextUtils.getMatcher(JAVASCRIPT_STRING_EXTRACTOR, cs);
    while (strings.find()) {
        CharSequence subsequence = cs.subSequence(strings.start(2), strings.end(2));

        if (UriUtils.isLikelyUriJavascriptContextLegacy(subsequence)) {
            String string = subsequence.toString();
            string = UriUtils.speculativeFixup(string, curi.getUURI());
            foundLinks++;// www . j ava  2 s.c  o m
            try {
                if (handlingJSFile) {
                    curi.createAndAddLinkRelativeToVia(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                } else {
                    curi.createAndAddLinkRelativeToBase(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                }
            } catch (URIException e) {
                // There may not be a controller (e.g. If we're being run
                // by the extractor tool).
                if (controller != null) {
                    controller.logUriError(e, curi.getUURI(), string);
                } else {
                    LOGGER.info(curi + ", " + string + ": " + e.getMessage());
                }
            }
        } else {
            foundLinks += considerStrings(curi, subsequence, controller, handlingJSFile);
        }
    }
    TextUtils.recycleMatcher(strings);
    return foundLinks;
}

From source file:dk.netarkivet.harvester.harvesting.extractor.ExtractorJS.java

public static long considerStrings(CrawlURI curi, CharSequence cs, CrawlController controller,
        boolean handlingJSFile) {
    long foundLinks = 0;
    Matcher strings = TextUtils.getMatcher(JAVASCRIPT_STRING_EXTRACTOR, cs);
    while (strings.find()) {
        CharSequence subsequence = cs.subSequence(strings.start(2), strings.end(2));

        if (UriUtils.isLikelyUriJavascriptContextLegacy(subsequence)) {
            String string = subsequence.toString();
            string = StringEscapeUtils.unescapeJavaScript(string);
            string = UriUtils.speculativeFixup(string, curi.getUURI());
            foundLinks++;//from  ww  w.  ja va 2s.c  om
            try {
                if (handlingJSFile) {
                    curi.createAndAddLinkRelativeToVia(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                } else {
                    curi.createAndAddLinkRelativeToBase(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                }
            } catch (URIException e) {
                // There may not be a controller (e.g. If we're being run
                // by the extractor tool).
                if (controller != null) {
                    controller.logUriError(e, curi.getUURI(), string);
                } else {
                    LOGGER.info(curi + ", " + string + ": " + e.getMessage());
                }
            }
        } else {
            foundLinks += considerStrings(curi, subsequence, controller, handlingJSFile);
        }
    }
    TextUtils.recycleMatcher(strings);
    return foundLinks;
}

From source file:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * @param   text        Full text// w  ww .j a  va 2s.  com
 * @param   offset      Offset from before which to extract one character
 * @return  String      Character BEFORE word at given caret offset
 */
public static String getCharBeforeOffset(CharSequence text, int offset) {
    if (text.length() == 0 || offset == 0)
        return "";

    if (offset > 0) {
        return text.subSequence(offset - 1, offset).toString();
    }

    return "";
}

From source file:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * @param   text        Full text/* ww  w. j  a  v  a2  s  .  c  o  m*/
 * @param   offset      Offset from after which to extract one character
 * @return  String      Character AFTER word at caret offset
 */
public static String getCharAfterOffset(CharSequence text, int offset) {
    if (text.length() < offset + 2 || offset == 0)
        return "";

    if (offset > 0) {
        return text.subSequence(offset + 1, offset + 2).toString();
    }

    return "";
}

From source file:com.oasisfeng.nevo.decorators.whatsapp.WhatsAppDecorator.java

private static CharSequence trim(final CharSequence cs) {
    final int last = cs.length() - 1;
    int start = 0;
    int end = last;
    while ((start <= end) && (cs.charAt(start) <= ' '))
        start++;//  w w  w .j a  v a2  s.c o m
    while ((end >= start) && (cs.charAt(end) <= ' '))
        end--;
    if (start == 0 && end == last)
        return cs;
    return cs.subSequence(start, end + 1);
}

From source file:org.jetbrains.jet.grammar.GrammarGenerator.java

private static StringBuilder markDeclarations(CharSequence allRules) {
    StringBuilder output = new StringBuilder();

    Pattern symbolReference = Pattern.compile("^\\w+$", Pattern.MULTILINE);
    Matcher matcher = symbolReference.matcher(allRules);
    int copiedUntil = 0;
    while (matcher.find()) {
        int start = matcher.start();
        output.append(allRules.subSequence(copiedUntil, start));

        String group = matcher.group();
        output.append("&").append(group);
        copiedUntil = matcher.end();/*  w  ww  .  j  ava 2s .c  om*/
    }
    output.append(allRules.subSequence(copiedUntil, allRules.length()));
    return output;
}

From source file:com.xebia.xsdnl.innorater.Crap.java

/**
 * Trim a CharSequence, both ends.//from   ww w .  ja v a  2  s .co  m
 * @param cs a char sequence.
 * @return a trimmed sub-sequence, or the entire original thing.
 */
public static CharSequence trim(@Nullable CharSequence cs) {
    if (cs == null || cs.length() == 0) {
        return cs;
    }
    int start = 0;
    int end = cs.length();
    for (; end > 1; end--) {
        if (!Character.isWhitespace(cs.charAt(end - 1))) {
            break;
        }
    }
    for (; start < end; start++) {
        if (!Character.isWhitespace(cs.charAt(start))) {
            break;
        }
    }
    return cs.subSequence(start, end);
}

From source file:com.oasisfeng.nevo.decorators.whatsapp.WhatsAppDecorator.java

/**
 * Patterns//  ww  w  .  j a v a 2  s .  c  o m
 *
 *       Title         Line
 *       -----         ----
 *    1.   Sender         Message
 *    2.   Sender @ Group   Message
 *    3.   WhatsApp      Sender: Message
 *    4.   Group         Sender: Message
 *    5.   Summary         Sender @ Group: Message
 *
 * @return CharSequence[] { who, group, message }
 */
private static CharSequence[] extract(final CharSequence title, final CharSequence line) {
    final int pos_colon = line.toString().indexOf(':');
    if (pos_colon < 0) { // Pattern 1 or 2
        final int pos_at = title.toString().indexOf('@');
        if (pos_at <= 0)
            return new CharSequence[] { title, null, line }; // Pattern 1
        final CharSequence who = trim(title.subSequence(0, pos_at));
        final CharSequence group = trim(title.subSequence(pos_at + 1, title.length()));
        return new CharSequence[] { who, group, line }; // Pattern 2
    } // Pattern 3, 4 or 5
    final CharSequence message = trim(line.subSequence(pos_colon + 1, line.length()));
    final CharSequence from = line.subSequence(0, pos_colon), who, group;
    final int pos_at = from.toString().indexOf('@');
    if (pos_at <= 0) {
        group = "WhatsApp".equals(title) ? null : title;
        return new CharSequence[] { from, group, message }; // Pattern 3 or 4
    } else {
        who = trim(from.subSequence(0, pos_at));
        group = trim(from.subSequence(pos_at + 1, from.length()));
        return new CharSequence[] { who, group, message }; // Pattern 5
    }
}

From source file:com.cyberway.issue.crawler.extractor.ExtractorJS.java

public static long considerStrings(CrawlURI curi, CharSequence cs, CrawlController controller,
        boolean handlingJSFile) {
    long foundLinks = 0;
    Matcher strings = TextUtils.getMatcher(JAVASCRIPT_STRING_EXTRACTOR, cs);
    while (strings.find()) {
        CharSequence subsequence = cs.subSequence(strings.start(2), strings.end(2));
        Matcher uri = TextUtils.getMatcher(STRING_URI_DETECTOR, subsequence);
        if (uri.matches()) {
            String string = uri.group();
            string = speculativeFixup(string, curi);
            foundLinks++;/*w  w  w. jav  a  2  s. co  m*/
            try {
                if (handlingJSFile) {
                    curi.createAndAddLinkRelativeToVia(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                } else {
                    curi.createAndAddLinkRelativeToBase(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                }
            } catch (URIException e) {
                // There may not be a controller (e.g. If we're being run
                // by the extractor tool).
                if (controller != null) {
                    controller.logUriError(e, curi.getUURI(), string);
                } else {
                    LOGGER.info(curi + ", " + string + ": " + e.getMessage());
                }
            }
        } else if (subsequence.toString().startsWith("/")) {
            try {
                curi.createAndAddLinkRelativeToBase(subsequence.toString(), Link.JS_MISC, Link.NAVLINK_HOP);
            } catch (URIException e) {
                if (controller != null) {
                    controller.logUriError(e, curi.getUURI(), subsequence);
                } else {
                    LOGGER.info(curi + ", " + subsequence + ": " + e.getMessage());
                }
            }
        }

        else {
            foundLinks += considerStrings(curi, subsequence, controller, handlingJSFile);
        }
        TextUtils.recycleMatcher(uri);
    }
    TextUtils.recycleMatcher(strings);
    return foundLinks;
}

From source file:Main.java

/**
 * This method determines if the direction of a substring is right-to-left.
 * If the string is empty that determination is based on the default system language
 * Locale.getDefault()./* w  w w.  j  ava 2s  .  c  om*/
 * The method can handle invalid substring definitions (start > end etc.), in which case the
 * method returns False.
 *
 * @return True if the text direction is right-to-left, false otherwise.
 */
public static boolean isRTL(CharSequence s, int start, int end) {
    if (s == null || s.length() == 0) {
        // empty string --> determine the direction from the default language
        return isRTL(Locale.getDefault());
    }

    if (start == end) {
        // if no character is selected we need to expand the selection
        start = Math.max(0, --start);
        if (start == end) {
            end = Math.min(s.length(), ++end);
        }
    }

    try {
        Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        return !bidi.baseIsLeftToRight();
    } catch (IndexOutOfBoundsException e) {
        return false;
    }
}