Example usage for java.util.regex Pattern UNICODE_CASE

List of usage examples for java.util.regex Pattern UNICODE_CASE

Introduction

In this page you can find the example usage for java.util.regex Pattern UNICODE_CASE.

Prototype

int UNICODE_CASE

To view the source code for java.util.regex Pattern UNICODE_CASE.

Click Source Link

Document

Enables Unicode-aware case folding.

Usage

From source file:org.languagetool.rules.patterns.PatternRuleHandler.java

/**
 * Create rule from an Element list.//from w  w w .  j a  va2  s.  c om
 * In case of OR groups, several rules are created recursively.
 * @since 2.3
 * 
 * @param elemList The complete original Element list
 * @param tmpPatternTokens Temporary list being created
 * @param numElement Index of elemList being analyzed
 */
private void createRules(List<PatternToken> elemList, List<PatternToken> tmpPatternTokens, int numElement) {
    String shortMessage = "";
    if (this.shortMessage != null && this.shortMessage.length() > 0) {
        shortMessage = this.shortMessage.toString();
    } else if (shortMessageForRuleGroup != null && shortMessageForRuleGroup.length() > 0) {
        shortMessage = this.shortMessageForRuleGroup.toString();
    }
    if (numElement >= elemList.size()) {
        AbstractPatternRule rule;
        if (tmpPatternTokens.size() > 0) {
            rule = new PatternRule(id, language, tmpPatternTokens, name, message.toString(), shortMessage,
                    suggestionsOutMsg.toString(), phrasePatternTokens.size() > 1,
                    interpretPosTagsPreDisambiguation);
            rule.setSourceFile(sourceFile);
        } else if (regex.length() > 0) {
            int flags = regexCaseSensitive ? 0 : Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
            String regexStr = regex.toString();
            if (regexMode == RegexpMode.SMART) {
                // Note: it's not that easy to add \b because the regex might look like '(foo)' or '\d' so we cannot just look at the last character
                regexStr = replaceSpacesInRegex(regexStr);
            }
            if (ruleAntiPatterns.size() > 0 || rulegroupAntiPatterns.size() > 0) {
                throw new RuntimeException(
                        "<regexp> rules currently cannot be used together with <antipattern>. Rule id: " + id
                                + "[" + subId + "]");
            }
            rule = new RegexPatternRule(id, name, message.toString(), shortMessage,
                    suggestionsOutMsg.toString(), language, Pattern.compile(regexStr, flags), regexpMark);
            rule.setSourceFile(sourceFile);
        } else {
            throw new IllegalStateException(
                    "Neither '<pattern>' tokens nor '<regex>' is set in rule '" + id + "'");
        }
        setRuleFilter(filterClassName, filterArgs, rule);
        prepareRule(rule);
        rules.add(rule);
    } else {
        PatternToken patternToken = elemList.get(numElement);
        if (patternToken.hasOrGroup()) {
            // When creating a new rule, we finally clear the backed-up variables. All the elements in
            // the OR group should share the values of backed-up variables. That's why these variables
            // are backed-up.
            List<Match> suggestionMatchesBackup = new ArrayList<>(suggestionMatches);
            List<Match> suggestionMatchesOutMsgBackup = new ArrayList<>(suggestionMatchesOutMsg);
            int startPosBackup = startPos;
            int endPosBackup = endPos;
            List<DisambiguationPatternRule> ruleAntiPatternsBackup = new ArrayList<>(ruleAntiPatterns);
            for (PatternToken patternTokenOfOrGroup : patternToken.getOrGroup()) {
                List<PatternToken> tmpElements2 = new ArrayList<>();
                tmpElements2.addAll(tmpPatternTokens);
                tmpElements2.add(ObjectUtils.clone(patternTokenOfOrGroup));
                createRules(elemList, tmpElements2, numElement + 1);
                startPos = startPosBackup;
                endPos = endPosBackup;
                suggestionMatches = suggestionMatchesBackup;
                suggestionMatchesOutMsg = suggestionMatchesOutMsgBackup;
                ruleAntiPatterns.addAll(ruleAntiPatternsBackup);
            }
        }
        tmpPatternTokens.add(ObjectUtils.clone(patternToken));
        createRules(elemList, tmpPatternTokens, numElement + 1);
    }
}

