Example usage for java.util.regex Pattern quote

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

Introduction

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

Prototype

public static String quote(String s) 

Source Link

Document

Returns a literal pattern String for the specified String .

Usage

From source file:Main.java

private static String applyRule(String encoded, String toReplace, String replacement) {
    return encoded.replaceAll(Pattern.quote(toReplace), replacement);
}

From source file:Main.java

private static void addPattern(Map<Pattern, Integer> map, String smile, int resource) {
    map.put(Pattern.compile(Pattern.quote(smile)), resource);
}

From source file:Main.java

public static String[] split(String value, char delimiter) {
    String regex = "(?<!\\\\)" + Pattern.quote(delimiter + "");
    return value.split(regex);
}

From source file:Main.java

/**
 * Splits a comma delimited String sequence into a String[]
 * @param delimited String to split//from   ww  w .  java2  s  . c  om
 * @return the split String[]
 */
public static String[] splitDelimitedString(String delimited) {
    return delimited.split(Pattern.quote(", "));
}

From source file:Main.java

public static double[] getVivaCurrentLocation(Context context) {
    String loc = getPreferences(context).getString(Viva_Current_Location, "0,0");
    String[] locArray = loc.split(Pattern.quote(","));
    return new double[] { Double.parseDouble(locArray[0]), Double.parseDouble(locArray[1]) };
}

From source file:Main.java

/**
 * Check whether the target string contains the indicated sub string, and case insensitive
 * //from  w  w  w.  j  av a  2s .  c o  m
 * @param target  the target string
 * @param subStr  the sub string
 * @return the check result
 */
public static boolean containsIgnoreCase(String target, String subStr) {
    target = deNull(target);
    subStr = deNull(subStr);

    return Pattern.compile(Pattern.quote(subStr), Pattern.CASE_INSENSITIVE).matcher(target).find();
}

From source file:Main.java

/**
 * Replace all occurrences of oldVarName in xpathExpr with newVarName
 * //from  w w  w.ja  va2  s  . c om
 * @param xpathExpr The {@link XPath} expression String
 * @param oldVarName The old variable name
 * @param newVarName The new variable name
 * @return {@link XPath} expression String with replaced {@link Variable}
 *         names
 */
public static String replaceVariableName(String xpathExpr, String oldVarName, String newVarName) {
    // First replace normal Variable occurrence
    String newExpr = xpathExpr.replaceAll(Pattern.quote("$" + oldVarName) + "(?!\\w{1})\\W??",
            "\\$" + newVarName);
    // Now replace all occurrences in bpel:getVariableProperty
    newExpr = newExpr.replaceAll("bpel:getVariableProperty\\(\"" + oldVarName + "\"",
            "bpel:getVariableProperty(\"" + newVarName + "\"");
    newExpr = newExpr.replaceAll("bpel:getVariableProperty\\('" + oldVarName + "'",
            "bpel:getVariableProperty('" + newVarName + "'");
    return newExpr;
}

From source file:Main.java

/**
 * Returns the path of one File relative to another.
 * <p/>//  w  w  w. j a  v  a2s . c  o m
 * From http://stackoverflow.com/questions/204784
 *
 * @param target the target directory
 * @param base   the base directory
 * @return target's path relative to the base directory
 */
public static String getRelativePath(File target, File base) {
    String[] baseComponents;
    String[] targetComponents;
    try {
        baseComponents = base.getCanonicalPath().split(Pattern.quote(File.separator));
        targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));
    } catch (IOException e) {
        return target.getAbsolutePath();
    }

    // skip common components
    int index = 0;
    for (; index < targetComponents.length && index < baseComponents.length; ++index) {
        if (!targetComponents[index].equals(baseComponents[index]))
            break;
    }

    StringBuilder result = new StringBuilder();
    if (index != baseComponents.length) {
        // backtrack to base directory
        for (int i = index; i < baseComponents.length; ++i)
            result.append("..").append(File.separator);
    }
    for (; index < targetComponents.length; ++index)
        result.append(targetComponents[index]).append(File.separator);
    if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\")) {
        // remove final path separator
        result.delete(result.length() - "/".length(), result.length());
    }
    return result.toString();
}

From source file:marytts.tools.analysis.CopySynthesis.java

/**
 * @param args//from   w w w  . j  a  v a  2s  .  c  o m
 */