From source file:de.undercouch.bson4jackson.BsonGenerator.java

/**
 * Converts a a Java flags word into a BSON options pattern
 *
 * @param flags the Java flags// w  ww  .j a v a2  s.c  o  m
 * @return the regex options string
 */
protected String flagsToRegexOptions(int flags) {
    StringBuilder options = new StringBuilder();
    if ((flags & Pattern.CASE_INSENSITIVE) != 0) {
        options.append("i");
    }
    if ((flags & Pattern.MULTILINE) != 0) {
        options.append("m");
    }
    if ((flags & Pattern.DOTALL) != 0) {
        options.append("s");
    }
    if ((flags & Pattern.UNICODE_CASE) != 0) {
        options.append("u");
    }
    return options.toString();
}

From source file:AndroidUninstallStock.java

private static LinkedHashMap<String, String> _getListFromPattern(LinkedHashMap<String, String> apkorliblist,
        HashMap<String, String> pattern, AusInfo info, String status, boolean library) {
    LinkedHashMap<String, String> res = new LinkedHashMap<String, String>();
    if (library && !pattern.get("in").equalsIgnoreCase("library")) {
        return res;
    }/* w w w.  ja v a 2  s. c  o m*/
    int flags = getBoolean(pattern.get("case-insensitive")) ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
            : 0;
    try {
        Pattern pat = Pattern.compile(getBoolean(pattern.get("regexp")) ? pattern.get("pattern")
                : Pattern.quote(pattern.get("pattern")), flags);
        for (Map.Entry<String, String> apk : apkorliblist.entrySet()) {
            String need = "";
            switch (pattern.get("in")) {
            case "library": // TODO well as to specify the pattern package...
            case "path":
                need = apk.getKey();
                break;
            case "path+package":
                need = apk.getKey() + apk.getValue();
                break;
            case "apk":
                need = apk.getKey().substring(apk.getKey().lastIndexOf('/') + 1);
                break;
            case "package":
            default:
                need = apk.getValue();
                break;
            }
            if (pat.matcher(need).find()) {
                res.put(apk.getKey(), apk.getValue());
                System.out.println(status + need + " - " + pat.pattern());
            }
        }
    } catch (PatternSyntaxException e) {
        System.out.println("Warring in: " + info + " pattern: " + e);
    }
    return res;
}

From source file:org.etudes.util.XrefHelper.java

/**
 * Create the embedded reference detection pattern. It creates four groups: 0-the entire match, 1- src|href, 2-the reference, 3-the terminating character.
 * //from   w w  w  .  j a  va  2 s  . c  om
 * @return The Pattern.
 */
protected static Pattern getPattern() {
    return Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}

From source file:org.talend.mdm.engines.client.ui.wizards.DeployOnMDMExportWizardPage.java

/**
 * Returns the root folder name.//from w  w  w  .ja  v a2 s  .c  om
 * 
 * @return
 */
private String getRootFolderName(ExportFileResource p) {
    IPath path = new Path(this.getDestinationValue(p));
    String subjectString = path.lastSegment();
    Pattern regex = Pattern.compile("(.*)(?=(\\.(tar|zip))\\b)", Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE //$NON-NLS-1$
            | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        subjectString = regexMatcher.group(0);
    }

    return subjectString.trim();
}

From source file:org.talend.repository.ui.wizards.exportjob.scriptsmanager.JobScriptsManager.java

/**
 * Returns the root folder name.//from  www  .j  a va  2s.com
 * 
 * @return
 */
public String getRootFolderName(String path) {
    String subjectString = new Path(path).lastSegment();
    Pattern regex = Pattern.compile("(.*)(?=(\\.(tar|zip))\\b)", Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE //$NON-NLS-1$
            | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        subjectString = regexMatcher.group(0);
    }
    return subjectString.trim();
}

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

/**
 * /*from ww w  . j  a va 2  s  .com*/
 * @param questionDocument
 * @param itemBody
 * @param question
 * @param questionParts
 * @return
 */
public Element getFillBlanksResponseChoices(Document questionDocument, Element itemBody,
        FillBlanksQuestionImpl question, Map<String, Element> questionParts) {
    if (question == null)
        return itemBody;

    // itemBody
    String text = question.getText();

    Pattern p_fillBlanks_curly = Pattern.compile("([^{]*.?)(\\{)([^}]*.?)(\\})",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m_fillBlanks = p_fillBlanks_curly.matcher(text);
    StringBuffer sb = new StringBuffer();

    // in each part look for {} fill in the blank symbol and create render_fib tag
    int count = 1;
    while (m_fillBlanks.find()) {
        String fib = m_fillBlanks.group(1);
        Element textDiv = questionDocument.createElement("div");
        textDiv.setTextContent(fib);
        itemBody.appendChild(textDiv);

        String fib_curly = m_fillBlanks.group(2);
        Element fbInteraction = questionDocument.createElement("textEntryInteraction");
        fbInteraction.setAttribute("responseIdentifier", "RESPONSE" + (count++));
        fbInteraction.setAttribute("expectedLength", Integer.toString(fib_curly.length()));
        itemBody.appendChild(fbInteraction);
        m_fillBlanks.appendReplacement(sb, "");
    }
    m_fillBlanks.appendTail(sb);

    if (sb.length() > 0) {
        Element textDiv = questionDocument.createElement("div");
        textDiv.setTextContent(sb.toString());
        itemBody.appendChild(textDiv);
    }

    // answer
    List<String> correctAnswers = new ArrayList<String>();
    question.parseCorrectAnswers(correctAnswers);

    int responseCount = 1;
    for (String answer : correctAnswers) {
        Element responseDeclaration = createResponseDeclaration(questionDocument, "RESPONSE" + responseCount,
                "single", "string");
        Element correctResponse = questionDocument.createElement("correctResponse");
        Element correctResponseValue = questionDocument.createElement("value");
        answer = FormattedText.unEscapeHtml(answer);
        correctResponseValue.setTextContent(answer);
        correctResponse.appendChild(correctResponseValue);
        responseDeclaration.appendChild(correctResponse);
        questionParts.put("responseDeclaration" + responseCount, responseDeclaration);
        responseCount++;
    }

    Element countDiv = questionDocument.createElement("div");
    countDiv.setTextContent(Integer.toString(responseCount));
    questionParts.put("responseDeclarationCount", countDiv);
    questionParts.put("itemBody", itemBody);
    return itemBody;
}

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

/**
 * BringIn embed media from instrcutions
 * /*from w  w  w .  j a  v  a 2s . c  o m*/
 * @param unzipBackUpLocation
 * @param context
 * @param text
 * @param embedMedia
 * @return
 * @throws Exception
 */
private String processInstructionsEmbedMedia(String unzipBackUpLocation, String context, String text,
        List<String> embedMedia) throws Exception {
    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    StringBuffer sb = new StringBuffer();
    Matcher m = p.matcher(text);

    while (m.find()) {
        if (m.groupCount() != 3)
            continue;
        String fileName = m.group(2);
        if (embedMedia != null)
            embedMedia.add(fileName);
        // add to collection
        Reference ref = transferEmbeddedData(unzipBackUpLocation + File.separator + fileName, fileName,
                context);
        if (ref == null)
            continue;

        // replace with collection Url
        String ref_id = attachmentService.processMnemeUrls(ref.getId());
        m.appendReplacement(sb, m.group(1) + "= \"" + ref_id + "\"");
    }
    m.appendTail(sb);
    return sb.toString();
}

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

/**
 * Parses the text and if embed media found then translates the path and adds the file to the zip package.
 * //from   w  w w .  j ava2 s.com
 * @param zip
 *        The zip package
 * @param text
 *        Text
 * @param mediaFiles
 *        List of embedded files found
 * @return The translated Text
 */
protected String translateEmbedData(ZipOutputStream zip, String subFolder, String writeSubFolder, String text,
        List<String> mediaFiles) {
    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    StringBuffer sb = new StringBuffer();
    subFolder = (subFolder == null || subFolder.length() == 0) ? "Resources/" : subFolder;
    Matcher m = p.matcher(text);

    // security advisor
    pushAdvisor();

    while (m.find()) {
        if (m.groupCount() != 3)
            continue;
        String ref = m.group(2);
        if (!ref.contains("/access/mneme/content/"))
            continue;

        String resource_id = ref.replace("/access/mneme", "");
        String resource_name = ref.substring(ref.lastIndexOf("/") + 1);
        resource_name = resource_name.replaceAll("%20", " ");
        resource_name = Validator.escapeResourceName(resource_name);
        mediaFiles.add(subFolder + resource_name);
        m.appendReplacement(sb, m.group(1) + "= \"" + writeSubFolder + resource_name + "\"");

        writeContentResourceToZip(zip, subFolder, resource_id, resource_name);
    }

    popAdvisor();
    m.appendTail(sb);
    return sb.toString();
}

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

/**
 * Creates elements for all embed media with in itembody element. Use this for question text
 * /*from w w w  . j av  a  2 s  .  co m*/
 * @param zip
 * @param subFolder
 * @param text
 * @param itemBody
 * @param mediaFiles
 * @return
 */
private Element translateEmbedData(ZipOutputStream zip, String subFolder, String text, Element itemBody,
        List<String> mediaFiles, Document questionDocument) {
    if (text == null || text.length() == 0)
        return itemBody;

    Element media = null;
    try {
        Pattern pa = Pattern.compile("<(img|a|embed)\\s+.*?/*>",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

        // TODO: write all attributes
        Pattern p_srcAttribute = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
        Matcher m = pa.matcher(text);
        StringBuffer sb = new StringBuffer();
        subFolder = (subFolder == null || subFolder.length() == 0) ? "Resources/" : subFolder;
        String embedSubFolder = "Resources/";
        int start = 0;

        while (m.find()) {
            int startIdx = m.start();

            String img_content = m.group(0);
            Matcher m_src = p_srcAttribute.matcher(img_content);
            if (m_src.find()) {
                String ref = m_src.group(2);
                if (!ref.contains("/access/mneme/content/"))
                    continue;

                Element div = questionDocument.createElement("div");
                if (startIdx <= text.length()) {
                    String divText = text.substring(start, startIdx);
                    div.setTextContent(divText);
                    start = m.end();
                }
                ref = ref.replaceAll("%20", " ");
                String resource_id = ref.replace("/access/mneme", "");
                String embedFileName = ref.substring(ref.lastIndexOf("/") + 1);
                ref = subFolder + embedFileName;
                mediaFiles.add(ref);

                media = questionDocument.createElement(m.group(1));
                if ("a".equalsIgnoreCase(m.group(1)))
                    media.setAttribute("target", "_blank");
                media.setAttribute(m_src.group(1), embedSubFolder + embedFileName);
                m.appendReplacement(sb, "");

                String fileName = Validator.getFileName(resource_id);
                fileName = fileName.replaceAll("%20", " ");
                fileName = Validator.escapeResourceName(fileName);
                writeContentResourceToZip(zip, subFolder, resource_id, fileName);
                itemBody.appendChild(div);
                itemBody.appendChild(media);
            }
        }
        m.appendTail(sb);
        if (start > 0 && start < text.length()) {
            Element div = questionDocument.createElement("div");
            div.setTextContent(text.substring(start));
            itemBody.appendChild(div);
        }
        return itemBody;
    } catch (Exception e) {
        M_log.debug("error in translating embed up blank img tags:" + e.getMessage());
    }
    return itemBody;
}