public static void main(String[] args) throws Exception {
    String wavFilename = null;
    String labFilename = null;
    String pitchFilename = null;
    String textFilename = null;

    String locale = System.getProperty("locale");
    if (locale == null) {
        throw new IllegalArgumentException("No locale given (-Dlocale=...)");
    }

    for (String arg : args) {
        if (arg.endsWith(".txt"))
            textFilename = arg;
        else if (arg.endsWith(".wav"))
            wavFilename = arg;
        else if (arg.endsWith(".ptc"))
            pitchFilename = arg;
        else if (arg.endsWith(".lab"))
            labFilename = arg;
        else
            throw new IllegalArgumentException("Don't know how to treat argument: " + arg);
    }

    // The intonation contour
    double[] contour = null;
    double frameShiftTime = -1;
    if (pitchFilename == null) { // need to create pitch contour from wav file 
        if (wavFilename == null) {
            throw new IllegalArgumentException("Need either a pitch file or a wav file");
        }
        AudioInputStream ais = AudioSystem.getAudioInputStream(new File(wavFilename));
        AudioDoubleDataSource audio = new AudioDoubleDataSource(ais);
        PitchFileHeader params = new PitchFileHeader();
        params.fs = (int) ais.getFormat().getSampleRate();
        F0TrackerAutocorrelationHeuristic tracker = new F0TrackerAutocorrelationHeuristic(params);
        tracker.pitchAnalyze(audio);
        frameShiftTime = tracker.getSkipSizeInSeconds();
        contour = tracker.getF0Contour();
    } else { // have a pitch file -- ignore any wav file
        PitchReaderWriter f0rw = new PitchReaderWriter(pitchFilename);
        if (f0rw.contour == null) {
            throw new NullPointerException("Cannot read f0 contour from " + pitchFilename);
        }
        contour = f0rw.contour;
        frameShiftTime = f0rw.header.skipSizeInSeconds;
    }
    assert contour != null;
    assert frameShiftTime > 0;

    // The ALLOPHONES data and labels
    if (labFilename == null) {
        throw new IllegalArgumentException("No label file given");
    }
    if (textFilename == null) {
        throw new IllegalArgumentException("No text file given");
    }
    MaryTranscriptionAligner aligner = new MaryTranscriptionAligner();
    aligner.SetEnsureInitialBoundary(false);
    String labels = MaryTranscriptionAligner.readLabelFile(aligner.getEntrySeparator(),
            aligner.getEnsureInitialBoundary(), labFilename);
    MaryHttpClient mary = new MaryHttpClient();
    String text = FileUtils.readFileToString(new File(textFilename), "ASCII");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mary.process(text, "TEXT", "ALLOPHONES", locale, null, null, baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder builder = docFactory.newDocumentBuilder();
    Document doc = builder.parse(bais);
    aligner.alignXmlTranscriptions(doc, labels);
    assert doc != null;

    // durations
    double[] endTimes = new LabelfileDoubleDataSource(new File(labFilename)).getAllData();
    assert endTimes.length == labels.split(Pattern.quote(aligner.getEntrySeparator())).length;

    // Now add durations and f0 targets to document
    double prevEnd = 0;
    NodeIterator ni = MaryDomUtils.createNodeIterator(doc, MaryXML.PHONE, MaryXML.BOUNDARY);
    for (int i = 0; i < endTimes.length; i++) {
        Element e = (Element) ni.nextNode();
        if (e == null)
            throw new IllegalStateException("More durations than elements -- this should not happen!");
        double durInSeconds = endTimes[i] - prevEnd;
        int durInMillis = (int) (1000 * durInSeconds);
        if (e.getTagName().equals(MaryXML.PHONE)) {
            e.setAttribute("d", String.valueOf(durInMillis));
            e.setAttribute("end", new Formatter(Locale.US).format("%.3f", endTimes[i]).toString());
            // f0 targets at beginning, mid, and end of phone
            StringBuilder f0String = new StringBuilder();
            double startF0 = getF0(contour, frameShiftTime, prevEnd);
            if (startF0 != 0 && !Double.isNaN(startF0)) {
                f0String.append("(0,").append((int) startF0).append(")");
            }
            double midF0 = getF0(contour, frameShiftTime, prevEnd + 0.5 * durInSeconds);
            if (midF0 != 0 && !Double.isNaN(midF0)) {
                f0String.append("(50,").append((int) midF0).append(")");
            }
            double endF0 = getF0(contour, frameShiftTime, endTimes[i]);
            if (endF0 != 0 && !Double.isNaN(endF0)) {
                f0String.append("(100,").append((int) endF0).append(")");
            }
            if (f0String.length() > 0) {
                e.setAttribute("f0", f0String.toString());
            }
        } else { // boundary
            e.setAttribute("duration", String.valueOf(durInMillis));
        }
        prevEnd = endTimes[i];
    }
    if (ni.nextNode() != null) {
        throw new IllegalStateException("More elements than durations -- this should not happen!");
    }

    // TODO: add pitch values

    String acoustparams = DomUtils.document2String(doc);
    System.out.println("ACOUSTPARAMS:");
    System.out.println(acoustparams);
}

From source file:Main.java

private static boolean matches(String[] filters, Matcher matcher) {
    for (String filter : filters) {
        // Is logcat filter?
        if (filter.matches("(\\w+|\\*):[\\w|\\*]")) {
            String[] matches = filter.split(":");
            // Tag and level match?
            if ((matches[0].equals("*") || matches[0].equals(matcher.group(6)))
                    && (matches[1].equals("*") || matches[1].equals(matcher.group(5)))) {
                return true;
            }/* www  .  j a v  a  2 s  . c  om*/
        } else {
            Pattern pattern = Pattern.compile(".*" + Pattern.quote(filter) + ".*", Pattern.CASE_INSENSITIVE);
            for (int i = 1; i < 8; i++) {
                if (pattern.matcher(matcher.group(i)).matches()) {
                    return true;
                }
            }
        }
    }
    return false;
}